1.安装
首先,你需要安装 mitmproxy:
pip install mitmproxy
2.编写代码开启请求监听
from mitmproxy import http
from json import loads, dumps # 导入 dumps 用于将字典转换为 JSON 字符串
def request(flow: http.HTTPFlow) -> None:
# 检查请求的 URL 是否是你想要监听的
if "https://lochost" in flow.request.pretty_url:
print(f"Intercepted request to: {flow.request.pretty_url}")
# 打印原始请求的 Body
if flow.request.content:
print("Original Request Body:", flow.request.content.decode('utf-8'))
Original = flow.request.content.decode('utf-8')
print(111111)
json_data = loads(Original) # 解析原始请求的 Body 为 JSON 对象
print(json_data)
print(222222)
json_data['username'] = 'admin\' --' # 修改 JSON 数据
# 将修改后的 JSON 数据转换为字符串,再编码为 bytes
modified_body = dumps(json_data).encode('utf-8') # 替换为你想要的内容
print(modified_body)
print(333333)
flow.request.content = modified_body # 赋值给 flow.request.content
print(444444)
print(flow.request.content)
# 打印修改后的请求 Body
print("Modified Request Body:", flow.request.content.decode('utf-8'))
3.打开本地代理
4.启动
mitmproxy -s modify_requests.py -p 8081
-p
自定义端口