編譯內核,添加layer7等模塊實現更強大的netfilter!!

本文介紹自己正學的四個模塊的的添加,使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

 

步驟:

 

  1. [root@test ~]# rpm -ivh kernel-2.6.18-194.el5.src.rpm 
  2. [root@test ~]# cd /usr/src/redhat/SPECS/ 
  3. [root@test ~]# rpmbuild -bp --target=$(uname -m) kernel-2.6.spec 
  4. [root@test ~]# cp -r /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i686 /usr/src/linux 
  5. [root@test ~]# cd /usr/src/linux/ 
  6. [root@test ~]# vim /usr/src/linux/Makefile 
  7. --------------------------- 
  8. VERSION = 2 
  9. PATCHLEVEL = 6 
  10. SUBLEVEL = 18 
  11. EXTRAVERSION = -194.el5     #這裏要修改爲內核的小版本好,就是uanme -r給出的2.6.18之後的部分# 
  12. --------------------------- 
  13. [root@test ~]# cd 
  14. [root@test ~]# tar xvf iptables-1.3.5.tar.bz2 
  15. [root@test ~]# cp -r iptables-1.3.5 /usr/src/iptables 
  16. [root@test ~]# export KERNEL_DIR=/usr/src/linux/ 
  17. [root@test ~]# export IPTABLES_DIR=/usr/src/iptables/ 
  18. [root@test ~]# tar xvf patch-o-matic-ng-20080214.tar.bz2 
  19. [root@test ~]# cd patch-o-matic-ng-20080214 
  20. [root@test ~]# ./runme --download 
  21. [root@test ~]# ./runme connlimit    #提示時選擇y確認即可 
  22. [root@test ~]# ./runme time    #提示時選擇y確認即可 
  23. [root@test ~]# ./runme ipp2p     #提示時選擇y確認即可 
  24. [root@test ~]# cd .. 
  25. [root@test ~]# tar zxvf netfilter-layer7-v2.9.tar.gz 
  26. [root@test ~]# cd /usr/src/linux/ 
  27. [root@test ~]# patch -p1 < /root/iptables_ext/netfilter-layer7-v2.9/kernel-2.6.18-2.6.19-layer7-2.9.patch 
  28. [root@test ~]# patch -p1 < /root/iptables_ext/netfilter-layer7-v2.9/iptables-layer7-2.9.patch 
  29. [root@test ~]# cd /usr/src/linux 
  30. [root@test ~]# make menuconfig    # 配置內核選項 
  31. ---------------------------- 
  32. networking 
  33.     ----> networking options 
  34.         ---> network packet filtering(replaces inchains) 
  35.             ---> [*] Network packet filtering debugging 
  36.             --->IP: Netfilter Configuration      #如果能看到含有下面的五項說明你前面的工作正確 
  37.                 ---> <M>  Layer 7 match support (EXPERIMENTAL)  
  38.                      [*]  Layer 7 debugging output 
  39.                      <M> TIME match  support 
  40.                      <M> IPP2P match support 
  41.                      <M> Connections/IP limit match support  
  42.  
  43. ---------------------------- 
  44. [root@test ~]# cd /usr/src/linux/net/ipv4/netfilter 
  45. [root@test ~]# cp Makefile Makefile.bak 
  46. [root@test ~]# vim Makefile  #清空內容,並配置如下: 
  47. ---------------------------- 
  48. obj-m :ipt_connlimit.o ipt_time.o ipt_ipp2p.o ipt_layer7.o 
  49. KDIR := /lib/modules/$(shell uname -r)/build 
  50. PWD := $(shell pwd) 
  51. default: $(MAKE) -C $(KDIR) M=$(PWD) modules 
  52. ---------------------------- 
  53. [root@test ~]# cd /usr/src/linux/ 
  54. [root@test ~]# make M=net/ipv4/netfilter/   #如果出錯請先運行下面兩條命令 
  55. [root@test ~]# make oldconfig && make prepare 
  56. [root@test ~]# make scripts 
  57. [root@test ~]# make M=net/ipv4/netfilter/  #這是在net/ipv4/netfilter就產生了我們需要的幾個模塊 ko後綴文件 
  58. [root@test ~]# ls net/ipv4/netfilter |grep ko 
  59. ipt_connlimit.ko 
  60. ipt_ipp2p.ko 
  61. ipt_layer7.ko 
  62. ipt_time.ko 
  63. [root@test ~]# cp net/ipv4/netfilter/*.ko /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ 
  64. [root@test ~]# insmod /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ipt_connlimit.ko 
  65. [root@test ~]# insmod /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ipt_time.ko 
  66. [root@test ~]# insmod /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ipt_ipp2p.ko 
  67. [root@test ~]# insmod /lib/modules/2.6.18-194.el5/kernel/net/ipv4/netfilter/ipt_layer7.ko 
  68. #如果有出現插入模塊出錯的情況,請先插入ip_conntrack x——tables 這兩個模塊 
  69. #插入模塊正常,但使用出錯時,是由於缺少所需的庫,我們需要編譯下iptables源碼產生需要的庫文件 
  70. [root@test ~]# cd /usr/src/iptables/ 
  71. [root@test ~]# make 
  72. #將產生的幾個庫文件拷貝至 /lib/iptables/ 下即可 
  73.  
  74. #安裝layer7的協議包 
  75. [root@test ~]# cd 
  76. [root@test ~]# tar xvf l7-protocols-2009-05-10.tar.gz 
  77. [root@test ~]# cd l7-protocols-2009-05-10 
  78. [root@test ~]# make install 
  79.  
  80. 至此,你的內核已經可以使用這些新添加的模塊功能了,ipp2p模塊有時遇到錯誤不好解決,正在探索
  81.  
  82.  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章