
我正在遭受針對 http 伺服器的 DDOS 攻擊,我嘗試了 iptables 和其他措施,但似乎沒有任何效果。這是access_log的一部分:
有很多連接,但 iptables 不會阻止攻擊,這是我的 iptables 規則:
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -s 147.135.37.113 -j ACCEPT
iptables -A INPUT -f -j DROP
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
iptables -N LOG_AND_DROP
iptables -N PORT21
iptables -A PORT21 -m recent --set --name lp21
iptables -A PORT21 -m recent --update --seconds 30 --hitcount 3 --name lp21 -j DROP
iptables -A PORT21 -m recent --update --seconds 300 --hitcount 10 --name lp21 -j LOG_AND_DROP
iptables -N PORT22
iptables -A PORT22 -m recent --set --name lp22
iptables -A PORT22 -m recent --update --seconds 30 --hitcount 3 --name lp22 -j DROP
iptables -A PORT22 -m recent --update --seconds 300 --hitcount 10 --name lp22 -j LOG_AND_DROP
iptables -N PORT80
iptables -A PORT80 -m recent --set --name lp80
iptables -A PORT80 -m recent --update --seconds 30 --hitcount 20 --name lp80 -j LOG_AND_DROP
iptables -N PORT443
iptables -A PORT443 -m recent --set --name lp433
iptables -A PORT443 -m recent --update --seconds 30 --hitcount 20 --name lp443 -j LOG_AND_DROP
iptables -N PORT10000
iptables -A PORT10000 -m recent --set --name lp10000
iptables -A PORT10000 -m recent --update --seconds 30 --hitcount 20 --name lp10000 -j LOG_AND_DROP
iptables -N PORT6900
iptables -A PORT6900 -m recent --set --name lp6900
iptables -A PORT6900 -m recent --update --seconds 30 --hitcount 10 --name lp6900 -j LOG_AND_DROP
iptables -A PORT6900 -m recent --update --seconds 50 --hitcount 20 --name lp6900 -j LOG_AND_DROP
iptables -N PORT6121
iptables -A PORT6121 -m recent --set --name lp6121
iptables -A PORT6121 -m recent --update --seconds 30 --hitcount 10 --name lp6121 -j LOG_AND_DROP
iptables -A PORT6121 -m recent --update --seconds 50 --hitcount 20 --name lp6121 -j LOG_AND_DROP
iptables -N PORT5121
iptables -A PORT5121 -m recent --set --name lp5121
iptables -A PORT5121 -m recent --update --seconds 30 --hitcount 10 --name lp5121 -j LOG_AND_DROP
iptables -A PORT5121 -m recent --update --seconds 50 --hitcount 20 --name lp5121 -j LOG_AND_DROP
iptables -A INPUT -p icmp --icmp-type echo-request -m hashlimit --hashlimit-name pings --hashlimit-mode srcip --hashlimit 10/min --hashlimit-burst 10 --hashlimit-htable-expire 30000 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 5/min -j LOG --log-prefix "[Pings]"
iptables -A INPUT -p icmp -j DROP
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -m state --state NEW -j PORT21
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j PORT22
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j PORT80
iptables -A INPUT -p tcp --dport 443 -m state --state NEW -j PORT443
iptables -A INPUT -p tcp --dport 10000 -m state --state NEW -j PORT10000
iptables -A INPUT -p tcp --dport 6900 -m state --state NEW -j PORT6900
iptables -A INPUT -p tcp --dport 6121 -m state --state NEW -j PORT6121
iptables -A INPUT -p tcp --dport 5121 -m state --state NEW -j PORT5121
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -m hashlimit --hashlimit-name p80 --hashlimit-mode srcip --hashlimit 50/min --hashlimit-burst 100 --hashlimit-htable-expire 10000 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -m hashlimit --hashlimit-name p443 --hashlimit-mode srcip --hashlimit 50/min --hashlimit-burst 100 --hashlimit-htable-expire 10000 -j ACCEPT
iptables -A INPUT -p tcp --dport 10000 -m hashlimit --hashlimit-name p10000 --hashlimit-mode srcip --hashlimit 50/min --hashlimit-burst 100 --hashlimit-htable-expire 10000 -j ACCEPT
iptables -A INPUT -p tcp --dport 6900 -j ACCEPT
iptables -A INPUT -p tcp --dport 6121 -j ACCEPT
iptables -A INPUT -p tcp --dport 5121 -j ACCEPT
iptables -A LOG_AND_DROP -m limit --limit 10/min -j LOG --log-prefix "[Log]"
iptables -A LOG_AND_DROP -j DROP
#iptables -A INPUT -m limit --limit 10/min -j LOG --log-prefix "[Default]"
iptables -A INPUT -d 147.135.37.113 -j DROP
任何建議都會受到歡迎。我嘗試了一切,但沒有任何效果。
答案1
與您的提供者(看起來像 OVH)討論他們對您系統的反 ddos 保護。並非全部,但大多數信譽良好的提供者都有辦法幫助您解決此問題。除此之外,您可能會考慮執行一個奇特的fail2ban規則,該規則可以監視您的Web伺服器日誌,以查找任何點擊該foro url的用戶端,並自動為它們添加IPTables規則。除此之外,你還可以在那裡放一些東西……也許是一個非常小的 1 位元組檔案?或將您的網頁伺服器置於 cloudflare 或其他 CDN 服務後面。大部分流量可以透過幾種方式處理,並且不會給您的伺服器或您帶來太大的壓力。不過,請注意日誌檔案是否會填滿您的檔案系統!這可能會產生一個沒人願意的其他問題。
查看此內容,以了解有關使用fail2ban 保護 apache 免受 DoS 攻擊的更多資訊:https://apache.tutorials24x7.com/blog/protect-apache-from-brute-force-and-ddos-attacks-using-fail2ban
答案2
用這個簡單的 iptables 解決了問題(也許其他規則太亂了)
iptables -A INPUT -i eno1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eno1 -m state --state INVALID -j DROP
iptables -A INPUT -i eno1 -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT
iptables -A INPUT -i eno1 -p tcp -m tcp --dport 21 -m state --state NEW -j ACCEPT
iptables -A INPUT -i eno1 -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT
iptables -A INPUT -i eno1 -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPT
iptables -A INPUT -i eno1 -p icmp -j ACCEPT
iptables -A INPUT -i eno1 -j DROP
攻擊不斷發生,但伺服器並未崩潰。
答案3
我想到了幾件事:
- 您可以將字串匹配與 iptables 一起使用:
iptables -A INPUT -m string --algo bm --string "GET /foro" -j DROP
或者如果可能的話,在您的系統中使用 TARPIT
iptables -A INPUT -m string --algo bm --string "GET /foro" -j TARPIT
- 不要使用fail2ban,而是製作您自己的腳本,例如掃描日誌的最後 1000 行並阻止與 ipsets 的匹配。將其新增至您的 iptables 腳本:
- ipset 破壞廁所
- ipset -N 廁所 iphash
- ipset沖水馬桶
然後使用腳本將要封鎖的 IP 位址新增到文字檔案中。最終您只需將所有 IP 位址循環到該廁所清單。
- ipset -A 廁所“$ipaddress”
使用 ipset(尤其是遮罩)比阻止 IP 位址更有效。我封鎖了大約 20,000 個位址和大約 200 個網路。
你可能會從我非常非常混亂的腳本中得到一些想法。 https://pastebin.com/4v5se0kh
我知道這很混亂,但它對我來說比fail2ban 更快、更容易。我將該腳本命名為 suc2ban。您只需要找到您自己的日誌檔案匹配項(您可能有 apache access.log,而不是我使用 auth.log 的範例)。