如何使iptables规则过期?

有人告诉我这是可能的,但我无法find谷歌或手册页上的任何东西。

我需要禁止一段时间的IP,然后自动解除绑定。

如果你的意思是让iptables彻底删除规则本身,你将无法做到,据我所知。 这是什么目的? 如果您需要某种自动临时禁用的标准解决scheme是fail2ban 。

或者,您可以使用cron作业来删除您添加的规则,或者,如果您想以交互方式执行,则更好:

 iptables -I INPUT -s 192.168.1.100 -j DROP echo "iptables -D INPUT -s 192.168.1.100 -j DROP" | at @10pm 

还要看看recent的iptables模块。 这与它的 – --seconds选项可能会有所帮助,根据您的实际需要。 man iptables的更多信息。

在规则中加上时间戳(可能是秒数以后的秒数)。 定期扫除过期的规则。

请注意,最新的Linux内核支持将IP地址dynamic加载到由iptable规则查询的caching中,而不是作为直接的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"; }' 

你当然可以用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 

这有助于通过将来自同一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 

在上面,我们创造了2条链; “ssh”和“black”,以及2个列表; “定时器”和“黑名单”。

简单地说, 上面显示的最后一条链是进入ssh链的“门道”。

  • 规则1在ssh链中检查源IP是否在列表“黑名单”中。 如果是这样,连接将被丢弃,24小时黑名单计时器将重新启动。 如果规则1是错误的,那么我们去规则2。
  • 规则2在ssh链中检查源IP是否在5分钟内做了4次以上的连接尝试。 如果是这样,它将数据包发送到“黑色”链,并将其添加到列表“黑名单”中。 链接“黑色”,然后丢弃连接,我们完成了。
  • 只有在规则1和规则2是假时才能达到链“ssh”中的规则3。 如果是这样,数据包被接受,源IP被添加到列表“计时器”,所以我们可以监视连接尝试频率。

“–reap”选项告诉内核search整个列表并清除超过设置时间限制的任何项目; 列表“定时器”5分钟,列表“黑名单”24小时。

注意:额外的空格是为了便于阅读,在shell脚本中是可选的。

IPTables有一个明确的function:IP集。 你做规则一次,它像往常一样坚持,但它检查一组ips(或端口)的匹配。 很酷的事情是,这个集合可以dynamic和有效地更新而不会干扰其他的防火墙。

主要网站 , 例子 。

所以,要使用它,您仍然必须使用atcron来安排移除。

您可以使用fail2ban来禁止ip地址,并configuration一个地址将被禁止的时间长度。

正如有人已经说过:你应该使用ipset这个function。

ipset可以添加具有超时值的ip地址。 超时结束后,logging将自动从ipset中删除。

timeout All set types supports the optional timeout parameter when creating a set and adding entries. The value of the timeout parameter for the create command means the default timeout value (in seconds) for new entries. If a set is created with timeout support, then the same timeout option can be used to specify non-default timeout values when adding entries. Zero timeout value means the entry is added permanent to the set. The timeout value of already added elements can be changed by readding the element using the -exist option. Example: ipset create test hash:ip timeout 300 ipset add test 192.168.0.1 timeout 60 ipset -exist add test 192.168.0.1 timeout 600

http://ipset.netfilter.org/ipset.man.html

这是控制这种行为的最好方法。

我需要禁止一段时间的IP,然后自动解除绑定。

你可以尝试下面的一个

 # iptables -I INTPUT -s xxx.xxx.xxx.xxx -m time --utc --datestart 2013-09-09T15:00 --datestop 2013-09-09T15:30 -j DROP 

取决于你想要完成的是netfilter的近期还是时间模块可以用来完成这个。

两者都logging在iptables手册页中 。