3.iptables 扩展模块

multiport:

添加多个不连续端口
示例 :10.0.0.10 访问本机20、21、80、443允许通过;

[root@route ~]# iptables -t filter -I INPUT -s 10.0.0.10 -d 10.0.0.200 -p tcp -m multiport --dport 20:22,80,443,873 -j ACCEPT
[root@route ~]# iptables -L -n 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  10.0.0.10            10.0.0.200           multiport dports 20:22,80,443,873

iprange:

指定一段连续的ip地址范围 用于匹配报文的源地址和目标地址 iprange扩展模块中有两个扩展匹配条件可以使用
--src-range "10.0.0.5-10.0.0.10"
--dst-range "10.0.0.5-10.0.0.10"

示例:10.0.0.5-10.0.0.10地址段ping本机,则丢弃; 41 51

[root@route ~]# iptables -t filter -F
[root@route ~]# iptables -t filter -I INPUT -p icmp -m iprange --src-range 10.0.0.5-10.0.0.10 --dst-range 10.0.0.200 -j DROP 
[root@route ~]# iptables -L -n

String:

iptables [-t 表名] -A 链名 -m string –string “XXX” –algo bm -j DROP
-m string
使用string功能,string是iptables的一个module,也就是做字符串匹配的。
–string “xxxx”
定义字符串内容,可以是URL里任意字符,如果是需要block下载某些类型的文件或请求,这个有很大的发挥空间,可自由想象喔。
–algo bm
设置字符匹配的查询算法,一般默认使用bm算法效果就可以了,另外还可以设置kmp算法,那是一种更复杂的算法,详细内容可自行参见高等数学里的资料。(bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)

示例:应用返回的报文中包含字符"hello",我们就丢弃当前报文,其余正常通过。

[root@route ~]# iptables -t filter -F
[root@route ~]# iptables -t filter -I OUTPUT -p tcp -m string --algo kmp --string "video" -j DROP

任意时间段都拒绝,应该根据上班时间点进行拒绝策略限制;
实例:用户请求iptables节点,如果请求中包含 “jd.oldxu.net” 则拒绝;

[root@route ~]# iptables -t filter -F
[root@route ~]# iptables -t filter -I INPUT -p tcp -m string --algo kmp --string "jd.oldxu.net" -j DROP

time:

time模块的作用是根据时间范围来匹配报文,例如在上午的8点30到下午18点30关于浏览淘宝的报文都拒绝。

time模块的常用参数:

--timestart:指定开始时间。
--timestop:指定结束时间。
--monthdays:指定一个月中的某一天。
--weekdays:指定一周中的周期,例如1-7。
--kerneltz:使用内核时区的时间。
可以在参数前面加!号表示去反。
time模块默认使用的实际UTC时间,UTC时间比我们正常的时间慢8小时。

--timestart: 14:12   -8:   06:12
--timestop: 14:14    -8:   06:14

编写防火墙规则
在周一到周五的8点到18点时间段,所有发往taobao.com的TCP 80端口的报文都会被拒绝。

[root@jxl-1 ~]# iptables -t filter -I OUTPUT -p tcp -d taobao.com --dport 80 -m time --timestart 00:00 --timestop 10:00 --weekdays 1,2,3,4,5 -j DROP

string: (路由器)
限制早上:8:00 ~ 12:00 (00:00-04:00)
限制下午:14:00 ~ 18:00 (06:00-10:00)

网络策略

上午:
iptables -t filter -I  FORWARD -p tcp -m string --string "qq" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
iptables -t filter -I  FORWARD -p tcp -m string --string "tb" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
iptables -t filter -I  FORWARD -p tcp -m string --string "jd" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
iptables -t filter -I  FORWARD -p tcp -m string --string "aqy" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
iptables -t filter -I  FORWARD -p tcp -m string --string "wx" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP

下午:
iptables -t filter -I  FORWARD -p tcp -m string --string "qq" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I  FORWARD -p tcp -m string --string "tb" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I  FORWARD -p tcp -m string --string "jd" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I  FORWARD -p tcp -m string --string "aqy" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I  FORWARD -p tcp -m string --string "wx" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP

主机策略:

上午:
iptables -t filter -I  INPUT -p tcp -m string --string "qq" --algo kmp -m time --timestart 00:00 --timestop 10:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "tb" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "jd" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "aqy" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "wx" --algo kmp -m time --timestart 00:00 --timestop 04:00 -j DROP
					   
下午:                 
iptables -t filter -I  INPUT -p tcp -m string --string "qq" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "tb" --algo kmp -m time --timestart 06:00 --timestop 06:24 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "jd" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "aqy" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP
iptables -t filter -I  INPUT -p tcp -m string --string "wx" --algo kmp -m time --timestart 06:00 --timestop 10:00 -j DROP

icmp:

--icmp-type:
echo-request 信号8(type 8为ping 命令请求信号)
echo-reply 信号0(type 0为ping 命令响应信号)

[root@route ~]# iptables -t filter -I INPUT -p icmp -m icmp --icmp-type "echo-request" -j REJECT

connlimit

connlimit模块的作用是限制请求报文对特定服务的并发连接数限制的,例如Telnet服务,默认情况下没有并发连接数的限制,可以允许n个客户端同时连接,如果应用了connlimit模块,可以对并发连接数进行限制。
限制每个客户端IP地址到服务器的并行连接数。

  • --connlimit-upto n:如果现有连接数小于或等于n,则匹配。
  • --connlimit-above n:如果现有连接数大于n,则匹配。
  • --connlimit-mask 24 or 32 :指定同一个网段的连接数上限,可以和 --connlimit-above 一起使用

限制同一IP的并发连接数;

iptables -t filter -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP

模拟攻击:

[root@redis-node1 ~]# ./a.out 10.0.0.200 
Starting flood connect attack on 10.0.0.200 port 80

limit:

limit模块的作用是针对报文的速率进行限制,限制的单位有秒、分钟、小时、天等,例如一分钟内只接收10个请求报文,多余的报文则会被丢弃。

limit模块的常用参数:

--limit rate[/second|/minute|/hour|/day]:指定限制的平均速率单位,以秒(s)、分(m)、时(h)、天为单位,默认是3小时。
--limit-burst:要匹配的最大初始包的数量,例如1分钟内限制10个请求报文,那么首先一次性会接收我们指定初始化包的数量,剩余请求包按着速率单位来进行接收,默认值为5个报文。

限制每分钟接收10个ICMP数据报文
1).先添加一条规则,限制1分钟内只收10个icmp协议的请求报文。

[root@jxl-1 ~]# iptables -t filter -I INPUT -p icmp -m limit --limit 10/minute -j ACCEPT

2).然后添加第二条规则,超出10个报文后,其余的报文全部丢弃。

[root@jxl-1 ~]# iptables -t filter -A INPUT -p icmp -j DROP 

一个数据包的大小大概在1500字节。

公式:(限制的带宽速率*1000)/(单个数据包的大小)

限速:限制传输速度最多300k;

	300k * 1000 = 300000 / 1500 = 200 

主机防护:
filter表OUTPUT链;

[root@route ~]# iptables -t filter -I OUTPUT -p tcp -m limit --limit 200/second -j ACCEPT
[root@route ~]# iptables -t filter -A OUTPUT -p tcp -j DROP

网络防护:
filter:

[root@route ~]# iptables -t filter -I FORWARD -p tcp -m limit --limit 200/second -j ACCEPT
[root@route ~]# iptables -t filter -A FORWARD -p tcp -j DROP

tcp-flags:

[root@route ~]# man 8 iptables-extensions
客户端连接服务端22端口第一次握手必须是客户端发起的,所以SYN必须为1,剩下全部为0。然后服务端可以通过22端口返回对应的报文(SYN+ACK=1)。

数据包流入本机时的策略:

iptables -t filter -I INPUT -p tcp --dport 22 -m tcp --tcp-flags SYN,ACk,FIN,RST SYN -j ACCEPT
iptables -t filter -I INPUT -p tcp --dport 22 -m tcp --tcp-flags SYN,ACk,FIN,RST ACK -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 22 -j REJECT

SYN+ACK数据包流出本机时策略:

iptables -t filter -I OUTPUT -p tcp --sport 22  -m tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --sport 22 -m tcp --tcp-flags SYN,ACK,FIN,RST ACK -j ACCEPT
iptables -t filter -A OUTPUT -p tcp -j REJECT
posted @ 2022-10-07 10:43  老夫聊发少年狂88  阅读(222)  评论(0编辑  收藏  举报