我是靠谱客的博主 怕孤单含羞草,这篇文章主要介绍hashlimit速率控制,现在分享给大家,希望可以做个参考。

iptables扩展匹配hashlimit在hashlimit-mode指定为空时,等同于limit匹配。如下hashlimit限制每秒不超过50个报文。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# iptables -A INPUT -p udp -j RATE-LIMIT # iptables --new-chain RATE-LIMIT # iptables --append RATE-LIMIT --match hashlimit --hashlimit-upto 50/sec --hashlimit-burst 20 --hashlimit-name conn_rate_limit --jump ACCEPT # iptables --append RATE-LIMIT --jump DROP # # iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination RATE-LIMIT udp -- 0.0.0.0/0 0.0.0.0/0 Chain RATE-LIMIT (1 references) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 limit: up to 50/sec burst 20 DROP all -- 0.0.0.0/0 0.0.0.0/0

根据hashlimit-name创建如下的PROC文件,由于没有指定hashlimit-mode,源IP、目的IP和源端口、目的端口字段都为零:

复制代码
1
2
3
$ cat /proc/net/ipt_hashlimit/conn_rate_limit 0 0.0.0.0:0->0.0.0.0:0 54975581200000 54975581200000 2748779060000

同等的功能由limit匹配实现,如下,限制每秒不超过50个报文。

复制代码
1
2
3
4
5
6
# iptables --flush # iptables -A INPUT -p udp -j RATE-LIMIT # iptables --new-chain RATE-LIMIT # iptables -A RATE-LIMIT -m limit --limit 50/sec --limit-burst 20 -j ACCEPT # iptables --append RATE-LIMIT --jump DROP

报文限制

使用hashlimit-mode参数指定srcip,依据源IP地址,将每个IP的速率限制在每分钟5个报文,哈希表项的超时时长为30秒钟。

复制代码
1
2
3
4
5
# iptables -I INPUT -p icmp -m hashlimit --hashlimit-name icmp-limit --hashlimit-mode srcip --hashlimit-srcmask 32 --hashlimit-above 5/minute --hashlimit-burst 2 --hashlimit-htable-expire 30000 -j DROP

通过PROC文件icmp-limit查看哈希表项。

复制代码
1
2
3
4
$ cat /proc/net/ipt_hashlimit/icmp-limit 29 192.168.1.114:0->0.0.0.0:0 804842551180032 3298534872000000 1649267436000000 29 192.168.1.117:0->0.0.0.0:0 748217702349568 3298534872000000 1649267436000000

使用dstip模式,限制访问每个目的IP地址的报文数量为每分钟5个。

复制代码
1
2
3
4
5
# iptables -I INPUT -p icmp -m hashlimit --hashlimit-name icmp-limit --hashlimit-mode dstip --hashlimit-srcmask 32 --hashlimit-above 5/minute --hashlimit-burst 2 --hashlimit-htable-expire 30000 -j DROP

通过PROC文件icmp-limit查看哈希表项。

复制代码
1
2
3
$ cat /proc/net/ipt_hashlimit/icmp-limit 27 0.0.0.0:0->192.168.9.133:0 675649895268352 3298534872000000 1649267436000000

流量限制

如下限制每个源IP地址的流量为每秒256kbit。

复制代码
1
2
3
4
5
# iptables -I INPUT -p icmp -m hashlimit --hashlimit-name icmp-traffic-limit --hashlimit-mode srcip --hashlimit-srcmask 32 --hashlimit-above 256kb/s --hashlimit-burst 500kb --hashlimit-htable-expire 30000 -j DROP

通过PROC文件icmp-traffic-limit查看哈希表项。

复制代码
1
2
3
$ cat /proc/net/ipt_hashlimit/icmp-traffic-limit 29 192.168.9.1:0->0.0.0.0:0 4194304000 2 255984

会话流量限制

基于五元组(icmp,srcip,dstip,srcport,dstport)进行流量控制。

复制代码
1
2
3
4
5
# iptables -I INPUT -p icmp -m hashlimit --hashlimit-name icmp-session-limit --hashlimit-mode srcip,dstip,srcport,dstport --hashlimit-above 256kb/s --hashlimit-burst 512kb --hashlimit-htable-expire 30000 -j DROP

通过PROC文件icmp-session-limit查看哈希表项,ICMP没有端口号。

复制代码
1
2
3
$ cat /proc/net/ipt_hashlimit/icmp-session-limit 29 192.168.9.1:0->192.168.9.133:0 4194304000 2 255984

最后

以上就是怕孤单含羞草最近收集整理的关于hashlimit速率控制的全部内容,更多相关hashlimit速率控制内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部