CentOS 6 Iptables关于--hitcount数值限制的问题

来源:互联网 发布:老虎证券 知乎 编辑:程序博客网 时间:2024/05/21 18:39

一直知道iptables 很强大,但是所知甚少,今天突然想做下iptables 限制连接数的问题,参照iptables man手册 recent 部分,在iptables 文件中写了两条规则,命令行写法如下:

    iptables -A INPUT -p tcp --dport 80 -m recent --name badguy --seconds 60 --update --hitcount 100 -j DROP    iptables -A INPUT -p tcp --dport 80 -m recent --name badguy --set -j ACCEPT

重启iptables,但是奇妙的事情发生了:
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: iptables-restore: line 17 failed
立刻去查看iptables 文件,发现第17行的内容为 COMMIT!一定是规则有问题,把规则注释掉,再重启,OK了!然后用各种姿势google,无解。查到网友的写法跟我的写法一样一样的,正常工作!WTF的节奏!
继续调整规则,发现把–hitcount 100 去掉后就OK,加上就failed,回头继续看man 手册,关于–hitcount 的描述如下:
This option must be used in conjunction with one of –rcheck or –update. When used, this will narrow the match to only happen when the address is in the list and packets had been received greater han or equal to the given value. This option may be used along with –seconds to create an even narrower match requiring a certain number of hits within a specific time frame. * The maximum value for the hitcount parameter is given by the “ip_pkt_list_tot” parameter of the xt_recent kernel module.Exceeding this value on the command line will cause the rule to be rejected.
粗体这句解释了问题,然后尝试将–hitcount 调低,调到10正常,20正常,大于20,failed 了,好了,问题就在这了,要突破ip_pkt_list_tot 的默认限制了。
google了下ip_pkt_list_tot,发现在/sys/module/xt_recent/parameters目录下,cat 了下ip_pkt_list_tot的值,还真是20,按照以往情况,直接echo 一个值进入就可以了,但是“白手起家”这位网友说不可,要进入到/etc/modprobe.d/ 目录下新建配置文件:
vim xt_recent.conf
options xt_recent ip_list_tot=1024 ip_pkt_list_tot=200
备注:(原文的文件名中没有.conf,试验后有警告提示:WARNING: All config files need .conf: /etc/modprobe.d/xt_recent, it will be ignored in a future release. 因此正确的写法应该加上.conf)
保存退出,调大–hitcount 的值为200,重启iptables ,OK!
再去cat ip_pkt_list_tot 的值,也变成了200。

搞定,收工!

0 0
原创粉丝点击