Fail2ban进阶配置指南:自定义过滤规则、多服务防护与封禁通知实战
Fail2ban通过分析日志文件,自动封禁频繁失败的IP地址,是服务器防暴力破解的标准工具。默认安装只保护SSH,本文带你扩展到Nginx、WordPress后台、Postfix邮件服务等多个场景,并配置封禁通知。
一、Fail2ban核心工作原理
日志文件(如 /var/log/auth.log)
↓
Fail2ban Filter(正则匹配失败行)
↓
达到maxretry次数 → 封禁IP(通过iptables/nftables)
↓
封禁时长到期 → 自动解封二、安装与基础验证
<code"># 安装Fail2ban apt install fail2ban -y systemctl enable fail2ban systemctl start fail2ban # 查看当前封禁状态 fail2ban-client status # 查看SSH jail状态 fail2ban-client status sshd # 手动解封某个IP fail2ban-client set sshd unbanip 1.2.3.4 # 查看Fail2ban日志 tail -f /var/log/fail2ban.log
三、主配置文件(jail.local)
<code"># /etc/fail2ban/jail.local # 注意:不要修改jail.conf,所有自定义写在jail.local中 [DEFAULT] # 全局默认设置 bantime = 3600 # 封禁1小时 findtime = 600 # 10分钟内 maxretry = 5 # 失败5次触发封禁 banaction = iptables-multiport banaction_allports = iptables-allports # IP白名单(永不封禁) ignoreip = 127.0.0.1/8 ::1 你的办公室IP/32 # 发送封禁通知邮件 action = %(action_mwl)s # 封禁 + 发邮件 + 附上日志 # 邮件配置 destemail = admin@yourdomain.com sendername = Fail2ban Alert mta = sendmail [sshd] enabled = true port = 2233 # 你自定义的SSH端口 logpath = /var/log/auth.log maxretry = 3 # SSH更严格,3次就封 bantime = 86400 # 封禁24小时
四、Nginx防护规则
防Nginx HTTP认证暴力破解
<code"># /etc/fail2ban/jail.local 中添加: [nginx-http-auth] enabled = true port = http,https logpath = /var/log/nginx/error.log maxretry = 5
防Nginx 400/444错误(恶意扫描)
<code"># 创建自定义过滤器 # /etc/fail2ban/filter.d/nginx-bad-request.conf [Definition] failregex = ^ .* "(GET|POST|HEAD).*HTTP.*" (400|444) .*$ ignoreregex =
<code"># jail.local中添加: [nginx-bad-request] enabled = true port = http,https filter = nginx-bad-request logpath = /var/log/nginx/access.log maxretry = 10 findtime = 60 bantime = 3600
防Nginx 4xx高频访问(CC攻击检测)
<code"># /etc/fail2ban/filter.d/nginx-cc.conf
[Definition]
# 检测1分钟内同一IP产生大量请求的行为
failregex = ^ -.* "(GET|POST|HEAD|OPTIONS) .*" (4\d{2}|5\d{2}) .*$
ignoreregex = ^ .* "GET /favicon.ico HTTP.*" 404<code"># jail.local中添加: [nginx-cc] enabled = true port = http,https filter = nginx-cc logpath = /var/log/nginx/access.log maxretry = 100 findtime = 60 bantime = 7200
五、WordPress登录防护
<code"># /etc/fail2ban/filter.d/wordpress.conf
[Definition]
# 匹配WordPress登录失败记录(需要WordPress输出登录失败日志)
failregex = ^ .* "POST /wp-login.php HTTP.*" 200 .*$
^ .* "POST .*xmlrpc.php HTTP.*" 200 .*$
ignoreregex =为让WordPress输出登录失败日志,在 functions.php 中添加:
<code">// 记录登录失败到Nginx access日志(通过特定响应体标识)
add_action('wp_login_failed', function($username) {
// 发送自定义HTTP头,供Fail2ban识别
// 注:需要在Nginx中记录此响应头到日志
error_log("WordPress login failed for: $username from: " . $_SERVER['REMOTE_ADDR']);
});<code"># /etc/fail2ban/filter.d/wordpress-login.conf [Definition] # 从PHP/Nginx错误日志中匹配WordPress登录失败 failregex = WordPress login failed for: .* from: ignoreregex =
<code"># jail.local中添加:
[wordpress-login]
enabled = true
port = http,https
filter = wordpress-login
logpath = /var/log/nginx/error.log
/var/log/php8.1-fpm.log
maxretry = 5
findtime = 300
bantime = 3600六、Postfix邮件服务防护
<code"># jail.local中添加邮件服务防护: [postfix-sasl] enabled = true port = smtp,465,submission filter = postfix[mode=auth] logpath = /var/log/mail.log maxretry = 3 bantime = 86400 [dovecot] enabled = true port = pop3,pop3s,imap,imaps,submission,465,sieve filter = dovecot logpath = /var/log/mail.log maxretry = 5
七、自定义封禁通知Action
钉钉通知(替代邮件)
<code"># /etc/fail2ban/action.d/dingtalk.conf
[Definition]
actionban = curl -s -X POST "%(webhook)s" \
-H "Content-Type: application/json" \
-d '{"msgtype":"text","text":{"content":"🚨 Fail2ban封禁告警\n服务:%(name)s\nIP:\n时间:%(now)s\n封禁时长:%(bantime)s秒"}}'
actionunban = curl -s -X POST "%(webhook)s" \
-H "Content-Type: application/json" \
-d '{"msgtype":"text","text":{"content":"✅ Fail2ban解封通知\nIP: 已解封"}}'
[Init]
webhook = https://oapi.dingtalk.com/robot/send?access_token=你的钉钉机器人Token<code"># jail.local中全局或针对特定服务启用:
[DEFAULT]
action = %(action_)s
dingtalk八、常用管理命令速查
<code"># 重载配置(修改配置后执行) fail2ban-client reload # 查看所有jail状态 fail2ban-client status # 查看特定jail的封禁列表 fail2ban-client status nginx-cc # 测试过滤器是否能正确匹配日志 fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-cc.conf # 手动封禁IP fail2ban-client set sshd banip 1.2.3.4 # 手动解封IP fail2ban-client set sshd unbanip 1.2.3.4 # 查看当前所有封禁的IP iptables -L f2b-sshd -n -v
九、总结
Fail2ban配置完善后,能自动拦截SSH暴力破解、WordPress登录爆破、Nginx扫描攻击和邮件暴力破解,大幅降低服务器被入侵风险。配合第09篇的Linux安全加固和第59篇的WordPress安全设置,构建完整的多层防御体系。IDC.Net的香港VPS使用KVM架构,iptables规则完整支持,Fail2ban封禁功能完全可用。
版权声明:
作者:后浪云
链接:https://idc.net/help/442800/
文章版权归作者所有,未经允许请勿转载。
THE END
