官网地址: https://mitmproxy.org/
github地址: https://github.com/mitmproxy
pypi地址: https://pypi.org/project/mitmproxy/
1.安装
pip install mitmproxy
2. 检测安装是否正常
完成后,系统将拥有 mitmproxy
、mitmdump
、mitmweb
三个命令,由于 mitmproxy
命令不支持在 windows 系统中运行(这没关系,不用担心),可以拿 mitmdump
测试一下安装是否成功,执行
mitmdump --version
运行成功,表明安装没有问题。
3. 运行
要启动 mitmproxy 用 mitmproxy
、mitmdump
、mitmweb
这三个命令中的任意一个即可,这三个命令功能一致,且都可以加载自定义脚本,唯一的区别是交互界面的不同。
mitmproxy
命令的交互操作稍显繁杂且不支持 windows 系统,而我们主要的使用方式又是载入自定义脚本,并不需要交互,所以原则上说只需要 mitmdump
即可,但考虑到有交互界面可以更方便排查错误,所以这里以 mitmweb
命令为例。
实际使用中可以根据情况选择任何一个命令。
mitmweb
mitmproxy 绑定了 *:8080
作为代理端口,并提供了一个 web 交互界面在 127.0.0.1:8081
-w 指定输出的文件
-s 指定抓包时执行的脚本
使用 mitmproxy 抓包时,发现很多请求会返回 413 错误,找到解决方案是抓包时候,添加 --set http2=false 参数,即:
mitmweb.exe -s .\gid.py --set http2=false
如果使用mitmdump -w d://lyc.txt
设定代理
开启抓包代理以后,浏览器打开链接 http://mitm.it/ ,安装SSL证书,便于抓取 HTTPS 。
4. 测试
关闭所有 Chrome 窗口,否则命令行启动时的附加参数将失效。打开 cmd,执行:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --proxy-server=127.0.0.1:8080 --ignore-certificate-errors
前面那一长串是 Chrome 的的安装路径,应当根据系统实际情况修改。
后面两参数 --proxy-server=127.0.0.1:8080设置了代理地址
--ignore-certificate-errors 强制忽略掉证书错误。
5. Python脚本的编写
import mitmproxy.http
import pickle
import os
import json
class GetSeq:
def __init__(self, domains=[], url_pattern=None, ):
self.num = 1
self.dirpath = "./flows/"
if not os.path.exists(self.dirpath):
os.mkdir(self.dirpath)
self.domains = domains
self.url_pattern = url_pattern
def http_connect(self, flow: mitmproxy.http.HTTPFlow):
"""
An HTTP CONNECT request was received. Setting a non 2xx response on
the flow will return the response to the client abort the
connection. CONNECT requests and responses do not generate the usual
HTTP handler events. CONNECT requests are only valid in regular and
upstream proxy modes.
"""
def requestheaders(self, flow: mitmproxy.http.HTTPFlow):
"""
HTTP request headers were successfully read. At this point, the body
is empty.
"""
def request(self, flow: mitmproxy.http.HTTPFlow):
"""
The full HTTP request has been read.
"""
def responseheaders(self, flow: mitmproxy.http.HTTPFlow):
"""
HTTP response headers were successfully read. At this point, the body
is empty.
"""
def response(self, flow: mitmproxy.http.HTTPFlow):
"""
The full HTTP response has been read.
"""
# 自行更改这里的保存代码,此处仅供参考
def save_flow():
fname = "{}flow-{:0>3d}-{}.pkl".format(self.dirpath, self.num, flow.request.host)
pickle.dump({
"num": self.num,
"request": flow.request,
"response": flow.response
}, open(fname, "wb"))
log_data = dict(
num = self.num,
url = flow.request.url,
fname = fname
)
with open("flow_que.log", "a+", encoding="utf8") as f:
s = json.dumps(log_data)
f.write(s)
self.num += 1
# 添加自己的过滤需求
if flow.request.headers.get('content-type', None) == "application/json":
save_flow()
if len(self.domains) == 0: save_flow()
for domain in self.domains:
if domain in flow.request.url:
save_flow()
def error(self, flow: mitmproxy.http.HTTPFlow):
"""
An HTTP error has occurred, e.g. invalid server responses, or
interrupted connections. This is distinct from a valid server HTTP
error response, which is simply a response with an HTTP error code.
"""
addons = [
GetSeq(
domains=[
"baidu.com",
],
url_pattern = None,
)
]
mitmproxy教程 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/371209542