佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > netfilter/iptables模塊編譯及應用

netfilter/iptables模塊編譯及應用

熱門標簽:美團 服務器配置 解決方案 硅谷的囚徒呼叫中心 家政服務網絡 外呼系統(tǒng) 電話機器人搭建 百度競價點擊價格的計算公式

by KindGeorge # yahoo.com  2005.4.2 at ChinaUnix.net
相信很多人都會用iptables,我也一直用,并且天天用.特別是看完platinum的如何給iptables添加新的模塊>;>;介紹后,覺得有必要深入了解一下它的拓展功能.于是立刻下載,先查看一下它的說明, 其功能很是令人感覺很興奮,例如:comment (備注匹配) ,string(字符串匹配,可以用做內容過濾),iprang(ip范圍匹配),time(時間匹配),ipp2p(點對點匹配),connlimit(同時連接個數(shù)匹配),Nth(第n個包匹配),geoip(根據(jù)國家地區(qū)匹配). ipp2p(點對點匹配), quota(配額匹配),還有很多......之后編譯,幾經測試,在rh7.3 kernel2.4.18-3和rh9.0 kernel2.4.20-8下均成功實現(xiàn)添加擴展功能.以下是介紹其部分功能,及編譯方法.環(huán)境rh9.0 kernel2.4.20-8.  root身份.
一,準備原碼.
1.        內核原碼:為了減少復雜性,不編譯所有內核和模塊,建議找一個跟當前版本一樣的內核原碼,推薦安裝時光盤的
a.        [root@kindgeorge] uname -r   (查看當前版本)
2.4.20-8
可以cd  /usr/src 查看是否有這個目錄2.4.20-8
b.        或者[root@kindgeorge]rpm -qa|grep kernel
  kernel-source-2.4.20-8   如果有這個說明已安裝了.
如果沒有安裝,可以在RH第二張光盤中拷貝過來或安裝  rpm -ivh kernel-source-2.4.18-3.i386.rpm. 安裝后會在/usr/src/出現(xiàn)linux-2.4連接和linux-2.4.20-8目錄.
          c.在http://www.kernel.org 或www.redhat.com下載一個和當前版本的內核原碼.
2.  先獲取最新的信息,當然要到http://www.netfilter.org 或 http://www.iptables.org  (這兩個網址是一樣的).
網站左方download欄已經說明了最新版本的iptables-1.3.1
[root@kindgeorge src] cd /usr/src/
a.        獲取最新iptables :  wget http://www.netfilter.org/files/iptables-1.3.1.tar.bz2
解壓:tar xjvf iptables-1.3.1.tar.bz2
b.        獲取最新patch-o-matic-ng的地址:ftp://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/
[root@kindgeorge src] wget ftp://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/patch-o-matic-ng-20050331.tar.bz2
解壓: 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 (版本錯誤)
        /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o was compiled for kernel version 2.4.20-8custom
        while this kernel is version 2.4.20-8.
/lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: insmod /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o failed
/lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: insmod ipt_iprange failed
3. [root@kindgeorge linux-2.4]# make mrproper
4. [root@kindgeorge linux-2.4]# make oldconfig 
'make oldconfig' - 采用以前的 .config 文件 (編譯時十分有用)
技巧:在make menuconfig時,我們面對眾多的選項常常不知道該如何選擇,此時可以把安裝時的配置文件copy到/usr/src/linux-2.4中:cp /boot/config-2.4.* /usr/src/linux-2.4/.config,再用make menuconfig編譯,它會讀取.config中原來的配置信息.
(二).給netfilter打補丁
解開tar xjvf patch-o-matic-ng-20050331.tar.bz2 包后,進入該目錄,就會發(fā)現(xiàn)有很多目錄,其實每個目錄對應一個模塊.
我們可以這樣來選擇,根據(jù)不同貯倉庫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
執(zhí)行后,會測試是否已經應用和提示你是否應用該模塊,但這樣會遍歷所有模塊,有很多是用不著的,并且可能和系統(tǒng)版本有沖突,如果不管三七二十一全部選擇的話,一般都會在編譯和使用時出錯.所以推薦用cat /模塊目錄名/info 和cat /模塊目錄名/help 看過后,認為適合自己,才選擇.
我是針對在上面看過后,有目的的一個一個的應用的,這樣做:
KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme string
執(zhí)行后,會測試是否已經應用和提示你是否應用該模塊,按"y"應用.然后繼續(xù)下一個
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,會看到增加很多模塊,每個新增的后面都會出現(xiàn)"NEW",把其想要的選中為模塊"M"
保存、退出,至此,給netfilter打補丁工作完成
(三).編譯netfilter模塊
1.這里只需要編譯netfilter,不需要編譯整個內核和模塊.這里我只需要ipv4的,ipv6我還沒用到,所以不管了
cd /usr/src/linux-2.4
make dep
make modules SUBDIRS=net/ipv4/netfilter
2.建立一個新目錄備份原來模塊,以防萬一:
mkdir /usr/src/netfilter
cp /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/*.o  /usr/src/netfilter/
3.應用新的模塊
cp -f /usr/src/linux-2.4/net/ipv4/netfilter/*.o /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/
4.更新你的modules.dep
depmod -a
當出現(xiàn)這個時,可以不用理會,因為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 -m string --string "騰訊" -j DROP
iptables -I FORWARD -s 192.168.3.159 -m string --string "qq.com" -j DROP
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 "qq.com" -j DROP -m comment --comment "denny go to qq.com"
3.并發(fā)連接應用
模塊 connlimit 作用:連接限制
--connlimit-above n 限制為多少個
--connlimit-mask n 這組主機的掩碼,默認是connlimit-mask 32 ,即每ip.
這個主要可以限制內網用戶的網絡使用,對服務器而言則可以限制每個ip發(fā)起的連接數(shù)...比較實用
例如:只允許每個ip同時5個80端口轉發(fā),超過的丟棄:
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 5 -j DROP
例如:只允許每組ip同時10個80端口轉發(fā):
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
以上均測試通過,只有geoip的geoipdb.bin沒下載到,所以沒測試
在此僅為拋個磚頭,更多的應用,要根據(jù)自己的需要來組合各個規(guī)則和模塊了.

本來此篇文章和netfilter/iptables模塊功能中文介紹>;>;是寫在一起的,由于篇幅太長,所以份成兩篇. 如果有更新請見我的blog: http://kindgeorge.at.3322.org
相關資料:
kindgeorge的
http://blog.chinaunix.net/article.php?articleId=19778blogId=4543
http://bbs.chinaunix.net/forum/viewtopic.php?t=525525    netfilter/iptables模塊功能中文介紹
platinum的 http://bbs.chinaunix.net/forum/viewtopic.php?t=505370 如何給iptables添加新的模塊v2.2(含視頻教程)
hongfengyue的 http://www.chinaunix.net/jh/4/508312.html iptables 添加模塊 (for kernel 2.6)
官方: http://www.netfilter.org/documentation/HOWTO/netfilter-extensions-HOWTO.html

標簽:臨沂 南昌 韶關 烏蘭察布 北海 邢臺 防城港 撫州

巨人網絡通訊聲明:本文標題《netfilter/iptables模塊編譯及應用》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    天柱县| 宁武县| 叶城县| 台中市| 邳州市| 赤城县| 凉山| 潮州市| 洛宁县| 原平市| 甘谷县| 台江县| 葫芦岛市| 田阳县| 海晏县| 米脂县| 嘉义县| 常熟市| 万载县| 丰宁| 邵阳市| 大悟县| 隆回县| 武汉市| 剑川县| 石河子市| 华亭县| 大方县| 陇南市| 信宜市| 方城县| 房山区| 渝中区| 五峰| 察哈| 高清| 赤峰市| 长乐市| 连平县| 莫力| 浪卡子县|