netfilter/iptables模塊功能介紹
netfilter/iptables模塊功能能做許多事情,讓我們看看netfilter/iptables模塊功能的介紹。
功能介紹 (每個info和help本是英文的,為方便閱讀我把它翻譯成中文,由于水平有限,如果有誤請有經驗者來信指正)
獲取最新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
進入該目錄
[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地址brad@info-link.net))
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
模塊 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
例如:只允許每組C類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. 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[]]]]] h,m,s start from 0 ; default to 1970)
(只是匹配這個開始日期值之后的(包括,格式: YYYY[:MM[:DD[:hh[:mm[]]]]] h,m,s start from 0 ; 默認是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)
(只是匹配這個開始日期值之前的(包括,格式: YYYY[:MM[:DD[:hh[:mm[]]]]] 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 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).
這個你需要額外的二進位文件geoipdb.bin 和它的索引文件geoipdb.idx.這兩個文件是國家地區網絡數據庫,是用csv2bin 工具生成的,可以在www.cookinglinux.org/geoip/得到.這些文件必須放在/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或http://www.ipp2p.org
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 查看相應的模塊信息和幫助.
由于我對其他某些模塊不太感興趣,或和我現在系統2.4.20-8有沖突,沒有再列出翻譯.如果有誰翻譯了,可以讓我參考下.謝謝
如果有更新,請看我的blog: http://kindgeorge.at.3322.org
相關資料:
安裝編譯 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
通過文章的介紹,我們可以利用netfilter/iptables模塊功能來實現各種事情,希望本文對大家有所幫助!
【編輯推薦】