Skip to content

Commit

Permalink
Feature | 更新部分功能,详情见CHANGELOG
Browse files Browse the repository at this point in the history
Feature | 更新部分功能,详情见CHANGELOG
  • Loading branch information
Cl0udG0d authored Mar 8, 2023
2 parents c715acc + cb685b5 commit c3905cd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 69 deletions.
8 changes: 1 addition & 7 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@
# @File : config.py
# @Github: https://github.com/Cl0udG0d


SearchKEY = ""
TimeSleep = 5



# Fofa-hack 版本号
VERSION_NUM="2.0.0"
VERSION_NUM="2.0.1"
# 登录最大重试次数
MAX_LOGIN_RETRY_NUM=3
# 页面URL获取最大重试次数
Expand Down
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG 代码变更记录

### 2.0.1
+ 修改filename,timeSleep等为类内部变量
+ 新增logoutInitMsg方法输出初始化信息
+ 新增fofa_fuzz_spider方法

### 2.0.0

+ 返璞归真,去除账号登录,直接使用时间戳来在第一页进行爬取
Expand Down
103 changes: 55 additions & 48 deletions fofa.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,25 @@
import time
from urllib.parse import quote_plus
import config

from tookit import unit, fofa_useragent
import argparse

from tookit.levelData import LevelData
from tookit.outputData import OutputData

filename=""







import re, requests

from lxml import etree



class Fofa:


def __init__(self):
self.headers_use = ""
self.level = 0
self.host_set = set()
self.timestamp_set = set()
self.oldLength = -1
self.endcount=0
self.filename = ""

print('''
____ ____ ____ ____
| ===|/ () \| ===|/ () \
Expand All @@ -50,8 +38,6 @@ def __init__(self):
|_| |_|/__/\__\\\\____)|__|\__\\ V{}
'''.format(config.VERSION_NUM))



def headers(self,cookie):
headers_use = {
'User-Agent': fofa_useragent.getFakeUserAgent(),
Expand All @@ -60,6 +46,16 @@ def headers(self,cookie):
}
return headers_use

def logoutInitMsg(self):
print('''[*] LEVEL = {} , 初始化成功
[*] 爬取延时: {}s
[*] 爬取关键字: {}
[*] 爬取结束数量: {}
[*] 是否FUZZ: {}
[*] 输出格式为: {}
[*] 存储文件名: {}'''.format(self.level,self.timeSleep,self.searchKey,self.endcount,self.fuzz,self.output,self.filename)
)
return

def init(self):
parser = argparse.ArgumentParser(description='Fofa-hack v{} 使用说明'.format(config.VERSION_NUM))
Expand All @@ -68,29 +64,21 @@ def init(self):
parser.add_argument('--endcount', '-e', help='爬取结束数量')
parser.add_argument('--level', '-l', help='爬取等级: 1-3 ,数字越大内容越详细,默认为 1')
parser.add_argument('--output', '-o', help='输出格式:txt、json、csv,默认为txt')
parser.add_argument('--fuzz', '-f', help='关键字fuzz参数,增加内容获取粒度',action='store_true')
args = parser.parse_args()
config.TimeSleep = int(args.timesleep)
print("[*] 爬取延时: {}s".format(config.TimeSleep))
config.SearchKEY = args.keyword
print("[*] 爬取关键字: {}".format(config.SearchKEY))
self.timeSleep= int(args.timesleep)
self.searchKey= args.keyword
if args.endcount:
self.endcount=int(args.endcount)
print("[*] 爬取结束数量: {}".format(self.endcount))
else:
self.endcount=100
self.level=args.level if args.level else "1"
self.levelData=LevelData(self.level)


self.fuzz=args.fuzz
self.output = args.output if args.output else "txt"
print("[*] 输出格式为: {}".format(self.output))


global filename
filename = "{}_{}.{}".format(unit.md5(config.SearchKEY), int(time.time()),self.output)
print("[*] 存储文件名: {}".format(filename))
self.outputData = OutputData(filename, pattern=self.output)
return
self.filename = "{}_{}.{}".format(unit.md5(self.searchKey), int(time.time()),self.output)
self.outputData = OutputData(self.filename, pattern=self.output)
self.logoutInitMsg()

def get_count_num(self, search_key):
"""
Expand All @@ -109,7 +97,6 @@ def get_count_num(self, search_key):
except Exception as e:
print("[-] error:{}".format(e))
countnum = '0'
standaloneIpNum = '0'
pass
print("[*] 存在数量:" + countnum)
# print("[*] 独立IP数量:" + standaloneIpNum)
Expand Down Expand Up @@ -148,13 +135,13 @@ def fofa_spider_page(self, searchbs64):
print("[*] 已爬取条数 [{}]: ".format(len(self.host_set))+str(self.levelData.formatData))

for i in self.levelData.formatData:
with open(filename, 'a+', encoding="utf-8") as f:
with open(self.filename, 'a+', encoding="utf-8") as f:
f.write(str(i) + "\n")
for url in self.levelData.formatData:
self.host_set.add(url)
for temptime in timelist:
self.timestamp_set.add(temptime)
time.sleep(config.TimeSleep)
time.sleep(self.timeSleep)
return
except Exception as e:
print("[-] error:{}".format(e))
Expand All @@ -167,20 +154,40 @@ def fofa_spider_page(self, searchbs64):
exit(0)




def fofa_spider(self, search_key, searchbs64):

def fofa_common_spider(self, search_key, searchbs64):
while len(self.host_set) < self.endcount and self.oldLength !=len(self.host_set):
self.oldLength=len(self.host_set)
self.timestamp_set.clear()
self.fofa_spider_page(searchbs64)
search_key_modify, searchbs64_modify = self.modify_search_url(search_key)
search_key_modify= self.modify_search_time_url(search_key)
searchbs64_modify = quote_plus(base64.b64encode(search_key_modify.encode()))
search_key = search_key_modify
searchbs64 = searchbs64_modify
if len(self.host_set) >= self.endcount:
print("[*] 数据爬取结束")
return
if self.oldLength == len(self.host_set):
print("[-] 数据无新增,退出爬取")
return

def fofa_fuzz_spider(self, search_key, searchbs64):
while len(self.host_set) < self.endcount and self.oldLength !=len(self.host_set):
self.oldLength=len(self.host_set)
self.timestamp_set.clear()
self.fofa_spider_page(searchbs64)
search_key_modify = self.modify_search_time_url(search_key)


def modify_search_url(self, search_key):
searchbs64_modify = quote_plus(base64.b64encode(search_key_modify.encode()))
search_key = search_key_modify
searchbs64 = searchbs64_modify
if len(self.host_set) >= self.endcount:
print("[*] 数据爬取结束")
return
if self.oldLength == len(self.host_set):
print("[-] 数据无新增,退出爬取")
return

def modify_search_time_url(self, search_key):
"""
根据时间修订搜索值
:param search_key:
Expand All @@ -206,24 +213,24 @@ def modify_search_url(self, search_key):
search_key = search_key + ' && ' + 'before="' + str(time_before) + '"'
search_key_modify = search_key

searchbs64_modify = quote_plus(base64.b64encode(search_key_modify.encode()))
# print('[*] 搜索词: ' + search_key_modify)

return search_key_modify, searchbs64_modify
# print('[*] 搜索词: ' + search_key_modify)

return search_key_modify

def run(self):
searchbs64 = self.get_count_num(config.SearchKEY)
self.fofa_spider(config.SearchKEY, searchbs64)
searchbs64 = self.get_count_num(self.searchKey)
if not self.fuzz:
self.fofa_common_spider(self.searchKey, searchbs64)
else:
self.fofa_fuzz_spider(self.searchKey, searchbs64)
print('[*] 抓取结束,共抓取数据 ' + str(len(self.host_set)) + ' 条\n')

def main(self):
self.init()
print('[*] 开始运行')
self.run()



if __name__ == '__main__':
fofa = Fofa()
fofa.main()
31 changes: 21 additions & 10 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@
import requests
from lxml import etree

def cleanData(urllist):
newlist=list()
for url in urllist:
newlist.append(url.strip())
return newlist
data=[1,2,3,4,5,6,7,8,9]
result=[]

resp = requests.get(url='https://fofa.info/result?qbase64=MQ==&page_size=10')
tree = etree.HTML(resp.text)
urllist1 = tree.xpath('//span[@class="hsxa-host"]/text()')

urllist2 = tree.xpath('//span[@class="hsxa-host"]/a/@href')

print(cleanData(urllist1)+urllist2)
for i in data:

for j in data:
for k in data:
for l in data:
for m in data:
for n in data:
num1=i*10+j
num2=k
num3=l*10+m
num4=n
if num1-num2==num3-num4:
templist=[i,j,k,l,m,n]
if templist.count(2)<2 and templist.count(3)<2 and templist.count(4)<2 and templist.count(5)<2 and templist.count(6)<2 and templist.count(7)<2 and templist.count(8)<2 and templist.count(9)<2:

print("{}-{}={}-{}".format(num1,num2,num3,num4))
result.append([num1,num2,num3,num4])

print(len(result))
4 changes: 0 additions & 4 deletions tookit/levelData.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ class LevelData:
formatData = []

def __init__(self, level="1"):

self.level = level if self.checkLevelStandard(level) else "1"
print("[*] LEVEL = {} , 初始化成功".format(self.level))

return

def checkLevelStandard(self, level):
"""
Expand Down

0 comments on commit c3905cd

Please sign in to comment.