fail2ban nginx 404 403 拦截

引言

1087378.png

前些日子入手了1年的阿里云ecs,想着部署一个CentOS,搭建几个测试项目,虽然配置低但是也够用了,因为多端多设备使用,所以我就保留了密码登录,然而web应用加上基本身份认证,也敌不过别人一直穷举破解,所以接入了fail2ban

安装

在这里,我就以centos来说了。

# 安装fail2ban

yum install fail2ban -y 

# 设置开机自启

systemctl enable fail2ban

配置

因为ssh服务已经由fail2ban配置好了,nginx虽然也有,但是更希望定制化一下,所以可以这样操作。

# 切换到filter目录
cd /etc/fail2ban/filter.d/

# 创建一个过滤器规则
vi nginx-custom.conf

解释:GET\POST请求,如果403或者401,都会触发记录

[Definition]
failregex =  ^<HOST>.*"(GET|POST).*" (401|403) .*$
ignoreregex =

到这里,还没有结束,还需要封禁规则

# 切换到filter目录
cd /etc/fail2ban/jail.d/

# 创建一个封禁规则
vi nginx-custom.local
[nginx-custom]
enabled = true
filter = nginx-custom
logpath = /var/log/nginx/*access.log
action = iptables-allports
maxretry = 5
findtime = 30
bantime = 3600

⚠:filter一定要和filter.d中文件名一致
⚠:logpath是nginx的日志路径
⚠:action是触发封禁操作的行为
⚠:maxretry是尝试次数
⚠:findtime是匹配时间间隔
⚠:bantime是封禁时间

常用命令

查看nginx_custom状态

命令:

fail2ban-client set [nginx-custom]

返回值:

Status for the jail: nginx-custom
|- Filter
|  |- Currently failed: 0
|  |- Total failed: 19
|  `- File list:    /var/log/nginx/access.log
`- Actions
   |- Currently banned: 1
   |- Total banned: 2
   `- Banned IP list: 223.*.*.*

解除禁用IP
命令:

fail2ban-client set nginx-custom unbanip 223...*

返回值:

1

评论 0

还没有评论,来说点什么吧。

提交后需要审核才会公开。