使用fail2ban增强Linux安全防护
by Elton on 七.26, 2009, under Linux
观察/var/log/messages你可能会经常发现有类似以下的访问记录
1 2 3 4 5 6 | Jul 26 04:16:22 prosight sshd[29679]: Invalid user benjamin from 123.140.230.12 Jul 26 04:16:23 prosight sshd[29681]: Invalid user daniel from 123.140.230.12 Jul 26 04:16:24 prosight sshd[29683]: Invalid user william from 123.140.230.12 Jul 26 04:16:24 prosight sshd[29685]: Invalid user anthony from 123.140.230.12 Jul 26 04:16:25 prosight sshd[29687]: Invalid user cameron from 123.140.230.12 Jul 26 04:16:25 prosight sshd[29689]: Invalid user james from 123.140.230.12 |
同一个用户在不断的尝试用各种用户来登录你的机器。 fail2ban可以很有效的阻止这种频繁的试图登录你的机器的尝试
安装fail2ban
对于gentoo来说很简单,只要emerge一下就可以了。 其他版本的Linux可以通过源码或者rpm包安装
1 | $ sudo emerge -av fail2ban |
设置fail2ban
/etc/fail2ban/fail2ban.conf 是fail2ban的全局基本配置,基本不用动
1 2 3 4 5 | $ cat /etc/fail2ban/fail2ban.conf loglevel = 3 logtarget = /var/log/fail2ban.log socket = /tmp/fail2ban.sock |
/etc/fail2ban/jail.conf 是fail2ban的规则配置文件,我们需要根据情况来编辑它
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [DEFAULT] ignoreip = 127.0.0.1 #可以忽略的ip,多个ip用空格隔开 #当在findtime时间内(秒)失败超过maxretry次数后,就被封bantime时间(秒) bantime = 3600 findtime = 600 maxretry = 3 backend = auto #如果你使用的是iptables就将这个规则设置为true [ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] mail-whois[name=SSH, dest=yourmail@mail.com] logpath = /var/log/sshd.log #你ssh日志存放的地址 |
启动fail2ban
1 | $ sudo /etc/init.d/fail2ban start |
查看fail2ban状态
启动之后,只要符合filter所定义的正则式规则的日志项出现,就会执行相应的action。由于0.8源码树采用客户机/服务器的模式,因此可以很方便的查询fail2ban的执行情况。比方所,要查询刚才定义的“ssh-iptables”段的情况,只要执行
1 | fail2ban-client status ssh-iptables |
输出结果:
1 2 3 4 5 6 7 8 | Status for the jail: ssh-iptables |- filter | |- Currently failed: 0 | `- Total failed: 5 `- action |- Currently banned: 1 | `- IP list: 192.168.210.21 `- Total banned: 1 |
fail2ban-client也可以直接定义运行中的fail2ban参数
比如增加屏蔽时间为一天
1 | fail2ban-client set ssh-iptables bantime 86400 |
重新读入配置文件
1 | fail2ban-client reload |
其它还有很多用法,可以不带参数执行fail2ban-client查看更多选项。
因为fail2ban的框架,所以可以执行修改filter或者action来满足自己的特殊需要,比如我希望改变fail2ban默认的 iptables规则插入方式,那么我就可以到action.d目录下,找到希望修改的action,这里的例子是iptables.conf
默认actionstart的iptables规则有一条是
1 | iptables -I INPUT -p <protocol> --dport <port> -j fail2ban-<name> |
这样就把fail2ban的规则插到INPUT链的最前面,而我希望自己写的一条iptables -A INPUT -p ALL -s 1.2.3.4/32 -j ACCEPT一直作为第一条规则从而使自己的IP作为信任IP不受防火墙后面规则的限制。那么就要修改fail2ban的启动规则,把上面那条改为
1 | iptables -I INPUT 2 -p <protocol> --dport <port> -j fail2ban-<name> |
这样fail2ban就会把自己的规则作为INPUT链的第二条规则插入,而不影响第一条。
这里只是一个很简单的例子,你可以根据自己的规则,对action做更多的修改。
而在filter.d目录里就是一些日志的正则式匹配规则,系统自带了一些常见软件的匹配,如 sshd,apache,postfix,vsftpd,pure-ftpd等等。来看看sshd的规则,就能了解这些filter应该怎么写,你就可以用fail2ban来保护更多自己的服务。
sshd.conf的内容
1 2 3 4 5 6 7 8 | [Definition] failregex = Authentication failure for .* from <HOST> Failed [-/w]+ for .* from <HOST> ROOT LOGIN REFUSED .* FROM <HOST> [iI](?:llegal|nvalid) user .* from <HOST> ignoreregex = |
可以看到,每行一则正则式,对应各种错误认证,如果你的sshd版本错误认证日志项不太一样,可以修改这里的,或者加入更多。



五月 28th, 2010 on 09:48
Great job!
四月 2nd, 2011 on 16:31
you like it or not
四月 10th, 2011 on 07:37
sure, I like it