我正在尝试将一些iptables规则转换为nftables,我想创建一条规则,如果在30秒内尝试次数超过4次,则阻止连接
我最初的iptables规则是:
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 30 --hitcount 4 --rttl --name SSH -j DROP
如何在nftables中执行相同(或等效)操作?
发布于 2021-09-14 05:14:36
我试了几个月,都找不到一个完全匹配的。但是我有一个变通办法。
sshPort=2222
nft add table ip sshGuard
nft add chain ip sshGuard input { type filter hook input priority 0 \; }
nft add set ip sshGuard denylist { type ipv4_addr \; flags dynamic, timeout \; timeout 5m \; }
nft add set ip sshGuard sshlist { type ipv4_addr \; flags dynamic, timeout \; timeout 5m \; }
nft add rule ip sshGuard input ct state established,related accept
nft add rule ip sshGuard input tcp dport $sshPort ct state new ip saddr @denylist reject
nft add rule ip sshGuard input tcp dport $sshPort ct state new ip saddr @sshlist add @denylist { ip saddr } accept
nft add rule ip sshGuard input tcp dport $sshPort ct state new limit rate over 2/minute burst 3 packets add @sshlist { ip saddr } counter accept
nft list table ip sshGuard
对于新的状态,任何新的IP都将接受大约5个连接。如果命中limit rate
,则新的IP (不在sshlist
中)有2次机会。对于sshlist
中的任何IP,只剩下一次机会。对于denylist
中的任何IP,任何新的连接都将被拒绝,直到它在5分钟的超时时间内从denylist
中删除。
发布于 2021-10-11 23:26:10
据我所知,iptables的最新模块在nftables中没有对应的模块,但是,您应该能够使用仪表来实现类似的功能。
nft add rule ip filter INPUT tcp dport 22 ct state new meter SSHban { ip saddr and 255.255.255.255 limit rate over 8/minute burst 4 packets } counter drop
文档记录在这里:https://wiki.nftables.org/wiki-nftables/index.php/Meters
https://stackoverflow.com/questions/67765358
复制相似问题