1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import  requests
from lxml import etree
import base64
import time


# 提取出来的结果保存起来
def Searchresults(results_IP):
Searchresults_document = open("url.txt", 'a') # 打开文件写的方式
Searchresults_document.write((results_IP+'\n')) # 写入
Searchresults_document.close() # 关闭文件

# 如果搜索结果多执行
def Multiple(Judge_page,coding,headers,Cookie,speed):
print("搜索结果有" + Judge_page + "页")
time.sleep(0.5) # 停顿一下
recording=0
for all in range(1,int(Judge_page)):
# 速度
time.sleep(speed)
print("现在提取是第"+str(all)+"页")

# 逐个的页面提取
html = requests.get('https://fofa.so/result?qbase64=' + coding+"&page="+str(all), headers=headers,cookies=Cookie)
html = etree.HTML(html.text)
divs = html.xpath(r'//span/a/@href') # 语法
for results_IP in divs:
print("IP地址是" + str(results_IP))
Searchresults(results_IP)
recording += 1

# 循环50次就停止
if recording >= 50:
print("普通用户只能放我50条信息")
break

# 分析查询到结果判断是否是一页
def Determine(html,headers,Cookie,coding,speed):
try:
html = etree.HTML(html.text)
Judge_page = divs = html.xpath(r'//ul[@class="el-pager"]/li[last()]/text()') # 查看是否有页数
if Judge_page: # 如果搜索结果多进行循环一个一个页面的读取
Multiple(Judge_page[0],coding,headers,Cookie,speed)
else:
print("搜索结果就一页!")
divs = html.xpath(r'//span[@class="aSpan"]//@href') # 探测IP
for results_IP in divs:
print("IP地址是" + str(results_IP))
Searchresults(results_IP)
except Exception as bc:
print("出差了:" + str(bc))



# 第一次请求
def cfq_insert_request(Cookie,coding,speed):
Cookie= {'fofa_token': Cookie}
headers = {
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
# 请求
html=requests.get("https://fofa.so/result?qbase64="+str(coding),headers)

# 分析查询到结果判断是否是一页
Determine(html,headers,Cookie,coding,speed)



# 保存我的输入的fofa_token值
def fofa_token():
i = str(input("输入'1'用上一次的Fofa的Cookie值:"))
Cookie_fofa_token = 0
if i == '1':
fofa_token_document = open("fofa_token.txt", 'r') # 读文件
Cookie_fofa_token = fofa_token_document.readline() # 读取文件的API
fofa_token_document.close() # 关闭文件
else:
Cookie_fofa_token = input("Fofa请输入Cookie值:") # 输入
fofa_token_document = open("fofa_token.txt", 'w') #打开文件写的方式
fofa_token_document.write(Cookie_fofa_token) # 写入
fofa_token_document.close()# 关闭文件
return Cookie_fofa_token

if __name__ == '__main__':
print("""
__ __ _ _ _
/ _| ___ / _| __ _ / \ __| | __| |_ __ ___ ___ ___
| |_ / _ \| |_ / _` | / _ \ / _` |/ _` | '__/ _ \/ __/ __|
| _| (_) | _| (_| | / ___ \ (_| | (_| | | | __/\__ \__ \.
|_| \___/|_| \__,_| /_/ \_\__,_|\__,_|_| \___||___/___/

*fofa提取IP脚本
*作者:赵赛赛
*扫描速度快对方可能会屏蔽IP的
""")

Cookie=fofa_token()
speed=int(input("请输入扫描速度:"))
z=input("请输入要搜索的关键字:")
print("你输入的是" + z)
# base64编码
coding=base64.b64encode(z.encode('utf-8')).decode("utf-8")
# 请求
cfq_insert_request(Cookie,coding,speed)