Hap*_*per 22 linux iptables centos
有人告诉我这是可能的,但我在谷歌或手册页上找不到任何内容。
我需要在一定时间内禁止 IP,然后自动取消禁止。
Edu*_*nec 18
如果您的意思是让 iptables 自己完全删除规则,据我所知,您将无法做到。这样做的目的是什么?如果您需要某种自动临时禁止,标准解决方案是fail2ban。
或者,您可以使用 cron 作业删除您正在添加的规则,或者,如果您想以交互方式执行此操作,则最好使用一个at作业:
iptables -I INPUT -s 192.168.1.100 -j DROP
echo "iptables -D INPUT -s 192.168.1.100 -j DROP" | at @10pm
Run Code Online (Sandbox Code Playgroud)
另外看看recentiptables的模块。这及其--seconds选项可能会有所帮助,具体取决于您的实际需要。man iptables想要查询更多的信息。
Set*_*son 13
在规则中添加带有时间戳(可能是自纪元以来的秒数)的评论。定期清除过期规则。
请注意,最新的 linux 内核支持将 IP 地址动态加载到由 iptable 规则而不是直接 iptables 规则查询的缓存中。
例子:
iptables -A INPUT -s 192.168.200.100/32 -m comment --comment "expire=`date -d '+ 5 min' +%s`" -j DROP
iptables -L INPUT -n --line-numbers | tac | perl -ne 'next unless /(^\d+).*expire=(\d+)/; if ($2 < time) { print "iptables -D INPUT $1\n"; }'
Run Code Online (Sandbox Code Playgroud)
您当然可以iptables -D INPUT $1代替打印命令。
如果满足用户定义的条件,iptables 有一种方法可以自动将 IP 地址添加到列表中。我使用以下方法来帮助避免对我的 ssh 端口的自动黑客攻击:
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --name ssh --seconds 60 --reap -j DROP
Run Code Online (Sandbox Code Playgroud)
通过将来自同一 IP 地址的连接尝试限制为每 60 秒一次,这有助于限制自动尝试访问服务器。
如果您想在一个时间范围内允许一定数量的尝试,例如 5 分钟内 4 次,并且在失败时将它们列入黑名单更长的时间,例如 24 小时,您可以执行以下操作:
iptables -X black
iptables -N black
iptables -A black -m recent --set --name blacklist -j DROP
iptables -X ssh
iptables -N ssh
iptables -I ssh 1 -m recent --update --name blacklist --reap --seconds 86400 -j DROP
iptables -I ssh 2 -m recent --update --name timer --reap --seconds 600 --hitcount 4 -j black
iptables -I ssh 3 -m recent --set --name timer -j ACCEPT
iptables -A INPUT -p TCP --dport ssh -m state --state NEW -j ssh
Run Code Online (Sandbox Code Playgroud)
在上面,我们创建了 2 个链;“ssh”和“black”,以及2个列表;“计时器”和“黑名单”。
简要地; 上面显示的最后一条链是进入 ssh 链的“门口”。
“--reap”选项告诉内核搜索列表并清除任何早于设置时间限制的项目;列表“计时器”为 5 分钟,列表“黑名单”为 24 小时。
注意:额外的空格是为了便于阅读,在您的 shell 脚本中是可选的。