netfilter/iptables模塊功能介紹
netfilter/iptables模塊功能能做許多事情,讓我們看看netfilter/iptables模塊功能的介紹。
功能介紹 (每個(gè)info和help本是英文的,為方便閱讀我把它翻譯成中文,由于水平有限,如果有誤請(qǐng)有經(jīng)驗(yàn)者來(lái)信指正)
獲取最新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 后,在/usr/src/下生成目錄patch-o-matic-ng-20050331
進(jìn)入該目錄
[root@kindgeorge src]# cd patch-o-matic-ng-20050331
統(tǒng)計(jì)一下
[root@kindgeorge patch-o-matic-ng-20050331]# ls |wc -l
99
其中共94個(gè)目錄,就是說(shuō)有九十多個(gè)拓展功能.這里只對(duì)部分感興趣,對(duì)這些進(jìn)行介紹.
patch-o-matic-ng-20050331目錄下有README 和README.newpatches.不過(guò)都是英文的,大概是介紹怎么應(yīng)用這些補(bǔ)丁和使用約定.里面每個(gè)拓展功能為一個(gè)目錄,每個(gè)目錄下面都有info(信息) 和help(使用幫助)文件,info里面一般是這樣的:
Title: terse description of the patch 標(biāo)題: 這個(gè)補(bǔ)丁的簡(jiǎn)單描述
Author: author (name, E-mail address) 作者(名字,email地址)
Status: Testing|Experimental|Alpha|Beta|Stable 狀況:試驗(yàn)的|根據(jù)實(shí)驗(yàn)的|測(cè)試一版|測(cè)試二版|穩(wěn)定的
Repository: submitted|pending|base|extra貯倉(cāng)庫(kù):順從的|未決的|基礎(chǔ)的|額外的
Requires: repository-entry ==|>;|<|>;=|<= kernel-version|iptables-version 要求: 貯倉(cāng)庫(kù)==|>;|<|>;=|<=內(nèi)核版本|iptables版本
Depends: [!]patch-name 依賴(lài)性:(否)補(bǔ)丁名字
Recompile: kernel|netfilter|iptables 重新編譯:內(nèi)核|netfilter|iptables
Successor: patch-name 繼承者: 補(bǔ)丁名字
1. string(字符串匹配,可以用做內(nèi)容過(guò)濾)
[root@kindgeorge patch-o-matic-ng-20050331]# cat string/info
Title: iptables string match (標(biāo)題: iptables 字符串匹配)
Author: Emmanuel Roger ; (作者:名字,email地址)
Status: Working, not with kernel 2.4.9 (狀況:正常工作, 但不能在內(nèi)核2.4.9工作)
Repository: extra (貯倉(cāng)庫(kù): 額外的)
Requires: linux < 2.6.0 (要求: linux < 2.6.0)
[root@kindgeorge patch-o-matic-ng-20050331]# cat string/help
This patch adds CONFIG_IP_NF_MATCH_STRING which allows you to
match a string in a whole packet.
THIS PATCH DOES NOT WORK WITH KERNEL 2.4.9 !!!
翻譯:這個(gè)補(bǔ)丁增加CONFIG_IP_NF_MATCH_STRING,允許在一整個(gè)包里面匹配一個(gè)字符串.
譯者:這個(gè)功能真是值得一用,實(shí)際上應(yīng)該說(shuō)是可以做內(nèi)容過(guò)濾吧,令人興奮啊.我測(cè)試過(guò),連中文也支持,我是這樣測(cè)試它的:
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 -d 192.168.3.0/24 -m string --string "色情" -j DROP
iptables -I FORWARD -d 192.168.3.0/24 -p tcp --sport 80 -m string --string "廣告" -j DROP
至于怎么靈活運(yùn)用就要看自己的需要了.
關(guān)于string其他安全應(yīng)用這里有個(gè)轉(zhuǎn)帖收錄:
http://blog.chinaunix.net/article.php?articleId=16909&blogId=4543
2. comment (備注匹配,可以支持最多256個(gè)字符)
[root@kindgeorge patch-o-matic-ng-20050331]# cat comment/info
Title: iptables comment match (標(biāo)題: iptables 注釋匹配)
Author: Brad Fisher ; (作者(名字,email地址brad@info-link.net))
Status: Part of 2.6.x mainline(狀況:2.6.x主流版本的一部分) [由于mainline我還在猶豫怎么翻譯,高手請(qǐng)?zhí)崾綸
Repository: submitted (貯倉(cāng)庫(kù): 順從的)
Requires: linux == 2.4 (要求: linux == 2.4)
Recompile: netfilter, iptables(重新編譯:netfilter|iptables)
[root@kindgeorge patch-o-matic-ng-20050331]# cat comment/help This option adds CONFIG_IP_NF_MATCH_COMMENT, which supplies a comment
match module. This match allows you to add comments (up to 256 characters)
to any rule.
Supported options:
--comment COMMENT
翻譯:這個(gè)選項(xiàng)增加CONFIG_IP_NF_MATCH_COMMENT,補(bǔ)充一個(gè)注釋匹配模塊.這個(gè)匹允許你增加一個(gè)備注都任何規(guī)則,這個(gè)備注最多支持256個(gè)字符,例如
Example:(例子:)
-A INPUT -s 192.168.0.0/16 -m comment --comment "A privatized IP block"
譯者:我是這樣測(cè)試使用這個(gè)comment
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"
這樣在iptables -L時(shí),就看到每條規(guī)則后面出現(xiàn)備注的內(nèi)容.可以提高可讀和理解該條規(guī)則的作用.
這個(gè)comment 在2.6.x中已經(jīng)被正式收錄.
3. connlimit(同時(shí)連接個(gè)數(shù)限制匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat connlimit/info
Title: iptables connlimit match (標(biāo)題: iptables同時(shí)連接個(gè)數(shù)限制匹配)
Author: Gerd Knorr ; (作者:名字,email地址)
Status: ItWorksForMe[tm] (狀態(tài):我可以運(yùn)作)
Repository: base (貯倉(cāng)庫(kù): 基礎(chǔ)的)
[root@kindgeorge patch-o-matic-ng-20050331]# cat connlimit/help
This adds an iptables match which allows you to restrict the number of parallel TCP connections to a server per client IP address(or address block).
翻譯:這個(gè)增加一個(gè)iptables匹配允許你限制每個(gè)客戶(hù)ip地址的并發(fā)tcp連接,即同時(shí)連接到一個(gè)服務(wù)器個(gè)數(shù).
Examples: 例子:
# allow 2 telnet connections per client host (允許每個(gè)客戶(hù)機(jī)同時(shí)兩個(gè)telnet連接)
iptables -p tcp --syn --dport 23 -m connlimit --connlimit-above 2 -j REJECT
# you can also match the other way around:(你也可以匹配其他的方法:)
iptables -p tcp --syn --dport 23 -m connlimit ! --connlimit-above 2 -j ACCEPT
# limit the nr of parallel http requests to 16 per class C sized (這下面例子限制80端口最多同時(shí)16個(gè)連接請(qǐng)求)
# network (24 bit netmask)
iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 16 --connlimit-mask 24 -j REJECT
模塊 connlimit 作用:連接限制
--connlimit-above n 限制為多少個(gè)
--connlimit-mask n 這組主機(jī)的掩碼,默認(rèn)是connlimit-mask 32 ,即每ip.
這個(gè)主要可以限制內(nèi)網(wǎng)用戶(hù)的網(wǎng)絡(luò)使用,對(duì)服務(wù)器而言則可以限制每個(gè)ip發(fā)起的連接數(shù)...比較實(shí)用
例如:只允許每個(gè)ip同時(shí)5個(gè)80端口轉(zhuǎn)發(fā),超過(guò)的丟棄:
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 5 -j DROP
例如:只允許每組C類(lèi)ip同時(shí)10個(gè)80端口轉(zhuǎn)發(fā):
iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j DROP
例如:為了防止DOS太多連接進(jìn)來(lái),那么可以允許最多15個(gè)初始連接,超過(guò)的丟棄.
/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. time(時(shí)間匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat time/info Title: iptables ``time'' match (標(biāo)題: iptables時(shí)間匹配)
Author: Fabrice MARIE ; (作者:名字,email地址)
Status: Works within it's limitations (狀況:運(yùn)作)
Repository: base (貯倉(cāng)庫(kù): 基礎(chǔ)的)
[root@kindgeorge patch-o-matic-ng-20050331]# cat time/help
This option adds CONFIG_IP_NF_MATCH_TIME, which supplies a time match module.
This match allows you to filter based on the packet arrival time/date(arrival time/date at the machine which the netfilter is running on) or
departure time/date (for locally generated packets).
翻譯:這個(gè)選項(xiàng)增加一個(gè)時(shí)間匹配模塊,這個(gè)匹配允許你過(guò)濾基于包到達(dá)時(shí)間/日期(這個(gè)到達(dá)時(shí)間和日期是指這個(gè)netfilter運(yùn)行的機(jī)器上的)或者發(fā)出時(shí)間/日期(本地產(chǎn)生的信息包)
Supported options are: (支持選項(xiàng)有:)
[ --timestart value ] (開(kāi)始時(shí)間 值)
Match only if it is after `value' (Inclusive, format: HH:MM ; default 00:00).
(只是匹配在這個(gè)值之后)(包括格式HH:MM ; 默認(rèn) 00:00)
[ --timestop value ] (結(jié)束時(shí)間 值)
Match only if it is before `value' (Inclusive, format: HH:MM ; default 23:59).
(只是匹配在這個(gè)值之前的)(包括格式HH:MM ; 默認(rèn) 23:59)
[ --days listofdays ] (--天 天列表)
Match only if today is one of the given days. (format: Mon,Tue,Wed,Thu,Fri,Sat,Sun ; default everyday)
(只是匹配已經(jīng)給出的天,格式Mon,Tue,Wed,Thu,Fri,Sat,Sun ;默認(rèn)每天)
[ --datestart date ] (開(kāi)始日期 日期)
Match only if it is after `date' (Inclusive, format: YYYY[:MM[:DD[:hh[:mm[]]]]] h,m,s start from 0 ; default to 1970)
(只是匹配這個(gè)開(kāi)始日期值之后的(包括,格式: YYYY[:MM[:DD[:hh[:mm[]]]]] h,m,s start from 0 ; 默認(rèn)是1970):
[ --datestop date ]
Match only if it is before `date' (Inclusive, format: YYYY[:MM[:DD[:hh[:mm[]]]]] h,m,s start from 0 ; default to 2037)
(只是匹配這個(gè)開(kāi)始日期值之前的(包括,格式: YYYY[:MM[:DD[:hh[:mm[]]]]] h,m,s start from 0 ; 默認(rèn)是2037):
Example: (例子:)
-A INPUT -m time --timestart 8:00 --timestop 18:00 --days Mon,Tue,Wed,Thu,Fri
will match packets that have an arrival timestamp in the range 8:00->;18:00 from Monday to Friday.
(上面將匹配從到達(dá)日期是星期一至星期五時(shí)間從8:00至18:00的包)
-A OUTPUT -m time --timestart 8:00 --timestop 18:00 --Days Mon --date-stop 2010
will match the packets (locally generated) that have a departure timestamp in the range 8:00->;18:00 on Monday only, until 2010
(上面將匹配本地產(chǎn)生的時(shí)間范圍直到2010年為止的每個(gè)星期一8:00至18:00的包)
NOTE: the time match does not track changes in daylight savings time
5. iprange (ip范圍匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat iprange/info
Title: iptables iprange match (標(biāo)題: iptables范圍匹配)
Author: Jozsef Kadlecsik ; (作者:名字,email地址)
Status: Works (狀況:運(yùn)作)
Repository: base (貯倉(cāng)庫(kù): 基礎(chǔ)的)
[root@kindgeorge patch-o-matic-ng-20050331]# cat iprange/help
This patch makes possible to match source/destination IP addresses against inclusive IP address ranges.
翻譯: 這個(gè)補(bǔ)丁令匹配源/目標(biāo) IP地址可以倚著給出的地址范圍進(jìn)行匹配
Examples:(例子)
iptables -A FORWARD -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT
這個(gè)例子是允許源ip地址范圍192.168.1.5-192.168.1.124的包通過(guò)
iptables -A FORWARD -m iprange --dst-range 10.0.0.0-10.255.255.255 -j ACCEPT
這個(gè)例子是允許目標(biāo)ip地址范圍10.0.0.0-10.255.255.255的包通過(guò)
6. geoip(根據(jù)地理位置匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat geoip/info
Title: iptables geoip match (標(biāo)題: iptables國(guó)家地區(qū)匹配)
Author: Samuel Jean ;; Nicolas Bouliane ; (作者:名字,email地址)
Status: Testing (狀況:測(cè)試)
Repository: extra (貯倉(cāng)庫(kù): 額外的)
Recompile: netfilter, iptables (重新編譯: netfilter, iptables)
[root@kindgeorge patch-o-matic-ng-20050331]# cat geoip/help
This patch makes possible to match a packet by its source or destination country.
翻譯:這個(gè)補(bǔ)丁令一個(gè)包能夠根據(jù)源或目的國(guó)家(地區(qū))匹配
GeoIP options: (選項(xiàng):)
[!] --src-cc, --source-country country[,country,country,...]
Match packet coming from (one of) the specified country(ies)
根據(jù)包的來(lái)源(或非來(lái)源)地區(qū)匹配
[!] --dst-cc, --destination-country country[,country,country,...]
Match packet going to (one of) the specified country(ies)
根據(jù)包的去向(或非去向)地區(qū)匹配
NOTE: The country is inputed by its ISO3166 code.
注意:這個(gè)國(guó)家地區(qū)列表放在ISO3166編碼里
The only extra files you need is a binary db (geoipdb.bin) & its index file (geoipdb.idx).Both files are generated from a countries & subnets database with the csv2bin tool,available at www.cookinglinux.org/geoip/. Both files MUST also be moved in /var/geoip/ as the shared library is statically looking for that pathname (ex.: /var/geoip/geoipdb.bin).
這個(gè)你需要額外的二進(jìn)位文件geoipdb.bin 和它的索引文件geoipdb.idx.這兩個(gè)文件是國(guó)家地區(qū)網(wǎng)絡(luò)數(shù)據(jù)庫(kù),是用csv2bin 工具生成的,可以在www.cookinglinux.org/geoip/得到.這些文件必須放在/var/geoip/下,作為一個(gè)共享庫(kù)查找路徑名字如/var/geoip/geoipdb.bin
7. Nth(第n個(gè)包匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat nth/info
Title: iptables nth match (標(biāo)題: iptables第N個(gè)匹配)
Author: Fabrice MARIE ; (作者:名字,email地址)
Status: Works (狀況:運(yùn)作)
Repository: base (貯倉(cāng)庫(kù): 基礎(chǔ)的)
[root@kindgeorge patch-o-matic-ng-20050331]# cat nth/help
This option adds an iptables `Nth' match, which allows you to match every Nth packet encountered. By default there are 16 different counters that can be used.
翻譯: 這個(gè)選項(xiàng)增加一個(gè)第N個(gè)匹配,允許你匹配每隔N個(gè)包,默認(rèn)有16不同的計(jì)算方法可以使用
This match functions in one of two ways
1) Match ever Nth packet, and only the Nth packet.
example:(例子)
iptables -t mangle -A PREROUTING -m nth --every 10 -j DROP
This rule will drop every 10th packet.
這個(gè)規(guī)則將丟棄每隔10個(gè)包
2) Unique rule for every packet. This is an easy and quick method to produce load-balancing for both inbound and outbound.
為每一個(gè)包應(yīng)用一個(gè)獨(dú)特的規(guī)則,這樣是一個(gè)容易和快速的負(fù)載均衡方法
example: (例如)
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 0 -j SNAT --to-source 10.0.0.5
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 1 -j SNAT --to-source 10.0.0.6
iptables -t nat -A POSTROUTING -o eth0 -m nth --counter 7 --every 3 --packet 2 -j SNAT --to-source 10.0.0.7
This example evenly splits connections between the three SNAT addresses.
上面例子由三個(gè)源ip地址平滑分割連接
By using the mangle table and iproute2, you can setup complex load-balanced routing. There's lot of other uses. Be creative!
配合iptables 的mangle表和高級(jí)路由iproute2,你能設(shè)置一個(gè)復(fù)合的負(fù)載平衡路由.還有其他的用途,具有創(chuàng)造性的設(shè)置
Suppported options are: (支持選項(xiàng)有:)
--every Nth Match every Nth packet (匹配每N 個(gè)包)
[--counter] num Use counter 0-15 (default:0) (用計(jì)算器(默認(rèn)是0))
[--start] num Initialize the counter at the number 'num' instead of 0. Must be between 0 and Nth-1
(初始化一個(gè)計(jì)算器,用這個(gè)num值而不是0. 必需是在0和N減1之間的數(shù)
[--packet] num Match on 'num' packet. Must be between 0 and Nth-1. If --packet is used for a counter than there must be Nth number of --packet rules, covering all values between 0 and Nth-1 inclusively.
匹配'num' 包,必需是在0和N減1之間的數(shù),如果該包被用一個(gè)計(jì)算器,必需是第Nth數(shù)字包規(guī)則,并覆蓋0至Nth-1的所有值(這個(gè)翻譯得不好,請(qǐng)指正)
8. ipp2p(點(diǎn)對(duì)點(diǎn)匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat ipp2p/info
Title: Detects some P2P packets (標(biāo)題: 偵查P2P包)
Author: Eicke Friedrich ; (作者:名字,email地址)
Status: Stable (狀況:穩(wěn)定的)
Repository: extra (貯倉(cāng)庫(kù): 額外的)
Recompile: netfilter, iptables (重新編譯:netfilter|iptables)
[root@kindgeorge patch-o-matic-ng-20050331]# cat ipp2p/help
This option makes possible to match some P2P packets therefore helps controlling such traffic.
Dropping all matches prohibits P2P networks.
Combined with conntrack,CONNMARK and a packet scheduler it can be used for accounting or shaping of P2P traffic.
這個(gè)選項(xiàng)能夠匹配某些P2P包,幫助控制流量.丟棄所有匹配的P2P.結(jié)合連接跟蹤,和一個(gè)包的調(diào)度器,它能被用作計(jì)算和整形一個(gè)P2流量
Examples: (例如:)
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
上例可以封殺很多bt等的P2P軟件.
更多參數(shù)更詳細(xì)的參考還有ipp2p/iptables/extensions/libipt_ipp2p.man或http://www.ipp2p.org
9. quota(配額匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat quota/info Title: iptables quota match (標(biāo)題: iptables配額匹配)
Author: Sam Johnston ; (作者:名字,email地址)
Status: worksforme (狀況:運(yùn)作的)
Repository: base (貯倉(cāng)庫(kù): 基礎(chǔ)的)
[root@kindgeorge patch-o-matic-ng-20050331]# cat quota/help
This option adds CONFIG_IP_NF_MATCH_QUOTA, which implements network quotas by decrementing a byte counter with each packet.
這個(gè)選項(xiàng)增加一個(gè)模塊匹配包實(shí)現(xiàn)網(wǎng)絡(luò)包通過(guò)的配額
Supported options are: (支持選項(xiàng)有:)
--quota ;
The quota in bytes. (用bytes為單位)
iptables -I FORWARD -s 192.168.3.159 -p tcp --dport 80 -m quota --quota 500 -j DROP
上例是這樣的,在ip192.168.3.159的目標(biāo)端口為80匹配的500個(gè)字節(jié)內(nèi),將被丟棄.直到匹配完這500字節(jié)后,才不會(huì)丟棄,就是說(shuō),才可以打開(kāi)網(wǎng)頁(yè).
KNOWN BUGS: this does not work on SMP systems.
知道的bug:這個(gè)不能工作在SMP系統(tǒng)上,(SMP, symmetric multiprocessing 多處理技術(shù))
10.其他的.
推薦用cat /模塊目錄名/info 和cat /模塊目錄名/help 查看相應(yīng)的模塊信息和幫助.
由于我對(duì)其他某些模塊不太感興趣,或和我現(xiàn)在系統(tǒng)2.4.20-8有沖突,沒(méi)有再列出翻譯.如果有誰(shuí)翻譯了,可以讓我參考下.謝謝
如果有更新,請(qǐng)看我的blog: http://kindgeorge.at.3322.org
相關(guān)資料:
安裝編譯 KindGeorge的:
http://blog.chinaunix.net/article.php?articleId=19797&blogId=4543
http://bbs.chinaunix.net/forum/viewtopic.php?t=525493
platinum 的http://www.chinaunix.net/jh/4/505370.html 如何給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
通過(guò)文章的介紹,我們可以利用netfilter/iptables模塊功能來(lái)實(shí)現(xiàn)各種事情,希望本文對(duì)大家有所幫助!
【編輯推薦】