歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> iptables 添加模塊

iptables 添加模塊

日期:2017/3/6 15:18:43   编辑:關於Unix
在rh9.0,內核2.4.20下做的 相關模塊: comment (備注匹配) ,string(字符串匹配,可以用做內容過濾),iprang(ip范圍匹配),time(時間匹配),ipp2p(點對點匹配),connlimit (同時 連接個數匹配),Nth(第n個包匹配),geoip(根據國家地區匹配). ipp2p(點對點匹配), quot   在rh9.0,內核2.4.20下做的

相關模塊:
 comment (備注匹配) ,string(字符串匹配,可以用做內容過濾),iprang(ip范圍匹配),time(時間匹配),ipp2p(點對點匹配),connlimit (同時

連接個數匹配),Nth(第n個包匹配),geoip(根據國家地區匹配). ipp2p(點對點匹配), quota(配額匹配),環境rh9.0 kernel2.4.20-8.  root身

份,

一,准備原碼.
1. [root@kindgeorge] uname -r   (查看當前版本)
  2.4.20-8
可以cd  /usr/src 下面是內核原碼,目錄為2.4.20-8
2. 或者[root@kindgeorge]rpm -qa|grep kernel 查找原碼是否安裝
3  先獲取最新的信息,當然要到http://www.netfilter.org 或 http://www.iptables.org下載iptables-1.3.1
下載地址: http://www.netfilter.org/files/iptables-1.3.1.tar.bz2
    放到/usr/src目錄下面: # cp iptables-1.3.1.tar.bz2 /usr/src
    解壓:   # tar xjvf iptables-1.3.1.tar.bz2
4.獲取path-o-matic-ng地址:
    http://ftp.tpnet.pl/vol/d2/ftp.netfilter.org/patch-o-matic-ng/snapshot/patch-o-matic-ng-20050331.tar.bz2
    放到/usr/src目錄下面: #cp iptables-1.3.1.tar.bz2 /usr/src
    解壓: #tar xjvf patch-o-matic-ng-20050331.tar.bz2

二.安裝說明
(一).處理內核源碼.
1. [root@kindgeorge src]# cd /usr/src/linux-2.4
2. [root@kindgeorge linux-2.4]# vi Makefile,
VERSION = 2
PATCHLEVEL = 4
SUBLEVEL = 20
EXTRAVERSION = -8custom
將“EXTRAVERSION = -8custom”改為“EXTRAVERSION = -8”
即:
VERSION = 2
PATCHLEVEL = 4
SUBLEVEL = 20
EXTRAVERSION = -8
為何要修改呢?因為不同版本的內容是不可以使用的,當不修改時,則變成這個版本(2.4.20-8custom)了,不同時是這樣出錯的:
/lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: kernel-module version mismatch (版本錯誤)
3. [root@kindgeorge linux-2.4]# make mrproper
4. [root@kindgeorge linux-2.4]# make menuconfig,什麼也不做,保存退出!  

(二).給netfilter打補丁
解開tar xjvf patch-o-matic-ng-20050331.tar.bz2 包後,進入該目錄,就會發現有很多目錄,其實每個目錄對應一個模塊.
我們可以這樣來選擇,根據不同貯倉庫submitted|pending|base|extra,例如:
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme base .
或:KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme extra
執行後,會測試是否已經應用和提示你是否應用該模塊,但這樣會遍歷所有模塊,有很多是用不著的,並且可能和系統版本有沖突,如果不管三七二

十一全部選擇的 話,一般都會在編譯和使用時出錯.所以推薦用cat /模塊目錄名/info 和cat /模塊目錄名/help 看過後,認為適合自己,才選擇

.
針對在上面看過後,有目的的一個一個的應用的,這樣做:
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme string
執行後,會測試是否已經應用和提示你是否應用該模塊,按"y"應用.然後繼續下一個
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme comment
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme connlimit
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme time
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme iprange
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme geoip
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme nth
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme ipp2p
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme quota
上面全部完成後,
cd /usr/src/linux-2.4
make menuconfig,確認[*] Prompt for development and/or incomplete code/drivers要選中
然後進入Networking options
再進入IP:Netfilter Configuration,會看到增加很多模塊,每個新增的後面都會出現"NEW",把其想要的選中為模塊"M" ,
保存、退出,至此,給netfilter打補丁工作完成
(三).編譯netfilter模塊
1.這裡只需要編譯netfilter,不需要編譯整個內核和模塊.這裡我只需要ipv4的,ipv6我還沒用到,所以不管了
  #cd /usr/src/linux-2.4
  #make dep
  #make modules SUBDIRS=net/ipv4/netfilter
  如果這步出現很多錯誤,如: ip_nat_irc.c錯誤,重新安裝內核原碼,問題就解決了!  
2.應用新的模塊
  #cp -f /usr/src/linux-2.4/net/ipv4/netfilter/*.o /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/
3.更新你的modules.dep
  #depmod -a   
  depmod -a命令和編譯過程並沒有關系,它是生成模塊間的依賴關系,這樣你啟動新內核之後,使用modprobe命令加載模塊時就能正確地定位

模塊
當出現這個時,可以不用理會,因為ipchains, ipfwadm模塊都沒用,也可以把出錯的刪除.
depmod: *** Unresolved symbols in /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipchains_core.o
depmod: *** Unresolved symbols in /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipfwadm_core.o
(四).編譯安裝新的iptables
解壓後有目錄iptables-1.3.1
cd /usr/src/iptables-1.3.1
export KERNEL_DIR=/usr/src/linux-2.4
export IPTABLES_DIR=/usr/src/iptables-1.3.1
make BINDIR=/sbin LIBDIR=/lib MANDIR=/usr/share/man install

三.安裝完成,測試及應用
1.內容過濾
iptables -I FORWARD -d 192.168.3.0/24 -m string --string "寬頻影院" -j DROP
iptables -I FORWARD -s 192.168.3.0/24 -m string --string "色情" -j DROP
iptables -I FORWARD -p tcp --sport 80 -m string --string "廣告" -j DROP
2.備注應用
iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -j DROP -m comment --comment "the bad guy can not online"
iptables -I FORWARD -s 192.168.3.159 -m string --string "xx.com" -j DROP -m comment --comment "denny go toxx.com"
3.並發連接應用
模塊 connlimit 作用:連接限制
--connlimit-above n 限制為多少個
--connlimit-mask n 這組主機的掩碼,默認是connlimit-mask 32 ,即每ip.
這個主要可以限制內網用戶的網絡使用,對服務器而言則可以限制每個ip發起的連接數...比較實用
例如:只允許每個ip同時5個80端口轉發,超過的丟棄:
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 5 -j DROP
例如:只允許每組ip同時10個80端口轉發:
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j DROP
例如:為了防止DOS太多連接進來,那麼可以允許最多15個初始連接,超過的丟棄.
/sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP
/sbin/iptables -A INPUT -s 192.186.1.0/24 -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT

4.ip范圍應用
iptables -A FORWARD -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT
5.每隔N個匹配
iptables -t mangle -A PREROUTING -m nth --every 10 -j DROP
6.封殺BT類P2P軟件
iptables -A FORWARD -m ipp2p --edk --kazaa --bit -j DROP
iptables -A FORWARD -p tcp -m ipp2p --ares -j DROP
iptables -A FORWARD -p udp -m ipp2p --kazaa -j DROP
7.配額匹配
iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -m quota --quota 500 -j DROP
iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -m quota --quota 500 -j ACCEPT

Copyright © Linux教程網 All Rights Reserved