来唉叹一下,每次查看LINUX网关服务器的安全日志时,就会看到N长的登陆失败的IP。很显然有人在做“好事”。那如何来防范,或是减少这种烦心的事情呢?

    呵呵,那你就使用fail2ban吧!它的工作原理就是通过比对配置文件里一些事先定义的参数(如对SSH的使用),当一个行为符合这些参数规则且达到一定的次数时,就会被fail2ban阻挡(结合iptables,但本文中,不用配置iptables)。fail2ban的官方网址是[url]http://fail2ban.sourceforge.net[/url]。[url]http://www.fail2ban.org/wiki/index.php/HOWTOs[/url] 各位需要详细的了解的话,就请前往。

      本文要介绍的是如何让SSH登陆一台机器,连续三次不成功后,被阻挡600秒的例子。
      官网上有TAR包。由于我自已比较懒汉,所以就直接下载了fail2ban-0.6.1-2jik.noarch.rpm.

一、安装环境:RedHat AS5 + fail2ban-0.6.1-2jik.noarch.rpm
                             fail2ban所在机器IP为 192.168.1.1

二、安装及简要配置:
       1、如下图,运行:rpm -Uvh fail2ban-0.6.1-2jik.noarch.rpm
                       同时,图中还显示了内核版本号。
       
       
         2、安装好后,配置文件会存在于/etc下面,名字为fail2ban.conf 所有的全局配置以及针对服务的配置均集中在此文件中。与TAR编译有所有不同。
        下图中就是显示的全局配置,也就是主配置的截图,其中maxfailures = 5是说明比对五次不同服务的参数均相同后,拒绝同样的访问600秒时间。

             

             下图是本文的正角色。SSH的配置截图,可以看到最下面三句话。这就是fail2ban要做的事情。这里没有做任何改。当然整个配置文件中也提到了apache ftp等。请各位自行研究学习。
   


                 

         3、OK,我对整个fail2ban.conf文件只是改变了原来的maxfailures = 5 为3.其它的不变。此时,要说明的是,采用此方法安装的fail2ban,会自动安装成服务,且系统启动时自启动,当然你可以使用 service  fail2ban  {start|stop|status|restart|condrestart}来进行相应的操作。这里,使用service fail2ban start。开启此服务。
  
三、测试安装效果:
       在另外一台机器192.168.1.3 上ssh root@192.168.1.1 连续输错3次密码后,再使用此命令时,就不会出现提示输入密码了。且在服务器上查看日志cat /var/log/fail2ban.log。可以看到deny的记录。
        
           

 

 

如果在日志显示如下:

[root@dns upload]# tail /var/log/fail2ban.log
2007-02-11 10:09:43,693 ERROR: time data did not match format:  data=Feb 11 10:02:45  fmt=%b %d %H:%M:%S
2007-02-11 10:09:43,694 ERROR: Please check the format and your locale settings.
2007-02-11 10:09:43,695 ERROR: time data did not match format:  data=Feb 11 02:02:45  fmt=%b %d %H:%M:%S
2007-02-11 10:09:43,696 ERROR: Please check the format and your locale settings.
2007-02-11 10:09:43,697 ERROR: time data did not match format:  data=Feb 11 10:02:48  fmt=%b %d %H:%M:%S
2007-02-11 10:09:43,698 ERROR: Please check the format and your locale settings.
2007-02-11 10:09:43,700 ERROR: time data did not match format:  data=Feb 11 02:02:48  fmt=%b %d %H:%M:%S
2007-02-11 10:09:43,701 ERROR: Please check the format and your locale settings.
2007-02-11 10:09:43,702 ERROR: time data did not match format:  data=Feb 11 02:02:48  fmt=%b %d %H:%M:%S
2007-02-11 10:09:43,703 ERROR: Please check the format and your locale settings.

解决办法:修改vi /etc/sysconfig/i18n 改为 LANG="en_US.UTF-8"

export   /etc/sysconfig/i18n      即可

~~~~~~~~~~~~~~~~~~~~~~~~

一、下载安装
#wget http://cdnetworks-kr-2.dl.sourceforge.net/project/fail2ban/fail2ban-stable/fail2ban-0.8.4/fail2ban-0.8.4.tar.bz2

#tar xvfj fail2ban-0.8.4.tar.bz2
#cd fail2ban-0.8.4
#python setup.py install
#cd files
# cp ./redhat-initd /etc/init.d/fail2ban
# chkconfig –add fail2ban
#service fail2ban start

注意:如果重起iptables 记的一定还要重起fail2ban,不然他就不能生效,fail2ban的过滤表是在iptables 启动后在加入的。

二、配置
1、fail2ban本身配置
默认fail2ban.conf里面就三个参数,而且都有注释。
#默认日志的级别
loglevel = 3
#日志的存放路径
logtarget = /var/log/fail2ban.log
#socket的位置
socket = /tmp/fail2ban.sock

2、fail2ban防护配置
全局设置
# vi /etc/fail2ban/jail.conf
# 忽悠 IP范围 如果有二组以上用空白做为间隔
ignoreip = 127.0.0.1
# 设定 IP 被封锁的时间(秒),如果值为 -1,代表永远封锁
bantime = 86400
# 设定在多少时间内达到 maxretry 的次数就封锁
findtime = 600
# 设定在多少时间内达到 maxretry 的次数就封锁
maxretry = 3
# 允许尝试的次数

分类设置
#针对sshd暴力***防护

[ssh-iptables]

enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
sendmail-whois[name=SSH, dest=you@mail.com, sender=fail2ban@mail.com]
logpath = /var/log/secure
# 如果有个别的次数设定就设在这里
maxretry = 3

#针对vsftpd暴力***防护

[vsftpd-iptables]

enabled = true
filter = vsftpd
action = iptables[name=VSFTPD, port=ftp, protocol=tcp]
sendmail-whois[name=VSFTPD, dest=you@mail.com]
logpath = /var/log/secure
maxretry = 3

建议设置成maxretry为 3 表示3次错误就封锁,另外logpath(Centos5和Rhel5中)要改成/var/log/secure。

然后我们设置启动服务:
#chkconfig –level 345 fail2ban on
#service fail2ban start

三、测试:
查看iptables 的规则多出了 iptables-ssh的规则

iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
fail2ban-SSH tcp — anywhere anywhere tcp dpt:ssh
fail2ban-SSH tcp — anywhere anywhere tcp dpt:ssh

Chain fail2ban-SSH (2 references)
target prot opt source destination
DROP all — 122.102.64.54 anywhere

# 这有一个被阻止的IP 拒绝时间根据在你的配置文件设置时间有关 我设置的是一天
RETURN all — anywhere anywhere

++++++++++++++++++++++++++++++++++++++++++++++++++++++++
在其他的机器上ssh 192.168.1.2
连续三次输错密码

使用命令fail2ban-client status ssh-iptables
查看阻止状态

或者

fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: ssh-iptables

测试结果:
#fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- filter
| |- File list: /var/log/secure
| |- Currently failed: 0
| `- Total failed: 3
`- action
|- Currently banned: 1
| `- IP list: 192.168.1.1
`- Total banned: 1

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

我们在来看看fail2ban的日志记录

2010-05-17 16:57:23,964 fail2ban.actions: WARNING [ssh-iptables] Ban 192.168.1.1
2010-05-17 21:35:00,449 fail2ban.actions: WARNING [ssh-iptables] Ban 218.108.85.244
2010-05-18 03:56:34,986 fail2ban.actions: WARNING [ssh-iptables] Ban 59.39.66.30

记录了被阻止的IP,成功阻止了ssh 密码猜测

这里只是只介绍了保护SSH和VPSFTP方法,配置文件中还有其他的服务配置,有时间了大家可以自己研究一下。

wget http://cdnetworks-kr-2.dl.sourceforge.net/project/fail2ban/fail2ban-stable/fail2ban-0.8.4/fail2ban-0.8.4.tar.bz2