作者:高玉涵
时间:2024.6.30 12:56
博客:blog.csdn.net/cg_i
环境:CentOS8
左右左右下下 按按 滴——抓娃娃机
Fail2ban是一款实用的入侵防御软件,旨在保护服务器免受暴力攻击,特别是通过密码猜测和暴力破解的方式。以下是关于Fail2ban的详细介绍:
/var/log/auth.log
、/var/log/apache/access.log
等,匹配日志中的错误信息(通过正则表达式匹配),并对符合特定规则的IP地址执行封禁操作。Fail2ban通过扫描系统日志文件来检测失败的登录尝试或恶意行为。当检测到某个IP地址在指定时间内超过预设的失败尝试次数时,Fail2ban会根据配置执行相应的封禁动作,如通过iptables更新防火墙规则以拒绝该IP地址的访问。此外,Fail2ban还可以配置为在封禁IP地址时发送邮件通知给管理员。
Fail2ban的安装和配置相对简单,以下以基于Debian和CentOS的系统为例说明安装步骤:
sudo apt-get update
sudo apt-get install -y fail2ban
sudo yum update
sudo yum install -y epel-release
sudo yum install -y fail2ban
配置Fail2ban时,通常建议编辑/etc/fail2ban/jail.local
文件(如果不存在,则从/etc/fail2ban/jail.conf
复制一份并重命名)。在该文件中,可以启用和配置不同的监控服务,设置全局参数(如封禁时间、最大尝试次数等),以及为特定服务配置日志路径和动作。
[root@centos8 ~]vi /etc/fail2ban/jail.local
添加以下内容并保存:
[sshd]
enabled = true
filter = sshd
port = 22
logpath = /var/log/secure
maxretry = 3
bantime = 600
下面是对每一行配置的解释:
enabled = true
:启用对sshd
服务的监控。filter = sshd
:使用sshd
过滤器来匹配日志中的错误信息。port = 22
:指定sshd
服务监听的端口号,默认是22。logpath = /var/log/secure
:指定sshd
服务的日志文件路径。在大多数基于Red Hat的系统中,sshd
的日志记录在/var/log/secure
文件中。maxretry = 3
:设置最大尝试次数。如果某个IP地址在指定时间内尝试连接sshd
服务并失败超过3次,则触发封禁动作。bantime = 600
:设置封禁时间,单位为秒。在这个例子中,被封禁的IP地址将在600秒内无法访问sshd
服务。systemctl enable --now fail2ban
因我已启用了 fail2ban 为了让新配置生效,重启服务即可。
[root@centos8 ~]# systemctl restart fail2ban
[root@centos8 ~]# systemctl status fail2ban
● fail2ban.service - Fail2Ban Service
Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2024-06-30 13:22:15 CST; 5s ago
Docs: man:fail2ban(1)
Process: 3847005 ExecStop=/usr/bin/fail2ban-client stop (code=exited, status=0/SUCCESS)
Process: 3847068 ExecStartPre=/bin/mkdir -p /run/fail2ban (code=exited, status=0/SUCCESS)
Main PID: 3847070 (fail2ban-server)
Tasks: 15 (limit: 49300)
Memory: 22.8M
CGroup: /system.slice/fail2ban.service
├─3847070 /usr/bin/python3.6 -s /usr/bin/fail2ban-server -xf start
├─3847098 /bin/bash /usr/sbin/sendmail -f root@centos8. root@localhost
├─3847116 /bin/bash /usr/sbin/sendmail -f root@centos8. root@localhost
├─3847134 /bin/bash /usr/sbin/sendmail -f root@centos8. root@localhost
├─3847137 /usr/bin/dotlockfile -p -l /root/.esmtp_queue/.deliver_lock
├─3847138 /usr/bin/dotlockfile -p -l /root/.esmtp_queue/.deliver_lock
└─3847492 /bin/bash /usr/sbin/sendmail -f root@centos8. root@localhost
Jun 30 13:22:15 centos8 systemd[1]: fail2ban.service: Succeeded.
Jun 30 13:22:15 centos8 systemd[1]: Stopped Fail2Ban Service.
Jun 30 13:22:15 centos8 systemd[1]: Starting Fail2Ban Service...
Jun 30 13:22:15 centos8 systemd[1]: Started Fail2Ban Service.
Jun 30 13:22:16 centos8 fail2ban-server[3847070]: Server ready
客户端:连续输错 3 次密码:
ssh root@10.10.10.138
root@10.10.10.138's password:(随便输入)
Permission denied,please try again.
root@10.10.10.138's password:(随便输入)
Permission denied,please try again.
root@10.10.10.138's password:(随便输入)
Permission denied(publickey,gssapi-keyex,gssapi-with-mic,password).
再次连接到服务器:
ssh root@10.10.10.138
ssh:connect to host 10.10.10.138 port 22: Connection refused(连接断开,没有给我们输入密码的机会)
通过防火墙策略查看当前封禁的 IP 地址:
[root@centos8 ~]firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
forward: no
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
rule family="ipv4" source address="10.10.10.128" port port="ssh" protocol="tcp" reject type="icmp-port-unreachable"(IP 被禁止访问 SSH 端口。)
通过 fail2ban-client status
命令检查:
fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd
可以看到此时有一条 Jail 生效为 sshd 的信息。要想进一步了解通过 fail2ban-client status sshd
命令获得详细信息。
[root@centos8 ~]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 1
| |- Total failed: 2
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 1
|- Total banned: 2
`- Banned IP list: 10.10.10.128
我们主要关注Actions
下Currently banned
当前封禁 IP 数量、Total banned
总封禁次数、Banned IP list
封禁的 IP 地址。
[nginx-http-auth]
# HTTP 验证防暴力破解
enabled = true
filter = nginx-http-auth
port = 80
logpath = /var/log/nginx/error.log
[nginx-badbots]
# 屏蔽爬虫
enabled = true
port = 80
filter = nginx-badbots
logpath = /var/log/nginx/access.log
maxretry = 2
[nginx-nohome]
# 避免恶意请求网站目录结构
enable = true
port = 80
filter = nginx-nohome
logpath = /var/log/nginx/access.log
maxretry = 2
[nginx-noproxy]
# 避免被人利用反向代理
enable = true
port = 80
filter = nginx-noproxy
logpath = /var/log/nginx/access.log
maxretry = 2