
go-http-proxy-to-socks v1.15.6
CLI MITM代理,将SOCKS4/SOCKS5转换为HTTP/HTTPS/HTTP2/HTTP3代理,具有透明TCP/UDP重定向、ARP/NDP/DNS欺骗、流量嗅探和数据包捕获功能。纯Go实现,无需libpcap。
GoHPTS - 用 Go 编写的 HTTP(S) 和 TCP/UDP 透明代理到 SOCKS4/SOCKS5 代理(链)

目录
- 简介
- 特性
- 安装
- 用法
- 透明代理
- 流量嗅探
- HTTP2 和 HTTP3 支持
- IPv4 和 IPv6 支持
- ARP 欺骗
- NDP 欺骗
- DNS 欺骗
- 数据包捕获
- 网络命名空间
- 混合服务器
- 链接
- 贡献
- 许可证
简介
GoHPTS CLI 工具是 HTTP 客户端与 SOCKS5 代理服务器或多个服务器(链)之间的桥梁。它在本地作为 HTTP 代理监听,接受标准 HTTP
或 HTTPS(通过 CONNECT)请求,并通过 SOCKS5 代理转发连接。灵感来自 http-proxy-to-socks 和 Proxychains
可能的用例:你需要通过 Postman 连接到外部 API,但该 API 只能从某个远程服务器访问。 以下命令将帮助你完成这样的任务:
通过 ssh 创建 SOCKS5 代理服务器:```shell
ssh -D 1080 -Nf
使用 `gohpts` 创建 HTTP 到 SOCKS5 的连接```shell
gohpts -s :1080 -l :8080
在 Postman 的代理配置中指定 http 服务器
功能特性
-
代理链功能
支持 SOCKS4/SOCKS5 代理的strict、dynamic、random、round_robin链 -
透明代理
支持redirect(SO_ORIGINAL_DST) 和tproxy(IP_TRANSPARENT) 模式 -
IPv4 和 IPv6 支持
可在IPv4-only、IPv6-only或dual stack模式下运行 -
TCP 和 UDP 透明代理
tproxy和tlocal(IP_TRANSPARENT) 处理 TCP 和 UDP 流量 -
流量嗅探
代理能够解析 HTTP 头、TLS 握手、DNS 消息等 -
ARP 欺骗
使用 ARP 欺骗方法代理整个子网 -
NDP 欺骗
使用路由器/邻居通告和 RDNSS 注入代理 IPv6 连接。 -
DNS 欺骗
通过 DNS 记录操纵将客户端重定向到任意域 -
数据包捕获
将流量捕获到 txt/pcap/pcapng 文件并使用 Wireshark 分析 -
DNS 泄漏保护
DNS 解析在 SOCKS5 服务器端进行。 -
CONNECT 方法支持
支持 HTTP CONNECT 隧道,启用 HTTPS 和其他基于 TCP 的协议。 -
HTTP2/HTTP3 支持
支持现代 HTTP/2 和 HTTP/3 传输,通过 TLS 1.3 实现高效的多路复用连接 -
网络命名空间支持
支持用于监听套接字和出站连接的自定义 Linux 网络命名空间 -
尾部标头支持
处理 HTTP 尾部标头 -
分块传输编码
处理分块和流式响应 -
SOCKS5 身份验证支持
支持 SOCKS5 代理的用户名/密码身份验证。 -
HTTP 身份验证支持
支持 HTTP 代理服务器的用户名/密码身份验证。 -
轻量且快速
以最小开销和高效请求处理为设计目标。 -
跨平台
兼容所有主流操作系统。
安装
- Arch Linux/CachyOS/EndeavourOS ```shell
yay -S gohpts
或使用 paru: ```shell
paru -S gohpts
- 从 [Releases](https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases) 页面下载适用于你平台的二进制文件: ```shell
GOHPTS_RELEASE=v1.15.6; wget -v https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases/download/$GOHPTS_RELEASE/gohpts-$GOHPTS_RELEASE-linux-amd64.tar.gz -O gohpts && tar xvzf gohpts && mv -f gohpts-$GOHPTS_RELEASE-linux-amd64 gohpts && ./gohpts -h
- 使用
go install命令安装(需要 Go 1.26 或更高版本): ```shell CGO_ENABLED=0 go install -ldflags "-s -w" -trimpath github.com/shadowy-pycoder/go-http-proxy-to-socks/cmd/gohpts@latest
这会将 gohpts 二进制文件安装到你的 $GOPATH/bin 目录中。
- 从源码构建: ```shell
git clone https://github.com/shadowy-pycoder/go-http-proxy-to-socks.git
cd go-http-proxy-to-socks
make build
./bin/gohpts
- 在 docker 中运行: ```shell
docker run -it --privileged --network host -v "$PWD/gohpts.yaml:/config.yaml" shadowypycoder/gohpts:latest -f config.yaml
用法
[返回]```shell gohpts -h
/ | | | | | __ _ / ____|
| | __ ___ | || | |) | | | | (__
| | |_ |/ _ | __ | / | | _
| |__| | () | | | | | | | ) |
_|_/|| ||| || |___/
GoHPTS: HTTP(S) Proxy to SOCKS4/SOCKS5 proxy by shadowy-pycoder GitHub: https://github.com/shadowy-pycoder/go-http-proxy-to-socks Codeberg: https://codeberg.org/shadowy-pycoder/go-http-proxy-to-socks
Usage: gohpts [OPTIONS] OPTIONS: General: -h Show this help message and exit -v Show version and build information -D Run as a daemon (provide -logfile to see logs) -I Display list of network interfaces and exit -f Path to proxy configuration file in YAML format
Proxy: -l Address of HTTP proxy server (Default: "127.0.0.1:8080" for IPv4, "[::1]:8080" for IPv6) -s Address of SOCKS proxy server (Default: "127.0.0.1:1080" for IPv4 "[::1]:1080" for IPv6) -c Path to certificate PEM encoded file -k Path to private key PEM encoded file -U User for HTTP proxy (basic auth). This flag invokes prompt for password (not echoed to terminal) -u User for SOCKS proxy authentication. This flag invokes prompt for password (not echoed to terminal) -i Bind proxy to specific network interface (either by interface name or index) -4 Force IPv4 stack for TCP and UDP (Default: dual stack) -6 Force IPv6 stack for TCP and UDP (Default: dual stack) -socks4 Use SOCKS4/SOCKS4a protocol for upstream proxy and mixed server (default: SOCKS5/SOCKS5h) -nohttp Disable HTTP proxy server -nosocks Disable SOCKS upstream proxy -dns Use custom DNS server (Example: "8.8.8.8" or "2001:4860:4860::8888") -mixed Accept SOCKS connections on HTTP proxy server address
Logs: -d Show logs in DEBUG mode -j Show logs in JSON format -logfile Log file path (Default: stdout) -nocolor Disable colored output for logs (no effect if -j flag specified) -pprof Address of pprof server with profiling data
Sniffing: -sniff Enable traffic sniffing for HTTP and TLS -snifflog Sniffed traffic log file path (Default: the same as -logfile) -body Collect request and response body for HTTP traffic (credentials, tokens, etc)
TProxy: -T Address of transparent proxy server -Tu Address of transparent UDP proxy server -M Transparent proxy mode: (redirect, tproxy, tlocal) -w Number of instances of transparent proxy server (Default: number of CPU cores) -wu Number of instances of transparent UDP proxy server (Default: number of CPU cores) -auto Automatically setup iptables and kernel parameters for transparent proxy (requires elevated privileges) -mark Set mark for each packet sent through transparent proxy (Default: redirect 0, tproxy 100, tlocal 100) -A Comma separated list of allowed ip addresses for proxying (Example: "8.8.8.8,2001:4860:4860::8888,192.168.1.0/24") -P Comma separated list of ports to ignore when proxying traffic (Example: "22,80,443,9092") -dump Dump iptables rules and other system settings generated by -auto flag
Spoofing: -arpspoof Enable ARP spoof proxy for selected targets (Example: "targets 10.0.0.1,10.0.0.5-10,192.168.1.*,192.168.10.0/24;fullduplex false;debug true;interval 10s") -ndpspoof Enable NDP spoof proxy for selected targets (Example: "ra true;na true;targets fe80::3a1c:7bff:fe22:91a4;fullduplex false;debug true;interval 10s")
Packet Capture: -pcap Enable packet capture (Example: "promisc true;expr ip proto tcp;snaplen 65535;timeout 10s;packet_count 100;packet_buffer 8192;exts txt,pcap,pcapng")
Namespaces: -in-netns Name or path of network namespace for inbound listeners (Default: default namespace) -out-netns Name or path of network namespace for outbound connections (Default: default namespace)
### 通过 CLI 标志进行配置
[[返回]](#table-of-contents)```shell
gohpts -s 1080 -l 8080 -d -j
输出:```shell {"level":"info","time":"2025-05-28T06:15:18+00:00","message":"SOCKS5 Proxy: :1080"} {"level":"info","time":"2025-05-28T06:15:18+00:00","message":"HTTP Proxy: :8080"} {"level":"debug","time":"2025-05-28T06:15:22+00:00","message":"HTTP/1.1 - CONNECT - www.google.com:443"}
指定 SOCKS5 代理服务器的用户名和密码:```shell
gohpts -s 1080 -l 8080 -d -j -u user
SOCKS5 Password: #you will be prompted for password input here
指定 HTTP 代理服务器的用户名和密码:```shell gohpts -s 1080 -l 8080 -d -j -U user HTTP Password: #you will be prompted for password input here
当同时存在 `-u` 和 `-U` 时,系统会提示你两次
通过 TLS 连接运行 http 代理```shell
gohpts -s 1080 -l 8080 -c "path/to/certificate" -k "path/to/private/key"
以守护进程方式运行 proxy(需要 logfile 来记录输出,否则你将看不到任何内容)```shell gohpts -D -logfile /tmp/gohpts.log
## 使用示例
### 基本用法
```bash
# 扫描单个目标
python3 cve_2025_55182.py -t https://target.example.com
# 使用代理扫描
python3 cve_2025_55182.py -t https://target.example.com -p http://127.0.0.1:8080
# 从文件扫描多个目标
python3 cve_2025_55182.py -f targets.txt
# 使用自定义超时和线程数扫描
python3 cve_2025_55182.py -f targets.txt -T 15 -c 20
# 使用自定义回调域名扫描
python3 cve_2025_55182.py -t https://target.example.com -d your-callback.com
# 详细输出
python3 cve_2025_55182.py -t https://target.example.com -v
命令行选项
选项:
-h, --help 显示此帮助信息并退出
-t TARGET, --target TARGET
单个目标 URL
-f FILE, --file FILE 包含目标 URL 的文件(每行一个)
-p PROXY, --proxy PROXY
用于请求的代理 URL
-T TIMEOUT, --timeout TIMEOUT
请求超时时间(秒,默认:10)
-c CONCURRENCY, --concurrency CONCURRENCY
并发线程数(默认:10)
-d DOMAIN, --domain DOMAIN
用于 payload 的自定义回调域名
-v, --verbose 启用详细输出
输出示例
[+] 正在扫描 https://target.example.com
[+] 目标存在漏洞:https://target.example.com
[+] 已发送回调至:your-callback.com
工作原理
- 目标验证:脚本首先验证目标是否可访问,并检查其是否运行 Next.js。
- Payload 生成:生成一个包含回调域名和唯一标识符的恶意 payload。
- 漏洞利用:将 payload 发送到存在漏洞的端点,触发服务器端请求。
- 回调验证:监控回调域名以确认漏洞利用是否成功。
- 结果报告:报告目标是否易受攻击。
受影响版本
- Next.js 15.x(15.0.0 至 15.5.6 之前)
- Next.js 14.x(14.0.0 至 14.2.34 之前)
- Next.js 13.x(13.0.0 至 13.5.10 之前)
- Next.js 12.x(12.0.0 至 12.3.6 之前)
- Next.js 11.x(11.0.0 至 11.1.3 之前)
缓解措施
- 升级 Next.js:升级到已修复的版本(15.5.7、14.2.35、13.5.11、12.3.7 或 11.1.4)。
- 网络分段:限制服务器端请求访问内部资源。
- 输入验证:验证并清理所有用户提供的输入。
- 监控:监控出站请求和异常活动。
免责声明
本工具仅供教育和授权安全测试目的使用。未经授权访问计算机系统是非法的。请务必获得适当授权后再使用此工具。
参考资料
许可证
本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。
贡献
欢迎贡献!请随时提交 Pull Request。
作者
致谢
- 感谢 Next.js 团队对安全问题的快速响应
- 感谢安全社区对漏洞的负责任披露```shell
output
gohpts pid:
## 漏洞利用
- 使用 `--exploit` 标志运行工具以启用漏洞利用模式。
- 该工具将尝试利用已识别的漏洞。
## 示例
```bash
python3 cve_2025_29972.py --target https://example.com --exploit
免责声明
本工具仅用于教育和道德测试目的。未经授权使用此工具攻击目标属于违法行为。作者对任何滥用行为不承担责任。```shell
kill the process
kill #or kill $(pidof gohpts)
`-u` 和 `-U` 标志在守护进程模式下不起作用(因此也无法用于身份验证),但你可以提供一个配置文件(见下文)
### 通过 YAML 文件进行配置
[[返回]](#table-of-contents)
当你希望预先配置代理而不必过多操作 CLI,或者只是需要针对不同需求使用多个配置文件时,配置文件非常有用。
以 SOCKS5 代理链模式运行 HTTP 代理(通过 YAML 配置文件指定服务器设置)```shell
gohpts -f "~/gohtps.yaml" -d -j
配置示例:```yaml
bind proxy to specific network interface (either by interface name or index)
interface: "eth0" # if specified, overrides http server IP address disable_http: false # disable http proxy (default: false) disable_socks: false # disable upstream socks proxy (default: false)
if ipv4 and ipv6 are both false or both true, dual stack is assumed
ipv4: false # this must be enabled for arpspoof (default: false) ipv6: false # this must be enabled for ndpspoof (default: false) socks4: false # use SOCKS4/SOCKS4a protocol (tcp only protocol, no udp tproxy or http3 possible) (default: false) dns: 8.8.8.8 # custom DNS server (used in direct dialer, namespaces, spoofing)
http_server: address: 127.0.0.1:8080
username and password for adding basic authentication (comment out to disable auth)
username: username password: password
list of socks5 proxy
if proxy_chain is disabled, uses first server in a list as upstream
proxy_list:
- address: 127.0.0.1:1080
- address: 127.0.0.1:1081
- address: :1082 # empty host means localhost
proxy_chain: enabled: false
Explanations for chains taken from /etc/proxychains4.conf
strict - Each connection will be done via chained proxies
all proxies chained in the order as they appear in the list
all proxies must be online to play in chain
dynamic - Each connection will be done via chained proxies
all proxies chained in the order as they appear in the list
at least one proxy must be online to play in chain
(dead proxies are skipped)
random - Each connection will be done via random proxy
(or proxy chain, see chain_len) from the list.
this option is good to test your IDS :)
round_robin - Each connection will be done via chained proxies
of chain_len length
all proxies chained in the order as they appear in the list
at least one proxy must be online to play in chain
(dead proxies are skipped).
the start of the current proxy chain is the proxy after the last
proxy in the previously invoked proxy chain.
if the end of the proxy chain is reached while looking for proxies
start at the beginning again.
These semantics are not guaranteed in a multithreaded environment.
type: strict # dynamic, strict, random, round_robin length: 2 # maximum number of proxy in a chain (works only for random chain and round_robin chain)
logging: debug: true json: false
defaults to standard output
#logfile: /tmp/gohpts.log
use colored output in logs (no effect if json enabled)
nocolor: false
profiling data
pprof: 127.0.0.1:8081
你可以通过提供相应的 CLI 标志来覆盖配置文件中指定的几乎所有设置:```shell
gohpts -l :6969 -f "~/gohtps.yaml" -nocolor
Proxy 会采用 ~/gohpts.yaml 中指定的所有设置,但在 127.0.0.1:6969 上启动 HTTP 服务器,而不是 127.0.0.1:8080,并且还会禁用日志中的彩色输出。
某些设置(例如 proxy_chain 和 dns 过滤器)只能通过文件进行配置。
完整版本的配置可以在这里找到:example_gohpts.yaml
要了解有关代理链的更多信息,请访问 Proxychains Github
透明代理
也称为
拦截代理、内联代理或强制代理,透明代理会拦截正常的应用层通信,而无需任何特殊的客户端配置。客户端无需知道代理的存在。透明代理通常位于客户端和互联网之间,代理执行网关或路由器的部分功能-- 来自 Wiki
此功能仅在 Linux 系统和 Android(arm64)上可用,并且需要额外设置(iptables、ip route 等)
-T address 标志指定透明代理服务器的地址
有三种模式 redirect、tproxy 和 tlocal(与 tproxy 相同,但还会拦截本地流量),可以通过 -M 标志指定
redirect(通过 NAT 和 SO_ORIGINAL_DST)
在此模式下,代理通过 iptables 的 nat 表和 REDIRECT 目标进行。传入数据包的主机地址会变为正在运行的 redirect 透明代理的地址,但它还包含可以通过 getsockopt(SO_ORIGINAL_DST) 获取的原始目标地址
要在此模式下运行 GoHPTS,请使用 -T 标志和 -M redirect
示例
[返回]```shell
run the proxy
gohpts -s 1080 -T 1090 -M redirect -d
[无输入内容]```shell
# run socks5 server on 127.0.0.1:1080
ssh remote -D 1080 -Nf
设置你的操作系统:```shell
commands below require elevated privileges (you can run it with sudo -i)
#enable ip forwarding sysctl -w net.ipv4.ip_forward=1
create GOHPTS nat chain
iptables -t nat -N GOHPTS
set no redirection rules for local, http proxy, ssh and redirect proxy itself
iptables -t nat -A GOHPTS -d 127.0.0.0/8 -j RETURN iptables -t nat -A GOHPTS -p tcp --dport 8080 -j RETURN iptables -t nat -A GOHPTS -p tcp --dport 1090 -j RETURN iptables -t nat -A GOHPTS -p tcp --dport 22 -j RETURN
redirect traffic to transparent proxy
iptables -t nat -A GOHPTS -p tcp -j REDIRECT --to-ports 1090
setup prerouting by adding our proxy
iptables -t nat -A PREROUTING -p tcp -j GOHPTS
intercept local traffic for testing
iptables -t nat -A OUTPUT -p tcp -j GOHPTS
测试连接:```shell
#traffic should be redirected via 127.0.0.1:1090
curl http://example.com
使用示例
基本用法
# 扫描单个目标
python3 cve_2025_55182.py -t https://target.example.com
# 扫描多个目标
python3 cve_2025_55182.py -f targets.txt
# 使用代理
python3 cve_2025_55182.py -t https://target.example.com -p http://127.0.0.1:8080
# 详细输出
python3 cve_2025_55182.py -t https://target.example.com -v
高级用法
# 自定义超时和线程数
python3 cve_2025_55182.py -f targets.txt --timeout 15 --threads 20
# 保存结果到文件
python3 cve_2025_55182.py -f targets.txt -o results.txt
# 使用自定义 User-Agent
python3 cve_2025_55182.py -t https://target.example.com -A "Mozilla/5.0 (Custom)"
命令行参数
| 参数 | 描述 | 默认值 |
|---|---|---|
-t, --target | 单个目标 URL | - |
-f, --file | 包含目标 URL 的文件 | - |
-p, --proxy | 用于请求的代理 | - |
-o, --output | 输出文件 | - |
-v, --verbose | 启用详细输出 | False |
--timeout | 请求超时时间(秒) | 10 |
--threads | 并发线程数 | 10 |
-A, --user-agent | 自定义 User-Agent | 默认 UA |
输出示例
[+] 正在扫描: https://target.example.com
[+] 目标存在漏洞: CVE-2025-55182
[+] 载荷: /api/v1/../../etc/passwd
[+] 响应时间: 0.45s
[+] 状态码: 200
免责声明
本工具仅供教育和安全研究目的使用。未经授权访问系统是违法的。请务必在测试前获得适当授权。
参考资料
许可证
本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。```shell #traffic should be redirected via 127.0.0.1:8080 curl --proxy http://127.0.0.1:8080 http://example.com
撤销所有操作:```shell
sysctl -w net.ipv4.ip_forward=0
iptables -t nat -D PREROUTING -p tcp -j GOHPTS
iptables -t nat -D OUTPUT -p tcp -j GOHPTS
iptables -t nat -F GOHPTS
iptables -t nat -X GOHPTS
redirect 模式的自动配置
要自动配置系统,请运行以下命令:```shell sudo env PATH=$PATH gohpts -d -T 8888 -M redirect -auto
请注意,自动配置需要 `sudo` 权限且非常通用,可能不适合您的需求。
您可以选择指定 `-mark <value>` 以防止可能的代理循环```shell
sudo env PATH=$PATH gohpts -d -T 8888 -M redirect -auto -mark 100
tproxy(通过 MANGLE 和 IP_TRANSPARENT)
在此模式下,代理通过 iptables 的 mangle 表和 TPROXY 目标实现。透明代理看到的目标地址保持原样,不会被内核重写。为此,代理需使用套接字选项 IP_TRANSPARENT 进行绑定,iptables 使用 TPROXY 目标拦截流量,路由规则将标记的数据包导向本地代理,而不改变其原始目标地址。
此模式需要提升权限才能运行 GoHPTS。你可以通过运行以下命令来实现:```shell
sudo setcap 'cap_net_admin+ep' ~/go/bin/gohpts
要在此模式下运行 `GoHPTS`,请使用 `-T` 标志并配合 `-M tproxy`
### 示例
[[返回]](#table-of-contents)```shell
# run the proxy
gohpts -s 1080 -T 0.0.0.0:1090 -M tproxy -d
漏洞利用
- 使用
--exploit标志运行工具以启用漏洞利用模式。 - 该工具将尝试利用已识别的漏洞。
示例
python3 cve_2025_29991.py --target https://example.com --exploit
免责声明
本工具仅用于教育和道德测试目的。未经授权使用此工具攻击目标属于违法行为。作者对任何滥用行为不承担责任。```shell
run socks5 server on 127.0.0.1:1080
ssh remote -D 1080 -Nf
设置你的操作系统:```shell
ip netns add ns-client
ip link add dev veth0 type veth peer name veth1 netns ns-client
ip addr add 10.0.0.1/24 dev veth0
ip link set dev veth0 up
ip netns exec ns-client ip addr add 10.0.0.2/24 dev veth1
ip netns exec ns-client ip link set dev lo up
ip netns exec ns-client ip link set dev veth1 up
ip netns exec ns-client ip route add default via 10.0.0.1
sysctl -w net.ipv4.ip_forward=1
iptables -t mangle -A PREROUTING -i veth0 -p tcp -j TPROXY --on-port 1090 --tproxy-mark 0x1/0x1
ip rule add fwmark 1 lookup 100
ip route add local 0.0.0.0/0 dev lo table 100
测试连接:```shell ip netns exec ns-client curl http://1.1.1.1
撤销所有操作:```shell
sysctl -w net.ipv4.ip_forward=0
iptables -t mangle -F
ip rule del fwmark 1 lookup 100
ip route flush table 100
ip netns del ns-client
tproxy 模式的自动配置
要自动配置您的系统,请运行以下命令(例如,在单独的虚拟机上):```shell ssh remote -D 1080 -Nf sudo env PATH=$PATH gohpts -d -T 8888 -M tproxy -auto -mark 100
在你的主机上运行以下命令:```shell
ip route show default > /tmp/default-route.txt
ip route add 0.0.0.0/1 via 192.168.0.1 # change with ip of your VM
ip route add 128.0.0.0/1 via 192.168.0.1
测试连接:```shell curl http://example.com #check logs on your VM
撤销所有操作:```shell
ip route del 0.0.0.0/1 via 192.168.0.1 2>/dev/null || true
ip route del 128.0.0.0/1 via 192.168.0.1 2>/dev/null || true
if [[ -f /tmp/default-route.txt ]]; then
eval $(awk '{print "ip route add "$0}' /tmp/default-route.txt)
rm -f /tmp/default-route.txt
else
echo "Something went wrong"
fi
UDP 支持
GoHPTS 具有 UDP 支持,可在 tproxy 和 tlocal 模式下启用。要使此设置正常工作,您需要连接到能够提供 UDP 连接(UDP ASSOCIATE)的 socks5 服务器。例如,您可以使用 https://github.com/wzshiming/socks5 在某个远程或本地机器上部署支持 UDP 的 socks5 服务器。一旦您有了要连接的服务器,请运行以下命令:```shell
sudo env PATH=$PATH gohpts -s remote -Tu :8989 -M tproxy -auto -mark 100 -d
此命令将配置你的操作系统并在 `0.0.0.0:8989` 地址上设置服务器。
要在本地测试它,你可以将 UDP 透明代理与 `-arpspoof` 标志结合使用。例如:
1. 在你的系统上设置一个虚拟机,使用任何支持 `tproxy` 的 Linux 发行版(例如 Kali Linux)。
2. 启用 `bridged` 网络,以便虚拟机可以访问你的宿主机。
3. 将 `gohpts` 二进制文件移动到虚拟机中(例如通过 `ssh`),或者如果操作系统/架构不同,则在虚拟机中构建它。
4. 在你的虚拟机上运行以下命令:```shell
# Do not forget to replace <socks5 server> and <your host> with actual addresses
sudo ./gohpts -s <socks5 server> -T 8888 -Tu :8989 -M tproxy -sniff -body -auto -mark 100 -d -arpspoof "targets <your host>;fullduplex true;debug false"
- 在宿主机上检查连接,流量应经过 Kali 机器。
Android 支持
透明代理可在具有 root 权限的 Android 设备(arm64)上启用。你可以安装 Termux 并在其中将 GoHPTS 作为 CLI 工具运行:```shell
you need to root your device first
pkg install tsu iproute2
Android support added in v1.10.2
GOHPTS_RELEASE=v1.10.2; wget -v https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases/download/$GOHPTS_RELEASE/gohpts-$GOHPTS_RELEASE-android-arm64.tar.gz -O gohpts && tar xvzf gohpts && mv -f gohpts-$GOHPTS_RELEASE-android-arm64 gohpts && ./gohpts -h
use your phone as router for LAN devices redirecting their traffic to remote socks5 server
sudo ./gohpts -s remote -T 8888 -Tu :8989 -M tproxy -sniff -body -auto -mark 100 -d -arpspoof "fullduplex true;debug false"
### YAML 配置
[[返回]](#table-of-contents)```yaml
transparent_proxy:
tcp:
enabled: true
address: 0.0.0.0:8888
# number of instances of transparent proxy server (Default: number of CPU cores)
workers: 1
udp:
enabled: true
address: 0.0.0.0:8889
# number of instances of transparent UDP proxy server (Default: number of CPU cores)
workers: 1
mode: "tproxy" # available modes are "redirect", "tproxy" and "tlocal" (udp requires tproxy or tlocal mode)
# automatically setup iptables and kernel parameters for transparent proxy (requires elevated privileges)
auto: true
# dump iptables rules and other system settings generated by auto setting
dump_rules: false
# list of ports to ignore when proxying traffic (Example: [22,80,443,9092])
ignored_ports: []
# set mark for each packet sent through transparent proxy (Default: redirect 0, tproxy 100, tlocal 100)
mark: 100
流量嗅探
GoHPTS 代理允许捕获和监控通过该服务的流量。此过程称为 流量嗅探、数据包嗅探 或简称 嗅探。具体而言,代理会尝试识别流量是明文(HTTP)还是 TLS 流量,识别完成后,它会解析请求/响应元数据并将其写入文件或控制台。对于 GoHTPS 代理,解析后的元数据如下所示(TLS 握手):
JSON 格式
[返回]```json [ { "connection": { "tproxy_mode": "redirect", "src_local": "127.0.0.1:8888", "src_remote": "192.168.0.107:51142", "dst_local": "127.0.0.1:56256", "dst_remote": "127.0.0.1:1080", "original_dst": "216.58.209.206:443" } }, { "tls_request": { "sni": "www.youtube.com", "type": "Client hello (1)", "version": "TLS 1.2 (0x0303)", "session_id": "2670a6779b4346e5e84d46890ad2aaf7a53b08adcfe0c9f6868c2d9882242e39", "cipher_suites": [ "TLS_AES_128_GCM_SHA256 (0x1301)", "TLS_CHACHA20_POLY1305_SHA256 (0x1303)", "TLS_AES_256_GCM_SHA384 (0x1302)", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 (0xc02b)", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xc02f)", "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 (0xcca9)", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (0xcca8)", "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 (0xc02c)", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (0xc030)", "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA (0xc00a)", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA (0xc009)", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA (0xc013)", "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA (0xc014)", "TLS_RSA_WITH_AES_128_GCM_SHA256 (0x9c)", "TLS_RSA_WITH_AES_256_GCM_SHA384 (0x9d)", "TLS_RSA_WITH_AES_128_CBC_SHA (0x2f)", "TLS_RSA_WITH_AES_256_CBC_SHA (0x35)" ], "extensions": [ "server_name (0)", "extended_master_secret (23)", "renegotiation_info (65281)", "supported_groups (10)", "ec_point_formats (11)", "session_ticket (35)", "application_layer_protocol_negotiation (16)", "status_request (5)", "delegated_credential (34)", "signed_certificate_timestamp (18)", "key_share (51)", "supported_versions (43)", "signature_algorithms (13)", "psk_key_exchange_modes (45)", "record_size_limit (28)", "compress_certificate (27)", "encrypted_client_hello (65037)" ], "alpn": ["h2", "http/1.1"] } }, { "tls_response": { "type": "Server hello (2)", "version": "TLS 1.2 (0x0303)", "session_id": "2670a6779b4346e5e84d46890ad2aaf7a53b08adcfe0c9f6868c2d9882242e39", "cipher_suite": "TLS_AES_128_GCM_SHA256 (0x1301)", "extensions": ["key_share (51)", "supported_versions (43)"], "supported_version": "TLS 1.3 (0x0304)" } } ]
以及使用 curl 的 HTTP 请求:```json
[
{
"connection": {
"tproxy_mode": "redirect",
"src_local": "127.0.0.1:8888",
"src_remote": "192.168.0.107:45736",
"dst_local": "127.0.0.1:37640",
"dst_remote": "127.0.0.1:1080",
"original_dst": "96.7.128.198:80"
}
},
{
"http_request": {
"host": "example.com",
"uri": "/",
"method": "GET",
"proto": "HTTP/1.1",
"header": {
"Accept": ["*/*"],
"My": ["Header"],
"User-Agent": ["curl/7.81.0"]
}
}
},
{
"http_response": {
"proto": "HTTP/1.1",
"status": "200 OK",
"content-length": 1256,
"header": {
"Cache-Control": ["max-age=2880"],
"Connection": ["keep-alive"],
"Content-Length": ["1256"],
"Content-Type": ["text/html"],
"Date": ["Tue, 17 Jun 2025 14:43:24 GMT"],
"Etag": ["\"84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134\""],
"Last-Modified": ["Mon, 13 Jan 2025 20:11:20 GMT"]
}
}
}
]
使用方式非常简单,只需在常规标志的基础上指定 -sniff 标志即可```shell
gohpts -d -T 8888 -M redirect -sniff -j
你也可以指定一个文件来写入嗅探到的流量:```shell
gohpts -sniff -snifflog ~/sniff.log -j
彩色格式
你可以在上图中看到彩色输出的示例。在此模式下,GoHPTS 会尝试高亮显示重要信息,例如 TLS 握手、HTTP 元数据、看起来像登录名/密码的内容,或不同类型的认证和密钥令牌。与 JSON 相比,输出内容有所限制,但对人类来说更易于阅读。
要在此模式下运行 GoHPTS,请使用以下标志:```shell
gohpts -sniff -body
你可以将嗅探与透明模式结合使用:```shell
./gohpts -T 8888 -M redirect -sniff -body
要禁用颜色,请添加 -nocolor:```shell
gohpts -sniff -body -nocolor
## HTTP2 和 HTTP3 支持
[[返回]](#table-of-contents)
`GoHPTS` 代理使用相同的服务器地址和 TLS 证书处理 HTTP/1.1、HTTP/2 和 HTTP/3 请求。这允许客户端自动选择最佳可用协议,而无需更改配置。TLS 证书可以通过多种方式获取:云服务提供商(Google、AWS、Cloudflare)、来自 Let's Encrypt 的免费证书,或者您可以使用 `openssl`(Linux/macOS)或 `New-SelfSignedCertificate`(Windows)创建自签名证书。
### 使用自签名证书的示例设置
[[返回]](#table-of-contents)
- 创建 `key.pem` 和 `cert.pem` 文件: ```shell
openssl req -x509 -newkey rsa:2048 \
-keyout key.pem \
-out cert.pem \
-sha256 \
-days 365 \
-nodes \
-subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=127.0.0.1" \
-addext "subjectAltName=IP:127.0.0.1"
- 准备支持 UDP ASSOCIATE 的 socks5 服务器 ```shell
git clone https://github.com/wzshiming/socks5.git && cd socks5
go build -o socks5_server ./cmd/socks5/main.go
./socks5_server -a 0.0.0.0:1080
- 打开另一个终端并安装
GoHPTS代理: ```shell go install github.com/shadowy-pycoder/go-http-proxy-to-socks/cmd/gohpts@latest
你可以使用 Installation 部分中描述的其他方法。
-
最后:
- 为你的代理创建最小配置 ```yaml
gohpts_config.yaml
http_server: address: 127.0.0.1:8080 cert_file: ./cert.pem key_file: ./key.pem
proxy_list:
- address: 127.0.0.1:1080
logging: debug: true
sniffing: enabled: true body: true
运行代理: ```shell gohpts -f ./gohpts_config.yaml
2. 或者如果您更喜欢命令行参数: ```shell
gohpts -l :8080 -s 1080 -c ./cert.pem -k ./key.pem -d -sniff -body
你应该会看到类似这样的内容: ```shell [15:20:32] INF SOCKS5 Proxy: 127.0.0.1:1080 [15:20:32] INF HTTPS Proxy: 127.0.0.1:8080 [15:20:32] INF HTTP3 Proxy (QUIC): 127.0.0.1:8080
### 测试连接
[[返回]](#table-of-contents)
- 对于 HTTP/2 代理服务器,你可以使用 `curl`: ```shell
curl -Nvk --http2 --proxy-insecure --proxy-http2 --proxy https://localhost:8080 "https://stream.wikimedia.org/v2/stream/recentchange"
按 Ctrl+C 停止正在运行的流。
-
对于 HTTP/3 则有所不同,因为(在撰写本文时)
curl不支持 HTTP3 代理,所以我将使用我为测试目的创建的自定义客户端。下载并安装 Simple HTTP3 to SOCKS5 proxy example: ```shell git clone https://github.com/shadowy-pycoder/http3-socks-proxy.git && cd http3-socks-proxy make
运行以下命令: ```shell ./bin/client -a 127.0.0.1:8080 www.google.com
你应该会看到一些类似 HTML 页面的乱码。
切换到运行 `GoHPTS` 代理的终端标签页并检查日志,你应该能在那里看到所有请求。
### 在浏览器中测试连接
[[返回]](#table-of-contents)
- 为浏览器创建合适的自签名证书: ```shell
git clone https://github.com/shadowy-pycoder/go-http-proxy-to-socks.git
cd go-http-proxy-to-socks
cp ./resources/makecert.sh makecert.sh && chmod +x makecert.sh
./makecert.sh
更多信息可在此处找到:创建浏览器信任的自签名 SSL 证书
- 将新创建的
rootCA.crt添加到系统信任存储:- Debian/Ubuntu: ```shell sudo cp rootCA.crt /usr/local/share/ca-certificates/rootCA.crt sudo update-ca-certificates
- Arch Linux/CachyOS/EndeavourOS: ```shell sudo trust anchor rootCA.crt
- 使用 `server.crt` 和 `server.key` 运行代理: ```shell
gohpts -l :8080 -s 1080 -c ./server.crt -k ./server.key -d -sniff -body
- 运行浏览器并访问任意网站: ```shell
chromium --proxy-server="https://127.0.0.1:8080"
IPv4 和 IPv6 支持
在网络层处理方面,GoHPTS 可以以三种模式运行:双栈、仅 IPv4 和 仅 IPv6。用户可以通过指定 -4 和 -6 标志来控制模式。当设置其中一个标志时,代理以相应模式启动;当两个标志都设置或都省略时,假定为 双栈。请注意,在“仅”模式下,只允许特定版本的 IP 地址,所有域名都会解析为特定 IP 版本(如果可能),所有监听地址都需要使用相同的版本,等等。
要启用 仅 IPv4 模式,只需添加 -4 标志:```shell
sudo ./gohpts -sniff -body -d -4
要在 IPv4 模式下测试代理,你可以使用任意 Linux 虚拟机:
1. 在你的虚拟机上:```shell
# add your host machine as gateway for VM
export GATEWAY="<host IPv4 address>"
ip route add 0.0.0.0/1 via "$GATEWAY"
ip route add 128.0.0.0/1 via "$GATEWAY"
- 在你的主机上:```shell
run proxy on your host
sudo ./gohpts -T 8888 -Tu 8889 -M tproxy -sniff -body -auto -d -4
3. 在虚拟机上访问任意网站,并在代理日志中查看流量
要启用 `IPv6-only` 模式,只需添加 `-6` 标志,例如与透明代理一起使用时:```shell
sudo ./gohpts -T 8888 -M redirect -sniff -body -auto -mark 100 -d -6
为此,你的 ISP 和远程 socks5 代理应具备活跃的 IPv6 支持,你可以访问 https://test-ipv6.com/ 来确认你是否能够访问 IPv6 地址。 要在 IPv6 模式下测试代理,你可以使用任意 Linux 虚拟机:
- 在你的虚拟机上:```shell
add your host machine as gateway IPv6 for VM
export GATEWAY6="" ip -6 route add ::/1 via "$GATEWAY6" dev eth0 ip -6 route add 8000::/1 via "$GATEWAY6" dev eth0
2. 在你的主机上:```shell
# run proxy on your host
sudo ./gohpts -T 8888 -Tu 8889 -M tproxy -sniff -body -auto -d -6
- 在虚拟机上访问任意网站,并在代理日志中查看流量
ARP 欺骗
GoHPTS 内置了 ARP 欺骗器,可用于让局域网中所有进行 TCP 通信的设备通过代理服务器连接到互联网。
这是通过添加 -arpspoof 标志及几个参数来实现的,参数之间用分号分隔。
示例:```shell ssh remote -D 1080 -Nf sudo env PATH=$PATH gohpts -d -T 8888 -M tproxy -sniff -body -auto -mark 100 -arpspoof "targets 192.168.10.0/24;fullduplex true;debug true"
Proxy 将扫描子网 `192.168.10.0/24` 中的设备,并向它们发送 ARP 数据包以伪装成网关;如果 `fullduplex` 为 true,
proxy 还会向网关发送 ARP 数据包,使其相信我们的 proxy 拥有子网上的每个 IP。
使用 `Ctrl+C` 停止 proxy 后,它会自动取消对所有目标的欺骗。
`GoHPTS` 还可以与 [Bettercap](https://github.com/bettercap/bettercap) 等工具一起使用,以代理 ARP 欺骗流量。
运行 proxy:```shell
ssh remote -D 1080 -Nf
sudo env PATH=$PATH gohpts -d -T 8888 -M tproxy -sniff -body -auto -mark 100
使用以下命令运行 bettercap(参见文档):```shell
sudo bettercap -eval "net.probe on;net.recon on;set arp.spoof.fullduplex true;arp.spoof on"
检查代理日志中来自局域网其他设备的流量
有关 arpspoof 选项的更多信息,请参阅 `gohpts -h` 和 [https://github.com/shadowy-pycoder/arpspoof](https://github.com/shadowy-pycoder/arpspoof)
## NDP 欺骗
[[返回]](#table-of-contents)
`GoHPTS` 内置了在 IPv6 网络中使用路由器通告(RA)和邻居通告(NA)数据包执行 NDP 欺骗的功能。它还在 RA 数据包中包含 RDNSS 选项,以将主机设置为受影响客户端的 IPv6 名称服务器。当与透明代理模式(TCP/UDP)结合使用时,NDP 欺骗允许 `gohpts` 为本地网络中的客户端代理流量。与 [ARP 欺骗](#arp-spoofing) 的情况一样,你可以使用单个 `-ndpspoof` 标志设置 ndp 欺骗选项:
示例:```shell
sudo env PATH=$PATH gohpts -d -T 8888 -M tproxy -sniff -body -auto -mark 100 -ndpspoof "ra true;na true;targets fe80::3a1c:7bff:fe22:91a4;fullduplex false;debug true"
有关 ndpspoof 选项的更多信息,请参阅 gohpts -h 和 https://github.com/shadowy-pycoder/ndpspoof
请注意,某些选项如 rdnss、gateway、interface 由 gohpts 自身自动设置,以便正确充当代理。
由于 gohpts 通过上游 SOCKS5 服务器代理所有连接,你需要一个支持 IPv4/IPv6 以及 TCP/UDP 的正常工作的服务器。显然,远程机器(例如 VPS)也应具备可用的 IPv6 连接。不用说,运行 gohpts 的机器应处于支持 IPv6 的网络中。
NDP 欺骗正确工作的示例设置:
- 连接到 VPS```shell ssh [email protected]
2. 安装依赖```shell
GO_VERSION=$(curl 'https://go.dev/VERSION?m=text' | head -n1)
cd ~/Downloads/ && wget https://go.dev/dl/$GO_VERSION.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf $GO_VERSION.linux-amd64.tar.gz
- 设置 SOCKS5 服务器(确保防火墙规则不阻止所使用的端口)```shell git clone https://github.com/wzshiming/socks5.git && cd socks5 go build -o ./bin/socks5_server ./cmd/socks5/*.go ./bin/socks5_server -a :3000
4. 返回你的主机并安装 `gohpts`(参见[安装](#installation))
5. 运行 `gohtps`:```shell
gohpts -s 203.0.113.10:3000 -T 8888 -Tu 8889 -M tproxy -sniff -body -auto -mark 100 -arpspoof "fullduplex true;debug true" -ndpspoof "ra true;debug true" -4 -6 -d
-
获取另一台设备(手机、平板电脑等)并将其连接到同一网络。尝试访问互联网,并检查主机上是否出现一些流量。使用一些在线工具检查公网 IP 地址(在这种情况下,它应与你的 VPS 地址
203.0.113.10或全局 IPv6 地址匹配) -
按 Ctrl+C 停止代理
-
大功告成!
DNS 欺骗
要强制执行 DNS 过滤并通过更改 DNS 记录来欺骗目标,运行 GoHPTS 的主机应成为局域网设备的默认网关。为此,只需运行启用 udp 的透明代理,并同时运行 ARP/NDP 欺骗,使目标使用你的 DNS 服务器。
由 GoHPTS 创建的 DNS 回复看起来像来自路由器或受信任 DNS 服务器(Google、Cloudflare)的正常数据包,这导致客户端用你告诉它们的内容更新其缓存。但请记住,它仅适用于“标准”未加密 DNS 流量(DOT/DOH 不会被过滤或欺骗)。
DNS 过滤器和用于欺骗的域可以在 yaml 文件配置的 dns_filter 部分中配置。所有列表都接受 URL、文件路径以及类似于通常在 hosts 文件中找到的条目,参见 https://en.wikipedia.org/wiki/Hosts_(file)>)。
示例:```yaml
dns filters require udp transparent proxy and arpspoof/ndpspoof
filters accept hosts like entries (use either links, file paths or just plain comma separated lists
dns_filter: enabled: true whitelist: ["/tmp/whitelisted_domains.txt", "example.com", "*.google.com"] # ip is optional, domains can start with *. to match all subdomains blacklist: ["https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"] blacklist_all: false # block all non whitelisted domains spooflist: ["127.0.0.1 example.com"] # ip address is required here
使用场景:
- 为所有局域网设备拦截广告和跟踪器
- 通过屏蔽特定类别的网站实现家长控制
- 拦截已知的网络钓鱼和恶意软件域名
- 为分析目的进行流量重定向
- 通过重定向进行凭据收集
- 流量劫持和操纵(注入广告、脚本、跟踪)
- 监控和用户画像
此设置的最小配置:```yaml
# gohpts_dns_spoof.yaml
proxy_list:
- address: 127.0.0.1:1080 # point to socks5 server supporting TCP/UDP
sniffing:
enabled: true
body: true
transparent_proxy:
tcp:
enabled: true
address: 0.0.0.0:8888
udp:
enabled: true
address: 0.0.0.0:8889
mode: "tproxy"
auto: true
arpspoof:
enabled: true
settings: "fullduplex 1;debug 1;interval 1s"
dns_filter:
enabled: true
whitelist: []
blacklist: [
"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts",
] # list of domains to filter
blacklist_all: true
# all requests for example.com will be redirected to 0.0.0.0 address
spooflist: ["0.0.0.0 example.com"]
运行:```shell sudo ./gohpts -f ./gohpts_dns_spoof.yaml
更多信息可在此处找到:[https://en.wikipedia.org/wiki/DNS_spoofing](https://en.wikipedia.org/wiki/DNS_spoofing)
## 数据包捕获
[[返回]](#table-of-contents)
流量可以捕获为 pcap、pcapng 或自定义 txt 格式,之后可使用 Wireshark、tcpdump 等众多工具进行分析。
首先,确保 `GoHPTS` 可执行文件具有提升的权限,以便能够捕获原始数据包,你有两种选择:
- 运行一次 `sudo setcap cap_net_raw+ep ~/go/bin/gohpts`,以授予代理原始流量访问权限
- 当需要在 CLI 中指定 `-pcap` 标志或在文件配置中指定 `pcap.enabled` 时,使用 `sudo` 运行代理。
使用 CLI 配置代理:```shell
gohpts -pcap "promisc true;timeout 10s;exts txt,pcap,pcapng"
配置文件:```yaml pcap: enabled: true settings: "promisc true;expr ip proto tcp;snaplen 65535;timeout 10s;packet_count 100;packet_buffer 8192;exts txt,pcap,pcapng"
这些命令会生成三个具有相应格式的数据包捕获文件,之后可由各种工具进行分析。
有关 pcap 选项的更多信息,请参阅 `gohpts -h` 和 [https://github.com/shadowy-pycoder/mshark](https://github.com/shadowy-pycoder/mshark)
## 网络命名空间
[[返回]](#table-of-contents)
默认情况下,`GoHPTS` 代理在单个网络命名空间内运行,但这可以被覆盖。由 `GoHPTS` 创建的监听套接字(例如 http 服务器或透明代理服务器)和出站套接字(socks 代理或直接拨号器)可以通过 Linux/Android [network_namespaces (7)](https://man7.org/linux/man-pages/man7/network_namespaces.7.html) 进行隔离。启动代理进程时,用户可以指定 `-in-netns`(监听器)和 `-out-netns`(拨号器)标志,并附带网络命名空间的名称或路径,以控制在哪个隔离环境中创建套接字。如果你希望在当前(默认)命名空间中创建监听器或拨号器,只需省略该标志即可。要显式指定主机命名空间,可以使用路径 `/proc/1/ns/net` —— 这允许代理正确识别系统名称服务器。
`GoHPTS` 支持 [ip-netns (8)](https://man7.org/linux/man-pages/man8/ip-netns.8.html) 约定,通过位于 `/etc/netns/NAME/` 目录中的文件提供网络配置。因此,要为 `ns1` 网络命名空间指定自定义名称服务器,你需要执行以下操作:```shell
sudo mkdir -p /etc/netns/ns1
sudo tee /etc/netns/ns1/resolv.conf << EOF
nameserver 8.8.8.8
nameserver 2001:4860:4860:0:0:0:0:8888
EOF
如果未找到配置,将使用 Google DNS 服务器来解析域名。
如果你的系统启用了 systemd-resolved.service (8),在通过自定义网络命名空间进行查询时,你可能希望暂时禁用它:```shell sudo ip netns exec ns1 unshare --mount bash -c ' mount --bind /dev/null /run/systemd/resolve/io.systemd.Resolve curl -Nvk https://example.com'
或使其对特定 shell 实例持久化:```shell
sudo ip netns exec ns1 unshare --mount bash -c '
mount --bind /dev/null /run/systemd/resolve/io.systemd.Resolve
exec bash --login'
Playground 设置
- 运行支持 UDP ASSOCIATE 的 socks5 服务器 ```shell
git clone https://github.com/wzshiming/socks5.git && cd socks5
go build -o socks5_server ./cmd/socks5/main.go
./socks5_server -a 0.0.0.0:1080
- 下载并安装 Simple HTTP3 to SOCKS5 proxy example: ```shell
git clone https://github.com/shadowy-pycoder/http3-socks-proxy.git
cd http3-socks-proxy
make
- 克隆仓库并编译 ```shell
git clone https://github.com/shadowy-pycoder/go-http-proxy-to-socks.git
cd go-http-proxy-to-socks
make
- 创建
key.pem和cert.pem文件: ```shell openssl req -x509 -newkey rsa:2048
-keyout key.pem
-out cert.pem
-sha256
-days 365
-nodes
-subj "/C=XX/ST=StateName/L=CityName/O=CompanyName/OU=CompanySectionName/CN=127.0.0.1"
-addext "subjectAltName=IP:127.0.0.1" - 创建网络命名空间
ns1并配置 veth 网络 ```shell sudo ip netns add ns1 sudo ip link add dev veth0 type veth peer name veth1 netns ns1 sudo ip addr add 10.0.0.1/24 dev veth0 sudo ip -6 addr add fd12:3456:789a::1/64 dev veth0 sudo ip link set dev veth0 up sudo ip netns exec ns1 ip addr add 10.0.0.2/24 dev veth1 sudo ip netns exec ns1 ip -6 addr add fd12:3456:789a::2/64 dev veth1 sudo ip netns exec ns1 ip link set dev lo up sudo ip netns exec ns1 ip link set dev veth1 up - 确定
wlan0的 IP 地址,以便能够连接到本地 socks5 ```shell WLAN_IP=$(ip -4 -c=never route get 8.8.8.8 | awk '{print $7}' | tr -d '\n')
使用示例
-
HTTP 代理 - 在
ns1中代理监听器(无默认路由,无互联网访问),出站套接字在主机上运行代理: ```shell sudo ./bin/gohpts -s 0.0.0.0:1080 -l :8083 -4 -6 -d -sniff -body -in-netns ns1
通过 ns1 发起请求 ```shell
sudo ip netns exec ns1 curl -Nv --proxy http://127.0.0.1:8083 https://example.com
请求应成功
2. **HTTP2 代理 - `ns1` 中的代理监听器(无默认路由,无互联网访问),主机上的出站套接字**
运行代理: ```shell
sudo ./bin/gohpts -s 0.0.0.0:1080 -l :8083 -4 -6 -d -sniff -body -in-netns ns1 -c ./cert.pem -k ./key.pem
通过 ns1 发起请求 ```shell
sudo ip netns exec ns1 curl -Nvk --http2 --proxy-insecure --proxy-http2 --proxy https://127.0.0.1:8083 https://example.com
请求应当成功
3. **HTTP3 代理 - `ns1` 中的代理监听器(无默认路由、无互联网访问),主机上的出站套接字**
运行代理: ```shell
sudo ./bin/gohpts -s 0.0.0.0:1080 -l :8083 -4 -6 -d -sniff -body -in-netns ns1 -c ./cert.pem -k ./key.pem
通过 ns1 发起请求 ```shell
sudo ip netns exec ns1 ./http3-socks-proxy/bin/client -a 127.0.0.1:8083 www.google.com
请求应当成功
4. **重定向透明代理(`-M redirect`)—— 代理监听器位于 `ns1`(默认路由,无互联网访问),出站套接字位于主机**
运行代理: ```shell
sudo ./bin/gohpts -s 0.0.0.0:1080 -l :8083 -4 -6 -d -sniff -body -in-netns ns1 -nohttp -M redirect -T :8888 -auto
通过 ns1 发起请求 ```shell
sudo ip netns exec ns1 curl -Nv https://example.com
请求应失败
将默认路由添加到 `ns1` ```shell
sudo ip netns exec ns1 ip route add default via 10.0.0.1
sudo ip netns exec ns1 ip -6 route add default via fd12:3456:789a::1
再试一次 ```shell sudo ip netns exec ns1 curl -Nv https://example.com
现在请求应该成功
5. **HTTP 代理 - 主机上的代理监听器,`ns1` 中的出站套接字(默认路由,可访问互联网)**
添加 NAT 规则以允许 `ns1` 通过 `wlan0` 连接到互联网 ```shell
sudo iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o wlan0 -j MASQUERADE
sudo ip6tables -t nat -A POSTROUTING -s fd12:3456:789a::/64 -o wlan0 -j MASQUERADE
运行代理: ``` sudo ./bin/gohpts -s :1080 -l :8083 -4 -6 -d -sniff -body -out-netns ns1 -i wlan0
通过主机发起请求 ```shell
curl -Nv --proxy http://$WLAN_IP:8083 https://example.com
请求应成功
-
HTTP3 代理 - 代理监听器位于主机上,出站套接字位于
ns1中(默认路由,可访问互联网)运行代理: ``` sudo ./bin/gohpts -s :1080 -l :8083 -4 -6 -d -sniff -body -out-netns ns1 -i wlan0 -c ./cert.pem -k ./key.pem
通过主机发起请求 ```shell ./http3-socks-proxy/bin/client -a $WLAN_IP:8083 www.google.com
请求应成功
7. **透明代理重定向 - 代理监听器在主机上,出站套接字在 `ns1` 中(默认路由,可访问互联网)**
运行代理(`-auto` 对我来说无法与本地 socks5 服务器配合使用,因此我使用远程服务器): ```
sudo ./bin/gohpts -s <remote> -4 -6 -d -sniff -body -out-netns ns1 -nohttp -M redirect -T :8888 -auto
通过主机发起请求 ```shell curl -Nv https://example.com
请求应成功
8. **HTTP 代理 - LAN(`ns2`(代理监听器)、`ns3`、`ns4`),`ns1` 中的出站套接字(默认路由,互联网访问)**
创建 LAN ```shell
sudo ip link add br0 type bridge
sudo ip addr add 10.0.1.1/24 dev br0
sudo ip -6 addr add fd12:3456:789b::1/64 dev br0
sudo ip link set br0 up
sudo ip netns add ns2
sudo ip link add veth2 type veth peer name veth3 netns ns2
sudo ip link set veth2 master br0
sudo ip link set veth2 up
sudo ip netns exec ns2 ip addr add 10.0.1.2/24 dev veth3
sudo ip netns exec ns2 ip -6 addr add fd12:3456:789b::2/64 dev veth3
sudo ip netns exec ns2 ip link set lo up
sudo ip netns exec ns2 ip link set veth3 up
sudo ip netns exec ns2 ip route add default via 10.0.1.1
sudo ip netns exec ns2 ip -6 route add default via fd12:3456:789b::1
sudo ip netns add ns3
sudo ip link add veth4 type veth peer name veth5 netns ns3
sudo ip link set veth4 master br0
sudo ip link set veth4 up
sudo ip netns exec ns3 ip addr add 10.0.1.3/24 dev veth5
sudo ip netns exec ns3 ip -6 addr add fd12:3456:789b::3/64 dev veth5
sudo ip netns exec ns3 ip link set lo up
sudo ip netns exec ns3 ip link set veth5 up
sudo ip netns exec ns3 ip route add default via 10.0.1.1
sudo ip netns exec ns3 ip -6 route add default via fd12:3456:789b::1
sudo ip netns add ns4
sudo ip link add veth6 type veth peer name veth7 netns ns4
sudo ip link set veth6 master br0
sudo ip link set veth6 up
sudo ip netns exec ns4 ip addr add 10.0.1.4/24 dev veth7
sudo ip netns exec ns4 ip -6 addr add fd12:3456:789b::4/64 dev veth7
sudo ip netns exec ns4 ip link set lo up
sudo ip netns exec ns4 ip link set veth7 up
sudo ip netns exec ns4 ip route add default via 10.0.1.1
sudo ip netns exec ns4 ip -6 route add default via fd12:3456:789b::1
运行代理: ``` sudo ./bin/gohpts -s $WLAN_IP:1080 -l 0.0.0.0:8083 -4 -6 -d -sniff -body -in-netns ns2 -out-netns ns1
发起请求 ```shell
curl -Nv --proxy http://10.0.1.2:8083 http://example.com
sudo ip netns exec ns2 curl -Nv --proxy http://10.0.1.2:8083 https://example.com
sudo ip netns exec ns3 curl -Nv --proxy http://10.0.1.2:8083 https://example.com
sudo ip netns exec ns4 curl -Nv --proxy http://10.0.1.2:8083 https://example.com
所有请求都应成功
-
HTTP3 代理 - LAN(
ns2(代理监听器)、ns3、ns4),ns1中的出站套接字(默认路由,互联网访问)运行代理: ``` sudo ./bin/gohpts -s $WLAN_IP:1080 -l 0.0.0.0:8083 -4 -6 -d -sniff -body -in-netns ns2 -out-netns ns1 -c ./cert.pem -k ./key.pem
发起请求 ```shell ./http3-socks-proxy/bin/client -a 10.0.1.2:8083 www.google.com sudo ip netns exec ns2 ./http3-socks-proxy/bin/client -a 10.0.1.2:8083 www.google.com sudo ip netns exec ns3 ./http3-socks-proxy/bin/client -a 10.0.1.2:8083 www.google.com sudo ip netns exec ns4 ./http3-socks-proxy/bin/client -a 10.0.1.2:8083 www.google.com
所有请求都应成功
10. **透明重定向代理 - LAN(`ns2`(代理监听器)、`ns3`、`ns4`),出站套接字在 `ns1`(默认路由,互联网访问)**
运行代理:
```shell
sudo ./bin/gohpts -s $WLAN_IP:1080 -4 -6 -d -sniff -body -in-netns ns2 -out-netns ns1 -nohttp -M redirect -T :8888 -auto
```
发起请求
```shell
sudo ip netns exec ns2 curl -Nv https://example.com
```
对于 `ns3` 和 `ns4`,请求失败
11. **使用 `IP_TRANSPARENT` 的透明代理(启用 arp/ndp 欺骗)LAN(`ns2`(代理监听器)、`ns3`、`ns4`),出站套接字在 `ns1`(默认路由,互联网访问)**
运行代理:
```shell
sudo ./bin/gohpts -s $WLAN_IP:1080 -4 -6 -d -sniff -body -in-netns ns2 -out-netns ns1 -nohttp -M tproxy -T :8888 -auto -arpspoof "fullduplex 1;debug 1;interval 1s" -ndpspoof "ra true;interval 10s;debug 1"
```
现在之前在 `ns3` 和 `ns4` 上的请求应该可以正常工作
```
sudo ip netns exec ns3 curl -Nv https://example.com
sudo ip netns exec ns4 curl -Nv https://example.com
```
12. **HTTP3 代理 - 代理监听器在 `ns1`,出站套接字在主机,`-nosocks` 标志**
运行代理:
```shell
sudo ./bin/gohpts -l 0.0.0.0:8083 -4 -6 -d -sniff -body -in-netns ns1 -c ./cert.pem -k ./key.pem -nosocks
```
发起请求
```shell
./http3-socks-proxy/bin/client -a 10.0.0.2:8083 www.google.com
```
请求应该成功
13. **HTTP3 代理 - 代理监听器在主机,出站套接字在 `ns1`,`-nosocks` 标志**
运行代理:
```shell
sudo ./bin/gohpts -l 0.0.0.0:8083 -4 -6 -d -sniff -body -out-netns ns1 -c ./cert.pem -k ./key.pem -nosocks
```
发起请求
```shell
./http3-socks-proxy/bin/client -a 127.0.0.1:8083 www.google.com
```
请求应该失败
向 `FORWARD` 链添加规则
```shell
sudo iptables -A FORWARD -i wlan0 -o veth0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i veth0 -o wlan0 -j ACCEPT
sudo ip6tables -A FORWARD -i veth0 -j ACCEPT
sudo ip6tables -A FORWARD -o veth0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
```
发起请求
```shell
./http3-socks-proxy/bin/client -a 127.0.0.1:8083 www.google.com
```
请求应该成功
## 混合服务器
[[返回]](#table-of-contents)
`GoHPTS` 可以配置为在同一地址上监听 SOCKS 连接,只需添加 `-mixed` 标志即可启动额外的 SOCKS 服务器。这使得 `GoHPTS` 不仅可以充当 `HTTP-to-SOCKS` 代理,还可以充当 `SOCKS-to-SOCKS` 代理。本地 SOCKS5 服务器支持 `UDP ASSOCIATE` 命令,因此从技术上讲,用户可以通过这个混合服务器发送 UDP 数据报。
运行代理:```shell
gohpts -s :1080 -l :8080 -mixed
测试连接:```shell curl -Nv --proxy socks5://127.0.0.1:8080 "https://example.com"
或者通过添加 `-nosocks` 禁用上游 SOCKS 代理并直接连接:```shell
gohpts -l :8080 -mixed -nosocks
测试连接:```shell curl -Nv --proxy socks5://127.0.0.1:8080 "https://example.com"
添加 `-socks4` 标志以改用 SOCKS4 协议:```shell
# :1080 should be a socks4 server
gohpts -s :1080 -l :8080 -mixed -socks4
测试连接:```shell curl -Nv --proxy socks4://127.0.0.1:8080 "https://example.com"
## 链接
[[返回]](#table-of-contents)
通过以下链接了解更多关于透明代理的信息:
- [Linux 内核中的透明代理支持](https://docs.kernel.org/networking/tproxy.html)
- [Gost 的透明代理教程](https://latest.gost.run/en/tutorials/redirect/)
- [简单的 tproxy 示例](https://github.com/FarFetchd/simple_tproxy_example)
- [Golang TProxy](https://github.com/KatelynHaworth/go-tproxy)
- [使用 eBPF 和 Go 实现透明代理](https://medium.com/all-things-ebpf/building-a-transparent-proxy-with-ebpf-50a012237e76)
- [https://github.com/heiher/hev-socks5-tproxy](https://github.com/heiher/hev-socks5-tproxy)
支持 `UDP ASSOCIATE` 的 `socks5` 代理:
- [https://github.com/wzshiming/socks5](https://github.com/wzshiming/socks5)
- [https://github.com/things-go/go-socks5](https://github.com/things-go/go-socks5)
- [https://github.com/0990/socks5](https://github.com/0990/socks5)
- [https://github.com/dizda/fast-socks5](https://github.com/dizda/fast-socks5)
- [https://github.com/semigodking/redsocks](https://github.com/semigodking/redsocks)
- [https://github.com/ginuerzh/gost](https://github.com/ginuerzh/gost)
IPv4/IPv6 网络安全:
- [https://caster0x00.com/legless/](https://caster0x00.com/legless/)
- [https://caster0x00.com/intercept/](https://caster0x00.com/intercept/)
- [https://www.prosec-networks.com/en/blog/ipv6-mitm/](https://www.prosec-networks.com/en/blog/ipv6-mitm/)
## 贡献
[[返回]](#table-of-contents)
你是开发者吗?
- Fork 该仓库
- 创建你的功能分支:`git switch -c my-new-feature`
- 提交你的更改:`git commit -am 'Add some feature'`
- 推送到该分支:`git push origin my-new-feature`
- 提交一个 pull request
## 许可证
[[返回]](#table-of-contents)
GPLv3