fail2ban 安装

参考文章:http://www.centos.bz/2012/04/prevent-ssh-break-in-with-fail2ban/ 

 

 

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

python安装

安装fail2ban需要Python >= 2.4,可以直接执行python查询版本号,但fail2ban 2.4有一个bug,所以还是推荐安装2.5以上的python
python安装方法:

  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日志轮循:

  1. vi /etc/logrotate.d/fail2ban

写入:

  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使用方法

配置文件:

  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文件,在文件中加入:

  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匹配的次数。
接着执行:

  1. service iptables start
  2. service fail2ban start

问题解决

1、WARNING: Unable to find a corresponding IP address for xxx
http://www.centos.bz/2012/05/warning-unable-to-find-a-corresponding-ip-address-for/
参考:http://www.fail2ban.org/wiki/index.php/MANUAL_0_8

posted @ 2013-09-11 17:28  小名辉辉  阅读(947)  评论(0编辑  收藏  举报