netfilter/iptables模組功能中文介紹(轉)

BSDLite發表於2007-08-17
netfilter/iptables模組功能中文介紹(轉)[@more@]功能介紹 (每個info和help本是英文的,為方便閱讀我把它翻譯成中文,由於水平有限,如果有誤請有經驗者來信指正)
獲取最新patch-o-matic-ng的地址:
[root@kindgeorge src] wget
解壓:
tar xjvf patch-o-matic-ng-20050331.tar.bz2 後,在/usr/src/下生成目錄patch-o-matic-ng-20050331
進入該目錄
[root@kindgeorge src]# cd patch-o-matic-ng-20050331
統計一下
[root@kindgeorge patch-o-matic-ng-20050331]# ls |wc -l
99
其中共94個目錄,就是說有九十多個擴充功能.這裡只對部分感興趣,對這些進行介紹.
patch-o-matic-ng-20050331目錄下有README 和README.newpatches.不過都是英文的,大概是介紹怎麼應用這些補丁和使用約定.裡面每個擴充功能為一個目錄,每個目錄下面都有info (資訊) 和help(使用幫助)檔案,info裡面一般是這樣的:
Title: terse description of the patch 標題: 這個補丁的簡單描述
Author: author (name, E-mail address) 作者(名字,email地址)
Status: Testing|Experimental|Alpha|Beta|Stable 狀況:試驗的|根據實驗的|測試一版|測試二版|穩定的
Repository: submitted|pending|base|extra貯倉庫:順從的|未決的|基礎的|額外的
Requires: repository-entry ==|>|=|<= kernel-version|iptables-version 要求: 貯倉庫==|>|=|<=核心版本|iptables版本
Depends: [!]patch-name 依賴性:(否)補丁名字
Recompile: kernel|netfilter|iptables 重新編譯:核心|netfilter|iptables
Successor: patch-name 繼承者: 補丁名字

1. string(字串匹配,可以用做內容過濾)
[root@kindgeorge patch-o-matic-ng-20050331]# cat string/info
Title: iptables string match (標題: iptables 字串匹配)
Author: Emmanuel Roger <> (作者:名字,email地址)
Status: Working, not with kernel 2.4.9 (狀況:正常工作, 但不能在核心2.4.9工作)
Repository: extra (貯倉庫: 額外的)
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 !!!
翻譯:這個補丁增加CONFIG_IP_NF_MATCH_STRING,允許在一整個包裡面匹配一個字串.
譯者:這個功能真是值得一用,實際上應該說是可以做內容過濾吧,令人興奮啊.我測試過,連中文也支援,我是這樣測試它的:
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
至於怎麼靈活運用就要看自己的需要了.
關於string其他安全應用這裡有個轉帖收錄:
http://blog.chinaunix.net/article.php?articleId=16909&blogId=4543

2. comment (備註匹配,可以支援最多256個字元)
[root@kindgeorge patch-o-matic-ng-20050331]# cat comment/info
Title: iptables comment match (標題: iptables 註釋匹配)
Author: Brad Fisher <> (作者(名字,email地址))
Status: Part of 2.6.x mainline(狀況:2.6.x主流版本的一部分) [由於mainline我還在猶豫怎麼翻譯,高手請提示]
Repository: submitted (貯倉庫: 順從的)
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
翻譯:這個選項增加CONFIG_IP_NF_MATCH_COMMENT,補充一個註釋匹配模組.這個匹允許你增加一個備註都任何規則,這個備註最多支援256個字元,例如
Example:(例子:)
-A INPUT -s 192.168.0.0/16 -m comment --comment "A privatized IP block"
譯者:我是這樣測試使用這個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時,就看到每條規則後面出現備註的內容.可以提高可讀和理解該條規則的作用.
這個comment 在2.6.x中已經被正式收錄.

3. connlimit(同時連線個數限制匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat connlimit/info
Title: iptables connlimit match (標題: iptables同時連線個數限制匹配)
Author: Gerd Knorr <> (作者:名字,email地址)
Status: ItWorksForMe[tm] (狀態:我可以運作)
Repository: base (貯倉庫: 基礎的)
[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).
翻譯:這個增加一個iptables匹配允許你限制每個客戶ip地址的併發tcp連線,即同時連線到一個伺服器個數.
Examples: 例子:
# allow 2 telnet connections per client host (允許每個客戶機同時兩個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埠最多同時16個連線請求)
# network (24 bit netmask)
iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 16 --connlimit-mask 24 -j REJECT
如果你看某個傢伙不順眼,就只允許他最多同時3個80埠訪問網頁吧:當然也可以匹配其他埠.自由發揮吧.
iptables -I FORWARD -s 192.168.3.159 -p tcp --syn --dport 80 -m connlimit --connlimit-above 3 --connlimit-mask 24 -j DROP

4. time(時間匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat time/info Title: iptables ``time'' match (標題: iptables時間匹配)
Author: Fabrice MARIE <> (作者:名字,email地址)
Status: Works within it's limitations (狀況:運作)
Repository: base (貯倉庫: 基礎的)
[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).
翻譯:這個選項增加一個時間匹配模組,這個匹配允許你過濾基於包到達時間/日期(這個到達時間和日期是指這個netfilter執行的機器上的)或者發出時間/日期(本地產生的資訊包)
Supported options are: (支援選項有:)
[ --timestart value ] (開始時間 值)
Match only if it is after `value' (Inclusive, format: HH:MM ; default 00:00).
(只是匹配在這個值之後)(包括格式HH:MM ; 預設 00:00)
[ --timestop value ] (結束時間 值)
Match only if it is before `value' (Inclusive, format: HH:MM ; default 23:59).
(只是匹配在這個值之前的)(包括格式HH:MM ; 預設 23:59)
[ --days listofdays ] (--天 天列表)
Match only if today is one of the given days. (format: Mon,Tue,Wed,Thu,Fri,Sat,Sun ; default everyday)
(只是匹配已經給出的天,格式Mon,Tue,Wed,Thu,Fri,Sat,Sun ;預設每天)
[ --datestart date ] (開始日期 日期)
Match only if it is after `date' (Inclusive, format: YYYY[:MM[:DD[:hh[:mm[:ss]]]]] h,m,s start from 0 ; default to 1970)
(只是匹配這個開始日期值之後的(包括,格式: YYYY[:MM[:DD[:hh[:mm[:ss]]]]] h,m,s start from 0 ; 預設是1970):
[ --datestop date ]
Match only if it is before `date' (Inclusive, format: YYYY[:MM[:DD[:hh[:mm[:ss]]]]] h,m,s start from 0 ; default to 2037)
(只是匹配這個開始日期值之前的(包括,格式: YYYY[:MM[:DD[:hh[:mm[:ss]]]]] h,m,s start from 0 ; 預設是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.
(上面將匹配從到達日期是星期一至星期五時間從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
(上面將匹配本地產生的時間範圍直到2010年為止的每個星期一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 (標題: iptables範圍匹配)
Author: Jozsef Kadlecsik <> (作者:名字,email地址)
Status: Works (狀況:運作)
Repository: base (貯倉庫: 基礎的)
[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.
翻譯: 這個補丁令匹配源/目標 IP地址可以倚著給出的地址範圍進行匹配
Examples:(例子)
iptables -A FORWARD -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT
這個例子是允許源ip地址範圍192.168.1.5-192.168.1.124的包透過
iptables -A FORWARD -m iprange --dst-range 10.0.0.0-10.255.255.255 -j ACCEPT
這個例子是允許目標ip地址範圍10.0.0.0-10.255.255.255的包透過


6. geoip(根據地理位置匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat geoip/info
Title: iptables geoip match (標題: iptables國家地區匹配)
Author: Samuel Jean <>; Nicolas Bouliane <> (作者:名字,email地址)
Status: Testing (狀況:測試)
Repository: extra (貯倉庫: 額外的)
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.
翻譯:這個補丁令一個包能夠根據源或目的國家(地區)匹配
GeoIP options: (選項:)
[!] --src-cc, --source-country country[,country,country,...]
Match packet coming from (one of) the specified country(ies)
根據包的來源(或非來源)地區匹配
[!] --dst-cc, --destination-country country[,country,country,...]
Match packet going to (one of) the specified country(ies)
根據包的去向(或非去向)地區匹配
NOTE: The country is inputed by its ISO3166 code.
注意:這個國家地區列表放在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 Both files MUST also be moved in /var/geoip/ as the shared library is statically looking for that pathname (ex.: /var/geoip/geoipdb.bin).
這個你需要額外的二進位檔案geoipdb.bin 和它的索引檔案geoipdb.idx.這兩個檔案是國家地區網路資料庫,是用csv2bin 工具生成的,可以在得到.這些檔案必須放在/var/geoip/下,作為一個共享庫查詢路徑名 字如/var/geoip/geoipdb.bin


7. Nth(第n個包匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat nth/info
Title: iptables nth match (標題: iptables第N個匹配)
Author: Fabrice MARIE <> (作者:名字,email地址)
Status: Works (狀況:運作)
Repository: base (貯倉庫: 基礎的)
[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.
翻譯: 這個選項增加一個第N個匹配,允許你匹配每隔N個包,預設有16不同的計算方法可以使用
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.
這個規則將丟棄每隔10個包
2) Unique rule for every packet. This is an easy and quick method to produce load-balancing for both inbound and outbound.
為每一個包應用一個獨特的規則,這樣是一個容易和快速的負載均衡方法
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.
上面例子由三個源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表和高階路由iproute2,你能設定一個複合的負載平衡路由.還有其他的用途,具有創造性的設定
Suppported options are: (支援選項有:)
--every Nth Match every Nth packet (匹配每N 個包)
[--counter] num Use counter 0-15 (default:0) (用計算器(預設是0))
[--start] num Initialize the counter at the number 'num' instead of 0. Must be between 0 and Nth-1
(初始化一個計算器,用這個num值而不是0. 必需是在0和N減1之間的數
[--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之間的數,如果該包被用一個計算器,必需是第Nth數字包規則,並覆蓋0至Nth-1的所有值(這個翻譯得不好,請指正)


8. ipp2p(點對點匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat ipp2p/info
Title: Detects some P2P packets (標題: 偵查P2P包)
Author: Eicke Friedrich <> (作者:名字,email地址)
Status: Stable (狀況:穩定的)
Repository: extra (貯倉庫: 額外的)
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.
這個選項能夠匹配某些P2P包,幫助控制流量.丟棄所有匹配的P2P.結合連線跟蹤,和一個包的排程器,它能被用作計算和整形一個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軟體.
更多引數更詳細的參考還有ipp2p/iptables/extensions/libipt_ipp2p.man或

9. quota(配額匹配)
[root@kindgeorge patch-o-matic-ng-20050331]# cat quota/info Title: iptables quota match (標題: iptables配額匹配)
Author: Sam Johnston <> (作者:名字,email地址)
Status: worksforme (狀況:運作的)
Repository: base (貯倉庫: 基礎的)
[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.
這個選項增加一個模組匹配包實現網路包透過的配額
Supported options are: (支援選項有:)
--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的目標埠為80匹配的500個位元組內,將被丟棄.直到匹配完這500位元組後,才不會丟棄,就是說,才可以開啟網頁.
KNOWN BUGS: this does not work on SMP systems.
知道的bug:這個不能工作在SMP系統上,(SMP, symmetric multiprocessing 多處理技術)

10.其他的.
推薦用cat /模組目錄名/info 和cat /模組目錄名/help 檢視相應的模組資訊和幫助.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-963797/,如需轉載,請註明出處,否則將追究法律責任。

相關文章