Shadowsocks Transparent Proxy

云水遥

遇到 proxychains 的已知问题,因此有了这篇文章。

参考 https://blog.outv.im/2020/cgroups-systemd-iptables-redir/

cgroup

目标是实现这样的东西:

cgprun ssh somehost

另外虽然已经有一个项目 cgproxy,但它不 work,只能手动配一个。

总之,我们要做的是把要代理的程序放到一个 cgroup 里,然后用 iptables 对流量进行透明代理。

首先我们建立一个什么也不干的 service:

[Unit]
Description=Cgroup bearer for transparent proxy

[Service]
Slice=proxy.slice
Type=simple
ExecStart=/usr/bin/sleep inf

[Install]
WantedBy=multi-user.target

然后用 systemctl start proxy.service 跑起来。

ss-redir

这里少数要做的改动是把 ss-local 改成 ss-redir,换个端口(1080 -> 8010),(可选)启动 UDP 转发(-u)

iptables

iptables -t nat -N TP-TCP

iptables -t nat -A TP-TCP -d 0.0.0.0/8 -j RETURN
iptables -t nat -A TP-TCP -d 127.0.0.0/8 -j RETURN
iptables -t nat -A TP-TCP -d 10.0.0.0/8 -j RETURN
iptables -t nat -A TP-TCP -d 169.254.0.0/16 -j RETURN
iptables -t nat -A TP-TCP -d 172.16.0.0/12 -j RETURN
iptables -t nat -A TP-TCP -d 192.168.0.0/16 -j RETURN
iptables -t nat -A TP-TCP -d 224.0.0.0/4 -j RETURN
iptables -t nat -A TP-TCP -d 240.0.0.0/4 -j RETURN

iptables -t nat -A TP-TCP -p tcp -j REDIRECT --to-ports 8010
iptables -t nat -A OUTPUT -m cgroup --path "proxy.slice" -p tcp -j TP-TCP

cgprun

systemd-run --uid 1000 --gid 1000 --working-directory "$PWD" --slice "proxy.slice" --quiet --pty "$@"

注意这个属于特权操作,需要 polkit 验证当前用户密码。

use nft

#!/usr/sbin/nft -f

flush ruleset

define RESERVED_IP = {
	10.0.0.0/8,
	100.64.0.0/10,
	127.0.0.0/8,
	169.254.0.0/16,
	172.16.0.0/12,
	192.0.0.0/24,
	224.0.0.0/4,
	240.0.0.0/4,
	255.255.255.255/32
}

table ip shadowsocks {
	chain output {
		type nat hook output priority mangle;
		ip daddr $RESERVED_IP return
		socket cgroupv2 level 1 "proxy.slice" tcp sport {32768-61000} redirect to 8010
	}

	chain input {
		type nat hook input priority mangle;
	}
}

Newer: USTC Hackergame 2022 writeup

Older: RWCTF 2022 Crypto SMT

Back to: Listing G2R