版权声明:欢迎转载与交流。https://blog.youkuaiyun.com/one2more/article/details/90368502
Fail2ban
是一款开源的防护软件。其功能主要是防止服务器密码被暴力破解
。Fail2ban可以监视系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是防火墙),而且可以发送E-mail通知系统管理员,是一款非常实用并且功能强大的防护软件!
Fail2ban的工作原理
是:通过分析一定时间内的相关服务日志,将满足动作的相关IP,利用iptables加入到DORP列表中一定时间。重启iptables服务后,所有DORP将被重置。
一、使用背景:(点击领取阿礼云1080元代金券)
可能大家会遇到:网站一直被别人暴力破解sshd服务密码,网站访问被拖慢。虽然没有成功,但会导致系统负载很高,原因是在暴力破解的时候,系统会不断地认证用户,从而增加了系统资源额外开销,导致访问网站速度很慢。
二、Fail2ban下载及版本介绍
Fail2ban的官方网站:http://www.fail2ban.org。截止目前,最新版0.10.X版本,最新稳定版0.9.4版本
experimental
(测试版) : fail2ban-0.10.x
stable
(稳定版) : fail2ban-0.9.4
very-stable
(非常稳定版) : fail2ban-0.8.14
在此我选择的是
yum安装
三、环境搭建
服务器端: 192.168.0.201 localhost
客户端: 192.168.0.111 testhost
安装openssh 详细的可以看一下之前分享的《实验探索openssh(一):服务安装搭建》
(1)安装
[root@localhost ~]# yum -y install openssh openssh-server openssh-clients
(2)查看安装的包
[root@localhost ~]# rpm -qa | grep ssh
openssh-clients-7.4p1-16.el7.x86_64
libssh2-1.4.3-10.el7_2.1.x86_64
openssh-7.4p1-16.el7.x86_64
openssh-server-7.4p1-16.el7.x86_64
(3)启动sshd服务
[root@localhost ~]# systemctl start sshd
(4)查看端口
[root@localhost ~]# netstat -luntp | grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1154/sshd
tcp6 0 0 :::22 :::* LISTEN 1154/sshd
四、解压安装Fail2ban
1、安装epel-release,拓展源
[root@localhost ~]# yum -y install epel-release
2、安装fial2ban
[root@localhost ~]# yum -y insatll fail2ban
3、Fail2ban主要配置文件
[root@localhost fail2ban]# cd /etc/fail2ban/
[root@localhost fail2ban]# ll
总用量 64
drwxr-xr-x 2 root root 4096 5月 24 19:09 action.d `#存放的是一些动作的目录,比如:发邮件、ban等动作`
-rw-r--r-- 1 root root 2328 5月 11 2017 fail2ban.conf `#定义fail2ban日志级别、日志位置及sock位置等的配置文件`
drwxr-xr-x 2 root root 6 7月 14 2017 fail2ban.d
drwxr-xr-x 3 root root 4096 5月 24 19:09 filter.d `#存放一些过滤条件的目录`
-rw-r--r-- 1 root root 21502 7月 14 2017 jail.conf `#监狱配置文件,其主配置文件,拦截等在这个文件中配置`
drwxr-xr-x 2 root root 31 5月 24 19:09 jail.d
-rw-r--r-- 1 root root 2375 5月 11 2017 paths-common.conf
-rw-r--r-- 1 root root 642 5月 11 2017 paths-debian.conf
-rw-r--r-- 1 root root 1070 5月 11 2017 paths-fedora.conf
-rw-r--r-- 1 root root 1156 5月 11 2017 paths-freebsd.conf
-rw-r--r-- 1 root root 975 5月 11 2017 paths-opensuse.conf
-rw-r--r-- 1 root root 290 5月 11 2017 paths-osx.conf
4、如何启动fail2ban服务
[root@localhost fail2ban]# systemctl start fail2ba
4、设置fail2ban开机自启动
[root@localhost fail2ban]# systemctl enable fail2ba
Created symlink from /etc/systemd/system/multi-user.target.wants/fail2ban.service to /usr/lib/systemd/system/fail2ban.service.
五、实战应用(点击领取阿礼云1080元代金券)
1、场景设定
ssh远程登录5分钟内3次密码验证失败,禁用用户IP访问主机1小时,1小时后该限制自动解除后用户可重新登录。
2、需要修改的配置文件说明
动作文件/etc/fail2ban/action.d/iptables.conf
以及日志匹配条件文件/etc/fail2ban/filter.d/sshd.conf
安装后是默认存在并且配置好的,基本不用做任何修改。因此我们主要要修改和配置的就只有/etc/fail2ban/jail.conf
文件,启用sshd服务的日志分析,指定动作的阈(yù)值即可。
[root@localhost fail2ban]# pwd
/etc/fail2ban
[root@localhost fail2ban]# vim jail.conf
1 #
2 # WARNING: heavily refactored in 0.9.0 release. Please review and
3 # customize settings for your setup.
... 省略 ...
`修改sshd模块内容为以下内容`
223 [sshd]
224
225 port = ssh `#端口号`
226 logpath = %(sshd_log)s
227 backend = %(sshd_backend)s
228 enabled=true `#是否激活此模块`
229 filter=sshd `#过滤规则名,对应filter.d目录下的sshd.conf`
230 action=iptables[name=SSH,port=ssh,protocol=tcp] `#动作相关配置,对应action.d/iptables.conf文件`
231 sendmail-whois[name=SSH,dest=you@example.com,sender=fail2ban@example.com,sendname="Fail2ban"] `#触发报警后的邮件发送相关配置。`
232 logpath=/var/log/secure `#监控的日志文件位置`
233 bantime=3600 `#禁止用户IP访问主机的时间,默认为秒。`
234 findtime=300 `#在指定时间内出现规定次数及禁止IP`
235 maxretry=3 `#密码验证失败允许次数`
... 省略 ...
3、清空SSH日志,重启服务,查看iptables规则链
(1)清空SSH认证日志
[root@localhost01 fail2ban]# > /var/log/secure
(2)重启fail2ban
[root@localhost01 fail2ban]# systemctl restart fail2ban.service
(3)查看iptables规则链
[root@localhost01 fail2ban]# iptables -L `#可以发现多生成了一条规则链,具体内容如下:`
Chain f2b-SSH (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
六、故意输错密码进行测试
[root@localhost ~]# ssh root@192.168.0.201
The authenticity of host '192.168.0.201 (192.168.0.201)' can't be established.
ECDSA key fingerprint is SHA256:3ddF8OIFum4r5lFE+1LDmxQv0QXTU64WzooM4floeGc.
ECDSA key fingerprint is MD5:c1:91:14:86:6b:f0:3b:3b:49:3f:a4:1f:51:a6:8e:04.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.201' (ECDSA) to the list of known hosts.
root@192.168.0.201's password: `#输入正确的密码,以正常登陆。`
Last login: Fri May 24 19:09:10 2019 from 192.168.0.111
[root@testhost01 ~]# exit
登出
Connection to 192.168.0.201 closed.
[root@localhost ~]# ssh root@192.168.0.201
root@192.168.0.201's password: `#第一次输错密码`
Permission denied, please try again.
root@192.168.0.201's password: `#第二次输错密码`
Permission denied, please try again.
root@192.168.0.201's password: `#第三次输错密码`
^C
[root@localhost ~]# ssh root@192.168.0.201 `#再次登陆提示:“连接被拒绝”`
ssh: connect to host 192.168.0.201 port 22: Connection refused
七、查看相关配置效果
1、查看iptables可以看到192.168.0.111被禁止所有访问
[root@localhost01 fail2ban]# iptables -L | tail -4
Chain f2b-SSH (1 references)
target prot opt source destination
REJECT all -- 192.168.0.111 anywhere reject-with icmp-port-unreachable
RETURN all -- anywhere anywhere
2、查看iptables的jail状态可以看到数字和列表都已多了一项
[root@localhost01 fail2ban]# fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd
3、也可通过具体的服务sshd查看详细信息
[root@localhost01 fail2ban]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0 `#目前失败次数`
| |- Total failed: 3 `#共登录失败次数`
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 1 `#目前被ban的数量`
|- Total banned: 1 `#总共被ban的次数`
`- Banned IP list: 192.168.0.111 `#被ban的IP列表`
4、通过fail2ban的日志查看相关信息(根据过滤条件,发现3次后,第四次开始ban)
[root@localhost01 fail2ban]# tail /var/log/fail2ban.log
2019-05-24 21:40:36,912 fail2ban.filter [38682]: INFO Set jail log file encoding to UTF-8
2019-05-24 21:40:36,912 fail2ban.actions [38682]: INFO Set banTime = 3600
2019-05-24 21:40:36,913 fail2ban.filter [38682]: INFO Set findtime = 300
2019-05-24 21:40:36,913 fail2ban.filter [38682]: INFO Set maxlines = 10
2019-05-24 21:40:36,967 fail2ban.filtersystemd [38682]: INFO Added journal match for: '_SYSTEMD_UNIT=sshd.service + _COMM=sshd'
2019-05-24 21:40:36,982 fail2ban.jail [38682]: INFO Jail 'sshd' started
2019-05-24 21:55:17,907 fail2ban.filter [38682]: INFO [sshd] Found 192.168.0.111
2019-05-24 21:55:19,968 fail2ban.filter [38682]: INFO [sshd] Found 192.168.0.111
2019-05-24 21:55:23,824 fail2ban.filter [38682]: INFO [sshd] Found 192.168.0.111
2019-05-24 21:55:24,109 fail2ban.actions [38682]: NOTICE [sshd] Ban 192.168.0.111
八、非默认端口的情况下如何配置(以2222为例)
[root@localhost01 fail2ban]# vim /etc/ssh/sshd_config
修改第17行 #Port 22 为 port 2222
[root@localhost01 fail2ban]# vim /etc/fail2ban/jail.conf
修改第233行 action=iptables[name=SSH,port=ssh,protocol=tcp] 为 action=iptables[name=SSH,port=2222,protocol=tcp]
[root@testhost01 fail2ban]# systemctl restart sshd
[root@testhost01 fail2ban]# systemctl restart fail2ban.service
九、释放被ban的IP
[root@testhost01 fail2ban]# fail2ban-client set sshd unbanip 192.168.0.111
192.168.0.111
[root@testhost01 fail2ban]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 0
|- Total banned: 1
`- Banned IP list:
十、特别说明(点击领取阿礼云1080元代金券)
如果清空iptables或重启ptables后也需要重启fail2ban
有需要源码安装
的可以查看我的私密文档《源码安装fail2ban来防止ssh暴力破解》
相关阅读:
《实验探索openssh(一):服务安装搭建》
《实验探索openssh(二):远程连接的使用方法及配置文件常用项解析》
《实验探索openssh(三):如何提升服务安全性,开启秘钥认证防止ssh暴力破解》
《实验探索openssh(五):通过第三方防护应用denyhosts来防止ssh暴力破解》