【Mitmproxy】Mac + Python + mitmproxy透明代理配置,拦截所有网络请求

时间:2025-03-19 06:57:06

Mac OS Monterey 版本 12.1
mitmproxy==8.0.0
Mitmproxy --version
Mitmproxy: 8.0.
Python: 3.9.13
OpenSSL: OpenSSL 1.1.1m 14 Dec 2021
Platform: macOS-12.1-x86_64-i386-64bit

        • 1、Mitmproxy环境安装
        • 2、一个正向代理场景
        • 3、透明代理配置 & 使用
        • 4、常见报错-filenotfounderror & mitmproxy has crashed!

1、Mitmproxy环境安装
brew install mitmproxy
pip3 install mitmproxy

2、一个正向代理场景
  • 本地写一个mitm脚本(脚本类似),执行:mitmdump -s
  • 如果脚本是用于拦截网络请求时,可能会出现这样的情况:

比如进入页面 A,mitmdump 打印出拦截请求 123
但再进去页面 A,mitmdump 打印出拦截请求 13,其中请求 2 丢了

解决办法就是用透明代理。


3、透明代理配置 & 使用

(1)配置参考: Transparent Proxying

// 官网上有些命令不好使,以下是完整成功的操作步骤

macOS
#  1. Enable IP forwarding.
sudo sysctl -w =1
#  2. Place the following line in a file called, say, .
echo "rdr pass on bridge100 inet proto tcp to any port {80, 443} -> 127.0.0.1 port 8080" | sudo pfctl -ef -
#  3. Configure pf with the rules.
sudo pfctl -f 
#  4. And now enable it.
sudo pfctl -e
#  5. Configure sudoers to allow mitmproxy to access pfctl.
Edit the file /etc/sudoers on your system as root. Add the following line to the end of the file:
// 这个文件修改 /etc/sudoers ,必须是用 vim 编辑,不要手动编辑;
// 提前把  /etc/sudoers 读写权限开启 - 显示简介 - 共享与权限 - 权限 - 读与写

ALL ALL=NOPASSWD: /sbin/pfctl -s state

// 修改完后,再把  /etc/sudoers 读写权限改成仅可读

#  6. Fire up mitmproxy.
mitmdump --mode  transparent -s 

#  7. Finally, configure your test device.
手机配置网关 & 下载证书安装
- wifi - IP设置 - 静态 - 路由器/网关改成:开启mitmproxy 的 pc 的 ip
- ,下载对应证书安装
- 手机上点开app,网络请求正常拦截了了了!
#The ports to redirec
4、常见报错-filenotfounderror & mitmproxy has crashed!

如果开启后,设备网络请求时出现以下报错,都是透明代理没配置好。(基于我的情况????

failure: filenotfounderror(2, 'no such file or directory')
Traceback (most recent call last):
File "mitmproxy/", line 54, in run_loop
File "urwid/main_loop.py", line 287, in run
File "urwid/main_loop.py", line 385, in _run
File "urwid/main_loop.py", line 1494, in run
File "urwid/", line 58, in reraise
File "asyncio/selector_events.py", line 164, in _accept_connection
File "", line 293, in accept
OSError: [Errno 24] Too many open files

mitmproxy has crashed!