POC批量验证Python脚本编写

更多黑客技能 公众号:白帽子左一

作者:掌控安全- webdogc

编写目的

批量验证poc,Python代码练习。

需求分析

1、poc尽可能简单。
2、多线程。
3、联动fofa获取目标。
4、随机请求头.

实现过程

脚本分为三个模块,获取poc及目标、多线程批量请求验证、输出结果。其中批量请求验证包括构造多线程,修改请求参数,发送请求三个部分。

Main函数

在main函数中,主要有三个部分获取poc及目标,多线程(将目标填充到队列,创建多线程并启动)、输出结果。
具体实现如下:
def main():
# 响应Ctrl+C停止程序
signal.signal(signal.SIGINT, quit)
signal.signal(signal.SIGTERM, quit)
showpocs()

## 获取目标
targetList = getTarget()

## 多线程批量请求验证
thread(targetList)

## 输出结果
putTarget(List)

获取目标

关于目标来源,设计单个目标、从文件中读取多个目标以及根据FoFa语法从FOFA_API中获取目标三种方式。


定义函数getTarget,函数分为两个部分

第一部分为根据 -f Fofa语法 获取目标,默认数目为30条,

第二部分为根据 -u url / -i file / -f num(数目,默认为10)获取要请求验证的目标,两部分以是否传参poc参数区别,最后返回一个targetList列表。

具体实现如下:

def getTarget():
    targetList=[]
    count=0
    if result.poc==None:
        if result.outfile!=None and result.fofa!=None :
            # FOFA读取目标
            if result.fofa!=None:
                qbase=result.fofa
                qbase64=str(base64.b64encode(qbase.encode("utf-8")), "utf-8")
                print("FOFA搜索:"+qbase)
                fofa_url="https://fofa.so/api/v1/search/all?email="+email+"&key="+key+"&qbase64="+qbase64+"&fields=title,host,ip,port,city&size=30"
                try:
                    res=requests.get(fofa_url)
                    results = json.loads(res.text)
                    filepath=result.outfile
                    with open(filepath,'w') as targets:
                        for i in results['results']:
                            targets.write(i[1]+'\n')
                            print(i[1])
                            count+=1
                        print("搜索结果有"+str(count)+"条,已保存在"+filepath+"里!")
                except Exception as e:
                    print(e)
            sys.exit()
    else:
        if result.url!=None or result.file!=None or result.fofa!=None:
            # 单个目标
            if result.url!=None:
                targetList.append(result.url)
            # 文件读取目标
            if result.file!=None:
                try:
                    filepath=result.file
                    with open(filepath,'r') as targets:
                        for target in targets.readlines():
                           targetList.append(target.strip())
                except Exception as e:
                    print(e)
            # FOFA读取目标
            if result.fofa!=None:
                qbase=""
                pocName = result.poc
                with open('poc.json',encoding='UTF-8') as f:
                    data = json.load(f)
                    for poc in data:
                        if pocName == poc:
                            qbase=data[poc]['fofa']
                qbase64=str(base64.b64encode(qbase.encode("utf-8")), "utf-8")
                try:
                    fofa_url="https://fofa.so/api/v1/search/all?email="+email+"&key="+key+"&qbase64="+qbase64+"&fields=title,host,ip,port,city&size="+str(result.fofa)
                    res=requests.get(fofa_url)
                    results = json.loads(res.text)
                    print("FOFA搜索:"+qbase)
                    print("搜索结果:"+str(result.fofa)+"条")
                    for i in results['results']:
                        targetList.append(i[1])
                    # print(targetList)
                except Exception as e:
                    print(e)
            return targetList
        else :
            sys.exit("参错有误!缺少目标!")

批量请求验证

定义thread函数,封装多线程请求相关代码,需传入获取到的目标参数targetList。
具体实现如下:

def thread(targetList):
    ## 获取poc
    poc=poc_load()

    ## 填充队列
    queueLock.acquire()
    for target in targetList:
        targetQueue.put(target)
    queueLock.release()

    ## 创建线程
    threadList = []
    threadNum=result.threadNum
    for i in range(0,threadNum):
        t=reqThread(targetQueue,poc)
        t.setDaemon(True)
        threadList.append(t)
    for i in threadList:
        i.start()

    # 等待所有线程完成
    for t in threadList:
        t.join()

加载POC
请求验证必须使用 -p pocName参数指定要使用的POC,所有POC在poc.json文件中存储。

具体实现如下:

·# 加载poc
def poc_load():
    if result.poc!=None:
        poc = result.poc
        isPoc = False # POC是否存在
        # 读取json文件
        with open('poc.json',encoding='UTF-8') as f:
            data = json.load(f)
            for key in data:
                if poc == key:
                    isPoc=True
        if isPoc==False:
            print("POC 不存在!")
            sys.exit("请通过--show查看poc列表!")
        else:
            return data[poc]
    else:
        pass

多线程类
定义reqThread线程类,传入队列以及poc两个参数,封装req请求方法。
具体实现如下:

class reqThread (threading.Thread):
    def __init__(self, q,poc):
        threading.Thread.__init__(self)
        self.q = q
        self.poc=poc
    def run(self):
        try:
            while not self.q.empty():
                queueLock.acquire()
                target=self.q.get()
                queueLock.release()
                if self.req(target):
                    print(target+" is vuln !")
                    List.append(target)
                else:
                    pass
        except Exception as e:
            pass
    def req(self,url):
        poc=self.poc
        payload=urlParse(url)+poc['request']['url']
        res=requests.request(method=poc['request'
  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值