Iptables 补丁

netfilter-layer7-v2.23.tar.bz2应用层网络过滤补丁包,是专门为iptables研发的一个补丁包,由于iptables存在的一些缺陷,该包内包含了linux内核版本为2.6.32-358.el6.x86_64的补丁包,和iptables的补丁包。因为该补丁要想生效,依赖于 "layer7" match support 内核模块,所以,必须先为内核打补丁,再为iptables打补丁才有意义。

iptables打补丁的几种方式及个中思想

软件包的安装方式

软件包的格式

   二进制格式-----------不需要编译

       rpm包-----------直接安装

       通用二进制包-----做一些配置

   源码格式-------------需要编译

1iptables-1.4.7-10.el6.src.rpm----安装,先安装,生成rpmbuild,打补丁,rpmbuild -bb iptables.spec生成rpm包,再安装rpm

2iptables-1.4.20.tar.bz2------直接打补丁,编译安装。

编译完成后,查看内核参数sysctl -a | grep acct

3,若没有发现net.netfilter.nf_conntrack_acct = 1,则没有装载内核模块,装载内核模块

# modprobe nf_conntrack

装载后,sysctl-a | grep acct 就可以看到net.netfilter.nf_conntrack_acct = 0,设置sysctl -w

net.netfilter.nf_conntrack_acct =1,就可以利用layer7对特有的一些协议利用防火墙控制了。

4,查看内核模块

# ls /usr/src/linux-2.6.32-358.el6/.tmp_versions/

可以看到所有内核模块

/usr/src/linux-2.6.32-358.el6/是内核的所在目录。

一,为内核打补丁

1,下载补丁包netfilter-layer7-v2.23.tar.bz2,解压到/usr/src下

# tar xf netfilter-layer7-v2.23.tar.bz2 -C /usr/src
# cd /usr/src/netfilter-layer7-v2.23
# ls
CHANGELOG iptables-1.4.3forward-for-kernel-2.6.20forward
kernel-2.6.32-layer7-2.23.patch  README

iptables-1.4.3forward-for-kernel-2.6.20forward  ------- iptables 补丁

kernel-2.6.32-layer7-2.23.patch  ----------  内核补丁

回到家目录,解压下载的内核源码包

# tar xf linux-2.6.32-358.el6.tar.bz2 -C /usr/src
# cd /usr/src
# ln -sv linux-2.6.32-358.el6 linux
# cd linux
# cp /boot/config-2.6.32-358.el6.x86_64 ./.config
# make menuconfig

注释:创建连接文件的原因是,系统启动或者其他程序调用内核功能会自动从/usr/src目录下 linux目录中查找。而为了防止编译发生错误,一般都是直接cp原内核模板

# cp /boot/config-2.6.32-358.el6.x86_64 ./.config

2make menuconfig 是配置编译环境,在进行真正的编译之前要先为内核打补丁,在linux目录下为内核打补丁。

# patch -p1 < ../netfilter-layer7-v2.23/kernel-2.6.32-layer7-2.23.patch

Patch 打补丁的命令,详google

3,编译

# make menuconfig    配置编译环境,相当于源代码编译安装时的./configure ..............这里不做改变,直接保存

# make modules_install  ----------- 编译内核模块,相当于源代码编译安装时的make

4,内核模块编译make出现如下错误

crypto/signature/ksign-publickey.c:2:17: error: key.h: No such file or directory
crypto/signature/ksign-publickey.c: In function ‘ksign_init’:
crypto/signature/ksign-publickey.c:10: error: ‘ksign_def_public_key’ undeclared (first use in this function)
crypto/signature/ksign-publickey.c:10: error: (Each undeclared identifier is reported only once
crypto/signature/ksign-publickey.c:10: error: for each function it appears in.)
crypto/signature/ksign-publickey.c:11: error: ‘ksign_def_public_key_size’ undeclared (first use in this function)
make[2]: *** [crypto/signature/ksign-publickey.o] Error 1
make[1]: *** [crypto/signature] Error 2
make: *** [crypto] Error 2

解决办法

# make menuconfig

Enable loadable module support --->  Module signature verification (EXPERIMENTAL) 禁用

主菜单,Cryptographic API ---> In-kernel signature checker (EXPERIMENTAL)禁用

5,编译内核模块

# make modules_install

6,编译内核

# make install ----------------  编译内核

编译安装需要很长时间,完了之后,我们make menuconfig会发现内核模块中多出了

Layer7 match support 模块,已经支持了iptables的相关功能,就可以为iptables打补丁了。

二,为iptables打补丁,分两种,一类是为iptables-1.4.20.tar.bz2类的包打补丁,一类是为

iptables-1.4.7-9.el6.src.rpm包打补丁。

1、iptables-1.4.20.tar.bz2类的包打补丁

因为在打完补丁重新安装之前要写在原有的iptables服务,但要是源码编译编译安装的程序没有配置文件和服务脚本,所以,我们实现cp出服务脚本和配置文件,然后卸载iptables

# rpm -qa | grep "iptables"
iptables-1.4.7-9.el6.x86_64
iptables-ipv6-1.4.7-9.el6.x86_64

查询安装有的iptables包,待会卸载的时候,就卸载这两个包

# cp /etc/sysconfig/iptables-config /root  ----------配置文件
# cp /etc/init.d/iptables /root          -----------服务文件
# rpm -e iptables-ipv6 iptables --nodeps----------------移除iptables包
# tar jxvf iptables-1.4.20.tar.bz2 –C  /usr/src
# cd /usr/src/iptables-1.4.20
#cp ../netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel-2.6.20forward/libxt_layer7.*   ./extensions

netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel-2.6.20forward下只有两个文件,就是原有的iptables包没有的补丁,复制过去,就算给它打补丁了。

# ./configure --prefix=/usr/local/iptables --sysconfdir=/etc/iptables.conf
--with-ksource=/sur/linux

--prefix=/usr/local/iptables -------------安装目录

--sysconfdir=/etc/iptables.conf-------------配置文件目录-----编译完了发现,根本没有,所以在移除原有的iptables之前,一定不要忘了复制出原有的配置文件/etc/sysconfig/iptables-config,

这一项可以没有。

--with-ksource=/usr/src/linux-----------------------打过补丁的内核目录

# make && make install
# cp iptables /etc/init.d/iptables
# cp iptables-config /etc/sysconfig/iptables-config------------将之前准备好的服务和配置归位。
# chkconfig --add iptables
# chkconfig iptables on
# service iptables start

2、为源码形式的iptables-1.4.7-9.el6.src.rpm包打补丁

RedHat src格式的rpm包:

http://ftp.redhat.com/redhat/linux/enterprise/6Server/en/os/SRPMS/

# useradd mockbuild-----------要安装这个包就需要添加一个用户,要没添加,系统会提示,不用担心
# rpm -ivh iptables-1.4.7-9.el6.src.rpm
warning: iptables-1.4.7-9.el6.src.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
   1:iptables               ########################################### [100%]

这时,可以看到所在目录下自动生成了一个目录rpmbuild,我们要做的就是在这个目录下制作rpm包,制作好之后,直接安装rpm包就行,不用编译。

# cd rpmbuild
# cd SOURCES
# ls
iptables-1.4.5-cloexec.patch           iptables-1.4.7-tproxy.patch       iptables.init
iptables-1.4.7-chain_maxnamelen.patch  iptables-1.4.7-xt_AUDIT_v2.patch  libxt_AUDIT.man
iptables-1.4.7-opt_parser_v2.patch     iptables-1.4.7-xt_CHECKSUM.patch
iptables-1.4.7.tar.bz2                 iptables-config
iptables-1.4.7.tar.bz2

这个是源码包,将它做成rpm包,就可以安装了。做rpm包依赖于rpmbuild/SPECS/iptables.spec这个文件,只需要一个命令即可,rpmbuild -bb iptables.spec,但在这之前,需要为源码打补丁。

# tar xf iptables-1.4.7.tar.bz2
# cd iptables-1.4.7
#cp ../netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel-2.6.20forward/libxt_layer7.*   ./extensions/---------------补丁打好了。
# cd ..
# rm -rf iptables-1.4.7.tar.bz2
# tar -jcf iptables-1.4.7.tar.bz2 iptables-1.4.7
# rm -rf iptables-1.4.7
# cd ..
# cd SPECS
# rpmbuild -bb iptables.spec
# cd ..
# ls RPMS/x86_64
iptables-1.4.7-9.el6.x86_64.rpm            iptables-devel-1.4.7-9.el6.x86_64.rpm
iptables-debuginfo-1.4.7-9.el6.x86_64.rpm  iptables-ipv6-1.4.7-9.el6.x86_64.rpm

生成了这几个rpm包,直接安装就行。iptables-debuginfo-1.4.7-9.el6.x86_64.rpm这个不需要安装。

# rpm -ivh iptables-1.4.7-9.el6.x86_64.rpm iptables-devel-1.4.7-9.el6.x86_64.rpm
iptables-ipv6-1.4.7-9.el6.x86_64.rpm
# chkconfig --add iptables
# chkconfig iptables on
# service iptables start

三、安装l7-protocols-2009-05-28.tar.gz

l7-protocols-2009-05-28.tar.gz---------layer7协议支持的各类协议,安装方式

解压,安装

# tar xf l7-protocols-2009-05-28.tar.gz
# cd l7-protocols-2009-05-28
# make install
mkdir -p /etc/l7-protocols
cp -R * /etc/l7-protocols

注释:l7-protocols-2009-05-28.tar.gz包安装的目的在于,将所有iptables支持的协议复制到配置文件中,以供所有程序共享。所有协议都在 /etc/l7-protocols/protocols/

四、实现对固定端口协议的拦截。

必须设置内核参数 net.netfilter.nf_conntrack_acct =1,才可以利用7层协议

# sysctl -agrep acct

发现没有我们要找的net.netfilter.nf_conntrack_acct ,这时首先加载模块nf_conntrack

# modprobe nf_conntrack

再次查看有没有内核参数net.netfilter.nf_conntrack_acct

# sysctl -agrep acct

# sysctl -w net.netfilter.nf_conntrack_acct =1

# iptables -A FORWARD -i eth1 -m layer7 --l7proto qq -j REJECT----------启动防火墙,使得以自己作为网关的内网用户无法使用qq登录。