fail2ban 的使用

前一直都是用自己写的一个脚本来做阻止恶意链接.原理就是按一定时间间隔分析日志,超过3次的 ip 加到 iptables 里面 ban 掉.一直也都相安无事.

不知是否是 pb4 发布的原因,今天发现日志里有很多 “authentication failure” 的记录,而且数量很多,数量相当大,我的脚本要淘汰了.只好上 fail2ban.

最新的 0.83 版的 fail2ban  需要在 python 2.4 以上.我的系统很老, as4u4 ,不能满足条件,直接升 python 又怕影响到系统其他工具出错.那就重新编个吧.

# wget http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tgz # tar -zxvf Python-2.5.2.tgz # cd Python-2.5.2 # ./configure --prefix=/usr/local/python-2.52 # make && make install # cd /usr/local/ # ln -s python-2.51/ python

这样 python 2.52 就装好了

# wget http://nchc.dl.sourceforge.net/sourceforge/fail2ban/fail2ban-0.8.3.tar.bz2 # bzip2 -d fail2ban-0.8.3.tar.bz2 # tar -xvf fail2ban-0.8.3.tar # mv fail2ban-0.8.3 fail2ban # cd fail2ban # /usr/local/python/bin/python setup.py install

移动生成的可执行文件到 /usr/bin

# mv /usr/local/python/bin/fail2ban-* /usr/bin

如果没有意外,fai2ban 也装好了.所有的配置文件都在 /etc/fail2ban 这个目录下.

拷贝源代码树files目录下的redhat-initd到init.d目录用来自启动.
cp files/redhat-initd /etc/init.d/fail2ban chmod 0755 /etc/init.d/fail2ban

最后写一个logrotate的配置文件,并拷贝成/etc/logrotate.d/fail2ban,用来定期清理日志文件

/var/log/fail2ban.log { missingok notifempty size 30k create 0600 root root postrotate /usr/bin/fail2ban-client reload 2> /dev/null || true endscript }

简单的设置下 jail.conf 就可启动 fail2ban 了.如下:

默认fail2ban.conf里面就三个参数,而且都有注释.
#默认日志的级别
loglevel = 3
#日志的目的
logtarget = /var/log/fail2ban.log
#socket的位置
socket = /tmp/fail2ban.sock
jail.conf配置里是fail2ban所保护的具体服务的配置,这里以SSH来讲。<!–break–>
在jail.conf里有一个[DEFAULT]段,在这个段下的参数是全局参数,可以被其它段所覆盖。
#忽略IP,在这个清单里的IP不会被屏蔽
ignoreip = 127.0.0.1 172.13.14.15
#屏蔽时间
bantime = 600
#发现时间,在此期间内重试超过规定次数,会激活fail2ban
findtime = 600
#尝试次数
maxretry = 3
#日志修改检测机制
backend = auto

[ssh-iptables]
#激活
enabled = true
#filter的名字,在filter.d目录下
filter = sshd
#所采用的工作,按照名字可在action.d目录下找到
action = iptables[name=SSH, port=ssh, protocol=tcp]
mail-whois[name=SSH, dest=root]
#目的分析日志
logpath = /var/log/secure
#覆盖全局重试次数
maxretry = 5
#覆盖全局屏蔽时间
bantime = 3600
对jail.conf进行一定的设置后,就可以使用fail2ban了。
启动fail2ban
/etc/init.d/fail2ban start
启动之后,只要符合filter所定义的正则式规则的日志项出现,就会执行相应的action。由于0.8源码树采用客户机/服务器的模式,因此可以很方便的查询fail2ban的执行情况。比方所,要查询刚才定义的“ssh-iptables”段的情况,只要执行
fail2ban-client status ssh-iptables
会打印出结果
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参数
比如增加屏蔽时间为一天
fail2ban-client set ssh-iptables bantime 86400
重新读入配置文件
fail2ban-client reload
其它还有很多用法,可以不带参数执行fail2ban-client查看更多选项。
因为fail2ban的框架,所以可以执行修改filter或者action来满足自己的特殊需要,比如我希望改变fail2ban默认的iptables 规则插入方式,那么我就可以到action.d目录下,找到希望修改的action,这里的例子是iptables.conf
默认actionstart的iptables规则有一条是
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的启动规则,把上面那条改为
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的内容
[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版本错误认证日志项不太一样,可以修改这里的,或者加入更多。
完全设置完毕后,过了一段时间,查看日志/var/log/fail2ban.log,嘿嘿~ :cool:
2007-05-30 17:42:49,681 fail2ban.actions: WARNING [ssh-iptables] Ban 219.235.231.76
2007-05-30 17:48:00,823 fail2ban.actions: WARNING [ssh-iptables] Ban 60.191.63.180
2007-05-30 18:42:50,456 fail2ban.actions: WARNING [ssh-iptables] Unban 219.235.231.76
2007-05-30 18:48:01,424 fail2ban.actions: WARNING [ssh-iptables] Unban 60.191.63.180
2007-05-30 23:14:43,921 fail2ban.actions: WARNING [ssh-iptables] Ban 59.42.210.176
2007-05-31 00:14:44,797 fail2ban.actions: WARNING [ssh-iptables] Unban 59.42.210.176
2007-05-31 01:49:14,241 fail2ban.actions: WARNING [ssh-iptables] Ban 58.143.242.123
2007-05-31 02:49:15,236 fail2ban.actions: WARNING [ssh-iptables] Unban 58.143.242.123
2007-05-31 07:20:54,717 fail2ban.actions: WARNING [ssh-iptables] Ban 210.51.22.207
2007-05-31 08:20:55,297 fail2ban.actions: WARNING [ssh-iptables] Unban 210.51.22.207



=============

源码可以在这里下载到:
http://sourceforge.net/project/showfiles.php?group_id=121032&package_id=132537

1.安装在RHEL5上


tar -jxvf fail2ban-0.8.3.tar.bz2

然后进入目录cd fail2ban-0.8.3/
用root用户执行

./setup.py install

现在config文件已经安装在/etc/fail2ban下面

接下来
cp fail2ban-0.8.3/files/suse-initd /etc/init.d/fail2ban
chmod 755 /etc/init.d/fail2ban

Integrate fail2ban into logrotate:

create file "/etc/logrotate.d/fail2ban":

/var/log/fail2ban.log {
  weekly
rotate 7
missingok
compress
postrotate
  /usr/bin/fail2ban-client reload 1>/dev/null || true endscript }

注:

The path to your fail2ban-client needs to be adjusted (# whereis fail2ban-client)



# 忽悠 IP范围 如果有二组以上以空白做为间隔 192.168.10.0/24
ignoreip = 127.0.0.1

# 设定 IP 被封锁的时间(秒),如果值为 -1,代表永远封锁
bantime  = 600

# 设定在多少时间内达到 maxretry 的次数就封锁
findtime  = 600

# 允许尝试的次数
maxretry = 3

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

[ssh-iptables]

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

#针对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
bantime  = 1800

建议设置成maxretry为 3 表示3次错误就封锁

2.现在启动fail2ban进行测试用一个IP不断尝试登录ssh

下面是secure的一个小段,修改过的

Did not receive identification string from 192.168.11.2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41017 ssh2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41096 ssh2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41162 ssh2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41209 ssh2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41267 ssh2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41323 ssh2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41376 ssh2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41433 ssh2
 Invalid user test from 192.168.11.2
 Failed password for invalid user test from 192.168.11.2 port 41484 ssh2

我们再来看看fail2ban的log

fail2ban.actions: WARNING [ssh-iptables] Ban 192.168.11.2
fail2ban.actions: WARNING [ssh-iptables] Unban 192.168.11.2

RPM安裝
a.下載fail2ban
elinks http://dag.wieers.com/rpm/packages/fail2ban/fail2ban-0.8.1-1.el5.rf.noarch.rpm
b.安裝
rpm -ivh fail2ban-0.8.1-1.el5.rf.noarch.rpm

3.設定
vim /etc/fail2ban/jail.conf

ignoreip = 192.168.1.0/24   (忽略的ip)
bantime = 3600
  (封鎖的時間)
findtime = 300
  (搜尋的時間範圍)
maxretry = 3
  (錯誤的次數)

例:
[ssh-iptables]

enabled = true
  (true 開啟 false關閉)
filter = sshd
action = iptables[name=SSH, port=22, protocol=tcp]
sendmail-whois[name=SSH, dest=you@mail.com, sender=fail2ban@mail.com]
logpath = /var/log/secure
  (LOG位置)
maxretry = 3


4.設定完後重新啟動服務
service fail2ban restart


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

而总共抓到的有三个

2008-04-20 01:39:55,645 fail2ban.actions: WARNING [ssh-iptables]  Ban 212.241.214.176
2008-04-20 02:39:56,301 fail2ban.actions: WARNING [ssh-iptables]  Unban 212.241.214.176
2008-04-20 03:59:58,811 fail2ban.actions: WARNING [ssh-iptables]  Ban 218.28.41.108
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值