Linux防火牆之iptables經常使用擴展匹配條件(一)

  上一篇博文講了iptables的基本匹配條件和隱式匹配條件,回顧請參考http://www.javashuo.com/article/p-cvnzbcmg-ed.html;今天在來講說iptabels的一些經常使用的顯示擴展匹配條件,所謂顯示擴展匹配條件?顯示擴展匹配條件就是咱們須要用到一些擴展的模塊,用-m選項去指定動態加載。要用iptabels的擴展匹配條件的前提是,咱們的系統上要有對應的擴展模塊。在Linux主機上/usr/lib64/xtables/這個目錄用來存放iptables的模塊的,這裏面的模塊以libip6t開頭的,表示適用於ipv6,其他的是ipv4協議版本的模塊。這個目錄下的模塊命名是這樣的,libipt_或者libip6t_後面的名字若是全是大寫,則該模塊用於處理動做擴展模塊,若是是小寫就是匹配條件的擴展模塊。對於這些模塊的幫助信息,在centos上用man iptables就能夠找到相應的說明和用法,以及模塊的選項等等,在centos7上咱們要查看擴展模塊的用法幫助,須要用man iptables-extensions 來查看;瞭解了iptables的擴展模塊,咱們接下來講說經常使用的幾種擴展模塊的使用和說明html

  一、multiport擴展,這個擴展模塊主要用於匹配多個源端口或目標端口,前面咱們瞭解了tcp和udp他們都有兩個隱式擴展來指定連續或單個源端口或目標端口,它不能同時指定多個離散的端口,multiport這個模塊就能夠以離散方式定義多端口匹配,固然它也支持連續的端口匹配,連續端口匹配同tcp/udp的連續端口匹配用法和寫法一直,它也支持,連續和非連續端口的混合匹配,但這個模塊最多匹配15個端口。這裏的15個端口不一樣於咱們理解的15個端口,這裏的15個端口是說用逗號隔開的離散端口,也就是說連續的端口,在這裏只算一個。算法

  [!] --source-ports,--sports port[,port|,port:port]...,這個選項表示匹配多個源端口centos

[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支持對指定端口取反,表示匹配除了指定端口之外的其餘端口。bash

  [!] --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表示匹配到的端口不論是源仍是目標,只要是指定的端口都能匹配獲得,而後作出相應的處理動做tcp

  二、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],此選項表示匹配目標地址範圍centos7

[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 ~]# 

  三、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 ~]# 

  四、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 ~]# 

   提示:能夠看到添加了規則後,咱們客戶端就不能再訪問咱們的網站了,這個就是經過過濾字符串來實現控制用戶的訪問

  五、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 ~]# 

  測試:不在容許範圍的主機和在容許範圍的主機都在容許時間是否能訪問服務器?

    提示:能夠看到雖然都是在容許的時間,在容許範圍的主機是能夠訪問的,再也不容許範圍的主機上不能訪問的。

  測試:容許的主機和不容許的主機,都在不在容許的時間是否能夠訪問服務器?

    提示:能夠看到都不在容許的時間,它倆是都不能訪問的,因此要知足在容許的時間內的同時還要知足是容許的主機才能夠,它倆條件必須是交集。

  六、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來容許鏈接數小於等於某個數來控制鏈接請求。

相關文章
相關標籤/搜索