1) iprange 扩展模块

iprange扩展模块中有两个扩展匹配条件可以使用

--src-range

--dst-range

举例说明:

[root@nat3 /]# iptables -t filter -I INPUT -m iprange --src-range 192.168.56.120-192.168.56.130 -j DROP
[root@nat3 /]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 7 packets, 404 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            source IP range 192.168.56.120-192.168.56.130
[root@nat3 /]# 

我们丢掉来自192.168.56.120-192.168.56.130 这个地址段的所有数据包

我们从192.168.56.130 ping 192.168.56.131试试看看能不能ping通

[root@nat2 ~]# ping 192.168.56.131
PING 192.168.56.131 (192.168.56.131) 56(84) bytes of data.

此时我们发现已经不能ping通

--dst-range 是一样的用法,可以自己尝试

2)string扩展模块

使用string扩展模块,可以指定要匹配的字符串,如果报文中包含对应的字符串,则符合匹配条件。


我们来拒绝所有包含“200”字符串的报文进入本机,这将组织所有能正常访问的网站,导致网站无法从本机访问

例子,我们来访问www.baidu.com 试试

[root@nat3 /]# iptables -t filter -I INPUT -m string --algo bm --string "200" -j REJECT
[root@nat3 /]# curl www.baidu.com
^C
[root@nat3 /]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 113 packets, 6678 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    6 10121 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "200" ALGO name bm TO 65535 reject-with icmp-port-unreachable
  915 76860 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            source IP range 192.168.56.120-192.168.56.130
[root@nat3 /]# 

-m string 表示使用string模块,

--algo bm 表示使用bm算法去匹配指定的字符串

可以看到我们已经不能正常访问百度。

3)time扩展模块

 我们可以通过time扩展模块,根据时间段区匹配报文,如果报文到达的时间在指定的时间范围以内,则符合匹配条件。

在09:00:00到18:00:00不能访问本机

[root@nat3 /]# iptables -t filter -I INPUT -m time -p tcp --timestart 09:00:00 --timestop 18:00:00 -j REJECT
[root@nat3 /]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 45 packets, 2628 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            TIME from 09:00:00 to 18:00:00 UTC reject-with icmp-port-unreachable
  920 77160 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            source IP range 192.168.56.120-192.168.56.130
[root@nat3 /]# 

在131 机器上尝试ssh到130机器,可以发现不能登录

[root@nat2 ~]# ssh root@192.168.56.131
ssh: connect to host 192.168.56.131 port 22: Connection timed out
[root@nat2 ~]# 

-m time 表示使用time扩展模块,

--timestart 选项用于指定起始时间,

--timestop选项用于指定结束时间。

--weekdays选项可以指定每个星期的具体哪一天,可以同时指定多个,用逗号隔开

--monthdays选项可以具体指定的每个月的哪一天

--datestart 选项与--datestop选项,指定具体的日期范围

4)connlimit扩展模块

使用connlimit扩展模块,可以限制每个IP地址同时链接到server端的链接数量,注意:我们不用指定IP,其默认就是针对"每个客户端IP",即对单IP的并发连接数限制。

限连接到本机(131)22端口的连接数量

[root@nat3 /]# iptables -t filter -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
[root@nat3 /]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 106 packets, 6148 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 #conn src/32 > 2 reject-with icmp-port-unreachable

我们同时打开3个130终端去连接131,发现第1,2个可以ssh到131但是第三个就会被拒绝

第一个:

[root@nat2 ~]# ssh root@192.168.56.131
root@192.168.56.131's password: 
Last login: Mon Jan  4 13:12:47 2021 from 192.168.56.130
[root@nat3 ~]# 

第二个:

[root@nat2 ~]# ssh root@192.168.56.131
root@192.168.56.131's password: 
Last login: Mon Jan  4 10:01:04 2021 from 192.168.56.1
[root@nat3 ~]# 

第三个:

[root@nat2 ~]# ssh root@192.168.56.131
ssh: connect to host 192.168.56.131 port 22: Connection refused
[root@nat2 ~]# 

5)limit扩展模块

connlimit模块是对连接数量进行限制的,limit模块是对"报文到达速率"进行限制的。

用大白话说就是,如果我想要限制单位时间内流入的包的数量,就能用limit模块。

我们可以以秒为单位进行限制,也可以以分钟、小时、天作为单位进行限制。

比如,限制每秒中最多流入3个包,或者限制每分钟最多流入30个包,都可以。

[root@nat3 /]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 21 packets, 2448 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   21  1764 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            limit: avg 10/min burst 5
    1    60 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 #conn src/32 > 2 reject-with icmp-port-unreachable
   37  3108 REJECT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
[root@nat3 /]# 

此模块的理解可以类比令牌桶算法

--limit 选项就是用于指定"多长时间生成一个新令牌的",

--limit-burst 选项就是用于指定"木桶中最多存放几个令牌的,默认值是5

上面的例子当中表示刚开始令牌桐中有5个令牌可以被用,以后每6秒生成一个令牌

所以结果会像下面这样子:

[root@nat2 ~]# ping 192.168.56.131
PING 192.168.56.131 (192.168.56.131) 56(84) bytes of data.
64 bytes from 192.168.56.131: icmp_seq=1 ttl=64 time=2.48 ms
64 bytes from 192.168.56.131: icmp_seq=2 ttl=64 time=2.56 ms
64 bytes from 192.168.56.131: icmp_seq=3 ttl=64 time=1.75 ms
64 bytes from 192.168.56.131: icmp_seq=4 ttl=64 time=2.35 ms
64 bytes from 192.168.56.131: icmp_seq=5 ttl=64 time=2.59 ms
From 192.168.56.131 icmp_seq=6 Destination Port Unreachable
64 bytes from 192.168.56.131: icmp_seq=7 ttl=64 time=2.20 ms
From 192.168.56.131 icmp_seq=8 Destination Port Unreachable
From 192.168.56.131 icmp_seq=9 Destination Port Unreachable
From 192.168.56.131 icmp_seq=10 Destination Port Unreachable
From 192.168.56.131 icmp_seq=11 Destination Port Unreachable
From 192.168.56.131 icmp_seq=12 Destination Port Unreachable
64 bytes from 192.168.56.131: icmp_seq=13 ttl=64 time=2.19 ms
From 192.168.56.131 icmp_seq=14 Destination Port Unreachable
From 192.168.56.131 icmp_seq=15 Destination Port Unreachable
From 192.168.56.131 icmp_seq=16 Destination Port Unreachable
From 192.168.56.131 icmp_seq=17 Destination Port Unreachable
From 192.168.56.131 icmp_seq=18 Destination Port Unreachable
64 bytes from 192.168.56.131: icmp_seq=19 ttl=64 time=1.77 ms
From 192.168.56.131 icmp_seq=20 Destination Port Unreachable
From 192.168.56.131 icmp_seq=21 Destination Port Unreachable
From 192.168.56.131 icmp_seq=22 Destination Port Unreachable
From 192.168.56.131 icmp_seq=23 Destination Port Unreachable
From 192.168.56.131 icmp_seq=24 Destination Port Unreachable
64 bytes from 192.168.56.131: icmp_seq=25 ttl=64 time=95.0 ms
From 192.168.56.131 icmp_seq=26 Destination Port Unreachable
From 192.168.56.131 icmp_seq=27 Destination Port Unreachable
From 192.168.56.131 icmp_seq=28 Destination Port Unreachable
From 192.168.56.131 icmp_seq=29 Destination Port Unreachable
From 192.168.56.131 icmp_seq=30 Destination Port Unreachable
64 bytes from 192.168.56.131: icmp_seq=31 ttl=64 time=2.12 ms
From 192.168.56.131 icmp_seq=32 Destination Port Unreachable
From 192.168.56.131 icmp_seq=33 Destination Port Unreachable
From 192.168.56.131 icmp_seq=34 Destination Port Unreachable
From 192.168.56.131 icmp_seq=35 Destination Port Unreachable
From 192.168.56.131 icmp_seq=36 Destination Port Unreachable

 

 

参考文档:

http://www.zsythink.net/archives/1564

posted on 2020-12-31 17:49  EZgod  阅读(310)  评论(0编辑  收藏  举报