Iptables

iptables 不允許 HTTP 流量

  • February 28, 2021

我有一些問題正在嘗試使用 iptables 規則進行故障排除。

當我執行以下命令並嘗試通過 Curl 連接到 Git 時,Git CLI 連接掛起。問題似乎僅限於 HTTPS,因為當我通過ufw連接允許 HTTPS 時沒有問題。

據我了解,以下規則應允許 HTTPS 在 443 上輸出,並允許 Git 工作所需的 9418。

我剛剛允許使用預設 DROP 策略的出站連接,其中 INBOUND 連接被以下允許:

iptables -I INPUT  -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

下面缺少什麼使 HTTPS 正常工作?

# Flush tables
iptables -F
ip6tables -F

# Whitelist my address 
iptables -I INPUT -p tcp  --dport 22 -s $whitelisted -j ACCEPT

# Set a default policy of DROP
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

# Accept any related or established connections
iptables -I INPUT  -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# Allow all traffic on the loopback interface
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# Allow outbound DHCP request
iptables -A OUTPUT -o eth0 -p udp --dport 67:68 --sport 67:68 -j ACCEPT

# Allow inbound SSH
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m state --state NEW  -j ACCEPT

# Allow inbound HTTPS
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 443 -m state --state NEW  -j ACCEPT

# Allow GIT
iptables -A OUTPUT -o eth0 -p tcp --dport 9418 -m state --state NEW -j ACCEPT

# Allow inbound HTTP
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 80 -m state --state NEW  -j ACCEPT


# Outbound DNS lookups
iptables -A OUTPUT -o eth0 -p udp -m udp --dport 53 -j ACCEPT

# Outbound PING requests
iptables -A OUTPUT -o eth0 -p icmp -j ACCEPT

# Outbound Network Time Protocol (NTP) requests
iptables -A OUTPUT -o eth0 -p udp --dport 123 --sport 123 -j ACCEPT

#### IPv6 Rules
# Drop all IPv6
ip6tables -P INPUT DROP
ip6tables -P FORWARD DROP
ip6tables -P OUTPUT DROP

# Must allow loopback interface
ip6tables -A INPUT -i lo -j ACCEPT

# Reject connection attempts not initiated from the host
ip6tables -A INPUT -p tcp --syn -j DROP

# Allow return connections initiated from the host
ip6tables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

我在 Ubuntu 20.04 中的伺服器

沒有允許傳出 HTTPS 流量的規則:

# Allow HTTPS
iptables -A OUTPUT -o eth0 -p tcp --dport 443 -m state --state NEW -j ACCEPT

引用自:https://serverfault.com/questions/1055393