使用 Fail2ban 防御 CC 攻击(Nginx Proxy Manager 实战教程)
CC(Challenge Collapsar)攻击本质上就是大量“看起来正常”的 HTTP 请求,短时间内把服务器带宽、CPU、连接数打满。相比 DDoS 的流量洪水,CC 更隐蔽,也更常见,尤其是 VPS 和小型网站。这篇文章用 **Fail2ban + Nginx Proxy Manager 日志分析 + iptables**,实现一套轻量级自动封禁方案。

安装 Fail2ban
在 Debian / Ubuntu 上安装并启动 Fail2ban:
sudo apt update
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban --no-pager
安装完成后,Fail2ban 会在后台运行,并且默认自带 SSH 防护(sshd jail)。
🔍 创建 Nginx Proxy Manager 日志过滤器
Fail2ban 的核心就是“读日志 + 正则匹配 + 封 IP”。
我们先创建一个专门解析 Nginx Proxy Manager 日志的 filter。
vi /etc/fail2ban/filter.d/nginx-proxy-manager.conf
写入内容:
[Definition]
# Nginx Proxy Manager 日志过滤器
# 日志格式: [时间] - 状态码 - 方法 协议 域名 "路径" [Client IP]
# 匹配客户端 IP(关键)
failregex = \[Client <HOST>\]
# 忽略规则(可选)
ignoreregex =
# 时间格式
datepattern = ^\[%d/%b/%Y:%H:%M:%S %z\]
👉 核心点:
<HOST>= Fail2ban 自动识别 IP- failregex 决定“什么行为算攻击”
🚨 创建 Jail 规则(核心防御逻辑)
接下来定义 CC 防御规则。
vi /etc/fail2ban/jail.d/nginx-proxy-manager.conf
写入:
[nginx-proxy-manager]
enabled = true
filter = nginx-proxy-manager
# NPM 日志路径(按实际修改)
logpath = proxy-host-15_access.log
backend = pyinotify
# 1分钟内最多请求数
maxretry = 1000
# 统计时间窗口(60秒)
findtime = 60
# 封禁时间(72小时)
bantime = 259200
# 使用自定义 iptables 动作
action = npm-iptables
# 白名单 IP
ignoreip = 127.0.0.1/8 ::1
192.168.0.0/16
10.0.0.0/8
🔥 创建 iptables 封禁动作
Fail2ban 默认也支持 iptables,但这里我们自定义更直观。
vi /etc/fail2ban/action.d/npm-iptables.conf
写入:
[Definition]
actionstart =
actionstop =
actioncheck =
# 封禁 IP
actionban = iptables -I INPUT 1 -s <ip> -j DROP
iptables -I FORWARD 1 -s <ip> -j DROP
# 解封 IP
actionunban = iptables -D INPUT -s <ip> -j DROP
iptables -D FORWARD -s <ip> -j DROP
[Init]
👉 逻辑说明:
- DROP = 直接丢弃请求(不返回任何响应)
- INPUT / FORWARD 双链封锁更彻底
🧪 测试 Fail2ban 配置
1️⃣ 测试过滤器是否生效
sudo fail2ban-regex proxy-host-15_access.log /etc/fail2ban/filter.d/nginx-proxy-manager.conf
如果匹配成功,会看到:
- matched lines
- detected IP
2️⃣ 检查 jail 配置
sudo fail2ban-client -d
查看所有 jail:
sudo fail2ban-client status
🚀 启动 Fail2ban
sudo systemctl restart fail2ban
sudo systemctl status fail2ban
🛠 常用管理命令
🔒 手动封 IP
sudo fail2ban-client set nginx-proxy-manager banip 1.2.3.4
🔓 手动解封 IP
sudo fail2ban-client set nginx-proxy-manager unbanip 1.2.3.4
📋 查看封禁列表
sudo fail2ban-client status nginx-proxy-manager
🔄 重新加载配置
sudo fail2ban-client reload
📊 监控与日志分析
查看 Fail2ban 日志
sudo tail -f /var/log/fail2ban.log
查看封禁记录
sudo grep "Ban" /var/log/fail2ban.log | tail -20
查看解封记录
sudo grep "Unban" /var/log/fail2ban.log | tail -20
📡 查看 iptables 防火墙规则
sudo iptables -L INPUT -n -v --line-numbers
sudo iptables -L FORWARD -n -v --line-numbers
📘 Nginx Access 日志分析(CC 攻击排查)
🔥 统计访问最频繁 IP
cat access.log | awk '{print $12}' | sort | uniq -c | sort -nr | more
🔥 查看某 IP 访问路径
cat access.log | grep '75.221.79.53' | awk '{print $10}' | sort | uniq -c | sort -nr
🔥 SQL 注入扫描行为
cat access.log | awk '/select/{print $1}' | sort | uniq -c | sort -nr
cat access.log | awk '/\/and\//||/\+and\+/||/%20and%20and/{print $1}' | sort -nr
cat access.log | awk '/sleep/{print $1}' | sort -nr
🧠 CC 攻击防御思路总结
Fail2ban 的核心逻辑就是:
“谁在短时间内请求太多 → 直接封 IP”
适用于:
- VPS 小网站
- Nginx / Proxy Manager
- API 服务
- WordPress / Node.js 站点
⚔️ 进阶优化建议
如果你想更强一点,可以加:
✔ 限流(Nginx)
limit_req_zone $binary_remote_addr zone=cc:10m rate=10r/s;
✔ Cloudflare WAF
- JS Challenge
- Rate Limit Rules
✔ Fail2ban + Cloudflare API(高级)
自动封 IP 到 Cloudflare 层
🧪 简易端口扫描脚本(辅助防护)
import socket
import termcolor
def scan(target, ports):
print('\nStarting Scan For ' + str(target))
for port in range(1, ports):
scan_port(target, port)
def scan_port(ipaddress, port):
try:
sock = socket.socket()
sock.connect((ipaddress, port))
print("[+] Port Opened " + str(port))
sock.close()
except:
pass
targets = input("Enter Targets (split by ,): ")
ports = int(input("Scan Port Range: "))
if ',' in targets:
for ip_addr in targets.split(','):
scan(ip_addr.strip(), ports)
else:
scan(targets, ports)
📌 结论
Fail2ban 并不是“防一切攻击”的工具,但在 VPS 场景里,它是:
💡 成本最低、效果最直接的 CC 防御方案之一
配合 Nginx + Cloudflare 使用,可以覆盖 80% 常见小流量攻击。


