- A+
一、关于Fail2ban
- Fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽),如:当有人在试探你的HTTP、SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,而且可以发送e-mail通知系统管理员,是一款很实用、很强大的软件!
- Fail2ban由python语言开发,基于logwatch、gamin、iptables、tcp-wrapper、shorewall等。如果想要发送邮件通知道,那还需要安装postfix或sendmail。
- 在外网环境下,有很多的恶意扫描和密码猜测等恶意攻击行为,使用Fail2ban配合iptables,
实现动态防火墙是一个很好的解决方案。
二、安装Fail2ban
从CentOS7开始,官方的标准防火墙设置软件从iptables变更为firewalld。为了使Fail2ban与iptables联动,需禁用自带的firewalld服务,同时安装iptables服务。因此,在进行Fail2ban的安装与使用前需根据博客CentOS7安装和配置iptables防火墙进行环境配置。
这里继续使用yum安装方式
- [root@kvmtest ~]# yum -y install epel-release
- [root@kvmtest ~]# yum -y install fail2ban
查看安装
- [root@kvmtest ~]# chkconfig --list fail2ban
- 注:该输出结果只显示 SysV 服务,并不包含
- 原生 systemd 服务。SysV 配置数据
- 可能被原生 systemd 配置覆盖。
- 要列出 systemd 服务,请执行 'systemctl list-unit-files'。
- 查看在具体 target 启用的服务请执行
- 'systemctl list-dependencies [target]'。
- 服务 fail2ban 信息读取出错:没有那个文件或目录
- [root@kvmtest ~]# systemctl list-dependencies fail2ban
- fail2ban.service
- ● ├─system.slice
- ● └─basic.target
- ● ├─firewalld.service
- ● ├─microcode.service
- ● ├─rhel-dmesg.service
- ● ├─selinux-policy-migrate-local-changes@targeted.service
- ● ├─paths.target
三、配置Fail2ban
安装完成后,目录结构如下:
- [root@kvmtest ~]# ll /etc/fail2ban
- 总用量 84
- drwxr-xr-x 2 root root 4096 12月 24 10:19 action.d
- -rw-r--r-- 1 root root 2817 1月 11 2020 fail2ban.conf
- drwxr-xr-x 2 root root 6 8月 28 19:55 fail2ban.d
- drwxr-xr-x 3 root root 4096 12月 24 10:19 filter.d
- -rw-r--r-- 1 root root 25757 12月 24 11:43 jail.conf
- -rw-r--r-- 1 root root 25740 12月 24 10:25 jail.conf.bak.20201224
- drwxr-xr-x 2 root root 31 12月 24 10:19 jail.d
- -rw-r--r-- 1 root root 2827 1月 11 2020 paths-common.conf
- -rw-r--r-- 1 root root 930 1月 11 2020 paths-fedora.conf
- [root@kvmtest ~]#
1.编辑/etc/fail2ban/jail.conf文件
这里只配置ssh登录
- [root@kvmtest ~]# ll /etc/fail2ban
- 总用量 84
- drwxr-xr-x 2 root root 4096 12月 24 10:19 action.d
- -rw-r--r-- 1 root root 2817 1月 11 2020 fail2ban.conf
- drwxr-xr-x 2 root root 6 8月 28 19:55 fail2ban.d
- drwxr-xr-x 3 root root 4096 12月 24 10:19 filter.d
- -rw-r--r-- 1 root root 25757 12月 24 11:43 jail.conf
- -rw-r--r-- 1 root root 25740 12月 24 10:25 jail.conf.bak.20201224
- drwxr-xr-x 2 root root 31 12月 24 10:19 jail.d
- -rw-r--r-- 1 root root 2827 1月 11 2020 paths-common.conf
- -rw-r--r-- 1 root root 930 1月 11 2020 paths-fedora.conf
- [root@kvmtest ~]#
2.登录测试
配置完成后重启一下服务,然后进行测试。
- [root@yunkzbd ~]# ssh 10.128.25.135
- The authenticity of host '10.128.25.135 (10.128.25.135)' can't be established.
- ECDSA key fingerprint is c7:74:10:b8:3e:30:b7:b2:24:58:ce:d2:6c:af:c3:59.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '10.128.25.135' (ECDSA) to the list of known hosts.
- root@10.128.25.135's password:
- Permission denied, please try again.
- root@10.128.25.135's password:
- Permission denied, please try again.
- root@10.128.25.135's password:
- Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
- [root@yunkzbd ~]# ssh 10.128.25.135
- ssh: connect to host 10.128.25.135 port 22: Connection refused
- [root@yunkzbd ~]# ssh 10.128.25.135
- ssh: connect to host 10.128.25.135 port 22: Connection refused
- [root@yunkzbd ~]#
登录三次失败后,拒绝ssh登录。
3.查看日志记录
- [root@kvmtest fail2ban]# tail /var/log/fail2ban.log
- 2020-12-26 11:00:58,342 fail2ban.filter [5489]: INFO Added logfile: '/var/log/secure' (pos = 0, hash = 3bef9d3cd1ce313bb8f7d5733425806a)
- 2020-12-26 11:00:58,359 fail2ban.jail [5489]: INFO Jail 'sshd' started
- 2020-12-26 11:00:58,360 fail2ban.jail [5489]: INFO Jail 'ssh-iptables' started
- 2020-12-26 11:06:21,978 fail2ban.filter [5489]: INFO [ssh-iptables] Found 192.168.150.121 - 2020-12-26 11:06:20
- 2020-12-26 11:06:24,707 fail2ban.filter [5489]: INFO [ssh-iptables] Found 192.168.150.121 - 2020-12-26 11:06:24
- 2020-12-26 11:06:25,435 fail2ban.filter [5489]: INFO [sshd] Found 192.168.150.121 - 2020-12-26 11:06:20
- 2020-12-26 11:06:25,435 fail2ban.filter [5489]: INFO [sshd] Found 192.168.150.121 - 2020-12-26 11:06:24
- 2020-12-26 11:06:28,704 fail2ban.filter [5489]: INFO [sshd] Found 192.168.150.121 - 2020-12-26 11:06:28
- 2020-12-26 11:06:29,014 fail2ban.filter [5489]: INFO [ssh-iptables] Found 192.168.150.121 - 2020-12-26 11:06:28
- 2020-12-26 11:06:29,395 fail2ban.actions [5489]: NOTICE [ssh-iptables] Ban 192.168.150.121