Fail2ban
一、简介
Fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是防火墙),例如当有人在试探你的SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,而且可以发送e-mail通知系统管理员,是一款很实用、很强大的IP自动屏蔽工具!
Fail2Ban scans log files like /var/log/pwdfail and bans IP that makes too many password failures. It updates firewall rules to reject the IP address. These rules can be defined by the user. Fail2Ban can read multiple log files such as sshd or Apache web server ones.
More documentation, FAQ, HOWTOs are available on the project website: http://www.fail2ban.org
Fail2ban的功能和特性
1、支持大量服务。如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。
二、系统环境
系统平台:openSUSE 11.4 (i586)
Fail2ban 版本:Fail2Ban v0.8.4
python 版本:python-2.7-8.2.i586
三、安装
David-Linux:~ # rpm -ivh fail2ban-0.8.4-11.14.1.noarch.rpm
源码安装参考 http://www.fail2ban.org/wiki/index.php/MANUAL_0_8#Installation
四、配置
相关主要文件说明:
David-Linux:~ # rpm -ql fail2ban
/etc/fail2ban/action.d #动作文件夹,内含默认文件。iptables以及mail等动作配置
/etc/fail2ban/fail2ban.conf #定义了fai2ban日志级别、日志位置及sock文件位置
/etc/fail2ban/filter.d #条件文件夹,内含默认文件。过滤日志关键内容设置
/etc/fail2ban/jail.conf #主要配置文件,模块化。主要设置启用ban动作的服务及动作阀值
/etc/rc.d/init.d/fail2ban #启动脚本文件
下面介绍一下主要的配置文件
/etc/fail2ban/fail2ban.conf
此文件定义了fai2ban日志级别、日志位置及sock文件位置。
David-Linux:/etc/fail2ban # cat fail2ban.conf
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
#
# $Revision: 629 $
#
[Definition]
# Option: loglevel
# Notes.: Set the log level output.
# 1 = ERROR
# 2 = WARN
# 3 = INFO
# 4 = DEBUG
# Values: NUM Default: 3
#
loglevel = 3
# Option: logtarget
# Notes.: Set the log target. This could be a file, SYSLOG, STDERR or STDOUT.
# Only one log target can be specified.
# Values: STDOUT STDERR SYSLOG file Default: /var/log/fail2ban.log
#
logtarget = /var/log/fail2ban.log
# Option: socket
# Notes.: Set the socket file. This is used to communicate with the daemon. Do
# not remove this file when Fail2ban runs. It will not be possible to
# communicate with the server afterwards.
# Values: FILE Default: /var/run/fail2ban/fail2ban.sock
#
socket = /var/run/fail2ban/fail2ban.sock
每一个设置,在此文件中都会有详细的说明。
/etc/fail2ban/jail.conf
此是fail2ban主要的配置文件,可以把jail理解成监狱。下面就来介绍一下这个文件的主要设置方法:
David-Linux:/etc/fail2ban # cat jail.conf
# Fail2Ban configuration file
#
# Author: Cyril Jaquier
#
# $Revision: 747 $
#
# The DEFAULT allows a global definition of the options. They can be override
# in each jail afterwards.
[DEFAULT] #全局设置
# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
# ban a host which matches an address in this list. Several addresses can be
# defined using space separator.
ignoreip = 127.0.0.1 #忽略IP,在这个清单里的IP不会被屏蔽
# "bantime" is the number of seconds that a host is banned.
bantime = 600 #屏蔽时间,以秒为单位
# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 600 #监测时间。在此期间内重试超过规定次数,会激活fail2ban采取相应的动作。(也可以理解成在这个时间段内超过规定次数会被ban掉。)
# "maxretry" is the number of failures before a host get banned.
maxretry = 3 #最大尝试次数
# "backend" specifies the backend used to get files modification. Available
# options are "gamin", "polling" and "auto". This option can be overridden in
# each jail too (use "gamin" for a jail and "polling" for another).
#
# gamin: requires Gamin (a file alteration monitor) to be installed. If Gamin
# is not installed, Fail2ban will use polling.
# polling: uses a polling algorithm which does not require external libraries.
# auto: will choose Gamin if available and polling otherwise.
backend = auto #日志修改检测机制(gamin、polling和auto这三种)
# This jail corresponds to the standard configuration in Fail2ban 0.6.
# The mail-whois action send a notification e-mail with a whois request
# in the body.
#下面都是一些子段的设置,但这里面的优先级会高于全局里面的设置,也就是说,当里面设置和全局设置有冲突的时候,会以此段设置为准。如果没有的话就会调用全局设置。
[ssh-iptables] #相当于标签说明
enabled = true #是否激活此项(true/false)
filter = sshd #过滤规则filter的名字,对应filter.d目录下的sshd.conf
action = iptables[name=SSH, port=ssh, protocol=tcp] #动作的相关参数。如果不是默认端口的话,记得也要相应的把服务改成端口
sendmail-whois[name=SSH, dest=you@mail.com, sender=fail2ban@mail.com] #发送邮件dest是目标,sender是发送者(记得要有SMTP服务啊)
#注意:此动作也和filter类似,都是调用相应的配置文件(动作的配置文件action.d下的ptables.conf和sendmail-whois.conf)
logpath = /var/log/messages #日志记录的位置
maxretry = 3 #最大尝试次数,此项会覆盖全局中maxretry设置
#此设置的大概意思就是:fail2ban会按照你的过滤规则(filter = sshd 也就是filter.d/sshd.conf)去查看相应的日志文件(logpath=/var/log/messages),然后在findtime = 600 “10分钟” (此时间以全局的为准)之内符合条件的记录下来,如果到达了maxretry = 3 “3次符合条件” 就对这个采取相应的动作action(ptables.conf和sendmail-whois.conf),并且限制的时间为bantime = 600 “10分钟”。
#关于其他的子段设置是类似的,所以大家参考来修改就OK了。
David-Linux:/etc/fail2ban #
五、fail2ban的相关命令
1、启动/关闭fail2ban
service fail2ban start/stop
2、状态查看
service fail2ban status
3、日志文件
/var/log/fail2ban.log
4、iptables内容查看
iptables -L -n
六、测试
注意:如果重新启动fail2ban,iptables中的规则就会全部被清空。
仔细看了一下过滤规则与动作内容,发现要玩好fail2ban的话,关键是要你会分析日志,然后根据日志来写过滤规则,然后就是写iptables动作,这些要以后慢慢修炼了。
最后提供一个fail2ban中文手册的下载地址 http://share.opsers.org/viewfile.php?file_id=131