linux命令使用netstat和iptables防止dos攻击

Mor*_*yan 11 networking security command-line-interface iptables

我想为每个 ip DROP 200 多个请求以防止 ddos​​ 攻击。这是我用来检测每个 ip 请求数的命令:

netstat -alpn | grep :80 | awk '{print $5}' |awk -F: '{print $(NF-1)}' |sort | uniq -c | sort -nr
Run Code Online (Sandbox Code Playgroud)

现在我想将所有发出超过 200 个请求的 ip 地址添加到 IPtables 中,以便 DROP 输入和输出。

daw*_*wud 18

您可以创建一个ipset. 通过这种方式,您可以根据需要向该集中添加任意数量的 IP,而无需修改iptables规则集。

ipset -N myset iphash
ipset -A myset 1.1.1.1
ipset -A myset 2.2.2.2
Run Code Online (Sandbox Code Playgroud)

或者,在您的情况下,使用脚本的输出,并使用以下内容读取它:

while read a; do ipset -A myset "$a"; done < <(your script here)
Run Code Online (Sandbox Code Playgroud)

并在您的iptables规则中引用它:

iptables -A INPUT -m set --set myset src -j DROP
Run Code Online (Sandbox Code Playgroud)

阅读联机帮助页了解更多详细信息和选项。

还有其他方法可以直接使用减轻DDOS 攻击iptables。阅读iptables有关connlimitrecent模块的联机帮助页部分。


Lin*_*Ops 12

您还可以使用 iptables 来限制传入连接的速率。例如,如果您不希望来自某个源的每分钟连接数超过 200:

iptables -I INPUT -p tcp --dport 80 -m state --state NEW -m recent --set

iptables -I INPUT -p tcp --dport 80 -m state --state NEW -m recent --update --seconds 60 --hitcount 200 -j DROP