使用python打造自己的信息收集工具 - FreeBuf网络安全行业门户

标签: | 发表时间:2021-09-23 17:36 | 作者:
出处:https://www.freebuf.com

介绍:

该篇章主要介绍如何编写自己的信息收集工具,主要流程如下:

1、向bing搜索引擎发起request请求,获取url数据

2、使用正则表达式对获取的数据进行处理

3、用多线程,对处理的数据进行二次请求,返回标题等数据

4、使用openyxl模块,将数据保存为.xlsx格式

请注意:

该篇章目的是熟悉python编程,学习python的一些常见模块,在编写程序的过程中会有很多操作和方式方法,望大家能共同加油学到东西。本文仅用于技术讨论与研究,这里使用的技术仅用于学习教育目的,如果列出的技术用于其他任何目标,本站及作者概不负责。

本文涉及到模块有:

#coding:utf-8        
import requests     #发起request请求
import urllib3     #处理请求https异常报错问题
import re       #使用正则表达式对请求到的数据进行处理
from optparse import OptionParser   #自定义输入参数
import threading       #多线程模块
import queue           #多线程辅助模块,使用队列的方式对多线程进行控制
from bs4 import BeautifulSoup   #与re类似 使用正则表达式对请求到的数据进行处理
import time,datetime   #获取当前的时间
from openpyxl import * #数据处理,将获取到的数据保存在excel文件中

属性:

heads = {                       #全局变量  请求头      
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',   #模拟浏览器请求
'Connection':'close',  
'Accept-Encoding':'gzip, deflate'
}
count=1                         #全局变量 用于序号字段
queueLock = threading.Lock()   #全局变量 使用线程锁处理 线程异常问题
class DoRun(threading.Thread): #自定义 多线程运行时使用的类

方法:

def get_Input():    #获取search语句 和 page      
def getUrls(search,page):       #构造搜索语句,在bing搜索引擎搜索数据并返回urls
def req(url):   #对url进行验证,返回numb,url,title,status
def init_excel(filename): #创建.xlsx表格,并初始化内容
def Save_Date(date,filename):   #将数据存储到表格当中
def run():     #核心代码

完整代码如下:

#coding:utf-8        
import requests     #发起request请求
import urllib3     #处理请求https异常报错问题
import re       #使用正则表达式对请求到的数据进行处理
from optparse import OptionParser   #自定义输入参数
import threading       #多线程模块
import queue           #多线程辅助模块,使用队列的方式对多线程进行控制
from bs4 import BeautifulSoup   #与re类似 使用正则表达式对请求到的数据进行处理
import time,datetime   #获取当前的时间
from openpyxl import * #数据处理,将获取到的数据保存在excel文件中

heads = {                       #全局变量 请求头
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',   #模拟浏览器请求
'Connection':'close',  
'Accept-Encoding':'gzip, deflate'
}
count=1                         #全局变量 用于序号字段
queueLock = threading.Lock()   #全局变量 使用线程锁处理 线程异常问题
class DoRun(threading.Thread): #自定义 多线程运行时使用的类
def __init__(self,queue,filename):
threading.Thread.__init__(self)
self._queue=queue
self._filename=filename
def run(self):
while not self._queue.empty():
js=req(self._queue.get())
#print(js)
queueLock.acquire()
if(js):
Save_Date(js,self._filename)
queueLock.release()        
def init_excel(filename): #创建.xlsx表格,并初始化内容
wb=Workbook()
filename=filename+".xlsx"
ws=wb.create_sheet(index=0,title="域名")
head=['序号','域名','标题','状态']
for i in range(0,4):
ws.cell(1,i+1).value=head[i]
wb.save(filename)
def Save_Date(date,filename):   #将数据存储到表格当中
filename=filename+".xlsx"
wb_save=load_workbook(filename)
ws_save=wb_save.worksheets[0]
current_row=ws_save.max_row+1
current_col=1
for key in date:
ws_save.cell(date['numb']+1,current_col).value=str(date[key])
current_col+=1
wb_save.save(filename)
def req(url):   #对域名进行验证,返回状态码,title
global count
dir={'numb':0,'url':'url','title':'None','status':0}
stat=0
title="None"

try:
urllib3.disable_warnings()
response = requests.get(url=url,headers=heads,verify=False,timeout=10)   #请求漏洞的url
if response.status_code == 200:
bs=BeautifulSoup(response.content,"html.parser")
title=bs.find("title").text
stat=response.status_code
dir['numb']=count
dir['url']=url
dir['title']=title
dir['status']=stat
count+=1
print("[+]"+url+"\ttitle:"+title)
return dir
else:
print('[-]请求失败:\t{}\t{}'.format(url,response.status_code))
except Exception as e:
print('[-]请求失败: {}\t'.format(e,url))
def getUrls(search,page):       #构造搜索语句,在bing搜索引擎 搜索数据并返回urls
count=1
urls=[]
url="https://cn.bing.com/search?q={}&first={}"
for i in range(1,page):
if(i!=1):
count=(i-2)*10+9
url=url.format(search,i)
try:
resp=requests.get(url=url,headers=heads)
html=resp.text
if(resp.status_code==200):
res=re.findall(r'<a target="_blank" href="(.*?)"',html)
for u in res:
if(u not in urls):
urls.append(u)
else:
print('[-]请求失败:\t{}\t{}'.format(url,resp.status_code))
except Exception as e:
print('[-]请求失败: {}\t'.format(e,url))

return urls
def get_Input():   #获取search语句 和 page
optParser = OptionParser()
optParser.add_option('-s','--search',action = 'store',type = "string" ,dest = 'search',help='漏扫文件的目录',default="search_def")
optParser.add_option("-p","--page", action="store", type="int",dest="page",help='要搜索的页数',default=10)            
optParser.add_option("-t","--threads", action="store", type="int",dest="threads",help='线程数量,默认为10',default=10)            
(options , args) = optParser.parse_args()
return options.search,options.page,options.threads
def run():          
que=queue.Queue()
print(datetime.datetime.now())             #打印开始时间
search,page,thread_count=get_Input()       #获取输入的参数 如searce 、线程数、页面数
if(search=="search_def"):
print(r"[-]错误,未输入指定参数:python3 temp.py -s site:qq.com [-p 10] [-t 20] ")
return
print(search)
threads=[]
urls=getUrls(search,page)     #获取urls
filename=''.join(re.findall("([a-z,0-9])",search))     #将输入的内容进行处理 ,作为文件的名称
init_excel(filename)       #创建并初始化excel  
for url in urls:
que.put(url)           #将获取的urls添加到queue中  
for i in range(thread_count):
threads.append(DoRun(que,filename))     #使用多线程 默认调用 run()函数
for i in threads:
i.start()               #启动多线程
for i in threads:
i.join()               #等待线程结束

print(datetime.datetime.now())     #打印结束时间
run()

使用说明:****

python3 .\bingying.py -s "site:.com" -p 10 -t 30      
Options:
-h, --help show this help message and exit
-s SEARCH, --search=SEARCH 搜索的语法(默认 site:.com)
-p PAGE, --page=PAGE 要搜索的页数(一页10条数据,默认10页)
-t THREADS, --threads=THREADS 线程数量,(默认为10)

结果呈现

1632291957_614acc75cf719787794df.png!small

相关 [python 信息 工具] 推荐:

使用python打造自己的信息收集工具 - FreeBuf网络安全行业门户

- -
该篇章主要介绍如何编写自己的信息收集工具,主要流程如下:. 1、向bing搜索引擎发起request请求,获取url数据. 2、使用正则表达式对获取的数据进行处理. 3、用多线程,对处理的数据进行二次请求,返回标题等数据. 4、使用openyxl模块,将数据保存为.xlsx格式. 该篇章目的是熟悉python编程,学习python的一些常见模块,在编写程序的过程中会有很多操作和方式方法,望大家能共同加油学到东西.

pystack--python stack trace--让python打印线程栈信息

- - BlogJava-首页技术区
类似java中的jstack功能. https://pypi.python.org/pypi/pdbx/0.3.0 下载, 或者直接通过easyinstall安装. python scripts中, import pdbx; pdbx.enable_pystack(); 开启pystack功能. kill -30 pid , 就可以打印stack信息了. .

[原][python]书籍信息爬虫示例

- - moxiaomomo的专栏
[python]书籍信息爬虫示例. 需要收集一些书籍信息,以豆瓣书籍条目作为源,得到一些有效书籍信息,并保存到本地数据库. 具体可参考这个链接:. 然后将这些分类标签链接存到本地某个文件,存储内容如下. 获取书籍信息,并保存本地数据库. 假设已经建好mysql表,如下:. 并已实现相关爬虫逻辑,主要用到了BeautifulSoup包,如下:.

我常用的Python调试工具(二)

- - 博客 - 伯乐在线
这是对 上一篇关于调试器的文章的一个小小的补充. 标准库有三种分析方法( cProfile和profile, hotshot)以及不计其数的第三方可视化工具,转化器,以及诸如此类的东西. 工具多了,不靠谱的建议自然少不了. 如果你不知道该用什么,那就用一个可视化的工具吧. 有很多的建议供你选择自己要用的可视化工具,但无论是使用如标准库的Stats模块这种文本形式的还是像 pycallgraph或者 gprof2dot这样的图形化的库,第一反应都是要写代码来生成报表.

XSStrike:基于Python的XSS测试工具

- - FreeBuf.COM | 关注黑客与极客
XSStrike 是一款用于探测并利用XSS漏洞的脚本. XSStrike目前所提供的产品特性:. 对参数进行模糊测试之后构建合适的payload. 使用payload对参数进行穷举匹配. 同时支持GET及POST方式. 大多数payload都是由作者精心构造. debian及kali系统可直接下载 本.deb安装包.

python获取bt种子的详细信息

- - CSDN博客编程语言推荐文章
bt种子具体文件结构如下: . 全部内容必须都为bencoding编码类型. 整个文件为一个字典结构,包含如下关键字 . announce:tracker服务器的URL(字符串) . announce-list(可选):备用tracker服务器列表(列表) . creation date(可选):种子创建的时间,Unix标准时间格式,从1970 1月1日 00:00:00到创建时间的秒数(整数) .

Python超级程序员使用的开发工具

- - 外刊IT评论网
我以个人的身份采访了几个顶尖的Python程序员,问了他们以下5个简单的问题:. 你在项目中使用的电脑是怎样的. 有什么给Python程序员的建议. 就是这几个问题,我找了几个顶尖的程序员和编程书籍作家,问他们这几个相同的问题. 下面是他们的回答,希望在他们的回答中你能找到一些可以让你的开发更便捷的工具.

saidar:系统信息工具

- - 笨兔兔
Saidar,是一款命令行下的小工具,可用来查看CPU、进程、负载、内存、交换分区、网络、磁盘、文件系统等系统信息. Ubuntu,Linux Mint,Debian,Linux Deepin 用户可通过下面命令安装:. 安装完毕之后,在命令行下运行:. 可使用 saidar -c有色输出结果. 5款 Linux 文件系统清洁工具.

[Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

- - CSDN博客推荐文章
前一篇文章" [python爬虫] Selenium爬取新浪微博内容及用户信息"简单讲述了如何爬取新浪微博手机端用户信息和微博信息. 用户信息:包括用户ID、用户名、微博数、粉丝数、关注数等. 微博信息:包括转发或原创、点赞数、转发数、评论数、发布时间、微博内容等. 它主要通过从文本txt中读取用户id,通过"URL+用户ID" 访问个人网站,如柳岩:.

Twitter信息抓取工具 – TinfoLeak

- - FreeBuf.COM
TinfoLeak是一款Python编写的Twitter用户信息抓取软件,主要的抓取的信息有:. 1、基础信息包括名字、头像、关注者、所在位置等. 2、Twitter用户所使用的操作系统和设备信息. 3、Twitter用户的应用和社交网络信息. 5、以GoogleEarth的形式展示用户的推文. 6、下载用户全部推过的图片.