iptables 和 ip_forward 问题

iptables 和 ip_forward 问题

我遇到了一个非常烦人的问题,我不知道哪里出了问题。我也觉得我很快就能成功了,所以肯定是我做错了什么。

我正在尝试连接到我的 VPN,然后将我的安装用作网络上其他设备的网关,以便它们都可以共享该单一连接。

# echo 1 > /proc/sys/net/ipv4/ip_forward

我输入此信息,然后在我的手机(我正在测试的设备)上将网关设置为安装的 IP(192.168.0.250)

运行完美 - 网站打开速度超快,转发一切正常,可以根据需要访问互联网和所有网站,并且所有本地设备都可以通过我的网络联系。

然后我运行这个 iptables 脚本,这个脚本是我在其他人的帮助下编写的,并尝试使其适合我想要做的事情:

#!/bin/bash
# Flush
iptables -t nat -F
iptables -t mangle -F
iptables -F
iptables -X

# Block All
iptables -P OUTPUT DROP
iptables -P INPUT DROP
iptables -P FORWARD DROP

# allow Localhost
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# Make sure you can communicate with any DHCP server
iptables -A OUTPUT -d 255.255.255.255 -j ACCEPT
iptables -A INPUT -s 255.255.255.255 -j ACCEPT

# Make sure that you can communicate within your own network
iptables -A INPUT -s 192.168.0.0/24 -d 192.168.0.0/24 -j ACCEPT
iptables -A OUTPUT -s 192.168.0.0/24 -d 192.168.0.0/24 -j ACCEPT

# Allow established sessions to receive traffic:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow TUN
iptables -A INPUT -i nordtun -j ACCEPT
iptables -A FORWARD -i nordtun -j ACCEPT
iptables -A FORWARD -o nordtun -j ACCEPT
iptables -t nat -A POSTROUTING -o nordtun -j MASQUERADE
iptables -A OUTPUT -o nordtun -j ACCEPT

# allow VPN connection
iptables -I OUTPUT 1 -p udp --destination-port 51820 -m comment --comment "Allow VPN Connection WireGuard 51820" -j ACCEPT
iptables -I OUTPUT 1 -p udp --destination-port 1197 -m comment --comment "Allow VPN Connection OpenVPN 1197" -j ACCEPT
iptables -I OUTPUT 1 -p udp --destination-port 1194 -m comment --comment "Allow VPN Connection OpenVPN 1194" -j ACCEPT

# Block All
iptables -A OUTPUT -j DROP
iptables -A INPUT -j DROP
iptables -A FORWARD -j DROP

# Log all dropped packages, debug only.

iptables -N logging
iptables -A INPUT -j logging
iptables -A OUTPUT -j logging
iptables -A logging -m limit --limit 2/min -j LOG --log-prefix "IPTables general: " --log-level 7
iptables -A logging -j DROP

echo "saving"
iptables-save > /etc/iptables.rules

然后我运行它 - 其背后的概念是,如果 VPN 发生故障,那么网关上的任何设备都不会泄漏数据包,因为它只允许 UDP 端口 51820、1197、1194 上的出站连接,这 3 个是 OpenVPN 或 WireGuard 的常用 VPN 端口。

到目前为止,一切都很好。

我运行了脚本,然后就无法再 ping google 或一般互联网上的任何设备。我仍然可以访问本地网络上的所有内容,没有问题

# nordvpn connect canada
Connected!

VPN 连接没有问题——允许其 UDP 端口完美运行。

我的所有流量都通过 NordVPN 传输,我可以再次访问完整的互联网,并且可以访问我所有的内部设备。

惊人的!

只是……现在我回到手机上尝试使用网关时,我被限制只能使用我的内部网络!它不再允许我访问一般互联网,只能访问 192.168.0.0/24 内部地址

我知道这已经非常接近成功了。我需要你的帮助!

为什么不通过内部网络外的网关传递数据包?

非常感谢

相关内容