yum安装fail2ban和防止暴力破解

Yum安装Fail2ban和防止暴力破解

fail2ban的优点是:使用简单、灵活、功能强大

实战背景:

最近公网网站一直被别人暴力破解SSHD服务密码。虽然没有成功,但会导致系统负载很高,原因是在暴力破解的时候,系统会不断地认证用户,从而增加了系统资源额外开销,导致访问公司网站速度很慢。

然而fail2ban程序可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是防火墙),而且可以发送e-mail通知系统管理员,很实用、很强大!

简单来说其功能就是防止暴力破解。工作的原理是通过分析一定时间内的相关服务日志,将满足动作的相关IP利用iptables加入到dorp列表一定时间。

注:重启iptables服务的话,所有DORP将重置。

1、需要安装python开发环境,并且版本要大于2.4。

[root@ localhost ~]# python -V      #查看当前系统中python的版本。
Python 2.6.6

2、使用Yum安装fail2ban

[root@ localhost ~]# yum -y install epel-release
[root@ localhost ~]# yum -y install fail2ban

3、应用实例

设置条件:SSH远程登录5分钟内3次密码验证失败,禁止用户IP访问主机1小时,1小时该限制自动解除,用户可重新登录。

因为动作文件(action.d/iptables.conf)以及日志匹配条件文件(filter.d/sshd.conf )安装后是默认存在的。基本不用做任何修改。所有主要需要设置的就只有jail.conf文件。启用SSHD服务的日志分析,指定动作阀值即可。

实例文件/etc/fail2ban/jail.conf及说明如下:

[root@ localhost ~]# vim /etc/fail2ban/jail.conf
[DEFAULT]									#全局设置。
ignoreip = 127.0.0.1/8						 #忽略的IP列表,不受设置限制。
bantime  = 600								#屏蔽时间,单位:秒。
findtime  = 600								#这个时间段内超过规定次数会被ban掉。
maxretry = 3								#最大尝试次数。
backend = auto								#日志修改检测机制(gamin、polling和auto这三种)。

[sshd]			#单个服务检查设置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置。
port   = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s

#加入如下内容
enabled  = true								#是否激活此项(true/false)修改成 true。
filter = sshd								#过滤规则filter的名字,对应filter.d目录下的sshd.conf。
action = iptables[name=SSH, port=ssh, protocol=tcp]	#动作的相关参数,对应action.d/iptables.conf文件。
sendmail-whois[name=SSH, dest=[email protected], sender=[email protected], sendername="Fail2Ban"]											   #触发报警的收件人。
logpath = /var/log/secure					 #检测的系统的登陆日志文件。这里要写sshd服务日志文件。 默认为logpath = /var/log/sshd.log 。

#5分钟内3次密码验证失败,禁止用户IP访问主机1小时。 配置如下。
bantime = 3600								#禁止用户IP访问主机1小时。
findtime = 300								#在5分钟内内出现规定次数就开始工作。
maxretry = 3								#3次密码验证失败。

4、启动服务

[root@ localhost ~]# systemctl start fail2ban 		#启动fail2ban服务。
[root@ localhost ~]# systemctl enable fail2ban 		#设置开机自动启动。

5、测试

[root@ localhost ~]# > /var/log/secure  			#清空日志内容。
[root@ localhost ~]# systemctl restart fail2ban		 #重启fail2ban服务。
[root@ localhost ~]# iptables -L –n   				#在fail2ban服务启动后,iptables会多生成一个规则链,

6、测试:故意输入错误密码3次,再进行登录时,会拒绝登录。

用另一台虚拟机进行访问
[root@ localhost ~]# ssh 10.0.0.66
The authenticity of host '10.0.0.66 (10.0.0.66)' can't be established.
RSA key fingerprint is 9b:57:b9:86:84:90:a4:4b:44:3e:18:9f:8a:29:6f:e5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.66' (RSA) to the list of known hosts.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Connection closed by 10.0.0.66

7、测试是否还能连接

[root@ localhost ~]# ssh 10.0.0.66
ssh: connect to host 10.0.0.66 port 22: Connection refused

8、需要注意的2点:

(1) 另外如果后期需要把iptables清空后或iptables重启后,也需要把fail2ban重启一下。

(2)如果修改ssh默认端口22为2015后,配置fail2ban来监控SSHD服务需要修改配置文件

发布了25 篇原创文章 · 获赞 9 · 访问量 2486

猜你喜欢

转载自blog.csdn.net/chen_jimo_c/article/details/103822086