d3f4lt0/tls-proxy
GitHub: d3f4lt0/tls-proxy
用 Go 实现的 MITM TLS 拦截代理,支持动态 CA 签发、流量解密检查及可编程的请求响应篡改流水线。
Stars: 2 | Forks: 0
# 🔐 TLS Proxy



**作者:** [d3f4lt0](https://github.com/d3f4lt0)
## 这是什么
一个透明的 MITM 代理,可以拦截普通的 HTTP 和 HTTPS 流量。它会在启动时生成自己的 Root CA,并按需为每个主机签发叶子证书,解密 TLS 会话,并将每一对请求/响应暴露给结构化的检查和篡改流水线。
本项目是对 Go 的 `crypto/tls` 内部机制、证书链以及 CONNECT 代理协议的一次实践探索——这些机制正是企业 DLP 网关、安全扫描器和渗透测试工具的核心基础。
## 架构
```
┌──────────────────────────────────────────────────────────────────┐
│ Client (browser / curl / app) │
│ → set proxy: http://localhost:8080 │
└───────────────────────────┬──────────────────────────────────────┘
│ HTTP CONNECT tunnel
▼
┌──────────────────────────────────────────────────────────────────┐
│ tls-proxy │
│ │
│ ┌─────────────┐ ┌──────────────┐ ┌────────────────────┐ │
│ │ Dynamic CA │───▶│ MITM Engine │───▶│ Tamper Engine │ │
│ │ │ │ │ │ (hook pipeline) │ │
│ │ • Root cert │ │ • Terminate │ │ │ │
│ │ • Per-host │ │ client TLS │ │ OnRequest hooks │ │
│ │ leaf cert │ │ • Re-encrypt │ │ OnResponse hooks │ │
│ │ • LRU cache │ │ upstream │ │ host glob match │ │
│ └─────────────┘ └──────┬───────┘ └────────────────────┘ │
│ │ │
│ ┌──────▼───────┐ │
│ │ Inspector │ │
│ │ • req/resp │ │
│ │ • timing │ │
│ │ • color log │ │
│ └─────────────┘ │
└───────────────────────────┬──────────────────────────────────────┘
│ re-encrypted TLS
▼
┌───────────────┐
│ Target server│
└───────────────┘
```
## 快速开始
```
# 构建
go build -o tls-proxy ./cmd/tls-proxy
# 运行(在当前目录中生成 ca.crt)
./tls-proxy -addr :8080
# Verbose 模式(输出 headers + bodies)
./tls-proxy -addr :8080 -v
# Passthrough 模式(无拦截隧道)
./tls-proxy -addr :8080 -passthrough
```
然后将你的客户端配置为使用 `http://localhost:8080` 作为 HTTP/HTTPS 代理。
## 安装 Root CA
启动时,`ca.crt` 会被写入当前目录。请将其安装到你的信任库中以避免 TLS 警告:
```
# macOS
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ca.crt
# Linux (Debian/Ubuntu)
sudo cp ca.crt /usr/local/share/ca-certificates/tls-proxy.crt && sudo update-ca-certificates
# curl / one-shot
curl -x http://localhost:8080 --cacert ca.crt https://example.com
```
## 输出示例
```
[#0001] 13:42:07.114 GET https://example.com/ 200 87ms
[#0002] 13:42:07.882 GET https://example.com/favicon.ico 404 12ms
[#0003] 13:42:09.001 POST https://api.example.com/v1/data 201 203ms
```
详细模式(`-v`)会额外导出所有的请求/响应标头以及截断的正文预览。
## 篡改引擎
以编程方式挂载请求/响应钩子:
```
engine := tamper.New()
// Inject a header into every request
engine.Add(tamper.AddRequestHeader("*", "X-Forwarded-For", "1.2.3.4"))
// Rewrite response bodies matching a host glob
engine.Add(tamper.ReplaceResponseBody("*.example.com", "staging", "production"))
// Custom hook with full access to req/resp
engine.Add(&tamper.Rule{
HostGlob: "api.target.com",
Methods: []string{"POST"},
OnRequest: func(req *http.Request) error {
req.Header.Set("Authorization", "Bearer ")
return nil
},
})
```
## 项目结构
```
tls-proxy/
├── cmd/tls-proxy/ ← CLI entrypoint, flags, wiring
├── internal/
│ ├── ca/ ← Dynamic CA: root cert + per-host leaf issuance + cache
│ ├── proxy/ ← MITM engine: HTTP + CONNECT handler, TLS termination
│ ├── inspector/ ← Traffic capture, timing, colored terminal output
│ └── tamper/ ← Rule pipeline: host glob + method match + hooks
└── .github/workflows/ ← CI: build + test + cross-platform release binaries
```
## 使用场景
- **渗透测试 / 红队** — 拦截并修改目标应用程序的流量
- **WAF 研究** — 检查企业代理添加了哪些标头/信号
- **API 调试** — 准确查看你的 HTTPS 客户端发送了什么
- **安全工具开发** — 构建自定义流量分析流水线的基础
- **协议学习** — 实践学习 TLS CONNECT 代理流程
## 相关项目
| 项目 | 功能 |
|---|---|
| [`http2-inspector`](https://github.com/d3f4lt0/http2-inspector) | 帧级别的 HTTP/2 协议追踪器 |
| [`fingerprint-audit`](https://github.com/d3f4lt0/fingerprint-audit) | 浏览器指纹诊断套件 |
| [`anti-bot-engine`](https://github.com/d3f4lt0/anti-bot-engine) | Go 中间件:速率限制 + 熵检测 + 蜜罐 |
| [`quantum-stealth-kernel`](https://github.com/d3f4lt0/quantum-stealth-kernel) | JA3/TLS 模拟 + CDP 指纹中和 |
## 许可证
MIT
标签:DLL注入, EVTX分析, Go, Ruby工具, TLS解密, 中间人攻击, 日志审计, 流量审计