这是后面的问题 ,我问我的iptablesconfiguration是否正确。
CentOS 5.3系统。
预期结果:阻止除ping,ssh,Apache和SSL之外的所有内容。
基于异警惕的build议和对这个问题的其他答复(谢谢你们),我创build了这个脚本:
# Establish a clean slate iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -F # Flush all rules iptables -X # Delete all chains # Disable routing. Drop packets if they reach the end of the chain. iptables -P FORWARD DROP # Drop all packets with a bad state iptables -A INPUT -m state --state INVALID -j DROP # Accept any packets that have something to do with ones we've sent on outbound iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # Accept any packets coming or going on localhost (this can be very important) iptables -A INPUT -i lo -j ACCEPT # Accept ICMP iptables -A INPUT -p icmp -j ACCEPT # Allow ssh iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow httpd iptables -A INPUT -p tcp --dport 80 -j ACCEPT # Allow SSL iptables -A INPUT -p tcp --dport 443 -j ACCEPT # Block all other traffic iptables -A INPUT -j DROP
现在,当我列出规则我得到…
# iptables -L -v Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DROP all -- any any anywhere anywhere state INVALID 9 612 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED 0 0 ACCEPT all -- lo any anywhere anywhere 0 0 ACCEPT icmp -- any any anywhere anywhere 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:http 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:https 0 0 DROP all -- any any anywhere anywhere Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 5 packets, 644 bytes) pkts bytes target prot opt in out source destination
我跑了,我仍然可以login,所以这很好。 任何人都注意到什么重大的怪事?
大部分看起来不错。 最主要的是你应该使用iptables-save和iptables-restore,而不是重复运行iptables。 iptables-save / restore方法为你提供primefaces批量更新(如数据库事务),所以你知道没有任何东西可以进入(或不进入),因为当networking数据包到达时,你的iptables更改是半完成的。 做这个改变也可以让你转储最初的ACCEPT策略,所以它只是设置首选策略(最好是拒绝),然后是单个规则(被接受的例外)。
除此之外,你可能想看看更多的ICMPlocking(而不是只是允许一切)。 我听说现在ICMP的某些方面是非常狡猾的。 就我个人而言,我不认为这是值得的,因为如此多的诊断和stream量pipe理的东西依赖于ICMP。
关于womble的“不要使用iptables”的评论:我不会说,你不应该直接使用iptables(或iptables-save / restore),但我build议看看FERM。 它基本上只是iptables,具有更多的expression和更less的重复性语言,加上可变的支持。 例如,你的iptables命令:
iptables -P INPUT ACCEPT ... # Allow ssh iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow httpd iptables -A INPUT -p tcp --dport 80 -j ACCEPT # Allow SSL iptables -A INPUT -p tcp --dport 443 -j ACCEPT
在费尔姆看起来更像这样:
# allow some incoming TCP chain INPUT { policy ACCEPT; proto tcp dport (ssh httpd https) ACCEPT; }
好多了,是吧? ;)
最后,不要在22的默认端口上运行SSH。将它移动到另一个地址(编辑configuration文件,并重新加载sshd)。 即使通过ssh连接,你也可以这样做,但最好是在使用ssh或防火墙规则(基于控制台,由虚拟专用主机提供)的情况下使用另一种访问方法。 另外,最终考虑设置fail2ban之类的东西。 但是如果没有固定的IP(在我的方面)和特定的防火墙规则,我不会使用它,无论如何,在阻止fail2ban之前,我都可以访问它。
看起来不错,我的个人喜好是补充
-m state --state NEW
遵守这些规则
# Allow ssh iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow httpd iptables -A INPUT -p tcp --dport 80 -j ACCEPT # Allow SSL iptables -A INPUT -p tcp --dport 443 -j ACCEPT
并将INPUT,FORWARD上的默认策略更改为DROP
# Block all other traffic iptables -A INPUT -j DROP
冗
我会说,如果你直接使用iptables,那么你做的不对。