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

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

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

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

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

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


3、透明代理配置 & 使用

(1)配置参考: Transparent Proxying

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

macOS
#  1. Enable IP forwarding.
sudo sysctl -w net.inet.ip.forwarding=1
#  2. Place the following line in a file called, say, pf.conf.
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 pf.conf
#  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 mitmHandler.py

#  7. Finally, configure your test device.
手机配置网关 & 下载证书安装
- wifi - IP设置 - 静态 - 路由器/网关改成:开启mitmproxy 的 pc 的 ip
- mitm.it,下载对应证书安装
- 手机上点开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/master.py", 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/compat.py", line 58, in reraise
File "asyncio/selector_events.py", line 164, in _accept_connection
File "socket.py", line 293, in accept
OSError: [Errno 24] Too many open files

mitmproxy has crashed!
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值