WordPress 安全防护 – 防 CC 攻击和恶意爬虫完整实战

作为一个运维过数十个 WordPress 网站的人,我深知 CC 攻击和恶意爬虫的困扰。去年我的一个网站遭受 CC 攻击,峰值 QPS 达到 3000+,如果没有提前配置防护,早就瘫痪了。今天把我压箱底的防护方案分享出来。

一、先说说我遇到的攻击类型

1. CC 攻击(Challenge Collapsar)

攻击者模拟大量用户请求,消耗服务器资源。特征:请求频率极高、集中在某个 URL、User-Agent 异常。

2. 暴力破解

针对 wp-login.php 和 xmlrpc.php 的密码爆破,一分钟可达上千次尝试。

3. 恶意爬虫

采集内容、扫描漏洞、爬取邮箱等敏感信息。

4. SQL 注入和 XSS

尝试通过插件漏洞注入恶意代码。

二、Nginx 层防护(第一道防线)

步骤 1:限制请求频率

在 nginx.conf 的 http 段添加:

limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=login:10m rate=1r/m;

# 在 server 段应用:
location / {
    limit_req zone=one burst=20 nodelay;
}

location ~ /wp-login {
    limit_req zone=login burst=5;
}

步骤 2:封禁恶意 User-Agent

if ($http_user_agent ~* (curl|wget|python|scanner)) {
    return 403;
}

步骤 3:限制单个 IP 并发连接数

limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn addr 10;

这个配置在实际应用中救了我多次,攻击者单 IP 并发超过 10 个连接直接被拒。

三、Fail2ban 实战配置

步骤 1:安装

apt install fail2ban -y

步骤 2:创建 WordPress 专用配置

cat > /etc/fail2ban/jail.d/wordpress.conf << 'EOF'
[wordpress]
enabled = true
port = http,https
filter = wordpress
logpath = /var/www/*/logs/access.log
maxretry = 100
findtime = 60
bantime = 3600

[wordpress-login]
enabled = true
port = http,https
filter = wordpress-login
logpath = /var/www/*/logs/access.log
maxretry = 3
findtime = 300
bantime = 86400
EOF

步骤 3:创建过滤规则

cat > /etc/fail2ban/filter.d/wordpress-login.conf << 'EOF'
[Definition]
failregex = ^.*"POST /wp-login.php
            ^.*"POST /xmlrpc.php
ignoreregex =
EOF

步骤 4:启动并验证

systemctl restart fail2ban
fail2ban-client status wordpress-login

四、Cloudflare CDN 防护(强烈推荐)

免费版 Cloudflare 就能提供很好的防护:

1. 开启 Under Attack 模式

遭受攻击时一键开启,所有访问者需要验证。

2. 配置防火墙规则

规则示例:
- 如果请求 URI 包含 /wp-admin 且国家不是中国,则质询
- 如果 User-Agent 为空或包含可疑关键词,则阻止
- 如果 10 分钟内请求超过 100 次,则质询

3. 启用 Bot Fight Mode

自动检测和阻止恶意机器人。

五、WordPress 安全插件推荐

1. Wordfence Security

功能:防火墙、恶意软件扫描、登录安全、实时流量监控。

2. iThemes Security

功能:隐藏登录页、双因素认证、文件完整性检查。

3. WPS Limit Login Attempts

轻量级,专门防止暴力破解。

六、我踩过的坑和经验

坑 1:过度防护误伤正常用户

曾经我把请求频率限制设为 5r/s,结果 SEO 爬虫和正常用户都被误伤。建议:初始设置宽松些,根据日志调整。

坑 2:只防护了 HTTP 忽略 HTTPS

确保 Nginx 的 443 端口也应用相同规则。

坑 3:忘记更新规则

攻击手段在不断变化,定期检查日志更新规则。

七、应急响应流程

当发现攻击时,按这个流程处理:

1. 快速定位

# 查看实时访问日志
tail -f /var/www/logs/access.log | grep -E "POST|wp-login"

# 统计 IP 请求排名
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -20

2. 临时封禁

# 使用 iptables 快速封禁
iptables -I INPUT -s 攻击 IP -j DROP

3. 切换到 CDN 防护模式

Cloudflare 开启"Under Attack"模式。

4. 分析日志找出攻击特征

5. 更新防护规则

八、日常监控建议

# 创建监控脚本
#!/bin/bash
LOG="/var/www/logs/access.log"
ALERT_EMAIL="[email protected]"

# 检测单 IP 1 分钟内请求超过 500 次
awk -v threshold=500 '{count[$1]++} END {for(ip in count) if(count[ip]>threshold) print ip, count[ip]}' $LOG

# 检测大量 404 请求(扫描特征)
grep "404" $LOG | awk '{print $1}' | sort | uniq -c | sort -rn | head -10

总结

安全防护是层层叠加的过程,不要指望单一方案解决所有问题。我的建议是:

1. Nginx 层做基础限流(性能最好)

2. Fail2ban 自动封禁异常 IP(自动化)

3. Cloudflare CDN 抗大流量攻击(扛住峰值)

4. WordPress 安全插件补充防护(应用层)

5. 定期查看日志,主动发现问题

6. 保持系统和插件更新

安全没有终点,保持警惕才能长治久安。

来源:https://mjj.728.hk/


已发布

分类

来自

标签: