python脚本之批量提取fofa的漏洞IP

python脚本之批量提取fofa的漏洞IP

前言:

用cookie的目的是API调用多的话,是要付费的,所以。。。。
2023/8/7 更新:实现多线程,目前测试线程为2是最稳定,超过2数据就乱了,又写了一个通过大量IP查web系统的脚本。
2023/3/14 更新:爬取ip的参数更改,注释了部分不重要代码,只用来提取IP到txt中,不用再cmd中运行了。
本次python脚本是通过学习小迪师傅的课程学习而来,由于现在的fofa改变域名为fofa.info,所以下文中的python脚本中的一些值是根据目前的fofa网站改写的

脚本:(poc.py)

import requests
import base64
import time
from lxml import etree
import threading

def fofa_search(search_data, page_data):
    # 必须要有cookie
    headers = {
        "cookie": ""
    }

    lock = threading.Lock()  # 创建线程锁

    def process_page(page):
        url = 'https://fofa.info/result?qbase64='
        search_data_bs = str(base64.b64encode(search_data.encode("utf-8")), "utf-8")
        urls = url + search_data_bs + '&page=' + str(page) + '&page_size=10'
        print(urls)

        try:
            retry_count = 3
            while retry_count > 0:
                try:
                    result = requests.get(url=urls, headers=headers, timeout=10).text
                    break  # 请求成功,跳出重试循环
                except requests.RequestException as e:
                    print("请求发生异常:", e)
                    retry_count -= 1
                    if retry_count == 0:
                        print("请求重试次数已达上限,放弃请求")
                        return  # 放弃当前页面的处理

            print("正在提取第" + str(page) + "页")
            #print(result)
            soup = etree.HTML(result, etree.HTMLParser())  # 初始化生成一个XPath解析对象
            ip_data = soup.xpath('//span[@class="hsxa-host"]/a/@href')
            print(ip_data)
            ipdata = '\n'.join(ip_data)
            print(ipdata)

            with lock:  # 使用线程锁保证写入文件的互斥性
                with open(r'ipip.txt', 'a+') as f:
                    print("写入文件中")
                    #f.write('页面:'+str(page)+'\n'+ipdata + '\n')
                    f.write(ipdata + '\n')
                    f.close()

            time.sleep(0.5)  # 防止速度过快导致部分数据被略过
        except Exception as e:
            print("异常:", e)

    max_threads = 2  # 设置最大线程数量为2,超过2会导致页面爬取内容为空
    threads = []
    for page in range(1, page_data + 1):
        while len(threads) >= max_threads:
            # 等待当前线程数量降到允许的最大值以下
            time.sleep(1)
            threads = [t for t in threads if t.is_alive()]

        t = threading.Thread(target=process_page, args=(page,))
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    print("完成提取IP")

if __name__ == '__main__':
    fofa_search('title="12345"', 500)





运行命令:(cmd中)----已删,如有需要自行添加。

python poc.py -s "要搜索的内容" -p 要查找的页数

如图:
在这里插入图片描述

尽量搜索国内IP,国外IP在漏洞验证时时间较长

收集的IP:
在这里插入图片描述

收集的漏洞IP:
在这里插入图片描述

注意:

此脚本只测试了针对glassfish4.0.1的漏洞检测,后续需要检测其他漏洞时,要修改验证漏洞的部分代码.

fofa批量IP查web服务

脚本

import requests
import base64
import time
from lxml import etree

def fofa_search(search_data, page_data):


#必须要有cookie
    headers={  #F12的cookie
            "cookie":"" }

    for page in range(1, page_data+1):
        url='https://fofa.info/result?qbase64='
        search_data_bs=str(base64.b64encode(search_data.encode("utf-8")), "utf-8")

        urls=url+search_data_bs+'&page='+str(page)+'&page_size=10'
        print(urls)


        try:
            result=requests.get(url=urls, headers=headers).content
            print("正在提取第"+str(page)+"页")

            soup=etree.HTML(result, etree.HTMLParser())   #初始化生成一个XPath解析对象
            ip_data=soup.xpath('//span[@class="hsxa-host"]/a/@href')
            ipdata='\n'.join(ip_data)  # join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

            print(ipdata)
            if ipdata == '':
                break
            with open(r'ipip.txt', 'a+') as f:
                print("写入文件中")
                f.write(ipdata+'\n')
                f.close()
            time.sleep(0.5)  #防止速度过快导致部分数据被略过
        except Exception as e:
            print("异常")

    print("完成提取IP")

if __name__ == '__main__':

    with open('ip.txt', 'r') as file:
        line = file.readline()
        while line != "":
            with open(r'ipip.txt', 'a+') as f:
                print("写入文件中")
                f.write('----------------'+line.strip()+'-----------------\n')
                f.close()
            fofa_search('ip='+line+'&&(status_code=200 || status_code=302)', 100)
            line = file.readline()



  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值