本文介紹自己正學的四個模塊的的添加,使netfilter/iptables添加新功能。
四個模塊分別爲:
ipt_connlimit:限制tcp併發連接,據說這個可以防DDOS;
ipt_ipp2p:控制P2P協議;
ipt_time:根據時間控制;
layer7:應用層的一些協議控制,QQ,迅雷
我的linux是RHEL5.5,實際上只缺少layer7和ipp2p兩個功能模塊,但這次都做了一下。
需求軟件包:
1.跟所應用linux系統正在使用的相同版本的內核源碼,如果是redhat GNU/linux的話,建議下載紅帽提供的源碼,我這裏使用的是: kernel-2.6.18-194.el5.src.rpm
2.跟系統iptables相同的netfilter源碼包,我使用的是:iptables-1.3.5.tar.bz2
3. layer7的內核補丁包:netfilter-layer7-v2.9.tar.gz 和應用層協議包:l7-protocols-2009-05-10.tar.gz
4.一個內核添加模塊的補丁工具:patch-o-matic-ng-20080214.tar.bz2
步驟:
- [root@test ~]# rpm -ivh kernel-2.6.18-194.el5.src.rpm
- [root@test ~]# cd /usr/src/redhat/SPECS/
- [root@test ~]# rpmbuild -bp --target=$(uname -m) kernel-2.6.spec
- [root@test ~]# cp -r /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i686 /usr/src/linux
- [root@test ~]# cd /usr/src/linux/
- [root@test ~]# vim /usr/src/linux/Makefile
- ---------------------------
- VERSION = 2
- PATCHLEVEL = 6
- SUBLEVEL = 18
- EXTRAVERSION = -194.el5 #這裏要修改爲內核的小版本好,就是uanme -r給出的2.6.18之後的部分#
- ---------------------------
- [root@test ~]# cd
- [root@test ~]# tar xvf iptables-1.3.5.tar.bz2
- [root@test ~]# cp -r iptables-1.3.5 /usr/src/iptables
- [root@test ~]# export KERNEL_DIR=/usr/src/linux/
- [root@test ~]# export IPTABLES_DIR=/usr/src/iptables/
- [root@test ~]# tar xvf patch-o-matic-ng-20080214.tar.bz2
- [root@test ~]# cd patch-o-matic-ng-20080214
- [root@test ~]# ./runme --download
- [root@test ~]# ./runme connlimit #提示時選擇y確認即可
- [root@test ~]# ./runme time #提示時選擇y確認即可
- [root@test ~]# ./runme ipp2p #提示時選擇y確認即可
- [root@test ~]# cd ..
- [root@test ~]# tar zxvf netfilter-layer7-v2.9.tar.gz
- [root@test ~]# cd /usr/src/linux/
- [root@test ~]# patch -p1 < /root/iptables_ext/netfilter-layer7-v2.9/kernel-2.6.18-2.6.19-layer7-2.9.patch
- [root@test ~]# patch -p1 < /root/iptables_ext/netfilter-layer7-v2.9/iptables-layer7-2.9.patch
- [root@test ~]# cd /usr/src/linux
- [root@test ~]# make menuconfig # 配置內核選項
- ----------------------------
- networking
- ----> networking options
- ---> network packet filtering(replaces inchains)
- ---> [*] Network packet filtering debugging
- --->IP: Netfilter Configuration #如果能看到含有下面的五項說明你前面的工作正確
- ---> <M> Layer 7 match support (EXPERIMENTAL)
- [*] Layer 7 debugging output
- <M> TIME match support
- <M> IPP2P match support
- <M> Connections/IP limit match support
- ----------------------------
- [root@test ~]# cd /usr/src/linux/net/ipv4/netfilter
- [root@test ~]# cp Makefile Makefile.bak
- [root@test ~]# vim Makefile #清空內容,並配置如下:
- ----------------------------
- obj-m := ipt_connlimit.o ipt_time.o ipt_ipp2p.o ipt_layer7.o
- KDIR := /lib/modules/$(shell uname -r)/build
- PWD := $(shell pwd)
- default: $(MAKE) -C $(KDIR) M=$(PWD) modules
- ----------------------------
- [root@test ~]# cd /usr/src/linux/
- [root@test ~]# make M=net/ipv4/netfilter/ #如果出錯請先運行下面兩條命令
- [root@test ~]# make oldconfig && make prepare
- [root@test ~]# make scripts
- [root@test ~]# make M=net/ipv4/netfilter/ #這是在net/ipv4/netfilter就產生了我們需要的幾個模塊 ko後綴文件
- [root@test ~]# ls net/ipv4/netfilter |grep ko
- ipt_connlimit.ko
- ipt_ipp2p.ko
- ipt_layer7.ko
- ipt_time.ko
- [root@test ~]# cp net/ipv4/netfilter/*.ko /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/
- [root@test ~]# insmod /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ipt_connlimit.ko
- [root@test ~]# insmod /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ipt_time.ko
- [root@test ~]# insmod /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ipt_ipp2p.ko
- [root@test ~]# insmod /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ipt_layer7.ko
- #如果有出現插入模塊出錯的情況,請先插入ip_conntrack x——tables 這兩個模塊
- #插入模塊正常,但使用出錯時,是由於缺少所需的庫,我們需要編譯下iptables源碼產生需要的庫文件
- [root@test ~]# cd /usr/src/iptables/
- [root@test ~]# make
- #將產生的幾個庫文件拷貝至 /lib/iptables/ 下即可
- #安裝layer7的協議包
- [root@test ~]# cd
- [root@test ~]# tar xvf l7-protocols-2009-05-10.tar.gz
- [root@test ~]# cd l7-protocols-2009-05-10
- [root@test ~]# make install
- 至此,你的內核已經可以使用這些新添加的模塊功能了,ipp2p模塊有時遇到錯誤不好解決,正在探索