前言
最近复习挺累的,也不想看视频了,就分享一下之前写的fofa工具,感觉功能挺强大的,就开了一个会员,来玩玩。
简化版
搜索HTTP响应头中含有"thinkphp"关键词的网站和IP
结果有170361条,肯定看不完的,用脚本爬取
import pyfofa
email = '' #邮箱
key = ''
search = pyfofa.FofaAPI(email,key)
for host,ip in search.get_data('title="header="thinkphp"',1,"host,ip")['results']:
print(host,ip)
看了一下,我只想爬取中国的,那我组合一下语法
#lanxing
#子域名查询
import pyfofa
email = '' #邮箱
key = ''
search = pyfofa.FofaAPI(email,key)
for host,ip in search.get_data('header="thinkphp" && country="CN"',1,"host,ip")['results']:
print(host,ip)
如果我想查询C段,也可以,不用爆破,防止测试网站挂了
哈哈哈
#lanxing
#子域名查询
import pyfofa
email = '' #邮箱
key = ''
search = pyfofa.FofaAPI(email,key)
for host,ip in search.get_data('domain="baidu.com"',1,"host,ip")['results']:
print(host,ip)
优化版
感觉还不满意,就是数据处理问题。
哈哈,上优化代码。
# #lanxing
import os #获取路径
import pyfofa #fofa登录库
import pandas as pd #读取csv表格库
import time #时间库
import csv #获取到的list数据保存到csv处理库
start=time.time()
def chax():
email = '' #y邮箱
key = '' #fofa个人key
search_fofa = input("请输入你要搜索的特征 :")
# print(type(search_fofa))
# print(search_fofa)
# search_fofa = 'domain="baidu.com"'
search = pyfofa.FofaAPI(email, key)
re_date = search.get_data(search_fofa, 1, "host,title,country_name,city,ip,port,server,protocol")['results'] #搜索请求,等到想要的数据
# print(re_date)
print(type(re_date))
print("*************************开始爬取url***************************")
# for host,title,city in search.get_data('app="TP_LINK-路由器"',1,"host,title,city")['results']:
# print('标题:' + title, '城市:'+city, 'url :'+host)
name = ['网站', 'Title','国家', '城市', 'ip地址', 'port','Server','协议'] #csv表格列
#test = pd.DataFrame(columns=name, data=re_date)
# print(test)
# test.to_csv('C:/Users/lanxing/Desktop/fofa_url.csv', encoding='utf-8')
print("*************************请稍等,正在爬取中**********************")
with open('fofa1_url.csv','w',encoding='utf-8',newline='') as f: #写入
writer = csv.writer(f)
writer.writerow(name)
writer.writerows(re_date)
print("*************************爬取完成***************************")
chax()
end=time.time()
sd=end-start #end-start,程序运行所需时间
print("******************大佬,fofa信息查询完毕!*************************")
lj = os.path.dirname(__file__) #获取当前目录
print('获取到的数据保存在:'+lj + '目录下面')
print('查询耗时:',str(sd)+'s') #Python 不允许直接把数字和字符拼接在一起(如果拼在一起就会报标题显示的错误),把数字型的字符串,转化为字符型就可以了,即str()就可以
运行状态
爬取数据处理
爬虫学习中,遇到数据保存问题,就是学习爬取fofa数据时候,得到的数据数据是list类似,用for打印出太low了,所以就想获取完数据,自动保存到文件中,记录一下。
1.用python创建新的csv文件
使用csv.writer()创建:
#lanxing
#1使用csv.writer()创建
import csv
headers = ['学号','姓名','分数']
rows = [('202001','张三','99'),('202002','李四','96'),('202003','王五','92')] #头部标题
with open('score.csv','w',encoding='utf8',newline='') as f: #打开csore.csv文件
writer = csv.writer(f)
writer.writerow(headers) #头部
writer.writerows(rows)
2.用python去读取csv文件数据
#使用pandas.read_csv()读取
import pandas
import pandas as pd
my_matrix = pandas.read_csv('score.csv')
print(my_matrix)
print(my_matrix.shape)