freeBuf
干货|从无到有学习Python编写poc
2023-06-28 21:58:22

干货|从无到有学习Python编写poc

声明

该公众号大部分文章来自作者日常学习笔记,也有部分文章是经过作者授权和其他公众号白名单转载。请勿利用文章内的相关技术从事非法测试,如因此产生的一切不良后果与文章作者和本公众号无关。

一.前言

1.确定目标

首先要明确你的目标是什么,你是想利用什么漏洞!

2.漏洞研究

在编写POC之前,深入研究目标漏洞是至关重要的。在博客,公众号,Github上收集相关资料,了解漏洞的工作原理、漏洞利用条件和可能的影响。

3.环境设置

根据目标漏洞的类型和相关应用程序的版本信息或系统的环境,搭建一个合适的实验环境。

4.编写POC

当你尝试编写POC来验证漏洞时,有几个关键方面需要特别关注。

1.你需要确定漏洞的输入点,也就是漏洞所接受的输入类型。

2.你需要构造恶意输入,以触发漏洞。

3.你需要进行目标验证。

二.编写

1.URL处理

url处理包括:识别url前缀,没有http&https的添加http或者https删除url路径部分url结尾有/ 则去掉/

def process_url(url):
    # 添加http或https前缀
    if not url.startswith('http://') and not url.startswith('https://'):
        url = 'http://' + url

    # 删除URL路径部分
    url_parts = url.split('/')
    url_without_path = '/'.join(url_parts[:3])

    # 去掉URL末尾的斜杠
    if url_without_path.endswith('/'):
        url_without_path = url_without_path[:-1]

    return url_without_path

# 主函数
if __name__ == "__main__":
    input_url = input("请输入URL:")
    processed_url = process_url(input_url)
    print("处理后的URL:", processed_url)

1687960361_649c3b29cbbb996b8800b.png!small?1687960362405

2.状态码识别

首先编写获取url状态码函数:getCode

import requests

def getCode(url):
    try:
        response = requests.get(url)
        return response.status_code
    except requests.exceptions.RequestException:
        return None

# 测试
if __name__ == "__main__":
    target_url = input("请输入目标URL:")
    status_code = getCode(target_url)

    if status_code is not None:
        print("状态码:", status_code)
    else:
        print("无法连接到URL")

1687960408_649c3b5846e3e57e78bc2.png!small?1687960408695

3.文件读取

为方便批量漏洞验证,增加文件中读取url的函数,编写文件读取函数:readFile

def readFile(filename):
    try:
        with open(filename, 'r') as file:
            urls = [line.strip() for line in file.readlines()]
        return urls
    except IOError:
        print(f"无法读取文件: {filename}")
        return []

# 测试示例
file_path = 'urls.txt'  # 文件路径
url_list = readFile(file_path)
print(url_list)  # 输出文件中的所有URL列表


合并上述所有代码,编写代码

import requests

def process_url(url):
    # 添加http或https前缀
    if not url.startswith('http://') and not url.startswith('https://'):
        url = 'http://' + url

    # 删除URL路径部分
    url_parts = url.split('/')
    url_without_path = '/'.join(url_parts[:3])

    # 去掉URL末尾的斜杠
    if url_without_path.endswith('/'):
        url_without_path = url_without_path[:-1]

    return url_without_path

def getCode(url):
    try:
        response = requests.get(url)
        return response.status_code
    except requests.exceptions.RequestException:
        return None

def readFile(filename):
    try:
        with open(filename, 'r') as file:
            urls = [line.strip() for line in file.readlines()]
        return urls
    except IOError:
        print(f"无法读取文件: {filename}")
        return []

def main():
    # 读取URL列表
    filename = input("请输入URL文件名:")
    urls = readFile(filename)

    if len(urls) == 0:
        print("未找到URL。")
        return

    # 处理每个URL并获取状态码
    for url in urls:
        processed_url = process_url(url)
        status_code = getCode(processed_url)

        print(f"\nURL: {processed_url}\n状态码: {status_code}")


# 主函数入口
if __name__ == "__main__":
    main()

1687960489_649c3ba9b2e6f9c755596.png!small?1687960490268


4.POC验证

首先要知道响应包里要验证的特征

匹配规则如下

response.status_code相应的状态码
response.json()响应体的特殊字段
response.text响应体里的文本内容
response.headers响应头字段
response.elapsed响应包的响应时间
response.content响应体的二进制数据

Post方法

结合上述代码验证模板(匹配状态码200)

import requests

def process_url(url):
    # 添加http或https前缀
    if not url.startswith('http://') and not url.startswith('https://'):
        url = 'http://' + url
    
    # 删除URL路径部分
    url_parts = url.split('/')
    url_without_path = '/'.join(url_parts[:3])
    
    # 去掉URL末尾的斜杠
    if url_without_path.endswith('/'):
        url_without_path = url_without_path[:-1]
    
    return url_without_path

def getCode(url):
    try:
        response = requests.get(url)
        return response.status_code
    except requests.exceptions.RequestException:
        return None

def poc_post(target):
    url = target + "/vulnerable_endpoint"  # 将目标URL替换为实际的漏洞点
    
    data = {
        'param1': 'value1',  # 根据漏洞点需要设置合适的参数和值
        'param2': 'value2'
    }

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }  # 可根据需要添加自定义请求头

    try:
        response = requests.post(url, data=data, headers=headers)
        
        if response.status_code == 200:  # 根据实际情况确定漏洞点返回状态码
            print("漏洞存在!")
            # 进一步处理漏洞,例如提取敏感信息或执行命令等
        else:
            print("漏洞不存在。")

    except requests.exceptions.RequestException as e:
        print("请求发生异常:", e)

def readFile(filename):
    try:
        with open(filename, 'r') as file:
            urls = [line.strip() for line in file.readlines()]
        return urls
    except IOError:
        print(f"无法读取文件: {filename}")
        return []

def main():
    # 读取URL列表
    filename = input("请输入URL文件名:")
    urls = readFile(filename)

    if len(urls) == 0:
        print("未找到URL。")
        return

    # 处理每个URL并获取状态码
    for url in urls:
        processed_url = process_url(url)
        
        print(f"\n验证URL: {processed_url}")
        
        # 使用POST请求进行POC测试
        poc_post(processed_url)


# 主函数入口
if __name__ == "__main__":
    main()

Get方法

结合上述代码验证模板(匹配状态码200)

import requests

def process_url(url):
    # 添加http或https前缀
    if not url.startswith('http://') and not url.startswith('https://'):
        url = 'http://' + url
    
    # 删除URL路径部分
    url_parts = url.split('/')
    url_without_path = '/'.join(url_parts[:3])
    
    # 去掉URL末尾的斜杠
    if url_without_path.endswith('/'):
        url_without_path = url_without_path[:-1]
    
    return url_without_path

def getCode(url):
    try:
        response = requests.get(url)
        return response.status_code
    except requests.exceptions.RequestException:
        return None

def poc_get(target):
    url = target + "/vulnerable_endpoint"  # 将目标URL替换为实际的漏洞点
    
    params = {
        'param1': 'value1',  # 根据漏洞点需要设置合适的参数和值
        'param2': 'value2'
    }

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }  # 可根据需要添加自定义请求头

    try:
        response = requests.get(url, params=params, headers=headers)
        
        if response.status_code == 200:  # 根据实际情况确定漏洞点返回状态码
            print("漏洞存在!")
            # 进一步处理漏洞,例如提取敏感信息或执行命令等
        else:
            print("漏洞不存在。")

    except requests.exceptions.RequestException as e:
        print("请求发生异常:", e)

def readFile(filename):
    try:
        with open(filename, 'r') as file:
            urls = [line.strip() for line in file.readlines()]
        return urls
    except IOError:
        print(f"无法读取文件: {filename}")
        return []

def main():
    # 读取URL列表
    filename = input("请输入URL文件名:")
    urls = readFile(filename)

    if len(urls) == 0:
        print("未找到URL。")
        return

    # 处理每个URL并获取状态码
    for url in urls:
        processed_url = process_url(url)
        
        print(f"\n验证URL: {processed_url}")
        
        # 使用POST请求进行POC测试
        poc_get(processed_url)


# 主函数入口
if __name__ == "__main__":
    main()

本文章来源嗨嗨安全

觉得内容不错,请点一下"赞"和"在看"

本文为 独立观点,未经允许不得转载,授权请联系FreeBuf客服小蜜蜂,微信:freebee2022
被以下专辑收录,发现更多精彩内容
+ 收入我的专辑
+ 加入我的收藏
文章目录