iptables 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的

 编译环境和内核配置
tar jxvf linux-2.6.25.19.tar.bz2 -C /usr/src/
tar zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/
cd /usr/src/linux-2.6.25.19/
patch –p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch
把 netfilter中的某些模块加入内核中
cp /boot/config-2.6.18-8.el5 .config 利用旧的内核配置
make menuconfig 选择需要编译的内核模块
//配置内核时,在 “Networking ---> Networking Options ---> Network Packet filtering framework (Netfilter) ”处主要注意两个地方:
    1) ---> Code Netfilter Configuration
        //将“Netfilter connection tracking suport (NEW)”选择编译为模块(M),需选取此项才能看到layer7支持的配置。
        //将layer7、string、state、time、IPsec、iprange、connlimit ,FTP……等编译成模块,根据需要看着办。
    2) ---> IP: Netfilter Configuration
        //将 “IPv4 connection tracking support (require for NAT)”编译成模块。
        //将“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”编译成模块。
模块选择正确后,安装编译新的内核和模块
make && make modules_install && make install
 
内核编译完成后卸载现有的 iptables
在卸载之前要想使安装后的 iptables正常启动,需要保留导入文件
cd /etc/init.d
cp iptables iptables.bak
rpm -e iptables –nodeps 卸载旧的 iptables
安装合并 iptables和layer7补丁
tar -jxvf iptables-1.4.2.tar.bz2 -C /usr/src/
cd /usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/
cp libxt_layer7.c libxt_layer7.man /usr/src/iptables-1.4.2/extensions/把相关文件拷贝进 iptables安装目录中。
cd /usr/src/iptables-1.4.2/
./configure --prefix=/ --with-ksource=/usr/src/linux-2.6.25.19
生成安装文件
make && make install
mv /etc/init.d/iptables.bak /etc/init.d/iptables 把原来的启动文件放进正确的位置
安装过滤七项协议的软件包
tar -zxvf l7-protocols-2008-10-04.tar.gz -C /etc/
mv /etc/l7-protocols-2008-10-04/ /etc/l7-protocols调整文位置,并命名新文件名
service iptables start启动 iptables
应用案例:
某公司有三个部门
工程部门 192.168.10.11—192.168.10.20
技术部门 192.168.10.21—192.168.10.30
经理办     192.168.10.31—192.168.10.40
公司上班时间 (周一---周五 08:20:00)
为了提高员工工作效率,现将公司网络做如下整改:
1、工程部门 上班时间可以访问ftp和http
不允许 qq聊天和迅雷下载,下班后无限制
2、软件部门 可以访问http   
不允许非法站点sina ,不允许使用迅雷 ,连接数最多3个
不允许聊天 ,下班后无限制
3、经理办公室    可以访问http资源 qq聊天,下班后无限制
4、dmz区域www服务器进行发布

案例拓扑图

vim /etc/sysctl.conf编辑配置文件
7 net.ipv4.ip_forward = 1
sysctl –p 使修改文件生效
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth1 -j SNAT --to 192.168.101.234 内网到外网的过滤条目
modprobe ip_nat_ftp 添加ftp模块
iptables -A INPUT -s 192.168.10.15 -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -d 192.168.10.15 -p tcp --sport 22 -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.11-192.168.10.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT
iptables -t filter -A FORWARD -p tcp --sport 21 -j ACCEPT
iptables -t filter -A FORWARD -p tcp --sport 20 -j ACCEPT
iptables -t filter -A FORWARD -p tcp --dport 20 -j ACCEPT
修改iptables的默认为拒绝所有,基于访问来记录数据流出的轨迹来返回信息。
iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.11-192.168.10.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT
iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t filter -A FORWARD -s 192.168.10.0/24 -m time --timestart 20:01 --timestop 07:59 -o eth1 -j ACCEPT
上班时间工程部门允许访问ftp,下班时间无限制。
iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -j ACCEPT
DNS
iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp --dport 53 -j ACCEPT
软件部门上班时间可以访问http资源。
iptables -t filter -A FORWARD -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m string --string "sina" --algo bm -j DROP
iptables -t filter -I FORWARD 1 -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m string --string "sina" --algo bm -j DROP
拒绝上班时间访问sina网页。
ptables -I FORWARD 2 -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j DROP
拒绝上班时间使用qq
iptables -I FORWARD 2 -m iprange --src-range 192.168.10.21-192.168.10.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --syn --dport 80 -m connlimit --connlimit-above 3 -j DROP
连接数目最大不超过3个
经理办公室的防火墙条目
iptables -I FORWARD 2 -m iprange --src-range 192.168.10.31-192.168.10.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp --dport 53 -j ACCEPT
允许访问31到40的主机上TCP协议在53端口上传输的DNS信息。
iptables -I FORWARD 2 -m iprange --src-range 192.168.10.31-192.168.10.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -j ACCEPT
允许访问31到40的主机上TCP协议在80端口上建立传输
iptables -I FORWARD 2 -m iprange --src-range 192.168.10.31-192.168.10.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j ACCEPT
允许上班时间登陆qq
iptables -I FORWARD 2 -m iprange --src-range 192.168.10.31-192.168.10.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto xunlei -j ACCEPT
允许上班时间使用迅雷
iptables -t nat -A PREROUTING -d 192.168.101.234 -p tcp --dport 80 -i eth1 -j DNAT --to 192.168.2.100
发布DMZ区域的服务器信息
iptables -t filter -A FORWARD -d 192.168.2.100 -p tcp --dport 80 -j ACCEPT
将访问外网的wed请求(通过80端口)转换为访问内网的web