linux命令,通过使用netstat和iptables来防止dos攻击

我想要每个ip请求超过200个请求,以防止ddos攻击。 这是我用来检测每个IP请求计数的命令:

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

现在我想添加所有IP地址,使超过200个请求到IPtables的DROPinput和输出。

您也可以使用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

你可以创build一个ipset 。 这样,您可以根据需要添加尽可能多的IP,而无需修改iptables规则集。

 ipset -N myset iphash ipset -A myset 1.1.1.1 ipset -A myset 2.2.2.2 

或者,在你的情况下,使用你的脚本的输出,并阅读它的东西:

 while read a; do ipset -A myset "$a"; done < <(your script here) 

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

 iptables -A INPUT -m set --set myset src -j DROP 

阅读手册页获取更多细节和选项。

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