使用 fail2ban 防止暴力破解 ssh 及 vsftpd 密码

转载请注明《文章来源:http://www.centos.bz/2012/04/prevent-ssh-break-in-with-fail2ban/
 php

此文介绍一个linux下经过监控日志防止密码被暴力破解的软件-fail2ban。fail2ban支持经常使用的服务,如sshd, apache, qmail, proftpd, sasl, asterisk等的密码验证保护,当发现暴力破解的迹像时,能够经过iptables, tcp-wrapper, shorewall等方式阻止此IP的访问。python

python安装

安装fail2ban须要Python >= 2.4,能够直接执行python查询版本号,但fail2ban 2.4有一个bug,因此仍是推荐安装2.5以上的python。
python安装方法:linux

  1. cd /tmp
  2. wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tgz
  3. tar xzf Python-2.7.3.tgz
  4. cd Python-2.7.3
  5. ./configure
  6. make && make install
  7. rm -rf /usr/bin/python
  8. ln -s /tmp/Python-2.7.3/python /usr/bin/

fail2ban安装

  1. cd /tmp
  2. wget https://github.com/downloads/fail2ban/fail2ban/fail2ban_0.8.6.orig.tar.gz
  3. tar xzf fail2ban_0.8.6.orig.tar.gz
  4. cd fail2ban-fail2ban-a20d1f8/
  5. ./setup.py install
  6. cp files/redhat-initd /etc/init.d/fail2ban
  7. chmod 755 /etc/init.d/fail2ban

配置fail2ban日志轮循:git

  1. vi /etc/logrotate.d/fail2ban

写入:github

  1. /var/log/fail2ban.log {
  2. weekly
  3. rotate 7
  4. missingok
  5. compress
  6. postrotate
  7. /etc/init.d/fail2ban restart 1>/dev/null || true
  8. endscript
  9. }

fail2ban使用方法

配置文件:web

  1. /etc/fail2ban/
  2. ├── action.d
  3. │ ├── dummy.conf
  4. │ ├── hostsdeny.conf
  5. │ ├── iptables.conf
  6. │ ├── mail-whois.conf
  7. │ ├── mail.conf
  8. │ └── shorewall.conf
  9. ├── fail2ban.conf
  10. ├── fail2ban.local
  11. ├── filter.d
  12. │ ├── apache-auth.conf
  13. │ ├── apache-noscript.conf
  14. │ ├── couriersmtp.conf
  15. │ ├── postfix.conf
  16. │ ├── proftpd.conf
  17. │ ├── qmail.conf
  18. │ ├── sasl.conf
  19. │ ├── sshd.conf
  20. │ └── vsftpd.conf
  21. ├── jail.conf
  22. └── jail.local

目录action.d下的文件指定知足条件时执行的一些动做,好比使用iptables禁止ip访问。
目录filter.d下的文件定义匹配日志的正则表达式。
fail2ban.conf文件是配置fail2ban-server程序启动的一些参数
jail.conf文件包含filter及action的指定。
每一个conf文件可被local文件覆盖,conf文件第一个被读取,接着是读取local文件,因此local文件中定义的参数会覆盖conf中的参数。因此咱们不须要添加全部的内容到local文件,只须要添加conf文件中你想覆盖的部分参数就好。正则表达式

防ssh及vsftpd暴力破解实例

创建/etc/fail2ban/jail.local文件,在文件中加入:apache

  1. [vsftpd-iptables]
  2. enabled = true
  3. filter = vsftpd
  4. action = iptables[name=VSFTPD, port=ftp, protocol=tcp]
  5. sendmail-whois[name=VSFTPD, dest=you@mail.com]
  6. logpath = /var/log/secure
  7. maxretry = 3
  8. [ssh-iptables]
  9. enabled = true
  10. filter = sshd
  11. action = iptables[name=SSH, port=ssh, protocol=tcp]
  12. sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com]
  13. logpath = /var/log/secure.log
  14. maxretry = 5

enabled:可选值false,true
filter:指定/etc/fail2ban/filter.d/目录下的正则文件,如filter = sshd则是指定/etc/fail2ban/filter.d/sshd.conf。
action:指定执行的动做,具体动做文件在/etc/fail2ban/action.d目录下。
logpath:指定监控日志的路径。
maxretry:执行action匹配的次数。
接着执行:centos

  1. service iptables start
  2. service fail2ban start

 

在以前开始使用fail2ban防止vsftpd或ssh暴力破解以后,有必定的效果,但在查看fail2ban.log日志时,发现一个问题:app

  1. fail2ban.filter : WARNING Unable to find a corresponding IP address for ns.rs996.com

咱们再查看secure日志:

  1. vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=www.centos.bz rhost=ns.rs996.com

fail2ban是根据rhost取得客户端的ip,并使用iptables禁止它,但咱们发现ns.rs996.com没法解析出ip地址,那它的ip去哪了呢?
通过分析得出结论:vsftpd默认对客户端ip进行反向解析,若是查询有结果,则把ip解析出来的域名记录secure日志中的rhost中,可是此反向解析出来的域名,自己又没有做A记录的解析,因此fail2ban再进行此域名的解析时,固然就没法获取到IP地址,也就没法禁止此客户端的暴力破解行为。
解决方法:
在vsftpd.conf中添加下面的代码禁止反向解析:

  1. reverse_lookup_enable=NO

以后重启vsftpd便可。生效以后不只解决了这个问题,也使得登陆vsftpd再也不停顿几秒了。
可是/var/log/secure里记录ftp登陆失败有一个缺点,就是用相同的用户名,不一样的密码登陆屡次,只是记录首次失败的信息,而以后的就只显示:last message repeated x times这样的信息,没法知足登陆失败三次的条件,因此就不能阻止此暴力破解,下面是解决这个问题的方法:
一、打开vsftpd.conf,添加以下代码:

  1. dual_log_enable=YES
  2. use_localtime=YES

二、打开/etc/fail2ban/jail.local,修改如下内容:

  1. [vsftpd-iptables]
  2. ...
  3. logpath = /var/log/vsftpd.log
  4. ...


参考:http://www.fail2ban.org/wiki/index.php/MANUAL_0_8