python之fofa接口调用脚本
import sys
import time
import base64
import urllib3
import requests
from tqdm import tqdm
import concurrent.futures
'''
如何实现这个漏洞批量化:
1.获取到可能存在漏洞的地址信息-借助Fofa进行获取目标
1.2 将请求的数据进行筛选
2.批量请求地址信息进行判断是否存在-单线程和多线程
'''
def fofa_search(search_data, page):
"""
使用FOFA API执行搜索并提取唯一的IP地址,将结果写入文件。
Args:
search_data (str): FOFA 搜索语句(base64编码)
page (int): 搜索的页数
Returns:
None
"""
headers = {
'User-Agent': 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)',
'Referer': 'https://www.baidu.com/'
}
def extract_ips(unique_ips):
"""
提取唯一的IP地址并将它们添加到 unique_ips集合。
Args:
unique_ips (set): 唯一IP地址的集合
Returns:
None
"""
search_data_base64 = str(base64.b64encode(search_data.encode("utf-8")), "utf-8")
email = 'xxx'
key = 'xxx'
url_api = 'https://fofa.info/api/v1/search/all?email=' + email + '&key=' + key + '&qbase64=' + search_data_base64
try:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
response = requests.get(url=url_api, headers=headers, timeout=15, verify=False).json()
if response.get('error', True):
print(f'FOFA 查询失败:{response}')
time.sleep(1)
return
subdomains = response.get("results", [])
if not subdomains:
print("未找到符合搜索条件的记录")
return
print('FOFA 查询成功!')
time.sleep(3)
global url
for subdomain in subdomains:
host = subdomain[0]
if "://" in host:
url = host.replace("http://",'') and host.replace('https://','')
else:
url = host
if url not in unique_ips:
unique_ips.add(url)
except Exception as e:
print(f'请求出错,正在尝试重新请求... {e}')
time.sleep(6)
unique_ips = set()
max_workers = 10
with concurrent.futures.ThreadPoolExecutor(max_workers) as executor:
futures = []
for _ in range(int(page)):
futures.append(executor.submit(extract_ips, unique_ips))
concurrent.futures.wait(futures)
with open('domain.txt', 'a+') as f:
for ip in unique_ips:
f.write(ip + '\n')
unique_ips = set()
max_workers = 10
with concurrent.futures.ThreadPoolExecutor(max_workers) as executor:
futures = []
for _ in range(int(page)):
futures.append(executor.submit(extract_ips, unique_ips))
concurrent.futures.wait(futures)
with open('domain.txt', 'a+') as f:
for ip in unique_ips:
f.write(ip + '\n')
if __name__ == '__main__':
fofa_search('"edu.cn" && country="CN" && status_code="200"',10000)
'''
#输入提示
if len(sys.argv) != 3:
print('Usage: python Glassfish_poc.py <search_data> <page>')
print('eg. python Glassfish_poc.py "app=\'HIKVISION-视频监控\' && status_code=\'200\' && after=\'2021-07-10\'" 1')
print(sys.argv)
sys.exit(1)
try:
#输入语法
search_data = sys.argv[1]
#替换字符串
search = search_data.replace("\'","\"")
print("--->"+search+"<---")
#输入页数
page_data = sys.argv[2]
#转换为整型
page = int(page_data)
fofa_search(search, page)
except Exception as e:
print(f"Error occurred: {e}")
'''