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

admin 2023年6月24日00:40:31评论6 views字数 5200阅读17分20秒阅读模式

一.前言

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)

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

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")

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

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()

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

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()

原文始发于微信公众号(嗨嗨安全):干货|从无到有学习Python编写poc

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年6月24日00:40:31
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   干货|从无到有学习Python编写pochttps://cn-sec.com/archives/1828493.html

发表评论

匿名网友 填写信息