ipset不适用于iptables

我试图过滤一个国家,不断探测我的SMTP服务器(CentOS6),我似乎无法让ipset工作在iptables正确。

我从ipdeny.com下载了这些国家的IP地址,并将其作为文本文件安装。 最初,我把所有的黑名单IP地址都放在一个很长的iptables链中,但是这样做可能会对CPU造成负面影响 – 所以我想使用ipset。

以下是该IP地址文件的摘录:

185.40.4.31 80.82.65.237 2.60.0.0/14 

所以现在我试图在ipset集中使用该列表。 我validationipset设置使用'ipset列表'填充。

 Name: blacklist Type: hash:net Header: family inet hashsize 2048 maxelem 65536 Size in memory: 108816 References: 1 Members: .... 185.40.4.31 185.40.152.0/22 ... 

有了这个ipset,我把它添加到iptables:

 iptables -A INPUT -p tcp -m set --set blacklist src -j DROP 

但是当我尝试使用hping3来testing这个集合时,这些包仍然会通过。

 hping3 --syn --destport 25 --count 3 -a 185.40.4.31 <server_ip> 

当我使用长长的iptables链,事情按预期工作。

这是iptables -L -n的缩写输出(我编辑了大部分6200+ ipdeny条目)

 Chain INPUT (policy DROP) target prot opt source destination DROP all -- 217.199.240.0/20 0.0.0.0/0 DROP all -- 217.199.208.0/20 0.0.0.0/0 ... DROP all -- 2.60.0.0/14 0.0.0.0/0 DROP all -- 94.102.50.41 0.0.0.0/0 DROP all -- 80.82.65.237 0.0.0.0/0 DROP all -- 185.40.4.31 0.0.0.0/0 ACCEPT all -- 192.168.2.0/24 0.0.0.0/0 ACCEPT all -- 192.168.1.0/24 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x17/0x02 state NEW ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:27944 state NEW ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21 state NEW ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:53 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 state NEW ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 state NEW ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:587 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:993 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:995 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:143 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:27940 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:110 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 8 LOG all -- 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 DROP all -- 0.0.0.0/0 0.0.0.0/0 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 match-set blacklist src Chain FORWARD (policy DROP) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 

您的规则永远不会生效,因为您已将其添加到链的末尾。 紧接着是一个规则,放弃所有的stream量,因此你的规则永远不会达到。 在iptables中,规则是按顺序匹配的; 这与许多其他防火墙不同。

要解决此问题,请将规则移至链中较早的位置。 如果你真的想把这些地址列入黑名单,应该尽早在链条上,例如第一条规则。