从 SSH 隧道创建网络接口

Bil*_*lly 3 ssh-tunneling tap network-interface

要通过 SSH 路由您的网络流量,大多数人会告诉您通过 -D在本地主机上创建 SOCKS 代理选项,您可以配置应用程序以使用该代理。

但是,简单的代理对我来说并不是最实用的解决方案。有没有办法访问 SSH 隧道,因为它是自己的接口,也许是 TUN/TAP?

我正在使用 Linux。

Rui*_*iro 8

在 Linux 中使用 OpenSSH,可以使用 TUN 或 TAP 接口通过 SSH 创建隧道,只要在适当的情况下设置了正确的路由和 ip 转发。

为了创建一个 TUN 隧道,这里会留下一个实用的脚本,来自Ip Tunnel Over Ssh With Tun;该脚本假定您以 root 身份运行。

将“PermitTunnel yes”添加到/etc/ssh/sshd_config
现在,在客户端上使用一些参数运行 ssh 就像运行 ssh 一样简单,我用于启动它的脚本是:

#!/bin/sh
HOST=REMOTE_PARTY_ADDRESS
HOST_PORT=22
TUN_LOCAL=0   # tun device number here.
TUN_REMOTE=0  # tun device number there
IP_LOCAL=192.168.111.2 # IP Address for tun here
IP_REMOTE=192.168.111.1 # IP Address for tun there.
IP_MASK=30 # Mask of the ips above.
NET_REMOTE=192.168.0.0/16 # Network on the other side of the tunnel
NET_LOCAL=192.168.8.0/24  # Network on this side of the tunnel

echo "Starting VPN tunnel ..." 
modprobe tun
ssh -w ${TUN_LOCAL}:${TUN_REMOTE} -f ${HOST} -p ${HOST_PORT} "\
ip addr add ${IP_REMOTE}/${IP_MASK} dev tun${TUN_REMOTE} \
&& ip link set tun${TUN_REMOTE} up \
&& ip route add ${NET_LOCAL} via ${IP_LOCAL} \
&& true"
sleep 3
ip addr add ${IP_LOCAL}/${IP_MASK} dev tun${TUN_LOCAL}
ip link set tun${TUN_LOCAL} up
ip route add ${NET_REMOTE} via ${IP_REMOTE}
echo "... done."
Run Code Online (Sandbox Code Playgroud)

如果你想访问/隧道网络而不是一台机器,你还必须激活 ip 转发,如下所示:

sudo sysctl -w net.ipv4.ip_forward=1
Run Code Online (Sandbox Code Playgroud)

https://github.com/trustedsec/tap/blob/master/scripts/ssh-tunnel.sh 上还有一个脚本,用于通过 TAP 接口创建 OpenSSH 隧道。