mitmproxy 入门

项目地址:mitmproxy/mitmproxy: An interactive TLS-capable intercepting HTTP proxy for penetration testers and software developers. (github.com) 使用文档:https://docs.mitmproxy.org/stable/overview-getting-started/

📌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.

mitmproxy是一个用python写成的http代理调试工具,有终端和web界面,完全开源,安装简单,使用方便,编写插件简单,但是手动调试相对burp要差一些。

安装

pip安装:pip/pip3 install mitmproxy

源码安装:https://github.com/mitmproxy/mitmproxy/blob/main/CONTRIBUTING.md

使用

1 终端代理

mitmproxy [options]

直接代理,会显示终端界面,默认监听在8080端口,可以通过一些按键进行操作,详情可以参考使用文档:User Interface (mitmproxy.org)

同时可以添加一些选项,常用的有-q, --quiet静默模式(不显示输出),--listen-host HOST指定监听地址,--listen-port PORT, -p PORT指定监听端口,--scripts SCRIPT, -s加载指定脚本,更多选项可以加-h查看。

2 web界面

mitmweb [options]

启动一个web UI界面,可以在界面代替终端进行一些操作,原理上就是通过web UI启动了一个mitmproxy,web UI监听在独立的端口。

也可以添加一些选项,大部分选项与终端代理重合,这里提一下--web-port PORTweb UI绑定端口,--web-host HOSTweb UI绑定地址。

3 编写脚本

插件开发说明:Addons (mitmproxy.org)

下面是一个简单示例,运行脚本代理,监听在9099端口,如果要看实时输出,将mitmproxy替换为mitmdump

mitmproxy -s TestAddon.py -p 9099

## TestAddon.py
from mitmproxy import ctx

class TestAddon:
    def __init__(self):
        pass

    def request(self,flow):
        ctx.log.info("print flow request{}".format(flow.request))
    def response(self,flow):
        ctx.log.info("print flow response{}".format(flow.response))

addons = [TestAddon()]

4 使用技巧

设置上游(二级)代理:mitmdump --mode upstream:http://127.0.0.1:19998 [-s custom.py]

可以通过设置upstream模式,使流量先经过mitmproxy,然后经过上游代理,相当于为mitmproxy设置一个代理。可以通过添加脚本,进行预处理,在发送请求之前对请求做处理,可以实现解密,pac代理等效果。