如果让iptables支持七层过滤,我们首先需要打补丁,编译内核等等。这需要耗费一些时间。网上也有许多这样的文章。这里就不作介绍。

编译内核时根据自己需要酌情添加模块。下面是我的案例需要添加的模块支持。

/配置内核时,在“Networking ---> Networking Options ---> Network Packet filtering framework (Netfilter) ”处主要注意两个地方:
    1) ---> Core Netfilter Configuration
        //将“Netfilter connection tracking suport (NEW)”选择编译为模块(M),需选取此项才能看到layer7支持的配置。
        //将FTP,layer7、string、state、time、IPsec、iprange、connlimit……等编译成模块,根据需要看着办。

    2) ---> IP: Netfilter Configuration
        //将“IPv4 connection tracking support (require for NAT)”编译成模块。
        //将“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”编译成模块。

通过一个案例简单理解iptables的规则

公司有三个部门

工程部门  192.168.145.11--145.20

软件部门  192.168.145.21-145.30
经理办    192.168.145.31-145.40

上班时间 (周一---周五 08:20:00)
          工程部门   上班时间ftp 不允许qq http   迅雷  下班后无限制

         软件部门  http   不允许非法站点sina ,不允许使用迅雷 qq,连接数 最多3个    
                      下班后无限制

       经理办公室   http  qq 都可以,下班后无限制

       dmz区域www服务器进行发布

拓扑图

p_w_picpath

linux主机添加网卡至三块

ip地址:

eth0:192.168.145.200

eth1:192.168.101.20

eth2:192.168.2.254

ftp服务器:192.168.101.21

[root@localhost ~]# service network restart

公司内部网关指向192.168.145.200

dmz主机网关指向192.168.2.254

编译过的内核

[root@localhost ~]# uname -r
2.6.25.19

[root@localhost ~]# vim /etc/sysctl.conf

7 net.ipv4.ip_forward = 1    //打开路由转发功能

[root@localhost ~]# sysctl –p   //使修改生效

对来源是192.168.145.0 网段的进行nat转换。把源地址变为eth1的接口地址

[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.145.0/24 -o eth1 -j MASQUERADE

root@localhost ~]# modprobe ip_nat_ftp     //加载模块

首先把filter表格中规则设为拒绝所有

由于我是通过终端ssh登录上去的,先把22端口设为允许通过

[root@localhost ~]# iptables -A INPUT -s 192.168.145.10 -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -d 192.168.145.10 -p tcp --sport 22 -j ACCEPT

[root@localhost ~]# iptables -P INPUT DROP

[root@localhost ~]# iptables -P OUTPUT DROP

[root@localhost ~]# iptables -P FORWARD DROP

[root@localhost ~]# date
2012年 09月 18日 星期二 10:00:03 CST

工程部门的时间和ftp规则

[root@localhost ~]# iptables -t filter –A FORWARD -m iprange --src-range 192.168.145.11-192.168.145.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.11-192.168.145.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 21 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

工程部门测试

p_w_picpath p_w_picpath

公司内部的人下班时间没有限制

[root@localhost ~]# iptables -t filter -A FORWARD -s 192.168.145.0/24 -o eth1 -m time --timestart 20:01 --timestop 07:59 -j ACCEPT

域名指向电信dns服务器222.88.88.88

[root@localhost ~]# date 091820012012
2012年 09月 18日 星期二 20:01:00 CST

p_w_picpath

[root@localhost ~]# date 091811032012
2012年 09月 18日 星期二 11:03:00 CST

软件部门规则

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 53 -j ACCEPT

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m string --string "sina" --algo bm -j DROP

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -j DROP

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.30 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto xunlei -j DROP

[root@localhost ~]# iptables -t filter -A FORWARD -m iprange --src-range 192.168.145.21-192.168.145.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

工程部门测试,dns服务器设为222.88.88.88

p_w_picpath p_w_picpath

经理办公室

[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.145.31-192.168.145.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p udp --dport 53  -j ACCEPT

[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.145.31-192.168.145.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport 80  -j ACCEPT

[root@localhost ~]# iptables -A FORWARD  -m iprange --src-range 192.168.145.31-192.168.145.40 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq  -j ACCEPT

p_w_picpath

dmz区域服务器向外发布

[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.101.20 -p tcp --dport 80 -i eth1 -j DNAT --to 192.168.2.100

[root@localhost ~]# iptables -t filter -A FORWARD -d 192.168.2.100 -p tcp --dport 80 -j ACCEPT

p_w_picpath