背景
在我看来抓包功能和Burp一样,但有时候需要对请求和响应高度自定义修改,比如参数中的limit
要调整为50,但offset
需要动态调整为0 50 100 150 200 ...
,且还需要根据时间戳计算签名,这个时候用burp实现可能就比较麻烦了,所以需要有一种新的高度自定义扩展的方案。
介绍
mitmproxy
is an interactive, SSL/TLS-capable intercepting proxy with a console interface for HTTP/1, HTTP/2, and WebSockets.
mitmdump
is the command-line version of mitmproxy. Think tcpdump for HTTP.
mitmweb
is a web-based interface for mitmproxy.
安装
pip3 install mitmproxy
安装后会有介绍中的3个工具,mitmproxy
、mitmdump
、mitmweb
,三个命令实际功能都差不多,只是展示的形式不太一样。
我平时感觉就 mitmproxy
(纯命令行)或者 mitmweb
(提供1个WEB UI)就够了。
基础使用
监听1234端口
mitmweb --listen-port 1234 --web-port 1235
mitmproxy --listen-port 1234
启动后和burp一样需要安装对应的CA证书,挂上http://127.0.0.1:1234
代理访问 http://mitm.it/
即可。
配置好后就可以正常看到流量包了。
编写扩展
上面能看到数据包就说明配置好了,现在就是编写自定义的扩展了,来实现修改我们的请求和响应。(这里只举例常用的修改http请求和响应,当然他还支持很多其他的,可参考 addons-overview
编写py脚本如下:
import mitmproxy.http
from mitmproxy import ctx
from mitmproxy import flowfilter
class Interceptor:
def __init__(self):
# 添加网址过滤器
self.filter = flowfilter.parse("~u https://www.baidu.com")
# 修改请求
def request(self, flow: mitmproxy.http.HTTPFlow):
if flowfilter.match(self.filter, flow):
ctx.log.info(f"get keyword {flow.request.query['wd']}")
# 替换搜索词
flow.request.query["wd"] = "123"
# 修改响应
def response(self, flow: mitmproxy.http.HTTPFlow):
if flowfilter.match(self.filter, flow):
ctx.log.info(f"get response lens {len(flow.response.content)}")
# 添加/修改headers
flow.response.headers["mitm"] = "test"
flow.response.content = "66666".encode(encoding="utf-8")
addons = [
Interceptor()
]
编写好后使用mitmproxy
加载即可。(注:mitmproxy
支持热更新,修改脚本保存后会自动重新加载
mitmweb --listen-port 1234 --web-port 1235 -s mitmproxytest.py
可见请求后wd参数被自动替换,响应也被替换。