首页 > 技术文章 > Fail2防止sshd暴力破解

bixiaoyu 2017-05-20 20:05 原文

简介:
fail2ban是一款实用软件,可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作。支持大量服务。如sshd,apache,qmail,proftpd,sasl等等
2、支持多种动作。如iptables,tcp-wrapper,shorewall(iptables第三方工具),mail notifications(邮件通知)等等。
3、在logpath选项中支持通配符
4、需要Gamin支持(注:Gamin是用于监视文件和目录是否更改的服务工具)
5、需要安装python,iptables,tcp-wrapper,shorewall,Gamin。如果想要发邮件,那必需安装postfix或sendmail

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的状态

 

推荐阅读