![iptables 似乎没有阻止我禁止的 IP](https://linux22.com/image/611597/iptables%20%E4%BC%BC%E4%B9%8E%E6%B2%A1%E6%9C%89%E9%98%BB%E6%AD%A2%E6%88%91%E7%A6%81%E6%AD%A2%E7%9A%84%20IP.png)
为了进行测试,我启动了 TorBrowser,获取了它的 IP,并通过以下命令在我的 VPS 上禁止了它:
sudo iptables -A INPUT -s <IP address> -j DROP
我仍然可以通过 TorBrowser 浏览我服务器托管的页面。我甚至仔细检查了 HTTP access.log,以确保 IP 是我禁止的,结果确实如此。我遗漏了什么?
我的 iptables 文件在启动时被读取(通过iptables-restore
)
# Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT
# IP bans
-A INPUT -s 42.121.24.80 -j DROP
-A INPUT -s 121.196.43.157 -j DROP
-A INPUT -s 192.30.85.135 -j DROP
-A INPUT -s 94.102.53.175 -j DROP
# Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
-A INPUT -p tcp --dport 8080 -j ACCEPT
# Mail
-A INPUT -p tcp --dport 993 -j ACCEPT
-A INPUT -p tcp --dport 465 -j ACCEPT
-A INPUT -p tcp --dport 587 -j ACCEPT
-A INPUT -p tcp --dport 25 -j ACCEPT
# Minecraft
-A INPUT -p tcp --dport 25565 -j ACCEPT
# Allow SSH connections
#
# The -dport number should be the same port number you set in sshd_config
#
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmp -j ACCEPT
# Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Drop all other inbound - default deny unless explicitly allowed policy
-A INPUT -j DROP
-A FORWARD -j DROP
COMMIT
并iptables -L
输出:
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
REJECT all -- anywhere 127.0.0.0/8 reject-with icmp-port-unreachable
DROP all -- out524-80.mail.aliyun.com anywhere
DROP all -- ip196.hichina.com anywhere
DROP all -- 192.30.85.135-IP-Static-VISPERAD.COM anywhere
DROP all -- tor-exit-nl1.privacyfoundation.dk anywhere
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere tcp dpt:http-alt
ACCEPT tcp -- anywhere anywhere tcp dpt:imaps
ACCEPT tcp -- anywhere anywhere tcp dpt:ssmtp
ACCEPT tcp -- anywhere anywhere tcp dpt:submission
ACCEPT tcp -- anywhere anywhere tcp dpt:smtp
ACCEPT tcp -- anywhere anywhere tcp dpt:25565
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
ACCEPT icmp -- anywhere anywhere
LOG all -- anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
DROP all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
答案1
dmourati 要求的iptables -L INPUT
原因,即您当前的规则。
在测试 torbrowser 之前,您有已发布的规则(或类似规则)。
现在在中间:
-A INPUT -p tcp --dport 80 -j ACCEPT
之后你执行
iptables -A INPUT -s <IP address> -j DROP
因此,您的规则在您接受所有 80 端口流量之后结束,因此无需丢弃任何流量,因为流量已被接受。
您应该使用以下方式添加规则
iptables -I INPUT -s <IP address> -j DROP
答案2
如果您希望优先使用 drop 命令,则需要在允许端口 80 的 append (-A) 命令之前插入 (-I) 该命令。
顺序很重要,请尝试:
sudo iptables -I INPUT -s <IP address> -j DROP