python爬虫脚本之fofa接口调用

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/'
        # 'Cookie': ''
    }

    def extract_ips(unique_ips):
        """
        提取唯一的IP地址并将它们添加到 unique_ips集合。

        Args:
            unique_ips (set): 唯一IP地址的集合

        Returns:
            None
        """
        # 将搜索语句转换为 base64 编码格式
        search_data_base64 = str(base64.b64encode(search_data.encode("utf-8")), "utf-8")

        # 设置FOFA API的请求URL
        email = 'xxx'  # 替换为您的 FOFA 账户邮箱
        key = 'xxx'  # 替换为您的 FOFA API 密钥
        url_api = 'https://fofa.info/api/v1/search/all?email=' + email + '&key=' + key + '&qbase64=' + search_data_base64

        try:
            # 禁用 SSL 证书警告,也可以将验证设置为 True
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
            # 发送 GET 请求到FOFA API,并解析响应结果
            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

            # 获取查询结果并提取唯一的 IP 地址
            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)

    # 使用线程池执行搜索,并使用集合存储唯一的 IP 地址
    unique_ips = set() # 创建一个空的集合来存储唯一IP地址
    #多线程,线程数20 max_workers=20
    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)
    #print(unique_ips)

    # 将唯一IP地址写入文件
    with open('domain.txt', 'a+') as f:
        for ip in unique_ips: # 遍历唯一IP地址集合,并将其转储到文件
            f.write(ip + '\n')    # 使用线程池执行搜索,并使用集合存储唯一的 IP 地址
    unique_ips = set() # 创建一个空的集合来存储唯一IP地址
    #多线程,线程数20 max_workers=20
    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)

    #print(unique_ips)
    # 将唯一IP地址写入文件
    with open('domain.txt', 'a+') as f:
        for ip in unique_ips: # 遍历唯一IP地址集合,并将其转储到文件
            f.write(ip + '\n')

if __name__ == '__main__':
    #调试
    #调用函数搜索信息    -    #fofa_search('"glassfish" && port="4848" && country="CN"', 1)
    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}")
    '''
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值