IPTables

1.更改安全策略后立即生效;

2.仅有TCP、UDP数据包具有端口;

3.当Linux作为路由器使用时最好打开ping功能,以便客户端查询;

4.ipv4的内核管理功能在:/proc/sys/net/ipv4/*     其系统配置文件是/etc/sysctl.conf

5.防火墙对病毒的阻挡并不敏感

 

 

简易防火墙配置脚本实例:

########################################################################################

#!/bin/bash

 

#############请先输入相关参数,不要输入错误################################

EXTIF="eth0" #外网网卡

INIF="eth1" #内网网卡

INNET="192.168.0.0/24" #若无内部网络接口,请填写此项

export  EXTIF INIF INNET

 

##############第一部分,针对本机的防火墙设置###############################

#1.先设置好内核的网络功能

echo "1" > /proc/sys/net/ipv4/tcp_syncookies #在系统端口快用尽时启动,断开与未回复正确序号的客户端的连接,以防止syn flooding***

echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts #取消对ping broadcast的回应

for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,send_redirects};

do

echo  "0" > $i

done

 

#2.清除规则、设置默认策略并且开放lo与相关的设置

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin;

export  PATH

iptables -F

iptables -X

iptables -Z

iptables -P  INPUT   DROP

iptables -P  OUTPUT  ACCEPT

iptables -P  FORWARD ACCEPT

iptables -A  INPUT  -i lo -j ACCEPT

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

 

#3.启动额外的防火墙脚本模块(自定义脚本)

if [ -f /usr/local/virus/iptables/iptables.deny ];then

sh /usr/local/virus/iptables/iptables.deny

fi

 

if [ -f /usr/local/virus/iptables/iptables.allow ];then

sh /usr/local/virus/iptables/iptables.allow

fi

 

if [ -f /usr/local/virus/iptables/iptables.http ];then

sh /usr/local/virus/iptables/iptables.http

fi

 

#4.允许某些类型的ICMP数据包进入

AICMP="0 3 3/4 4 11 12 14 16 18" #8号ICMP即就是普通的ping,如果主机是路由器,最好不要禁掉8

for tyicmp in $AICMP

do

iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT

done

 

#5.允许某些服务的进入,请依照自己的环境开启

#iptables -A INPUT -p TCP -i $EXTIF --dport 21 --sport 1024:65534 -j ACCEPT #FTP Server

#iptables -A INPUT -p TCP -i $EXTIF --dport 22 --sport 1024:65534 -j ACCEPT #SSH Server

#iptables -A INPUT -p TCP -i $EXTIF --dport 25 --sport 1024:65534 -j ACCEPT #SMTP Server

#iptables -A INPUT -p UDP -i $EXTIF --dport 53 --sport 1024:65534 -j ACCEPT #DNS Server

#iptables -A INPUT -p TCP -i $EXTIF --dport 53 --sport 1024:65534 -j ACCEPT #DNS Server

#iptables -A INPUT -p TCP -i $EXTIF --dport 80 --sport 1024:65534 -j ACCEPT #HTTP Server

#iptables -A INPUT -p TCP -i $EXTIF --dport 110 --sport 1024:65534 -j ACCEPT #POP3 Server

#iptables -A INPUT -p TCP -i $EXTIF --dport 443 --sport 1024:65534 -j ACCEPT #HTTPS Server

 

#############################第二部分,针对后端主机的防火墙设置###########################################

#1.先加载一些有用的模块

modules="ip_tables  iptables_nat  ip_nat_ftp  ip_nat_irc  ip_conntrack  ip_conntrack_ftp  ip_conntrack_irc"

for mod in $modules

do

testmod=`lsmod | grep "^${mod}" | awk '{print $1}'`

if [ "$testmod" == " " ];then

modprobe $mod

fi

done

 

#2.清除NAT table的规则

iptables  -F -t nat

iptables  -X -t nat

iptables  -Z -t nat

iptables  -t nat -P PREROUTING  ACCEPT

iptables  -t nat -P POSTROUTING ACCEPT

iptables  -t nat -P OUTPUT ACCEPT

 

#3.若有内部接口的存在(双网卡)开放成为路由器,且为IP分享器

if [ "$INIF" != " " ];then

iptables -A INPUT -i $INIF -j ACCEPT

echo "1" > /proc/sys/net/ipv4/ip_forward

if [ "$INNET" != " " ];then

for innet in $INNET

do

iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE

done

fi

fi

 

##########################################################################################################

#如果出现MSN一直无法连接或者部分网站打不开,则也可能是MTU的问题,采用下面这一行限制MTU的范围##############

#iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu

##########################################################################################################

 

#4.NAT服务器后端的LAN内对外之服务器设置

#iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 -j DNAT --to-destination 192.168.0.11:80 #WWW Server

 

#5.特殊功能,包括Windows远程桌面所产生的规则,假设桌面主机IP为192.168.1.11

#iptables -t nat -A PREROUTING -p tcp -s 192.168.1.11 --dport 6000 -j DNAT --to-destination  192.168.0.111

#iptables -t nat -A PREROUTING -p tcp -s 192.168.1.11 --dport 3389 -j DNAT --to-destination  192.168.0.112

 

#6.最后将这些功能存储下来

/etc/init.d/iptables save

#######################################################################################