你使用的是CentOS?发现FTP、SSH经常被无聊人暴力测试?那好接着往下看,保证你看完,以后小人远离你了哦
先来个官方介绍
fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽),如:当有人在试探你的SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,而且可以发送e-mail通知系统管理员,是一款很实用、很强大的软件!
正式开始防小人了啊
一、安装 Fail2ban 服务
下载rpmforge , 里面有大量最新的rpm包.
wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
rpm -ivh rpmforge-release-0.3.6-1.el5.rf.i386.rpm
yum安装吧~~!
yum install fail2ban
fail2ban noarch 0.8.2-3.el5.rf rpmforge 125 k
Installing for dependencies:
gamin
gamin-python
安装是十分简单的拉.
fail2ban 原理
fail2ban 调用 iptables 实时阻挡外界的攻击,按照你的要求在一段时间内找出符合条件的日志,然后动作。
二、设定fail2ban服务
fail2ban 的设定档在这里
/etc/fail2ban
fail2ban.conf 日志设定文档
jail.conf 阻挡设定文档
/etc/fail2ban/filter.d 具体阻挡内容设定目录
三、实施保护
1. 保护 SSH 拦截
先看看fail2ban是如何拦截的?
cat /etc/fail2ban/filter.d/sshd.conf
failregex = ^%(__prefix_line)s(?:error: PAM: )?Authentication failure for .* from <HOST>\s*$
^%(__prefix_line)sFailed [-/\w]+ for .* from <HOST>(?: port \d*)?(?: ssh\d*)?$
^%(__prefix_line)sROOT LOGIN REFUSED.* FROM <HOST>\s*$
^%(__prefix_line)s[iI](?:llegal|nvalid) user .* from <HOST>\s*$
^%(__prefix_line)sUser \S+ from <HOST> not allowed because not listed in AllowUsers$
^%(__prefix_line)sauthentication failure; logname=\S* uid=\S* euid=\S* tty=\S* ruser=\S* rhost=<HOST>(?:\s+user=.*)?\s*$
^%(__prefix_line)srefused connect from \S+ \(<HOST>\)\s*$
^%(__prefix_line)sAddress <HOST> .* POSSIBLE BREAK-IN ATTEMPT\s*$
fail2ban 使用了正则表达式找出:
Authentication failure/authentication failure
ROOT LOGIN REFUSED
refused connect from
POSSIBLE BREAK-IN ATTEMPT
not allowed because not listed in AllowUsers
以上的状况,可以根据你实际要求删减. 其中<HOST> 为建立连接的IP
开启 SSH 拦截
vi /etc/fail2ban/jail.conf
[ssh-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
sendmail-whois[name=SSH, dest=收件者电邮地址, sender=寄件者电邮地址]
logpath = /var/log/secure
maxretry = 3
findtime = 300
bantime = 86400
fail2ban 会按照你的要求去查看/var/log/secure日志文件,然后在findtime = 300 “5分钟” 之内符合条件
的记录下来,如果到达了maxretry = 3 “3次符合条件” 就阻挡这个IP连接22端口 bantime = 86400 “一天的时间”.
2.保护FTP拦截
FTP的服务端种类这里有4种:proftpd、wuftpd、vsftpd、pure-ftpd
注意你自己的FTP使用的种类,好操作哦
这里以 vsftpd 为例子,只要学会一种,大家就能举一反三了。
cat /etc/fail2ban/filter.d/vsftpd.conf
failregex = vsftpd(?:\(pam_unix\))?(?:\[\d+\])?:.* authentication failure; .* rhost=<HOST>(?:\s+user=\S*)?\s*$
\[.+\] FAIL LOGIN: Client “<HOST>”\s*$
这里表示错误地输入用户名/密码的FTP连接.
vi /etc/fail2ban/jail.conf
[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/vsftpd.log
maxretry = 3
bantime = 864000
3. POP3 保护拦截
cat /etc/fail2ban/filter.d/courierlogin.conf
failregex = LOGIN FAILED, .*, ip=\[<HOST>\]$
这里表示错误地输入用户名/密码的pop3连接.
开启 pop3 保护
vi /etc/fail2ban/jail.conf
[POP3]
enabled = true
filter = courierlogin
action = iptables[name=pop3, port=110, protocol=tcp]
logpath = /var/log/maillog
bantime = 1800
findtime = 300
maxretry = 15
大家应该懂得如何看了吧? 我不写拉!好累。参数可以按照你实际要求修改噢~!
4 SMTP 保护拦截
cat /etc/fail2ban/filter.d/couriersmtp.conf
failregex = postfix/smtpd.* warning: unknown\[<HOST>\]: SASL LOGIN authentication failed: authentication failure
这里表示错误地输入用户名/密码的smtp连接.
vi /etc/fail2ban/jail.conf
[SMTP]
enabled = true
filter = couriersmtp
action = iptables[name=smtp, port=25:366, protocol=tcp]
logpath = /var/log/maillog
bantime = 1800
findtime = 300
maxretry = 15
参数可以按照你实际要求修改.
四、看看iptables
iptables -L -nv
pkts bytes target prot opt in out source destination
0 0 fail2ban-pop3-ssl tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:995
773 83329 fail2ban-postfix tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:25
299 12660 fail2ban-pop3 tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:110
301 12740 fail2ban-ftp tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
3354 253K fail2ban-SSH tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
438 33979 fail2ban-httpd tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destinationChain OUTPUT (policy ACCEPT 5703 packets, 829K bytes)
pkts bytes target prot opt in out source destinationChain fail2ban-SSH (1 references)
pkts bytes target prot opt in out source destination
3354 253K RETURN all — * * 0.0.0.0/0 0.0.0.0/0Chain fail2ban-ftp (1 references)
pkts bytes target prot opt in out source destination
301 12740 RETURN all — * * 0.0.0.0/0 0.0.0.0/0Chain fail2ban-httpd (1 references)
pkts bytes target prot opt in out source destination
438 33979 RETURN all — * * 0.0.0.0/0 0.0.0.0/0Chain fail2ban-pop3 (1 references)
pkts bytes target prot opt in out source destination
299 12660 RETURN all — * * 0.0.0.0/0 0.0.0.0/0Chain fail2ban-pop3-ssl (1 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all — * * 0.0.0.0/0 0.0.0.0/0Chain fail2ban-postfix (1 references)
pkts bytes target prot opt in out source destination
773 83329 RETURN all — * * 0.0.0.0/0 0.0.0.0/0Chain fail2ban-smtp (0 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all — * * 0.0.0.0/0 0.0.0.0/0
如何监察呢?
watch -n 1 fail2ban-client status POSTFIX (这里的POSTFIX对应你在设置文件内的设定,更换能查看更多)
Status for the jail: POSTFIX
|- filter
| |- File list: /var/log/maillog
| |- Currently failed: 2
| `- Total failed: 22
`- action
|- Currently banned: 0
| `- IP list:
`- Total banned: 0
看看那些垃圾邮件者真是好无聊!不断地试探我的邮件服务器呢。
五、看看fail2ban的日志
cat /var/log/fail2ban.log | grep ‘] Ban ‘
2012-01-07 20:22:44,575 fail2ban.actions: WARNING [POSTFIX] Ban ip地址
就以看到过去有那些IP拦截了,以及是那个服务。
cat /var/log/fail2ban.log | grep ‘] Unban ‘
可以查看什么时候解的。
最后附上一些设置的中文解释
默认fail2ban.conf里面就三个参数,而且都有注释。
#默认日志的级别
loglevel = 3
#日志的目的
logtarget = /var/log/fail2ban.log
#socket的位置
socket = /tmp/fail2ban.sock
jail.conf配置里是fail2ban所保护的具体服务的配置,这里以SSH来讲。
在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
再附上一些高级应用的方法
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 –dport -j fail2ban-
这样就把fail2ban的规则插到INPUT链的最前面,而我希望自己写的一条iptables -A INPUT -p ALL -s 1.2.3.4/32 -j ACCEPT一直作为第一条规则从而使自己的IP作为信任IP不受防火墙后面规则的限制。那么就要修改fail2ban的启动规则,把上面那条改为
iptables -I INPUT 2 -p –dport -j fail2ban-
这样fail2ban就会把自己的规则作为INPUT链的第二条规则插入,而不影响第一条。
这里只是一个很简单的例子,你可以根据自己的规则,对action做更多的修改。
而在filter.d目录里就是一些日志的正则式匹配规则,系统自带了一些常见软件的匹配,如
sshd,apache,postfix,vsftpd,pure-ftpd等等。来看看sshd的规则,就能了解这些filter应该怎么写,你就可以用fail2ban来保护更多自己的服务。
sshd.conf的内容
[Definition]
failregex = Authentication failure for .* from
Failed [-/\w]+ for .* from
ROOT LOGIN REFUSED .* FROM
[iI](?:llegal|nvalid) user .* from
ignoreregex =
可以看到,每行一则正则式,对应各种错误认证,如果你的sshd版本错误认证日志项不太一样,可以修改这里的,或者加入更多。
完全设置完毕后,过了一段时间,查看日志/var/log/fail2ban.log,嘿嘿~
20012-01-07 17:42:49,681 fail2ban.actions: WARNING [ssh-iptables] Ban 219.235.231.76
20012-01-07 17:48:00,823 fail2ban.actions: WARNING [ssh-iptables] Ban 60.191.63.180
20012-01-07 18:42:50,456 fail2ban.actions: WARNING [ssh-iptables] Unban 219.235.231.76
20012-01-07 18:48:01,424 fail2ban.actions: WARNING [ssh-iptables] Unban 60.191.63.180
20012-01-07 23:14:43,921 fail2ban.actions: WARNING [ssh-iptables] Ban 59.42.210.176
20012-01-08 00:14:44,797 fail2ban.actions: WARNING [ssh-iptables] Unban 59.42.210.176
20012-01-08 01:49:14,241 fail2ban.actions: WARNING [ssh-iptables] Ban 58.143.242.123
20012-01-08 02:49:15,236 fail2ban.actions: WARNING [ssh-iptables] Unban 58.143.242.123
20012-01-08 07:20:54,717 fail2ban.actions: WARNING [ssh-iptables] Ban 210.51.22.207
20012-01-08 08:20:55,297 fail2ban.actions: WARNING [ssh-iptables] Unban 210.51.22.207s
写到这里基本上就结束了,不过有一个要提醒大家一下哦,256MB的VPS运行这个有些吃力,用不用就看你自己的选择了。
