Mitmproxy 数据包解密实战篇
字数 1087 2025-08-19 12:41:20
Mitmproxy 数据包解密实战教程
1. Mitmproxy 简介
mitmproxy 是一组工具,提供用于 HTTP/1、HTTP/2 和 WebSocket 的交互式、支持 SSL/TLS 的拦截代理。它包含三个主要组件:
| 工具名称 | 描述 |
|---|---|
| mitmproxy | 交互式控制台界面的拦截代理 |
| mitmweb | 基于网页界面的拦截代理 |
| mitmdump | 命令行版本,可理解为 HTTP 的 tcpdump |
2. 安装方法
macOS 安装
brew install mitmproxy
其他系统
从mitmproxy官网下载对应版本安装
3. 基本使用
3.1 编写插件示例
以下是一个简单的插件示例,用于在每个响应中添加 HTTP 头:
class AddHeader:
def __init__(self):
self.num = 0
def response(self, flow):
self.num = self.num + 1
flow.response.headers["count"] = str(self.num)
addons = [AddHeader()]
3.2 启动 mitmdump
mitmdump -s http-add-header.py -p 6666
然后配置浏览器代理为 127.0.0.1:6666
4. 应用实战:数据包解密
4.1 加密/解密分析
从 Java 代码分析加密逻辑:
public static String[] decode(String data) {
byte[] dataByte = CodeHandler.strToByteArrayByUTF8(data);
dataByte = CodeHandler.deCode(dataByte);
String decodedData = CodeHandler.byteArrayToStrByUTF8(dataByte);
// ...
}
public byte[] decode(byte[] dataByte) {
int i = 0;
for(int j = 0; j < dataByte.length; ++j) {
byte tmp = dataByte[i];
if (tmp > 0 && tmp < decodeArray.length) {
byte encodeChar = decodeArray[tmp];
if (encodeChar != 0) {
dataByte[i] = encodeChar;
}
}
++i;
}
return dataByte;
}
4.2 Python 解密脚本实现
import logging
import re
class Mimit():
decode_array = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32, 87, 0, 0, 0, 47, 0, 56, 97, 89, 84, 43, 0, 103, 106, 37,
113, 49, 121, 78, 114, 112, 110, 48, 76, 55, 123, 0, 0, 0, 0, 0,
0, 40, 88, 120, 115, 41, 77, 107, 71, 104, 53, 52, 80, 54, 51, 65,
33, 117, 105, 108, 68, 90, 66, 83, 122, 81, 86, 93, 0, 91, 0, 102,
0, 69, 119, 73, 109, 126, 45, 118, 100, 99, 82, 116, 75, 57, 39, 79,
101, 46, 72, 42, 67, 50, 74, 111, 70, 95, 85, 58, 0, 0, 98, 0
]
def request(self, flow):
if flow.request.path.endswith("/RMIServlet") and flow.request.method == "POST":
req = flow.request.get_text()
logging.info("requestData:" + req)
match = re.search(r'encode=([^&]+)', req)
decoded_str = ""
if match:
encode_value = match.group(1)
data_byte = bytearray(encode_value, "UTF-8")
decoded_byte = self.decode(data_byte)
decoded_str = decoded_byte.decode("UTF-8")
logging.info(decoded_str)
else:
logging.info("Encode value not found")
array = decoded_str.split("+")
data = f'className={array[0]}&methodName={array[1]}&params={array[2]}'
flow.request.set_text(data)
def response(self, flow):
if flow.request.path.endswith("/RMIServlet") and flow.request.method == "POST":
response = flow.response.get_text()
data_byte = bytearray(response, "UTF-8")
encoded_byte = self.encode(data_byte)
encoded_str = encoded_byte.decode("UTF-8")
flow.response.set_text(encoded_str)
def decode(self, data_byte):
for i in range(len(data_byte)):
tmp = data_byte[i]
if 0 < tmp < len(self.decode_array):
encode_char = self.decode_array[tmp]
if encode_char != 0:
data_byte[i] = encode_char
return data_byte
def encode(self, data_byte):
for i in range(len(data_byte)):
tmp = data_byte[i]
if 0 < tmp < len(self.decode_array) and tmp not in (37, 47):
encode_char = self.decode_array[tmp]
if encode_char != 0:
data_byte[i] = encode_char
return data_byte
addons = [Mimit(), ]
4.3 代理配置
使用以下命令启动 mitmdump:
mitmdump -p 6666 -s main.py --mode upstream:http://127.0.0.1:6662 --ssl-insecure
配置说明:
-p 6666:设置监听端口-s main.py:指定插件脚本--mode upstream:http://127.0.0.1:6662:设置上游代理为 BurpSuite--ssl-insecure:忽略 SSL 证书验证
BurpSuite 配置:
- 打开 Proxy → Options
- 添加 Proxy listener,绑定 6662 端口
- 确保 "Support invisible proxying" 选项启用
5. 其他配置方式
5.1 双向加密/解密流程
当服务器只识别密文时,可以配置两重代理:
- 第一层 mitmdump(解密):
mitmdump -p 6666 -s dec.py --mode upstream:http://127.0.0.1:6662 --ssl-insecure
- 第二层 mitmdump(加密):
mitmdump -p 7777 -s enc.py
- BurpSuite 配置:
- 在 Proxy → Options → Upstream Proxy Servers 中添加规则
- 目标主机:应用服务器地址
- 代理主机:127.0.0.1
- 代理端口:7777
6. 关键点总结
- 插件开发:通过编写 Python 插件实现请求/响应的修改
- 代理链:使用
--mode upstream构建代理链 - SSL 绕过:
--ssl-insecure参数忽略证书验证 - 双向处理:请求解密和响应加密需要分别处理
- BurpSuite 集成:通过上游代理配置实现与 BurpSuite 的联动