DDoS(分布式拒绝服务)攻击通过大量恶意流量淹没服务器,导致正常用户无法访问。搬瓦工VPS作为单机服务器,防御能力有限,但通过Cloudflare代理、Nginx限速和内核加固等多层防御策略,可以有效抵御中小规模的攻击。
Tip: 对于大规模DDoS攻击(超过10Gbps),单靠VPS端的配置无法防御,必须使用Cloudflare等CDN服务在网络边缘吸收攻击流量。
Cloudflare是最有效的DDoS防护方案,免费版即可提供基本的DDoS防护。Cloudflare作为反向代理,所有流量先经过其全球节点过滤,恶意流量不会到达你的VPS。
接入步骤:
Cloudflare安全设置建议:
使用Cloudflare后,最重要的是隐藏VPS的真实IP地址。如果攻击者知道真实IP,可以绕过Cloudflare直接攻击:
# Nginx仅允许Cloudflare IP访问(拒绝直接IP访问)
# 创建Cloudflare IP白名单
cat > /etc/nginx/conf.d/cloudflare-ips.conf << 'EOF'
# Cloudflare IPv4
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
real_ip_header CF-Connecting-IP;
EOF
# 拒绝非Cloudflare IP直接访问
# 在server块中添加:
# if ($http_cf_connecting_ip = "") {
# return 403;
# }
同时通过防火墙限制HTTP/HTTPS端口只接受Cloudflare的IP:
# 使用UFW限制80/443端口
# 先删除原有的开放规则
ufw delete allow 80/tcp
ufw delete allow 443/tcp
# 仅允许Cloudflare IP段访问
ufw allow from 173.245.48.0/20 to any port 80,443 proto tcp
ufw allow from 103.21.244.0/22 to any port 80,443 proto tcp
ufw allow from 103.22.200.0/22 to any port 80,443 proto tcp
ufw allow from 103.31.4.0/22 to any port 80,443 proto tcp
ufw allow from 141.101.64.0/18 to any port 80,443 proto tcp
ufw allow from 108.162.192.0/18 to any port 80,443 proto tcp
ufw allow from 190.93.240.0/20 to any port 80,443 proto tcp
ufw allow from 188.114.96.0/20 to any port 80,443 proto tcp
ufw allow from 197.234.240.0/22 to any port 80,443 proto tcp
ufw allow from 198.41.128.0/17 to any port 80,443 proto tcp
ufw allow from 162.158.0.0/15 to any port 80,443 proto tcp
ufw allow from 104.16.0.0/13 to any port 80,443 proto tcp
ufw allow from 172.64.0.0/13 to any port 80,443 proto tcp
Nginx内置的限速模块可以有效防御CC攻击(HTTP层的DDoS):
# 在nginx.conf的http段添加限速区域定义
http {
# 限制请求频率:每个IP每秒10个请求
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
# 限制并发连接数:每个IP最多50个连接
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
server {
# 应用请求频率限制
limit_req zone=req_limit burst=20 nodelay;
# 应用并发连接限制
limit_conn conn_limit 50;
# 限速返回状态码
limit_req_status 429;
limit_conn_status 429;
# 对特定路径加强限制
location /wp-login.php {
limit_req zone=req_limit burst=3 nodelay;
}
location /xmlrpc.php {
deny all;
return 403;
}
}
}
SYN Flood是最常见的网络层DDoS攻击,通过大量SYN包耗尽服务器连接资源:
# 内核参数防护SYN Flood
cat >> /etc/sysctl.d/99-ddos-protection.conf << 'EOF'
# 启用SYN Cookie(核心防护)
net.ipv4.tcp_syncookies = 1
# 减少SYN-ACK重试次数
net.ipv4.tcp_synack_retries = 2
# 增大SYN队列
net.ipv4.tcp_max_syn_backlog = 65535
# 加速TIME_WAIT回收
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 10
# 减少孤儿连接
net.ipv4.tcp_max_orphans = 65535
# 增大连接跟踪表
net.netfilter.nf_conntrack_max = 131072
net.netfilter.nf_conntrack_tcp_timeout_established = 300
# 增大本地端口范围
net.ipv4.ip_local_port_range = 1024 65535
EOF
sysctl -p /etc/sysctl.d/99-ddos-protection.conf
# 限制每IP每秒新连接数
iptables -A INPUT -p tcp --syn -m limit --limit 50/s --limit-burst 100 -j ACCEPT
iptables -A INPUT -p tcp --syn -j DROP
# 限制单IP并发连接数
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 100 -j REJECT
iptables -A INPUT -p tcp --dport 443 -m connlimit --connlimit-above 100 -j REJECT
# 丢弃无效数据包
iptables -A INPUT -m state --state INVALID -j DROP
# 丢弃碎片包
iptables -A INPUT -f -j DROP
# 限制ICMP速率(防Ping Flood)
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 4 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
# 丢弃异常TCP标志组合
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
# 保存规则
netfilter-persistent save
当VPS正在遭受DDoS攻击时的应急处理步骤:
# 1. 查看当前连接状态
ss -ant | awk '{print $1}' | sort | uniq -c | sort -rn
# 2. 查看连接最多的IP
ss -ant | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -20
# 3. 临时封禁攻击IP
iptables -A INPUT -s 攻击IP -j DROP
# 4. 批量封禁(连接数超过100的IP)
ss -ant | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | \
awk '$1 > 100 {print $2}' | while read ip; do
iptables -A INPUT -s $ip -j DROP
echo "Blocked: $ip"
done
# 5. 如果使用Cloudflare,开启Under Attack Mode
# 在Cloudflare控制台 > Security > Under Attack Mode
# 6. 查看网络流量
iftop -i eth0
nload eth0
# 7. 查看Nginx错误日志
tail -100 /var/log/nginx/error.log
如果VPS不需要UDP服务,可以直接在防火墙中拒绝UDP流量:
# 仅允许DNS查询的UDP(如果VPS需要解析域名)
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
# 拒绝其他所有UDP入站
iptables -A INPUT -p udp -j DROP
# 使用UFW
ufw deny proto udp from any to any
| 防御层 | 工具/方案 | 防御类型 |
|---|---|---|
| 网络边缘 | Cloudflare CDN | 所有类型DDoS + WAF |
| 网络层 | iptables规则 | SYN Flood、UDP Flood |
| 内核层 | sysctl参数 | SYN Cookie、连接管理 |
| 应用层 | Nginx限速 | CC攻击、HTTP Flood |
| 应用层 | Fail2ban | 暴力破解、恶意扫描 |
防DDoS是一个系统工程,建议至少做到:接入Cloudflare + 隐藏真实IP + 内核参数加固。相关教程:防火墙配置 | 内核参数优化 | Fail2ban安装 | VPS安全加固
Tip: 更多教程请查看新手教程。