怎样安装fail2ban防止暴力登录

2015年8月31日 | 分类: 【技术】

fail2ban的功能就是防止暴力破解:fail2ban scans log files like /var/log/pwdfail or /var/log/apache/error_log and bans IP that makes too many password failures. It updates firewall rules to reject the IP address.

工作原理:通过分析一定时间内的相关服务日志,将满足动作的相关IP利用iptables加入到dorp列表一定时间。注:重启iptables服务的话,所有DORP将重置。

在CentOS中安装fail2ban:

安装:

# yum install fail2ban 

主要文件说明:

# 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                #启动脚本文件

应用实例:

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

因为动作文件(action.d/iptables.conf)以及日志匹配条件文件(filter.d/sshd.conf )安装后是默认存在的。基本不用做任何修改。所有主要需要设置的就只有jail.conf文件。启用sshd服务的日志分析,指定动作阀值即可。实例文件及说明如下:

[DEFAULT]               #全局设置
ignoreip = 127.0.0.1       #忽略的IP列表,不受设置限制
bantime  = 600             #屏蔽时间,单位:秒
findtime  = 600             #这个时间段内超过规定次数会被ban掉
maxretry = 3                #最大尝试次数
backend = auto            #日志修改检测机制(gamin、polling和auto这三种)

[sshd]                   #单个服务检查设置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置。
enabled  = true             #是否激活此项(true/false)
filter   = sshd              #过滤规则filter的名字,对应filter.d目录下的sshd.conf
action   = iptables[name=SSH, port=ssh, protocol=tcp]             #动作的相关参数,对应action.d/iptables.conf文件
logpath  = /var/log/secure         #检测的日志文件path
bantime  = 3600
findtime  = 300
maxretry = 3

启动服务:

# service fail2ban start
Starting fail2ban:                                         [确定]

测试:

故意输入错误密码进行测试。。测试发现确实有一定的延迟。设定3次。。在尝试第7次的时候才被加入到DORP列表。

# iptables -L |tail -4
Chain fail2ban-SSH (1 references)
target     prot opt source               destination
DROP       all  --  192.168.7.142        anywhere
RETURN     all  --  anywhere             anywhere

查看fail2ban的日志能够看到相关的信息

2011-12-03 18:24:34,231 fail2ban.actions: WARNING [sshd] Ban 192.168.7.142

参考文档:

http://www.opsers.org/linux-home/security/use-fail2ban-to-prevent-brute-force-ftp-ssh-and-other-services