fail2ban防止暴力破解-防止nginx服务器web目录被黑客扫描

版权声明:本文为博主原创文章,如需转载请在显眼的地方注明源地址。 https://blog.csdn.net/Dorisnzy/article/details/82926067

1. 背景

刚买了阿里云服务器,准备用来部署自己的一些站点。结果刚把lnmp环境搭建好,才一天的时间就被来自不同地域IP不断的扫描web站点目录,这运气怕是没几个人能遇到了,幸好之前有熟悉过防止暴力破解fail2ban服务。下面就来介绍一下这款服务软件。写这篇博客参加以下文章:

http://www.361way.com/fail2ban-nginx/1825.html  参考-匹配RUL规则。

https://www.cnblogs.com/wangxiaoqiangs/p/5630325.html 参考-安装教程

https://blog.csdn.net/shuchengzhang/article/details/50931123 参考-相关配置文件

https://blog.csdn.net/w958660278/article/details/80592928  参考-fail2ban从黑名单中移除IP

http://www.zsythink.net/archives/1199 参考-iptables配置规则

http://www.fail2ban.org/wiki/index.php/Main_Page 参考-官网文档

2. fail2ban的简单介绍

Fail2ban 能够监控系统日志,匹配日志中的错误信息(使用正则表达式),执行相应的屏蔽动作(支持多种,一般为调用 iptables ),是一款很实用、强大的软件。

如:攻击者不断尝试穷举 SSH 、SMTP 、FTP 密码等,只要达到预设值,fail2ban 就会调用防火墙屏蔽此 IP ,并且可以发送邮件通知系统管理员。

功能、特性:

1、支持大量服务:sshd 、apache 、qmail 等
2、支持多作动作:iptables 、tcp-wrapper 、shorewall 、mail notifications 等
3、logpath 选项中支持通配符
4、需要 Gamin 支持(Gamin 用于监控文件和目录是否更改)
5、如果需要邮件通知,则系统事先要确保能够正常发送邮件

扫描二维码关注公众号,回复: 3614592 查看本文章

3. fail2ban的安装、配置文件介绍

3.1 安装

# 安装epel-release
yum -y install epel-release
# 安装fail2ban
yum -y install fail2ban

3.2 文件目录结构

/etc/fail2ban               ## fail2ban 服务配置目录
/etc/fail2ban/action.d      ## iptables 、mail 等动作文件目录
/etc/fail2ban/filter.d      ## 条件匹配文件目录,过滤日志关键内容
/etc/fail2ban/jail.conf     ## fail2ban 防护配置文件
/etc/fail2ban/fail2ban.conf ## fail2ban 配置文件,定义日志级别、日志、sock 文件位置等

4. fail2ban的配置文件介绍

4.1 fail2ban.conf配置文件

shell > grep -v ^# /etc/fail2ban/fail2ban.conf

[Definition]
loglevel = 3                              ## 定义日志级别,默认
logtarget = /var/log/fail2ban.log         ## 定义 fail2ban 日志文件
socket = /var/run/fail2ban/fail2ban.sock  ## sock 文件存放位置,默认
pidfile = /var/run/fail2ban/fail2ban.pid  ## pid 文件存放位置,默认

4.2 jail.conf 防护配置

shell > grep -v ^# /etc/fail2ban/jail.conf
[DEFAULT]                         ## 全局设置,优先级最小
ignoreip = 127.0.0.1/8            ## 不受限制的 IP ,多组用空格分割
bantime = 600                     ## 非法 IP 被屏蔽时间(秒),-1 代表永远封锁
findtime = 600                    ## 设置多长时间(秒)内超过 maxretry 限制次数即被封锁
maxretry = 3                      ## 最大尝试次数
backend = auto                    ## 日志修改检测机制(gamin 、polling 、auto 三种)
usedns = warn
[ssh-iptables]                    ## 分类设置(基于 SSHD 服务的防护)
enabled = true                    ## 是否开启防护,false 为关闭
filter = sshd                     ## 过滤规则 filter 名称,对应 filter.d 目录下的 sshd.conf
action = iptables[name=SSH, port=ssh, protocol=tcp] ## 动作参数 
logpath = /var/log/secure         ## 检测系统登陆日志文件
maxretry = 5                      ## 最大尝试次数

更多项………………

4.3 配置防止nginx服务器web目录被黑客扫描

 在jail.conf文件末尾加以下内容

shell > vim /etc/fail2ban/jail.conf
[nginx]
enabled = true
port = http,https
filter = nginx
action = iptables[name=nginx, port=http, protocol=tcp]
logpath = /www/lnmp/log/nginx/access.log
bantime = 3600
findtime = 60
maxretry = 5

在etc/fail2ban/filter.d目录下新增nginx.conf文件并追加以下内容:

shell > vim /etc/fail2ban/filter.d/nginx.conf

[Definition]
failregex = <HOST> -.*- .*HTTP/1.* 404 .*$
ignoreregex =

5. fail2ban跳过的坑

5.1 fail2ban无法启动起来

检查配置文件格式是否正确

fail2ban-regex /www/lnmp/log/nginx/access.log /etc/fail2ban/filter.d/nginx.conf

根据启动信息查询启动原因,入坑(nginx日志文件路径配置错误)

fail2ban-client start

5.2 如何删除fail2ban中已被禁用的黑名单IP

fail2ban-配置

格式:
fail2ban-client set 服务规则 unbanip IP地址
例子:
fail2ban-client set nginx unbanip 8.8.8.8

iptaables 删除对应的规则

# 按编号列出规则信息
shell > iptables -nL --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    f2b-nginx  tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain f2b-nginx (1 references)
num  target     prot opt source               destination         
1    REJECT     all  --  8.8.8.8        0.0.0.0/0            reject-with icmp-port-unreachable
2    REJECT     all  --  9.9.9.9      0.0.0.0/0            reject-with icmp-port-unreachable
3    RETURN     all  --  0.0.0.0/0            0.0.0.0/0 
# iptables 删除标识 规则名称 该规则下的编号          
shell > iptables -D f2b-nginx 1

猜你喜欢

转载自blog.csdn.net/Dorisnzy/article/details/82926067