GRE 隧道。使用 IPTables 从 VPS 到本地服务器的端口转发不起作用

GRE 隧道。使用 IPTables 从 VPS 到本地服务器的端口转发不起作用

亲爱的 ServerFault 社区,

我有一个带有 3 个公共 IP(1.1.1.1、2.2.2.2、3.3.3.3)的 OVH VPS,我正尝试通过端口转发将它们分别转发到我办公室的服务器(IP 5.5.5.5),每个 IP 对应一个隧道。这样,当我在办公室服务器上运行服务时,我就可以隐藏我的办公室 IP。

1.1.1.1 为 VPS 的 SSH 保留。(除端口 23 外,所有端口均被删除)。

我通过 WireGuard 隧道和 IPTables 转发了一个 IP(2.2.2.2),并且它可以与curl --interface wg0 ifconfig.co端口转发一起正常工作(我可以通过 2.2.2.2:80 访问 Apache)。

WireGuard 子网:

1.0.0.1/32 for the VPS and 1.0.0.2/32 for the peer

对于第二个 IP(3.3.3.3),我尝试通过使用 GRE 隧道创建第二个子网和隧道,该隧道作为目标和源 IP 使用 WireGuard 的端点。

OVH VPS 上的 GRE 隧道设置:

iptunnel add gre1 mode gre local 10.0.0.1 remote 10.0.0.2 ttl 255
ip addr add 10.1.0.1/30 dev gre1
ip link set gre1 up

Office Server 上的 GRE 隧道设置:

iptunnel add gre1 mode gre local 10.0.0.2 remote 10.0.0.1 ttl 255
ip addr add 10.1.0.2/30 dev gre1
ip link set gre1 up

GRE 子网:

1.1.0.1/30 for the VPS and 1.1.0.2/30 for the peer

GRE 连接正常,我可以使用它访问互联网。此外,还curl --interface gre1 ifconfig.co显示了正确的 IP(3.3.3.3)。

唯一的问题是端口转发不起作用。我尝试在访问 3.3.3.3:80 时在 VPS 和 Office Server 上进行 TCPDump,似乎 Office Server 从 VPS 接收数据,但没有发送任何数据。

来自 VPS 的 TCPDump(访问 3.3.3.3:80 时):

17:23:18.982509 IP {CENSORED}.52946 > 10.1.0.2.http: Flags [S], seq 1181521223, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
17:23:18.983462 IP {CENSORED}.52947 > 10.1.0.2.http: Flags [S], seq 2207908725, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
17:23:19.246446 IP {CENSORED}.52949 > 10.1.0.2.http: Flags [S], seq 13463282, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
17:23:19.992556 IP {CENSORED}1.52946 > 10.1.0.2.http: Flags [S], seq 1181521223, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
17:23:19.993397 IP {CENSORED}1.52947 > 10.1.0.2.http: Flags [S], seq 2207908725, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
17:23:20.258502 IP {CENSORED}.52949 > 10.1.0.2.http: Flags [S], seq 13463282, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
17:23:22.004496 IP {CENSORED}.52946 > 10.1.0.2.http: Flags [S], seq 1181521223, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
17:23:22.004531 IP {CENSORED}.52947 > 10.1.0.2.http: Flags [S], seq 2207908725, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
17:23:22.268496 IP {CENSORED}.52949 > 10.1.0.2.http: Flags [S], seq 13463282, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0

来自 Office 服务器的 TCPDump(访问 3.3.3.3:80 时):

19:26:22.313047 IP {CESNORED}.53010 > 10.1.0.2.http: Flags [S], seq 43942198, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
19:26:22.313051 IP {CESNORED}.53011 > 10.1.0.2.http: Flags [S], seq 2711874582, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
19:26:23.326891 IP {CESNORED}.53010 > 10.1.0.2.http: Flags [S], seq 43942198, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
19:26:23.327948 IP {CESNORED}.53011 > 10.1.0.2.http: Flags [S], seq 2711874582, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
19:26:25.336925 IP {CESNORED}.53011 > 10.1.0.2.http: Flags [S], seq 2711874582, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
19:26:25.337102 IP {CESNORED}.53010 > 10.1.0.2.http: Flags [S], seq 43942198, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
19:26:29.338287 IP {CESNORED}.53011 > 10.1.0.2.http: Flags [S], seq 2711874582, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0
19:26:29.338290 IP {CESNORED}.53010 > 10.1.0.2.http: Flags [S], seq 43942198, win 64240, options [mss 1440,nop,wscale 8,nop,nop,sackOK], length 0

OVH VPS 的 IPTables:

# Generated by iptables-save v1.8.7 on Tue May 31 15:25:37 2022
*filter
:INPUT ACCEPT [1698:235639]
:FORWARD ACCEPT [1053:163056]
:OUTPUT ACCEPT [1451:166474]
:f2b-sshd - [0:0]
-A INPUT -s 1.1.1.1/32 -p tcp -m tcp --dport 23 -j ACCEPT
-A FORWARD -p GRE -j ACCEPT
-A INPUT -p GRE -j ACCEPT
-A INPUT -s 1.1.1.1/32 -j DROP
COMMIT
# Completed on Tue May 31 15:25:37 2022
# Generated by iptables-save v1.8.7 on Tue May 31 15:25:37 2022
*nat
:PREROUTING ACCEPT [435:15811]
:INPUT ACCEPT [428:15399]
:OUTPUT ACCEPT [32:2255]
:POSTROUTING ACCEPT [119:6298]


-A PREROUTING -d 2.2.2.2/32 -p tcp -m tcp -m multiport --dports 1000:51820 -j DNAT --to-destination 10.0.0.2
-A PREROUTING -d 2.2.2.2/32 -p tcp -m tcp -m multiport --dports 51826:65534 -j DNAT --to-destination 10.0.0.2
-A PREROUTING -d 2.2.2.2/32 -p udp -m udp -m multiport --dports 1000:51820 -j DNAT --to-destination 10.0.0.2
-A PREROUTING -d 2.2.2.2/32 -p udp -m udp -m multiport --dports 51826:65534 -j DNAT --to-destination 10.0.0.2
-A PREROUTING -d 2.2.2.2/32 -p tcp -m tcp -m multiport --dports 21,22,23,80,25,995,110,443,465,993,143 -j DNAT --to-destination 10.0.0.2
-A PREROUTING -d 2.2.2.2/32 -p udp -m udp -m multiport --dports 21,22,80,23,25,995,110,443,465,993,143 -j DNAT --to-destination 10.0.0.2


-A PREROUTING -d 3.3.3.3/32 -p tcp -m tcp -m multiport --dports 1000:51820 -j DNAT --to-destination 10.1.0.2
-A PREROUTING -d 3.3.3.3/32 -p tcp -m tcp -m multiport --dports 51826:65534 -j DNAT --to-destination 10.1.0.2
-A PREROUTING -d 3.3.3.3/32 -p udp -m udp -m multiport --dports 1000:51820 -j DNAT --to-destination 10.1.0.2
-A PREROUTING -d 3.3.3.3/32 -p udp -m udp -m multiport --dports 51826:65534 -j DNAT --to-destination 10.1.0.2
-A PREROUTING -d 3.3.3.3/32 -p tcp -m tcp -m multiport --dports 21,22,23,80,25,995,110,443,465,993,143 -j DNAT --to-destination 10.1.0.2
-A PREROUTING -d 3.3.3.3/32 -p udp -m udp -m multiport --dports 21,22,80,23,25,995,110,443,465,993,143 -j DNAT --to-destination 10.1.0.2
-A PREROUTING -p gre -j DNAT --to-destination 10.1.0.2


-A POSTROUTING -s 10.1.0.2/30 ! -o gre+ -j SNAT --to-source 3.3.3.3


-A POSTROUTING -s 10.0.0.2/32 -p tcp -m tcp -m multiport --sports 1000:51820 -j SNAT --to-source 2.2.2.2
-A POSTROUTING -s 10.0.0.2/32 -p tcp -m tcp -m multiport --sports 51826:65534 -j SNAT --to-source 2.2.2.2
-A POSTROUTING -s 10.0.0.2/32 -p tcp -m tcp -m multiport --sports 21,22,23,80,25,995,110,443,465,993,143 -j SNAT --to-source 2.2.2.2
-A POSTROUTING -s 10.0.0.2/32 -p udp -m udp -m multiport --sports 1000:51820 -j SNAT --to-source 2.2.2.2
-A POSTROUTING -s 10.0.0.2/32 -p udp -m udp -m multiport --sports 51826:65534 -j SNAT --to-source 2.2.2.2
-A POSTROUTING -s 10.0.0.2/32 -p udp -m udp -m multiport --sports 21,22,23,80,25,995,110,443,465,993,143 -j SNAT --to-source 2.2.2.2


-A POSTROUTING -s 10.1.0.2/30 -p tcp -m tcp -m multiport --sports 1000:51820 -j SNAT --to-source 3.3.3.3
-A POSTROUTING -s 10.1.0.2/30 -p tcp -m tcp -m multiport --sports 51826:65534 -j SNAT --to-source 149.202.147.64
-A POSTROUTING -s 10.1.0.2/30 -p tcp -m tcp -m multiport --sports 21,22,23,80,25,995,110,443,465,993,143 -j SNAT --to-source 3.3.3.3
-A POSTROUTING -s 10.1.0.2/30 -p udp -m udp -m multiport --sports 1000:51820 -j SNAT --to-source 3.3.3.3
-A POSTROUTING -s 10.1.0.2/30 -p udp -m udp -m multiport --sports 51826:65534 -j SNAT --to-source 3.3.3.3
-A POSTROUTING -s 10.1.0.2/30 -p udp -m udp -m multiport --sports 21,22,23,80,25,995,110,443,465,993,143 -j SNAT --to-source 3.3.3.3


COMMIT
# Completed on Tue May 31 15:25:37 2022

Office Server 上执行 curl --interface wg0 ifconfig.co 的结果:

2.2.2.2

Office Server 上执行 curl --interface gre1 ifconfig.co 的结果:

3.3.3.3

(两者均可连接互联网)

当 Apache 监听 0.0.0.0:80 时,我可以通过 2.2.2.2:80 访问 Web 服务器,但不能通过 3.3.3.3:80 访问。

即使 Apache 绑定到 10.1.0.2,我仍然无法访问 Web 服务器。

任何帮助都将非常感激!

非常感谢您的宝贵时间!

此致,

尼科洛

答案1

根据“来自 Office 服务器的 TCPDump(访问 3.3.3.3:80 时)”的输出,看起来端口转发正在正常工作。

这可能是办公室服务器的路由问题:其路由表可能设置为通过 wg0 路由所有流量(本地网络除外)。因此,到达 3.3.3.3:80 的访问者将收到来自 2.2.2.2 的响应。

可以通过从 VPS 在 wg0 上运行 tcpdump 来调查这种可能性。当请求发送到 3.3.3.3:80 时,应该没有来自 1.0.0.2:80 的流量。

如果是这种情况,解决方案就是基于源的路由策略:

相关内容