CC 攻擊模擬大量使用者不斷請求頁面,耗盡伺服器資源。本文整理一組實用的 netstat、tcpdump 指令,快速分析 80 埠連線分佈、識別異常 IP,並提供 iptables 封鎖範例。
netstat -nat | grep -i "80" | wc -l
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -rn
netstat -anlp | grep 80 | grep tcp | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -n20
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr | head -20
netstat -n | grep TIME_WAIT | awk '{print $5}' | sort | uniq -c | sort -rn | head -n20
netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
# 封單一 IP
iptables -I INPUT -s 211.1.0.0 -j DROP
# 封 IP 段 /16
iptables -I INPUT -s 211.1.0.0/16 -j DROP
# 封整個 A 類 /8
iptables -I INPUT -s 211.0.0.0/8 -j DROP
# 封多個 C 類
iptables -I INPUT -s 61.37.80.0/24 -j DROP
iptables -I INPUT -s 61.37.81.0/24 -j DROP
# 方法 1:寫入 rc.local
iptables-save > /etc/sysconfig/iptables
# 方法 2:service 儲存
service iptables save
iptables -D INPUT -s IP地址 -j REJECT
iptables -F # 清空所有規則(慎用)
結合 fail2ban 或 nginx limit_req 模組自動化防禦,單純靠人工指令難以應對持續攻擊。
