为iptables增加layer7补丁(Linux2.6.25内核)


系统环境
RHEL5 [2.6.18-164.el5]

软件环境

http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.19.tar.bz2
http://www.netfilter.org/projects/iptables/files/iptables-1.4.2.tar.bz2

http://ie.archive.Ubuntu.com/sourceforge/l/l7/l7-filter/netfilter-layer7-v2.20.tar.gz
http://ie.archive.ubuntu.com/sourceforge/l/l7/l7-filter/l7-protocols-2008-10-04.tar.gz

目标功能


为iptables增加layer7补丁,实现应用层过滤。
一、重新编译内核

1、将所需软件下载并上传到家目录

[root@localhost ~]# ll

总计 99824

-rw------- 1 root root 1175 08-30 04:50 anaconda-ks.cfg

drwxr-xr-x 2 root root 4096 08-30 21:14 Desktop

-rw-r--r-- 1 root root 35236 08-30 04:50 install.log

-rw-r--r-- 1 root root 3995 08-30 04:49 install.log.syslog

-rw-r--r-- 1 root root 435891 09-18 11:43 iptables-1.4.2.tar.bz2

-rw-r--r-- 1 root root 128196 09-18 11:43 l7-protocols-2008-10-04.tar.gz

-rw-r--r-- 1 root root 48622017 09-18 11:43 linux-2.6.25.19.tar.bz2

-rw-r--r-- 1 root root 174790 09-18 11:43 netfilter-layer7-v2.20.tar.gz

2、合并kernel+layer7补丁

[root@localhost ~]# tar jxvf linux-2.6.25.19.tar.bz2 -C /usr/src/

[root@localhost ~]# tar zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/linux-2.6.25.19/

[root@localhost linux-2.6.25.19]#patch -p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch

3、配置新内核

[root@localhost linux-2.6.25.19]# cp /boot/config-2.6.18-164.el5 .config   //沿用旧的内核

[root@localhost linux-2.6.25.19]# make menuconfig

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

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

4、编译及安装模块、新内核

[root@localhost linux-2.6.25.19]make && make modules_install && make install

5、编译安装成后,重启选择使用新的内核(2.6.25.19)引导系统

二、重新编译iptables

1、将原来的iptables备份

[root@localhost ~]# uname -r
2.6.25.19              //查看内核版本

[root@localhost init.d]# cp iptables iptables.bak

[root@localhost init.d]# iptables    //查看原来的iptables版本

iptables v1.3.5: no command specified

Try `iptables -h' or 'iptables --help' for more information.

2、卸载现有iptables

[root@localhost init.d]# rpm -e iptables --nodeps

warning: /etc/sysconfig/iptables-config saved as /etc/sysconfig/iptables-config.rpmsave

3、合并iptables+layer7补丁

[root@localhost ~]# tar -jxvf iptables-1.4.2.tar.bz2 -C /usr/src/

[root@localhost ~]# cd /usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/

[root@localhost iptables-1.4.1.1-for-kernel-2.6.20forward]# ll

总计 16

-rw-r--r-- 1 1000 1000 9356 2008-08-22 libxt_layer7.c

-rw-r--r-- 1 1000 1000 648 2008-08-22 libxt_layer7.man

[root@localhost iptables-1.4.1.1-for-kernel-2.6.20forward]# cp * /usr/src/iptables-1.4.2/extensions/

4、编译安装

[root@localhost iptables-1.4.1.1-for-kernel-2.6.20forward]# cd /usr/src/iptables-1.4.2/

[root@localhost iptables-1.4.2]# ./configure --prefix=/ --with-ksouce=/usr/src/linux-2.6.25.19/

[root@localhost iptables-1.4.2]# make && make install

5、将原备份iptables还原

[root@localhost iptables-1.4.2]# mv /etc/init.d/iptables.bak /etc/init.d/iptables

[root@localhost iptables-1.4.2]# ll /etc/init.d/iptables

-rwxr-xr-x 1 root root 7460 09-18 17:24 /etc/init.d/iptables

6、启动iptables

[root@localhost ~]# service iptables restart

7、安装l7-protocols模式包

[root@localhost ~]# tar -zxvf l7-protocols-2008-10-04.tar.gz -C /etc/

[root@localhost ~]# mv /etc/l7-protocols-2008-10-04 /etc/l7-protocols

8、测试

[root@localhost ~]# iptables -t filter -A FORWARD -m layer7 --l7proto qq -j DROP

[root@localhost ~]# iptables -L

相关内容