在CentOS 6上configurationIptables将我locking

我以root身份运行以下bash脚本来configurationiptables(我通过SSHlogin):

#!/bin/bash # Delete all existing rules iptables --flush # Set default chain policies iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT DROP # Allow port 80 (http) iptables -A INPUT -p tcp --sport 80 -j ACCEPT iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT # Allow port 443 (https) iptables -A INPUT -p tcp --sport 443 -j ACCEPT iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT # Allow port 8443 iptables -A INPUT -p tcp --sport 8443 -j ACCEPT iptables -A OUTPUT -p tcp --dport 8443 -j ACCEPT # Allow port 22 (ssh) iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A OUTPUT -p udp --sport 22 -j ACCEPT # Allow port 25 (smtp) iptables -A INPUT -p tcp --sport 25 -j ACCEPT iptables -A OUTPUT -p tcp --dport 25 -j ACCEPT # Allow port 110 (pop) iptables -A INPUT -p tcp --sport 110 -j ACCEPT iptables -A OUTPUT -p tcp --dport 110 -j ACCEPT # Allow port 995 iptables -A INPUT -p tcp --sport 995 -j ACCEPT iptables -A OUTPUT -p tcp --dport 995 -j ACCEPT # Allow port 143 (imap) iptables -A INPUT -p tcp --sport 143 -j ACCEPT iptables -A OUTPUT -p tcp --dport 143 -j ACCEPT # Allow port 993 iptables -A INPUT -p tcp --sport 993 -j ACCEPT iptables -A OUTPUT -p tcp --dport 993 -j ACCEPT # Allow port 465 (smtp) iptables -A INPUT -p tcp --sport 465 -j ACCEPT iptables -A OUTPUT -p tcp --dport 465 -j ACCEPT # Allow port 8447 iptables -A INPUT -p tcp --sport 8447 -j ACCEPT iptables -A OUTPUT -p tcp --dport 8447 -j ACCEPT # Ping rate limit (from outside) iptables -A INPUT -p icmp -m icmp --icmp-type address-mask-request -j REJECT iptables -A INPUT -p icmp -m icmp --icmp-type timestamp-request -j REJECT iptables -A INPUT -p icmp -m icmp -m limit --limit 1/second -j ACCEPT # Prevent DoS attack iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT # Loopback iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # User feedback service iptables save echo "Rules set, restarting iptables..." service iptables restart echo "Finished configuring iptables" 

该脚本执行并立即踢我SSH – 所有端口也closures(80,443,21等)。 如果我将默认链策略更改为:

 # Set default chain policies iptables -P INPUT REJECT iptables -P FORWARD REJECT iptables -P OUTPUT REJECT 

它工作正常,我可以通过端口80和SSH(22)。 不过,运行iptables --list显示如下:

 Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp spt:http ACCEPT tcp -- anywhere anywhere tcp spt:https ACCEPT tcp -- anywhere anywhere tcp spt:pcsync-https ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ACCEPT tcp -- anywhere anywhere tcp spt:smtp ACCEPT tcp -- anywhere anywhere tcp spt:pop3 ACCEPT tcp -- anywhere anywhere tcp spt:pop3s ACCEPT tcp -- anywhere anywhere tcp spt:imap ACCEPT tcp -- anywhere anywhere tcp spt:imaps ACCEPT tcp -- anywhere anywhere tcp spt:urd ACCEPT tcp -- anywhere anywhere tcp spt:8447 REJECT icmp -- anywhere anywhere icmp address-mask-request reject-with icmp-port-unreachable REJECT icmp -- anywhere anywhere icmp timestamp-request reject-with icmp-port-unreachable ACCEPT icmp -- anywhere anywhere icmp any limit: avg 1/sec burst 5 ACCEPT tcp -- anywhere anywhere tcp dpt:http limit: avg 25/min burst 100 ACCEPT all -- anywhere anywhere Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:http ACCEPT tcp -- anywhere anywhere tcp dpt:https ACCEPT tcp -- anywhere anywhere tcp dpt:pcsync-https ACCEPT udp -- anywhere anywhere udp spt:ssh ACCEPT tcp -- anywhere anywhere tcp dpt:smtp ACCEPT tcp -- anywhere anywhere tcp dpt:pop3 ACCEPT tcp -- anywhere anywhere tcp dpt:pop3s ACCEPT tcp -- anywhere anywhere tcp dpt:imap ACCEPT tcp -- anywhere anywhere tcp dpt:imaps ACCEPT tcp -- anywhere anywhere tcp dpt:urd ACCEPT tcp -- anywhere anywhere tcp dpt:8447 ACCEPT all -- anywhere anywhere 

…所有的默认策略都设置为ACCEPT。 谁能告诉我为什么会发生这种情况?

你的规则中有许多错误。 从运动/ dport错误到指定udp而不是TCP。 你也忘记了DNS。 服务iptables重新启动也是不必要的。

忘记所有的输出规则,只要有一条规则,允许INPUT和OUTPUT顶部的已build立连接上的所有输出stream量。

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

然后只添加规则INPUT,并使用dport在这些,而不是运动。

并添加一个输出规则的DNS,使邮件和SSH再次工作。 再次使用dport。

ping速率限制是无用的 。 它们根本不阻止DOS / DDOS:数据包仍然到达您的服务器。 所以不要打扰那些。 恕我直言,http速率限制应该在应用程序中处理,而不是防火墙,但这是更多的意见,而不是事实。

这其中包括这条线

 iptables -A OUTPUT -p udp --sport 22 -j ACCEPT 

是你的问题。 你只允许在端口22上发送UDP数据包。它应该是

 iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT 

你的规则的另一个缺陷是,ICMP太大的错误不是例外。