x011/smtp-tunnel-proxy

GitHub: x011/smtp-tunnel-proxy

一个将 TCP 流量伪装成 SMTP 邮件以规避 DPI 的 Python 隧道代理工具。

Stars: 1560 | Forks: 144

# 📧 SMTP 隧道代理 ``` ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌──────────────┐ │ Application │─────▶│ Client │─────▶│ Server │─────▶│ Internet │ │ (Browser) │ TCP │ SOCKS5:1080 │ SMTP │ Port 587 │ TCP │ │ │ │◀─────│ │◀─────│ │◀─────│ │ └─────────────┘ └─────────────┘ └─────────────┘ └──────────────┘ │ │ │ Looks like │ │ Email Traffic │ ▼ ▼ ┌────────────────────────────────┐ │ DPI Firewall │ │ ✅ Sees: Normal SMTP Session │ │ ❌ Cannot see: Tunnel Data │ └────────────────────────────────┘ ``` ## 🎯 功能特性 | 功能 | 描述 | |---------|-------------| | 🔒 **TLS 加密** | 所有流量在 STARTTLS 后使用 TLS 1.2+ 加密 | | 🎭 **DPI 规避** | 初始握手模仿真实 SMTP 服务器(Postfix) | | ⚡ **高速传输** | 握手后使用二进制流协议 - 开销极小 | | 👥 **多用户支持** | 每个用户独立的密钥、IP 白名单和日志设置 | | 🔑 **身份认证** | 使用 HMAC-SHA256 的每个用户预共享密钥 | | 🌐 **SOCKS5 代理** | 标准代理接口,兼容任何应用程序 | | 📡 **多路复用** | 单个隧道承载多个连接 | | 🛡️ **IP 白名单** | 基于 IP 地址/CIDR 的用户访问控制 | | 📦 **便捷安装** | 一键安装服务器并配置 systemd 服务 | | 🎁 **客户端包** | 为每个用户自动生成 ZIP 压缩包 | | 🔄 **自动重连** | 客户端在连接丢失时自动重连 | ## ⚡ 快速开始 ### 📋 先决条件 - **服务器**: 已安装 Python 3.8+ 的 Linux VPS,开放 587 端口 - **客户端**: 安装 Python 3.8+ 的 Windows/macOS/Linux - **域名**: 用于 TLS 证书验证(免费选项: [DuckDNS](https://www.duckdns.org), [No-IP](https://www.noip.com), [FreeDNS](https://freedns.afraid.org)) ## 🚀 服务器设置(VPS) ### 步骤 1️⃣: 获取域名 获取指向你 VPS 的免费域名: - 🦆 **[DuckDNS](https://www.duckdns.org)** - 推荐,简单免费 - 🌐 **[No-IP](https://www.noip.com)** - 提供免费套餐 - 🆓 **[FreeDNS](https://freedns.afraid.org)** - 提供多种域名选择 示例:`myserver.duckdns.org` → `203.0.113.50`(你的 VPS IP) ### 步骤 2️⃣: 运行安装器 ``` curl -sSL https://raw.githubusercontent.com/x011/smtp-tunnel-proxy/main/install.sh | sudo bash ``` 安装程序将执行以下操作: 1. 📥 下载并安装所有依赖 2. ❓ 询问你的域名 3. 🔐 自动生成 TLS 证书 4. 👤 创建第一个用户 5. 🔥 配置防火墙 6. 🚀 启动服务 **完成!** 你的服务器已就绪。 ### ➕ 后续添加更多用户 ``` smtp-tunnel-adduser bob # Add user + generate client ZIP smtp-tunnel-listusers # List all users smtp-tunnel-deluser bob # Remove a user ``` ### 🔄 更新服务器 ``` smtp-tunnel-update # Updates code, preserves config/certs/users ``` ## 💻 客户端设置 ### 选项 A:简便方式(推荐) 1. 从服务器管理员获取 `username.zip` 文件 2. 解压 ZIP 文件 3. 运行启动器: | 平台 | 运行方式 | |----------|------------| | 🪟 **Windows** | 双击 `start.bat` | | 🐧 **Linux** | 运行 `./start.sh` | | 🍎 **macOS** | 运行 `./start.sh` | 启动器会自动安装依赖并启动客户端。 ✅ 你应该看到: ``` SMTP Tunnel Proxy Client User: alice [INFO] Starting SMTP Tunnel... [INFO] SOCKS5 proxy will be available at 127.0.0.1:1080 Connecting to myserver.duckdns.org:587 Connected - binary mode active SOCKS5 proxy on 127.0.0.1:1080 ``` ### 选项 B:手动方式 ``` cd alice pip install -r requirements.txt python client.py ``` ### 选项 C:自定义配置 ``` # 下载文件 scp root@myserver.duckdns.org:/etc/smtp-tunnel/ca.crt . # 创建 config.yaml: cat > config.yaml << EOF client: server_host: "myserver.duckdns.org" server_port: 587 socks_port: 1080 username: "alice" secret: "your-secret-from-admin" ca_cert: "ca.crt" EOF # 运行客户端 python client.py -c config.yaml ``` ## 📖 使用说明 ### 🌐 配置你的应用程序 将 SOCKS5 代理设置为:`127.0.0.1:1080` #### 🦊 Firefox 1. 设置 → 网络设置 → 设置 2. 手动代理配置 3. SOCKS 主机: `127.0.0.1`,端口: `1080` 4. 选择 SOCKS v5 5. ✅ 勾选“使用 SOCKS v5 时代理 DNS” #### 🌐 Chrome 1. 安装 "Proxy SwitchyOmega" 扩展 2. 创建使用 SOCKS5 的配置:`127.0.0.1:1080` #### 🪟 Windows(系统级) 设置 → 网络和 Internet → 代理 → 手动设置 → `socks=127.0.0.1:1080` #### 🍎 macOS(系统级) 系统偏好设置 → 网络 → 高级 → 代理 → SOCKS 代理 → `127.0.0.1:1080` #### 🐧 Linux(系统级) ``` export ALL_PROXY=socks5://127.0.0.1:1080 ``` #### 💻 命令行 ``` # curl curl -x socks5h://127.0.0.1:1080 https://ifconfig.me # git git config --global http.proxy socks5://127.0.0.1:1080 # 环境变量 export ALL_PROXY=socks5://127.0.0.1:1080 ``` ### ✅ 测试连接 ``` # 应显示您的 VPS IP curl -x socks5://127.0.0.1:1080 https://ifconfig.me ``` ## ⚙️ 配置参考 ### 🖥️ 服务器选项(`config.yaml`) | 选项 | 描述 | 默认值 | |--------|-------------|---------| | `host` | 监听接口 | `0.0.0.0` | | `port` | 监听端口 | `587` | | `hostname` | SMTP 主机名(必须与证书匹配) | `mail.example.com` | | `cert_file` | TLS 证书路径 | `server.crt` | | `key_file` | TLS 私钥路径 | `server.key` | | `users_file` | 用户配置文件路径 | `users.yaml` | | `log_users` | 全局日志记录设置 | `true` | ### 👥 用户选项(`users.yaml`) 每个用户可拥有独立设置: ``` users: alice: secret: "auto-generated-secret" # whitelist: # Optional: restrict to specific IPs # - "192.168.1.100" # - "10.0.0.0/8" # CIDR notation supported # logging: true # Optional: disable to stop logging this user bob: secret: "another-secret" whitelist: - "203.0.113.50" # Bob can only connect from this IP logging: false # Don't log Bob's activity ``` | 选项 | 描述 | 默认值 | |--------|-------------|---------| | `secret` | 用户认证密钥 | 必需 | | `whitelist` | 允许的 IP 列表(支持 CIDR) | 全部 IP | | `logging` | 启用该用户的活动日志 | `true` | ### 💻 客户端选项 | 选项 | 描述 | 默认值 | |--------|-------------|---------| | `server_host` | 服务器域名 | 必需 | | `server_port` | 服务器端口 | `587` | | `socks_port` | 本地 SOCKS5 端口 | `1080` | | `socks_host` | 本地 SOCKS5 接口 | `127.0.0.1` | | `username` | 你的用户名 | 必需 | | `secret` | 你的认证密钥 | 必需 | | `ca_cert` | CA 证书用于验证 | 推荐 | ## 📋 服务管理 ``` # 检查状态 sudo systemctl status smtp-tunnel # 配置更改后重启 sudo systemctl restart smtp-tunnel # 查看日志 sudo journalctl -u smtp-tunnel -n 100 # 卸载 sudo /opt/smtp-tunnel/uninstall.sh ``` ## 🔧 命令行选项 ### 🖥️ 服务器 ``` python server.py [-c CONFIG] [-d] -c, --config Config file (default: config.yaml) -d, --debug Enable debug logging ``` ### 💻 客户端 ``` python client.py [-c CONFIG] [--server HOST] [--server-port PORT] [-p SOCKS_PORT] [-u USERNAME] [-s SECRET] [--ca-cert FILE] [-d] -c, --config Config file (default: config.yaml) --server Override server domain --server-port Override server port -p, --socks-port Override local SOCKS port -u, --username Your username -s, --secret Override secret --ca-cert CA certificate path -d, --debug Enable debug logging ``` ### 👥 用户管理 ``` smtp-tunnel-adduser [-u USERS_FILE] [-c CONFIG] [--no-zip] Add a new user and generate client package smtp-tunnel-deluser [-u USERS_FILE] [-f] Remove a user (use -f to skip confirmation) smtp-tunnel-listusers [-u USERS_FILE] [-v] List all users (use -v for detailed info) smtp-tunnel-update Update server to latest version (preserves config/certs/users) ``` ## 📁 文件结构 ``` smtp_proxy/ ├── 📄 server.py # Server (runs on VPS) ├── 📄 client.py # Client (runs locally) ├── 📄 common.py # Shared utilities ├── 📄 generate_certs.py # Certificate generator ├── 📄 config.yaml # Server/client configuration ├── 📄 users.yaml # User database ├── 📄 requirements.txt # Python dependencies ├── 📄 install.sh # One-liner server installer ├── 📄 smtp-tunnel.service # Systemd unit file ├── 🔧 smtp-tunnel-adduser # Add user script ├── 🔧 smtp-tunnel-deluser # Remove user script ├── 🔧 smtp-tunnel-listusers # List users script ├── 🔧 smtp-tunnel-update # Update server script ├── 📄 README.md # This file └── 📄 TECHNICAL.md # Technical documentation ``` ### 📦 安装路径(执行 install.sh 后) ``` /opt/smtp-tunnel/ # Application files /etc/smtp-tunnel/ # Configuration files ├── config.yaml ├── users.yaml ├── server.crt ├── server.key └── ca.crt /usr/local/bin/ # Management commands ├── smtp-tunnel-adduser ├── smtp-tunnel-deluser ├── smtp-tunnel-listusers └── smtp-tunnel-update ``` ## 🔧 故障排除 ### ❌ "连接被拒绝" - 检查服务器是否运行:`systemctl status smtp-tunnel` 或 `ps aux | grep server.py` - 检查端口是否开放:`netstat -tlnp | grep 587` - 检查防火墙:`ufw status` ### ❌ "认证失败" - 验证 `username` 和 `secret` 在 users.yaml 中匹配 - 检查服务器时间是否准确(误差在 5 分钟内) - 运行 `smtp-tunnel-listusers -v` 验证用户是否存在 ### ❌ "IP 未被列入白名单" - 检查用户的白名单配置 - 你的当前 IP 必须匹配白名单条目 - 支持 CIDR 记法(例如 `10.0.0.0/8`) ### ❌ "证书验证失败" - 确保使用域名而非 IP 地址 -验证 `server_host` 与证书主机名匹配 - 确保拥有来自服务器的正确 `ca.crt` ### 🐛 调试模式 ``` # 启用详细日志记录 python server.py -d python client.py -d # 查看 systemd 日志 journalctl -u smtp-tunnel -f ``` ## 🔐 安全注意事项 - ✅ **始终使用域名** 进行正确的 TLS 验证 - ✅ **始终使用 `ca_cert`** 防止中间人攻击 - ✅ **使用 `smtp-tunnel-adduser`** 自动生成强密钥 - ✅ **使用每个用户的 IP 白名单**,如果你知道客户端 IP - ✅ **保护 `users.yaml`** - 包含所有用户密钥(权限设为 600) - ✅ **对敏感用户禁用日志**,设置 `logging: false` ## 📄 许可证 本项目仅供教育和授权使用。请负责任并遵守适用法律使用。 ## ⚠️ 免责声明 本工具设计用于合法的隐私和规避审查目的。用户需确保其使用符合适用法律法规。 *用心制作,为互联网自由*
标签:C2通信, DPI绕过, HMAC-SHA256, Postfix模拟, SMTP隧道, SOCKS5代理, SSL/TLS加密, TLS1.2, 代理隧道, 多用户认证, 流量伪装, 深度包检测, 网络安全, 网络安全分析, 网络规避, 逆向工具, 邮件协议隧道, 隐私保护, 隐蔽通道, 预共享密钥, 高速传输