fail2ban和防止暴力破解 yum安装

需要python开发环境,并且版本大于2.4

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

使用Yum安装fail2ban

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

修改实例文件/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, [email protected], [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次密码验证失败。

启动fail2ban服务

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

测试

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

测试:故意输入错误密码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

测试是否还能连接

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

防暴力破解完成

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章