自部署OpenBSD以来,经常发现有script kid使用暴力破解的方式试探SSH的密码,一直以来都不以为然,最近总部下发的通报让我痛下决心,非解决这个问题不可。
解决方式有两种:
- 将密码验证更改为证书认证;
- 实时监控
/var/log/authlog
,一旦发现非法IP,就列入黑名单,使用pf对黑名单进行过滤。
考虑到证书方式有些麻烦,所以我选择了第二种,经过一番研究,决定采用pf+fail2ban来实现。
1. 工作原理
OpenBSD使用/var/log/authlog来中记录每一次SSH访问,包括访问IP、用户名,以及认证结果(失败或成功),如下所示:
从上面的记录可以看出,除了221.182.194.246
这个用户输入了正确的帐号/密码外,211.137.70.139
则一直在尝试不同的用户名和密码,这个用户的异常行为是:短时间内重复尝试登录失败。从这可以判断该IP是一个非法IP,这时候我们可以使用一个定时执行的脚本,扫描/var/log/authlog
,统计IP的访问失败次数,当达到一定阀值的时候就将结果添加到pf的这个table中,再利用block语句就可以将恶意IP过滤掉。
fail2ban就是这个定时脚本。
2. 安装
fail2ban最新版本为0.9,OpenBSD 4.9的port中版本比较低,因此我采用了编译源码方式安装fail2ban最新版本。
3. 配置
3.1 pf
pf的配置非常简单,只要在pf.conf
配置文件靠前的位置添加anchor fail2ban
这一条语句即可。这就在pf.conf
中创建了一个临时锚,同时创建了一个同名的table,fail2ban将对该table进行增、删IP的操作。
3.2 fail2ban
在配置fail2ban之前,需说明fail2ban是如何工作的,先来看看它的配置文件目录:
- fail2ban是一个python脚本,将通过
fixme
启动,随后一直在后台运行,实时监控/var/log/authlog
这个文件;
- 一旦
/var/log/authlog
发生变化,fail2ban将根据fail2ban.conf
和filter.d/
的定义,使用sshd-pf.conf
和sshd-ddos-pf.conf
这两个过滤器对变化日志进行比对;
- 若变化日志符合规则,而且数量超出定义的阀值,fail2ban将触发
action.d/
中相对应的的指令,将非法IP添加到pf的fail2ban这个黑名单中;
- 该非法IP再次访问访问OpenBSD SSHd时候将被block语句拒绝掉;
- fail2ban脚本会定时对pf黑名单进行清理,也就是过一段时间后,该IP又可以重新访问OpenBSD的SSHd服务。之所以这么设计是有原因的,因为黑客往往通过僵尸网络对目标进行攻击,非法IP是动态变化的,所以不能永久把它列入黑名单。
下面将对配置文件进行分别说明:
fail2ban.conf
该文件配置fail2ban的log级别、log文件路径和socket。
jail.conf
,jail是监牢的意思,该文件相当于一个监狱,建了很多个单间,每个单间针对一个单独的服务,在本例中就启用了ssh-pf和ssd-ddos-pf这两个jail,相对应的,filter.d/
目录里面也必须包含同名的配置文件。
filter.d/
,该目录中包含了sshd-pf.conf
和ssh-ddos-pf.conf
这两个文件,相当于过滤器,sshd-pf.conf
文件的内容如下:
这些配置文件中最重要的是failregex
,这个语句由一组正则表达式组成,将被用于与非法入侵日志进行比较。看起来像火星文,不过读者大可放心,fail2ban已经提供了丰富的过滤器,无需担心。
action.d/
,fail2ban根据该目录的文件向防火墙发出指令。譬如iptables.conf,
由于fail2ban并不支持pf,所以需用户自行创建pf-drop-all.conf
这个文件,内容如下:
这样就完成了整个fail2ban的配置,实际需要配置的内容很少,主要是要创建pf-drop-all.conf
这个文件。
4. 启动&停止
启动
停止
5. 排错
如果发现异常,fail2ban会显示非法IP信息。