<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

一、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.

一句话: Fail2ban 扫描应用日志,通过 iptables 等防火墙将有***威胁的 IP 拒之门外。

本文以 Fail2ban<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />0.8.4 源码包在 RHEL 5.2 下安装配置为例介绍。

配置实现目标: ssh 用户 10 分钟内 4 次密码验证失败,禁止用户 IP 访问主机 1 小时, 1 小时该限制自动解除,用户可重新登录。

二、  Fail2ban安装:

安装前需检查是否有 python 运行环境。

[root@ sys ]# rpm -qa | grep python | grep 2.4.3

python-2.4.3-21.el5

python-devel-2.4.3-21.el5

开始安装

tar xvfj fail2ban-0.8.4.tar.bz2

cd fail2ban-0.8.4

python setup.py install

执行以上三步即完成 Fail2ban 的安装,安装后需要根据自己的需求进行配置。

我这里以配置 Fail2ban 加固 sshd 为例。

Fail2ban 主要配置文件如下:

[root@dbserver ~]# ls /etc/fail2ban/

action.d  fail2ban.conf  filter.d  jail.conf

u fail2ban.conf

fail2ban.conf 定义了 fai2ban 日志位置及日志级别 , 此配置文件保持默认即可,一般不做修改。

默认 fail2ban.conf 里面就三个参数,而且都有注释。

loglevel = 3 # 默认日志的级别

logtarget = /var/log/fail2ban.log# 日志的目的

socket = /tmp/fail2ban.sock#socket 的位置

u jail.conf

jail.conf fail2ban 主要的 配置 文件,在 jail.conf 里有一个 [DEFAULT] 段,这个段下的参数是全局参数 .

ignoreip = 127.0.0.1  # 忽略 IP, 在这个清单里的 IP 不会被屏蔽

bantime = 600 # 屏蔽时间,该 bantime 将被 [ssh-iptables] bantime 覆盖;

findtime = 600 # 发现时间,在此期间内重试超过规定次数,会激活 fail2ban

maxretry = 3 # 默认尝试次数

backend = auto # 日志修改检测机制

 

[ssh-iptables]   # 该段为 sshd 服务小节,段内配置优先全局配置。

enabled = true # 激活 ,默认是未激活;

filter = sshd   # 过滤规则 f ilter 的名字, 对应 filter.d 目录下 sshd.conf

# 所采用的工作,按照名字可在 action.d 目录下找到

action = iptables[name=SSH, port=ssh, protocol=tcp]

mail-whois[name=SSH, dest=root]

logpath = /var/log/secure   #sshd log ,根据 linux 发行版本不同而不同,注意修改;

maxretry = 4 # 覆盖全局重试次数

bantime = 3600   # 覆盖全局屏蔽时间

u filter.d action.d

这个两个文件夹分别放着日志的过滤规则及所用采取的动作。

filter.d/sshd.conf 有可对 sshd 日志 /var/log/secure 进行分析。

验证过滤规则是否有效: fail2ban-regex [logfile] [filter.conf]

在我系统上,我是这样执行的:

Fail2ban-regex /var/log/secure /etc/fail2ban/filter.d/sshd.conf

注意:如果你的日志启用了压缩,默认的过滤规则无法正确解析类似 last message repeated 3 times 。请根据自身情况调整 findtime 参数。

Action.d 文件夹为根据过滤情况要执行的动作。默认是 iptable+mail-whois 。执行何动作在 jail.conf 文件中定义。如是 ssh-iptables 中配置如下:

action = iptables[name=SSH, port=ssh, protocol=tcp]

mail-whois[name=SSH, dest=root]

三、  fail2ban启停

Fail2ban 3 个可执行程序,分别是 fail2ban-server fail2ban-client fail2ban-regex

u  启动 fail2ban

Fail2ban-client start

u  关闭 fail2ban

Fail2ban-client stop

u  验证 fail2ban 工作情况

观察 fail2ban 运行情况,我常用的有三条命令:

1、   fail2ban-client status ,会看到有个 Jail list 名称为 sshd-iptables

2 iptables –nL 正常的话会看到多出来一个 chain 名为 fail2ban-SSH

3 、查看 fail2ban 日志 /var/log/fail2ban.log ,看 fail2ban 是否启动正常,被禁用的 IP 也会在这里看到;

/var/log/fail2ban.log 显示如下所示:

2009-09-11 23:20:51,202 fail2ban.server : INFO   Changed logging target to /var/log/fail2ban.log for Fail2ban v0.8.4

2009-09-11 23:20:51,203 fail2ban.jail   : INFO   Creating new jail 'ssh-iptables'

2009-09-11 23:20:51,273 fail2ban.jail   : INFO   Jail 'ssh-iptables' uses Gamin

2009-09-11 23:20:51,390 fail2ban.filter : INFO   Added logfile = /var/log/secure

……

2009-09-11 23:20:51,395 fail2ban.actions: INFO   Set banTime = 6000

2009-09-11 23:20:51,530 fail2ban.jail   : INFO   Jail 'ssh-iptables' started

……

2009-09-11 23:20:51,530 fail2ban.actions: WARNING [ssh-iptables] Unban 58.143.242.123

2009-09-11 23:20:5 2 ,530 fail2ban.actions: WARNING [ssh-iptables] Ban 210.51.22.207

2009-09-11 23: 3 0:5 2 ,530 fail2ban.actions: WARNING [ssh-iptables] Unban 210.51.22.207

其他命令可以用 fail2ban-server –help

u  设置 fail2ban 开机自动启动,

cp ./redhat-initd /etc/init.d/fail2ban #redhat-initd 在解压到目录下

# chkconfig --add fail2ban

#service fail2ban start

至此,使用 fail2ban0.8.4 安全加固 sshd 服务配置完成。希望本文对你增加 linux 系统安全有帮助。