安全的Web主机iptables防火墙脚本
下面以自己的Web服务器举例说明之,系统的默认策略是INPUT为DROP,OUTPUT、FORWARD链为ACCEPT,DROP设置得比较宽松,因为我们知道出去的数据包比较安全;为了验证脚本的通用性,我特的查看了服务器的内核及iptables版本,命令如下所示:
[root@ud50041 ~]# uname -a
Linux ud50041 2.6.9-34.ELsmp #1 SMP Fri Feb 24 16:54:53 EST 2006 i686 i686 i386 GNU/Linux
[root@ud50041 ~]# iptables -V
iptables v1.2.11
[root@ud50041 ~]# lsb_release -a
LSB Version: :core-3.0-ia32:core-3.0-noarch:graphics-3.0-ia32:graphics-3.0-noarch
Distributor ID: RedHatEnterpriseAS
Description: Red Hat Enterprise Linux AS release 4 (Nahant Update 3)
Release: 4
Codename: NahantUpdate3
大家可以发现,此系统为RHEL4_i386系统,系统内核版本为2.6.9-34,iptables版本为1.2.11;另外我在别的Centos5.5 x86_64机器上也成功部署了此脚本;由于后续的recent安全模块对系统内核有要求(这个作为主机防护脚本也经常用到),如果大家要采用iptables作为主机防火墙时,建议用Centos5.6 x86_64或更高级版本,不然系统会有如下提示错误信息:
iptables: Unknown error 18446744073709551615
iptables:Invalid argument
在tail -f /var/log/messages时能发下面的的出错提示
ip_tables: connlimit match: invalid size 32 != 16
ip_tables: connlimit match: invalid size 32 != 24
另外,在生产环境下调试iptables脚本前,强烈建议编写crontab任务,每5分钟关闭一次iptalbes脚本,防止将SSH客户端锁在外面,命令如下所示:
*/5 * * * * root /etc/init.d/iptables stop
脚本代码如下所示:
- #!/bin/bash
- iptables -F
- iptables -F -t nat
- iptables -X
- iptables -P INPUT DROP
- iptables -P OUTPUT ACCEPT
- iptables -P FORWARD ACCEPT
- #load connection-tracking modules
- modprobe iptable_nat
- modprobe ip_conntrack_ftp
- modprobe ip_nat_ftp
- iptables -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
- iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 10 -j ACCEPT
- iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
- iptables -A INPUT -s 122.70.x.x -j ACCEPT
- 最新评论
