上一篇博文講了iptables的基本匹配條件和隱式匹配條件,回顧請參考https://www.cnblogs.com/qiuhom-1874/p/12269717.html;今天在來說說iptabels的一些常用的顯示擴展匹配條件,何謂顯示擴展匹配條件呢?顯示擴展匹配條件就是我們需要用到一些擴展的模塊,用-m選項去指定動態加載它。要用iptabels的擴展匹配條件的前提是,我們的系統上要有對應的擴展模塊。在Linux主機上/usr/lib64/xtables/這個目錄用來存放iptables的模塊的,這里面的模塊以libip6t開頭的,表示適用於ipv6,其余的是ipv4協議版本的模塊。這個目錄下的模塊命名是這樣的,libipt_或者libip6t_后面的名字如果全是大寫,則該模塊用於處理動作擴展模塊,如果是小寫就是匹配條件的擴展模塊。對於這些模塊的幫助信息,在centos上用man iptables命令就可以找到相應的模塊說明和用法,以及模塊的選項等等,在centos7上我們要查看擴展模塊的用法幫助,需要用man iptables-extensions命令來查看;了解了iptables的擴展模塊,我們接下來說說常用的幾種擴展模塊的使用和說明
1、multiport擴展,這個擴展模塊主要用於匹配多個源端口或目標端口,前面我們了解了tcp和udp他們都有兩個隱式擴展來指定連續或單個源端口或目標端口,它不能同時指定多個離散的端口,multiport這個模塊就可以以離散方式定義多端口匹配,當然它也支持連續的端口匹配,連續端口匹配同tcp/udp的連續端口匹配用法和寫法一直,它也支持,連續和非連續端口的混合匹配,但這個模塊最多匹配15個端口。這里的15個端口不同於我們理解的15個端口,這里的15個端口是說用逗號隔開的離散端口,也就是說連續的端口,在這里只算一個。
[!] --source-ports,--sports port[,port|,port:port]...,這個選項表示匹配多個源端口
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 18 packets, 1292 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 1212 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -s 172.16.0.0/16 -d 192.168.0.99 -p tcp -m multiport --sports 20:50,80,3306,9000 -j ACCEPT [root@test ~]# iptables -A my_chain -s 172.16.0.0/16 -d 192.168.0.99 -p tcp -m multiport ! --sports 53,123,323 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 24 packets, 1740 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 172.16.0.0/16 192.168.0.99 multiport sports 20:50,80,3306,9000 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 17 packets, 1580 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination 0 0 DROP tcp -- * * 172.16.0.0/16 192.168.0.99 multiport sports !53,123,323 [root@test ~]#
提示:--sports支持對指定端口取反,表示匹配除了指定端口以外的其他端口。
[!] --destination-ports,--dports port[,port|,port:port]...,這個選項表示匹配多個目標端口
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 8 packets, 528 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 5 packets, 620 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -d 192.168.0.99 -p tcp -m multiport --dports 22,80,3306,41319 -j ACCEPT [root@test ~]# iptables -A INPUT -d 192.168.0.99 -p tcp -m multiport ! --dports 22,80,3306,41319 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 152 12112 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.99 multiport dports 22,80,3306,41319 0 0 DROP tcp -- * * 0.0.0.0/0 192.168.0.99 multiport dports !22,80,3306,41319 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 17 packets, 1580 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
[!] --ports port[,port|,port:port]...多個源或目標端口
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 18 packets, 1292 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 1212 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -d 192.168.0.99 -p tcp -m multiport --ports 22,3306,41319 -j ACCEPT [root@test ~]# iptables -A INPUT -d 192.168.0.99 -p tcp -m multiport ! --ports 22,3306,41319 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 121 9468 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.99 multiport ports 22,3306,41319 6 304 DROP tcp -- * * 0.0.0.0/0 192.168.0.99 multiport ports !22,3306,41319 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 25 packets, 3120 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
提示:--ports表示匹配到的端口不管是源還是目標,只要是指定的端口都能匹配得到,然后做出相應的處理動作
2、iprange擴展,此擴展模塊主要用於匹配連續的ip地址范圍
[!] --src-range from[-to] 此選項表示匹配源ip地址范圍
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 25 packets, 1832 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 19 packets, 1832 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -p tcp -m iprange --src-range 192.168.0.200-192.168.0.245 -j ACCEPT [root@test ~]# iptables -A INPUT -p tcp -m iprange ! --src-range 192.168.0.200-192.168.0.245 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 144 12000 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.0.200-192.168.0.245 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range ! 192.168.0.200-192.168.0.245 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 15 packets, 1396 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
[!] --dst-range from[-to],此選項表示匹配目標地址范圍
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 29 packets, 2096 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 20 packets, 1856 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A OUTPUT -p tcp -m iprange --dst-range 192.168.0.100-192.168.0.245 -j ACCEPT [root@test ~]# iptables -A OUTPUT -p tcp -m iprange ! --dst-range 192.168.0.100-192.168.0.245 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 103 packets, 7240 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 175 16212 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 destination IP range 192.168.0.100-192.168.0.245 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 destination IP range ! 192.168.0.100-192.168.0.245 Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
3、mac擴展,該模塊用於匹配主機的MAC地址,適用於PREROUTING和FORWARD,INPUT鏈上
[!] --mac-source XX:XX:XX:XX:XX:XX,此選項表示匹配源MAC地址
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 19 packets, 1332 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 1212 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -p tcp -m mac --mac-source 00:24:81:68:ce:45 -j ACCEPT [root@test ~]# iptables -A INPUT -s 192.168.0.151 -p tcp -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 65 packets, 16202 bytes) pkts bytes target prot opt in out source destination 18 1480 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 MAC 00:24:81:68:CE:45 0 0 DROP tcp -- * * 192.168.0.151 0.0.0.0/0 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 70 packets, 19646 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
4、string擴展,此模塊主要對報文中的應用層數據做字符串模式匹配檢測
--algo {bm|kmp} ,指定字符串匹配檢測算法,這個必須指定
--from offset:從第幾個字節開始匹配
--to offset :到底幾個字節結束
[!] --string pattern 指定要檢測到字符串模式
[!] --hex-string pattern 知道那個要檢測字符串模式,16進制格式
示例:入站報文有loganalyzer的字眼的報文,給予丟棄
在沒有設置規則的是可以正常訪問的
添加如下規則
[root@test ~]# iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string "loganalyzer" -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 35 packets, 2328 bytes) pkts bytes target prot opt in out source destination 8 1840 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 STRING match "loganalyzer" ALGO name bm TO 65535 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 28 packets, 3200 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
提示:可以看到添加了規則后,我們客戶端就不能再訪問我們的網站了,這個就是通過過濾字符串來實現控制用戶的訪問
5、time擴展,此模塊根據將報文到達的時間與指定的時間范圍進行匹配
--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 指定開始日期
--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 指定結束日期
--timestart hh:mm[:ss] 指定開始時間
--timestop hh:mm[:ss] 指定結束時間
[!] --monthdays day[,day...] 指定每個月的幾號
[!] --weekdays day[,day...] 指定星期幾,1 – 7 分別表示星期一到星期日
--kerneltz:使用內核配置的時區而非默認的UTC,CentOS7系統默認為UTC;注意: centos6 不支持kerneltz ,--localtz指定本地時區(默認)
通常情況我們用--mouthdays 和--timestart 、--timestop結合或者--weekdays day 和--timestart 、--timestop來結合使用很少和--datastart 、datastop使用;最后我們還有指定為使用的時區,如果我們不指定,它默認使用的是UTC時區,在centos6 上需要用--localtz來指定時區
示例:允許任何客戶端在晚上的20:00:00 到20:50:00 通過telnet 來訪問我們服務器
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 19 packets, 1332 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 1212 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -p tcp --dport 23 -m time --timestart 20:00:00 --timestop 20:50:00 --kerneltz -j ACCEPT [root@test ~]# iptables -A INPUT -p tcp --dport 23 -j DROP [root@test ~]# iptables -A OUTPUT -p tcp --sport 23 -m time --timestart 20:00:00 --timestop 20:50:00 --kerneltz -j ACCEPT [root@test ~]# iptables -A OUTPUT -p tcp --sport 23 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 19 packets, 1332 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 TIME from 20:00:00 to 20:50:00 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 1212 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp spt:23 TIME from 20:00:00 to 20:50:00 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp spt:23 Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
測試:在允許的時間內通過Telnet訪問服務器
提示:可以看到在允許的時間訪問服務器上沒有問題,我們等會不再允許的時間范圍內在訪問下,看看是不是可以正常訪問呢
提示:可以看到不在允許的時間范圍呢 是不可以訪問的
通過time模塊我們可以做到在某個時間允許或拒絕客戶端的訪問,時間可以用上面的三種時間組合來確定一個范圍,也可以同其他擴展模塊聯合使用,比如我們又要控制時間,又要控制部分源ip 來訪問我們服務器,我們可以用-m指定iprange 的范圍,iptables里的一條規則匹配條件都是取並集,也就說一條規則是否匹配到報文,要看這條規則里的匹配條件是否對數據包都匹配,換句話說就是一個數據要通過某一條規則,那么這個數據包需要滿足我們給定規則的所有條件。
示例2:允許192.168.0.10-192.168.0.200 的服務器在21:00:00到21:20:00 允許通過Telnet訪問我們服務器
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 20 packets, 1372 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 1212 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -p tcp --dport 23 -m iprange --src-range 192.168.0.10-192.168.0.200 -m time --timestart 21:00:00 --timestop 21:20:00 --kerneltz -j ACCEPT [root@test ~]# iptables -A INPUT -p tcp --dport 23 -j DROP [root@test ~]# iptables -A OUTPUT -p tcp --sport 23 -m iprange --dst-range 192.168.0.10-192.168.0.200 -m time --timestart 21:00:00 --timestop 21:20:00 --kerneltz -j ACCEPT [root@test ~]# iptables -A OUTPUT -p tcp --dport 23 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 14 packets, 924 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 source IP range 192.168.0.10-192.168.0.200 TIME from 21:00:00 to 21:20:00 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 11 packets, 1908 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp spt:23 destination IP range 192.168.0.10-192.168.0.200 TIME from 21:00:00 to 21:20:00 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
測試:不在允許范圍的主機和在允許范圍的主機都在允許時間是否能訪問服務器?
提示:可以看到雖然都是在允許的時間,在允許范圍的主機是可以訪問的,不在允許范圍的主機上不能訪問的。
測試:允許的主機和不允許的主機,都在不在允許的時間是否可以訪問服務器?
提示:可以看到都不在允許的時間,它倆是都不能訪問的,所以要滿足在允許的時間內的同時還要滿足是允許的主機才可以,它倆條件必須是交集。
6、connlimit擴展,此模塊可根據每客戶端IP做並發連接數數量匹配,可防止CC(Challenge Collapsar挑戰黑洞)攻擊
--connlimit-upto #:連接的數量小於等於#時匹配
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 14 packets, 1004 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 11 packets, 996 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -p tcp --dport 23 -m connlimit --connlimit-upto 2 -j ACCEPT [root@test ~]# iptables -A INPUT -p tcp --dport 23 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 23 packets, 1668 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 #conn src/32 <= 2 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 17 packets, 1548 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
提示:以上規則表示,同一客戶端連接我本機服務器上的23號端口(Telnet服務),如果連接數小於等於2 允許連接。
測試:同一主機開三個窗口對服務器,看看第三個連接是否可以連接
提示:可以看到當192.168.0.151 的第三個連接是被服務器拒絕了
--connlimit-above #:連接的數量大於#時匹配
[root@test ~]# iptables -F [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 20 packets, 1372 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 1212 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]# iptables -A INPUT -d 192.168.0.99 -p tcp --dport 23 -m connlimit --connlimit-above 2 -j DROP [root@test ~]# iptables -nvL Chain INPUT (policy ACCEPT 23 packets, 1596 bytes) pkts bytes target prot opt in out source destination 0 0 DROP tcp -- * * 0.0.0.0/0 192.168.0.99 tcp dpt:23 #conn src/32 > 2 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 15 packets, 1396 bytes) pkts bytes target prot opt in out source destination Chain my_chain (0 references) pkts bytes target prot opt in out source destination [root@test ~]#
提示:我們把上面的規則更改為同一主機連接數大於2時 就丟棄,其他連接走默認同意放行連接,也就是說只要同一ip 連接數大於2 就拒絕
測試:同一主機開三個窗口對服務器,看看第三個連接是否可以連接
提示:可以看到同一主機連接大於2時就拒絕鏈接了
提示:在同一主機連接數大於2時 用另外的主機去連接是不受影響的
從以上測試看,connlimit模塊可以控制單台客戶端的並發連接數,並且不對其他客戶端產生影響,通常情況--connlimit-upto 和--connlimit-above 和默認策略結合使用,如果默認策略是允許所有不匹配的報文,那么我就用--connlimit-above 來控制連接上限,然后再拒絕。如果默認策略是拒絕所有不匹配的報文那么我們就用--connlimit-upto來允許連接數小於等於某個數來控制連接請求。