sshhuttle 实现 子网代理功能

首先实现讲本地流量转发远程指定的网段

  • 将本地的子网 192.168.3.1/24 和 10.172.0.12/24 转发到远程主机 172.16.37.208 的端口 5004。
  • 远程主机必须是访问通过的主机 IP

实现方式

sshuttle -r root@172.16.37.208:5004 192.168.3.1/24 10.172.0.12/24 

不同端口

sshuttle -r root@172.16.37.208:7023 172.20.10.37/24

接口

sshuttle -r root@172.16.37.208:5004 192.168.3.1/24 10.172.0.12/24

原理

  • 使用 ssh 隧道将 127.0.0.1:12300 流量转转发到172.16.37.208:5004
  • 使用 iptable 或者 pfctl 将通过代理 将子网 192.168.3.1/24 和 10.172.0.12/24转到 127.0.0.1:12300

实验验证

  • 下载 shuttle 源码
git clone  https://github.com/sshuttle/sshuttle.git
cd sshuttle
python  __main__.py  -r root@172.16.37.208:5004 192.168.3.1/24 10.172.0.12/24 -v
  • 查看输出日志

核心代码

  • 监听服务 sshuttle/client.py:934 MultiListener
  • 转发流量设置 sshuttle/firewall.py:298 method.setup_firewall

防火墙实现的步骤

  • 将目的地址为 92.168.3.1/24 和 10.172.0.12/24 的TCP数据包重定向到 127.0.0.1:12300
  • 将目的地址为 92.168.3.1/24 和 10.172.0.12/24 的TCP数据包路由到 lo0 接口并保持连接状态
  • 允许本机发出的所有 TCP 数据包发送到 127.0.0.1

liunux 上 iptables

iptables -t nat -N sshuttle-12300
iptables -t nat -F sshuttle-12300
iptables -t nat -I OUTPUT 1 -j sshuttle-12300
iptables -t nat -I PREROUTING 1 -j sshuttle-12300

iptables -t nat -A sshuttle-12300 -j RETURN   --dest 127.0.0.0/8 -p tcp
iptables -t nat -A sshuttle-12300 -j REDIRECT --dest 10.172.0.12/24 -p udp --dport 53 --to-ports 12300 -m ttl ! --ttl 42
iptables -t nat -A sshuttle-12300 -j REDIRECT --dest 192.168.3.1/24 -p udp --dport 53--to-ports 12300 -m ttl ! --ttl 42

mac bsd pfctl

rdr pass on lo0 inet proto tcp from ! 127.0.0.1 to 192.168.3.1/24 -> 127.0.0.1 port 12300
rdr pass on lo0 inet proto tcp from ! 127.0.0.1 to 10.172.0.12/24 -> 127.0.0.1 port 12300
pass out route-to lo0 inet proto tcp to 192.168.3.1/24 keep state
pass out route-to lo0 inet proto tcp to 10.172.0.12/24 keep state
pass out inet proto tcp to 127.0.0.1/32

终止是删除防火墙规则

mac bsd pfctl

 pfctl -a sshuttle-12300 -F all
 pfctl -X 10002489529858848017

liunux 上 iptables

iptables -t nat -D OUTPUT-j sshuttle-12300
iptables -t nat -D PREROUTING -j sshuttle-12300
iptables -t nat -F sshuttle-12300
iptables -t nat -X sshuttle-12300
posted @ 2023-06-29 11:15  vx_guanchaoguo0  阅读(122)  评论(0编辑  收藏  举报