使用Fail2ban阻挡针对公司邮件系统的暴力破解
作者: 日期:2014年01月10日 阅:2,262

ddos

fail2ban通过扫描日志文件,利用正则式匹配登录错误的IP地址,然后可以将IP地址列入防火墙中,同时还能给我发送email以提醒我有哪个IP地址被ban了。

公司使用的邮件系统经常被人使用pop暴力尝试破解,由于设置密码时并没有验证密码的复杂度,因此对于攻击者来说想破解那么一些账号真是轻而易举的事情;同时不断被人尝试破解也拖慢了系统速度。

不过还好邮件系统针对登录有日志(还不会烂到连日志都没有的地步吧 = =!),会记录尝试登录者的IP地址,因此利用iptables对可疑IP进行封锁便成为一个似乎可以执行的方案了,但是怎么做呢?搜索之后终于发现了fail2ban这款软件(点32个赞!)。

 

fail2ban是用python编写的开源软件:http://www.oschina.net/p/fail2ban,项目首页地址:http://www.fail2ban.org,授权协议为GPLv2。

其通过扫描日志文件,利用正则式匹配登录错误的IP地址,然后可以将IP地址列入防火墙中,同时还能给我发送email以提醒我有哪个IP地址被ban了。

嗯,正是我要的!开始安装它吧!

一、系统环境

OS:RHEL 5.6(32位)

Python:2.7.5(自带的2.4应该也可以)

二、安装软件

1. 安装fail2ban

http://www.fail2ban.org/wiki/index.php/Downloads 下载stable版本,然后源码安装:

[root@localhost tmp]# tar xvfj fail2ban-0.8.11.tar.bz2
[root@localhost tmp]# cd fail2ban-0.8.11
[root@localhost fail2ban-0.8.11]# python setup.py install

软件位于/usr/share/,二进制执行脚本位于/usr/bin,配置文件位于/etc/fail2ban。

然后将fail2ban放入开机启动列表里:

[root@localhost fail2ban-0.8.11]# chkconfig -a fail2ban

2. 安装pyinotify

由于邮件日志logrotate,每天凌晨会有一分钟左右不存在日志文件,如果使用默认的idle会导致fail2ban停止工作,因此需要安装pyinotify,该软件地址:http://www.oschina.net/p/pyinotify

https://github.com/seb-m/pyinotify 下载pyinotify,然后源码安装:

[root@localhost tmp]# cd pyinotify
[root@localhost pyinotify]# python setup.py install

3. (可选)安装mailx

由于RHEL 5自带的mailx太老,无法配置自定义的SMTP,因此需要安装最新的mailx程序。这个程序如何安装配置,这里就不赘述了。

三、配置

配置前首先来看一下日志文件长什么样:

[root@localhost ~]# cat /var/log/pop_authlog
1381124478 M caohui example.com 0 ip:127.0.0.1 status:1
1381124481 M info example.com 0 ip:89.151.140.52 status:0
1381124482 M zhouqing example.com 0 ip:111.164.210.227 status:1
1381124487 M shell example.com 0 ip:89.151.140.52 status:0
1381124493 M linux example.com 0 ip:89.151.140.52 status:0
1381124495 M zhaoyong1 example.com 0 ip:127.0.0.1 status:1
1381124497 M unix example.com 0 ip:89.151.140.52 status:0
1381124502 M yangjun example.com 0 ip:180.109.216.129 status:1
1381124502 M webadmin example.com 0 ip:89.151.140.52 status:0
1381124508 M test example.com 0 ip:89.151.140.52 status:0
1381124511 M qc example.com 0 ip:180.166.242.30 status:1
1381124513 M admin example.com 0 ip:89.151.140.52 status:0
......

每一行关键数据是:账号,ip地址和状态,状态为1表示成功,状态为0表示失败。观察日志文件我们可以发现,来自89.151.140.52的设备正在不断更换常见账号尝试破解,因此我们需要让fail2ban将此ip用iptables封掉!

进入配置文件目录我们可以看到有这几个文件和目录:

/etc/fail2ban/
├── action.d
│   ├── dummy.conf
│   ├── hostsdeny.conf
│   ├── iptables.conf
│   ├── mail-whois.conf
│   ├── mail.conf
│   └── shorewall.conf
├── fail2ban.conf
├── fail2ban.local
├── filter.d
│   ├── apache-auth.conf
│   ├── apache-noscript.conf
│   ├── couriersmtp.conf
│   ├── postfix.conf
│   ├── proftpd.conf
│   ├── qmail.conf
│   ├── sasl.conf
│   ├── sshd.conf
│   └── vsftpd.conf
├── jail.conf
└── jail.local

每一个.conf文件都对应一个同名的.local文件,软件将先读取.conf文件,再读取对应的.local文件,.local文件会覆盖.conf文件中同名的配置。

1. 创建自定义规则

[root@localhost ~]# vim /etc/fail2ban/jail.local
#模块名称
[pop3-iptables]
#是否启用
enabled = true
#过滤器名称,注意这个名称与下面第二步所创建的过滤器文件名需一致
filter = my-pop3
#针对IP的行为,由于我们这里需要封的是pop3,因此端口是110,协议是tcp;第二行发送提醒信到管理员邮箱
action = iptables[name=pop3, port=110, protcol=tcp]
         mail-whois[name=POP3, dest=plutonji@example.com]
#检查的日志文件路径
logpath = /var/log/pop_authlog
#封禁时间(单位:秒)
bantime = 86400
#多少秒内匹配多少次就执行上面的action,这里是30s内匹配6次
findtime = 30
maxretry = 6

2. 创建过滤器

[root@localhost ~]#vim /etc/fail2ban/filter.d/my-pop3.conf
[Definition]

# Option:  failregex
# Notes.:  regex to match the password failures messages in the logfile. The
#          host must be matched by a group named "host". The tag "<HOST>" can
#          be used for standard IP/hostname matching and is only an alias for
#          (?:::f{4,6}:)?(?P<host>[\w\-.^_]+)
# Values:  TEXT
#使用正则表达式匹配到需要封的IP地址,即下面的<HOST>部分
failregex = ^.*ip:<HOST>\sstatus:0$
# Option:  ignoreregex
# Notes.:  regex to ignore. If this regex matches, the line is ignored.
# Values:  TEXT
#
ignoreregex =

四、启动程序

	[root@localhost ~]# service fail2ban start

关键词:

申明:本文系厂商投稿收录,所涉观点不代表安全牛立场!


相关文章