天天看點

Linux防火牆之iptables常用擴充比對條件(一)

Linux防火牆之iptables常用擴充比對條件(一)

multiport擴充,這個擴充子產品主要用于比對多個源端口或目标端口,前面我們了解了tcp和udp他們都有兩個隐式擴充來指定連續或單個源端口或目标端口,它不能同時指定多個離散的端口,multiport這個子產品就可以以離散方式定義多端口比對,當然它也支援連續的端口比對,連續端口比對同tcp/udp的連續端口比對用法和寫法一直,它也支援,連續和非連續端口的混合比對,但這個子產品最多比對15個端口。這裡的15個端口不同于我們了解的15個端口,這裡的15個端口是說用逗号隔開的離散端口,也就是說連續的端口,在這裡隻算一個。

  上一篇博文講了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的字眼的封包,給予丢棄

  在沒有設定規則的是可以正常通路的

Linux防火牆之iptables常用擴充比對條件(一)

   添加如下規則

[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 ~]# 
      
Linux防火牆之iptables常用擴充比對條件(一)

   提示:可以看到添加了規則後,我們用戶端就不能再通路我們的網站了,這個就是通過過濾字元串來實作控制使用者的通路

  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通路伺服器

Linux防火牆之iptables常用擴充比對條件(一)

    提示:可以看到在允許的時間通路伺服器上沒有問題,我們等會不再允許的時間範圍内在通路下,看看是不是可以正常通路呢

Linux防火牆之iptables常用擴充比對條件(一)

    提示:可以看到不在允許的時間範圍呢 是不可以通路的

  通過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 ~]# 
      

  測試:不在允許範圍的主機和在允許範圍的主機都在允許時間是否能通路伺服器?

Linux防火牆之iptables常用擴充比對條件(一)

    提示:可以看到雖然都是在允許的時間,在允許範圍的主機是可以通路的,不在允許範圍的主機上不能通路的。

  測試:允許的主機和不允許的主機,都在不在允許的時間是否可以通路伺服器?

Linux防火牆之iptables常用擴充比對條件(一)

    提示:可以看到都不在允許的時間,它倆是都不能通路的,是以要滿足在允許的時間内的同時還要滿足是允許的主機才可以,它倆條件必須是交集。

  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 允許連接配接。

測試:同一主機開三個視窗對伺服器,看看第三個連接配接是否可以連接配接

Linux防火牆之iptables常用擴充比對條件(一)

   提示:可以看到當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 就拒絕

  測試:同一主機開三個視窗對伺服器,看看第三個連接配接是否可以連接配接

Linux防火牆之iptables常用擴充比對條件(一)

    提示:可以看到同一主機連接配接大于2時就拒絕連結了

Linux防火牆之iptables常用擴充比對條件(一)

    提示:在同一主機連接配接數大于2時 用另外的主機去連接配接是不受影響的

  從以上測試看,connlimit子產品可以控制單台用戶端的并發連接配接數,并且不對其他用戶端産生影響,通常情況--connlimit-upto 和--connlimit-above 和預設政策結合使用,如果預設政策是允許所有不比對的封包,那麼我就用--connlimit-above 來控制連接配接上限,然後再拒絕。如果預設政策是拒絕所有不比對的封包那麼我們就用--connlimit-upto來允許連接配接數小于等于某個數來控制連接配接請求。

作者:Linux-1874

出處:https://www.cnblogs.com/qiuhom-1874/

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.

繼續閱讀