当服务器从某个 IP 接收到一定数量的数据时禁止该 IP

use*_*570 5 firewall denial-of-service linux-networking

我需要的 :

通过每圈时间的请求量添加删除规则有很多结果,但我需要在一段时间内删除来自特定地址的接收字节数。

我调查的内容:

我查看了 iptables :对于第一种情况,我看到了一个专用match。我也看到了配额匹配,但是数据计数是全局跟踪的。
我不知道如何混合这两个规则来跟踪每个 IP 收到的数据。

其他事情 :

我知道跟踪每个 IP 的字节数可能会使用大量内存,这就是为什么我也希望保持较短的时间段。
我可以接受其他方法,只要有详细示例即可。

Ant*_*lov 3

您可以使用带有超时和计数器选项的 IPSET。这看起来像这样:

#create ipset for accounting with default lifetime 300 secs
ipset create IP_QUOTA_SET hash:ip timeout 300 counters

#create separated rule chain
iptables --new-chain PER_IP_QUOTING

#send packets to chain
iptables -t filter -A INPUT \
  -i <in-iface> --dst <ip>  \
  -p tcp --dport <dstport>  \
  -j PER_IP_QUOTING

#if ip doesn't exist in the set, add it
iptables -t filter -A PER_IP_QUOTING    \
  -m set ! --match-set IP_QUOTA_SET src \
  -j SET --add-set IP_QUOTA_SET src --timeout 300

#if packet exists in the set, check bytes
#if byte counter > quota then close connection
#by sending of tcp-reset packet.
iptables -t filter -A PER_IP_QUOTING    \
  -m set --match-set IP_QUOTA_SET src   \
  --bytes-gt 1000 -j REJECT --reject-with tcp-rst

#pass other packets (for debug purpose)
iptables -t filter -A PER_IP_QUOTING \
  -j RETURN
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您可以检查该列表并通过 ipset 命令对其进行编辑。显示带有计数器和超时的当前列表:ipset list IP_QUOTA_SET。

有关详细信息,请阅读文档。

  • 好的 !我想 `--bytes-gr` 应该读作 `--bytes-gt` (2认同)