首页 > 代码库 > Fail2防止sshd暴力破解

Fail2防止sshd暴力破解

 

 

Fail2ban 防止sshd暴力破解

[root@localhost ~]# ls

\                       install.log         模板  文档  桌面

anaconda-ks.cfg         install.log.syslog  视频  下载

fail2ban-0.8.14.tar.gz  公共的              图片  音乐

实战演练

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

我们使用fail2ban 可以实现锁ip

Fail2ban可以监视日志文件,房后匹配日志文件的错误信息(正则表达式)执行相应的屏蔽动作(一般来说是防火墙),而且可以发送e-mail通知系统管理员,

Fail2ban运行机制:简单来说其功能就是防止暴力破解,

工作原理是通过分析一定时间内相关的服务日志,将满足动作的相关的ip利用iptables加入到dorp(弄丢)列表一定时间

应用实例

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

[root@sshd ~]# tar zxvf fail2ban-0.8.14.tar.gz -C /usr/src/

[root@sshd ~]# cd /usr/src/fail2ban-0.8.14/

[root@sshd fail2ban-0.8.14]# python setup.py install

[root@sshd fail2ban-0.8.14]# chkconfig --add fail2ban

[root@sshd fail2ban-0.8.14]# chkconfig fail2ban   on

[root@sshd fail2ban-0.8.14]# chkconfig --list | grep   fail2ban

fail2ban       0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

查看fail2ban启动配置文件的启动状态

[root@sshd fail2ban-0.8.14]# grep chkconfig ./* -R --color

./files/redhat-initd:# chkconfig: - 92 08

[root@sshd fail2ban-0.8.14]# vim /etc/fail2ban/jail.conf

技术分享

enabled  = true

filter   = sshd

action   = iptables[name=SSH, port=ssh, protocol=tcp]

           sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"]

logpath  = /var/log/secure

findtime  = 300

maxretry = 3

bantime  = 3600

注释:

enabled  = true  #是否激活此项(true/false)修改成 true

logpath  = /var/log/secure #检测的系统的登陆日志文件。这里要写sshd服务日志文件。

#完成:5分钟内3次密码验证失败,禁止用户IP访问主机1小时。 配置如下

findtime  = 300  #在5分钟内内出现规定次数就开始工作,默认时间单位:秒

maxretry = 3    #3次密码验证失败

bantime  = 3600         #禁止用户IP访问主机1小时 

[root@sshd fail2ban-0.8.14]# service fail2ban start

客户端

验证:故意输错三次密码

[root@localhost ~]# ssh root@192.168.1.1

root@192.168.1.1‘s password:

Permission denied, please try again.

root@192.168.1.1‘s password:

Permission denied, please try again.

root@192.168.1.1‘s password:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

技术分享

[root@localhost ~]# ssh root@192.168.1.1

ssh: connect to host 192.168.1.1 port 22: Connection refused

查看sshd服务端fail2ban日志

[root@sshd fail2ban-0.8.14]# sudo tail -f /var/log/fail2ban.log

技术分享

查看sshd服务端fail2ban的状态

技术分享

 

Fail2防止sshd暴力破解