我需要的 :

我需要的 :

我需要的 :

透過每圈的請求量添加丟棄規則有很多結果,但我需要根據一段時間內從特定位址接收的位元組計數來丟棄。

我調查的內容:

我查看了 iptables :對於第一種情況,我看到了一個專用的匹配。我還看到了配額匹配但是,數據計數是在全球範圍內追蹤的。
我不知道如何混合這兩個規則來追蹤每個 IP 接收到的資料。

其他事情 :

我知道追蹤每個 IP 的位元組數可能會使用大量內存,這就是為什麼我也想保持較短的時間段。
我可以接受其他方法,只要有詳細的例子。

答案1

您可以使用具有逾時和計數器選項的 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

在這種情況下,您可以檢查該清單並透過 ipset 命令對其進行編輯。顯示帶有計數器和逾時的目前清單:ipset list IP_QUOTA_SET。

有關詳細信息,請閱讀文件。

相關內容