- 求职 : Linux运维
- 论坛徽章:
- 203
|
fail2ban。
安装:yum -y install fail2ban
如果提示找不到的话,先yum -y install epel-release 安装一下这个源。
安装过后,编辑配置文件:
vim /etc/fail2ban/jail.conf
复制代码
找到[ssh-iptables] 这一段
[ssh-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp] #在iptables INPUT 链中的信息
sendmail-whois[name=SSH, dest=admin@163.com, sender=admin@163.com, sendername="Fail2Ban"]
#dest是配置受到攻击后发送警报邮件的地址,sender配置发送人邮件可以随便写,sendername配置发件人可以不改
logpath = /var/log/secure #日志存放位置
maxretry = 5 #尝试登录次数,如果ssh登录错误超过5次,禁止在bantime设置的时间内再次尝试登录了
复制代码
如果登录超过5次都失败的话,在bantime设置的时间内,用户是无法登录的。默认bantime的设置是300秒,也就是5分钟,我设置的是3600秒,也就是1小时。
如果你想让某些IP不受fail2ban的控制,可以修改ignoreip 这个参数。
:wq 保存后。启动服务
service fail2ban start
复制代码
设置服务开机启动
chkconfig fail2ban on
复制代码
在开启服务后你的邮箱就可以收到一封fail2ban服务启动的邮件,相应的停止服务也会收到邮件
开启服务邮件 正文
Hi,
The jail SSH has been started successfully.
Regards,
Fail2Ban
复制代码
停止服务邮件正文
Hi,
The jail SSH has been stopped.
Regards,
Fail2Ban
复制代码
拒绝登录邮件正文
Hi,
The IP 222.186.56.101 has just been banned by Fail2Ban after
5 attempts against SSH.
Here is more information about 222.186.56.101:
missing whois program
Regards,
Fail2Ban
复制代码
如果你仔细的查看配置文件就会发现,fail2ban 不只能防止ssh的恶意攻击,还有好多项目可以配置如:
pop3, nagios,mysql, asterisk, php, vsftp, apache等的登录都可以进行设置,只不过默认的只有ssh的enabled的参数是true,其他项目的都是false
由于我这台机器是跳板机,通过路由器设置对外开放的只是22端口因此默认的就够用了。
用shell脚本提取lastb命令登录失败超过指定次数的IP加入到iptables,来禁止这些IP登录主机,达到防止恶意攻击的目的。后来为了给主机提供更全面的防护,又安装了fail2ban。
今天早上我收到fail2ban发过来的报警邮件提示我禁止了一个IP登录,IP是219.235.4.22。我登录主机,输入last命令后,结果是这样的
root ssh:notty host-219-235-4-2 Thu Apr 23 19:32 - 19:32 (00:00)
root ssh:notty host-219-235-4-2 Thu Apr 23 19:32 - 19:32 (00:00)
root ssh:notty host-219-235-4-2 Thu Apr 23 19:32 - 19:32 (00:00)
root ssh:notty host-219-235-4-2 Thu Apr 23 19:32 - 19:32 (00:00)
root ssh:notty host-219-235-4-2 Thu Apr 23 19:32 - 19:32 (00:00)
root ssh:notty 61.160.247.150 Thu Apr 23 02:18 - 02:18 (00:00)
root ssh:notty 61.160.247.150 Thu Apr 23 02:18 - 02:18 (00:00)
root ssh:notty 61.160.247.150 Thu Apr 23 02:18 - 02:18 (00:00)
复制代码
前面多了一个host,而且IP的分隔是用“-”,最后一位的IP地址无法显示出来。
我马上想到了查看fail2ban的日志,该日志默认是/var/log/secure
输入命令:
grep 'Failed password for root from' /var/log/secure|grep '219.235.4.22'
复制代码
执行结果为:
Apr 23 19:32:27 localhost sshd[17856]: Failed password for root from 219.235.4.22 port 4993 ssh2
Apr 23 19:32:30 localhost sshd[17856]: Failed password for root from 219.235.4.22 port 4993 ssh2
Apr 23 19:32:31 localhost sshd[17856]: Failed password for root from 219.235.4.22 port 4993 ssh2
Apr 23 19:32:34 localhost sshd[17856]: Failed password for root from 219.235.4.22 port 4993 ssh2
Apr 23 19:32:36 localhost sshd[17856]: Failed password for root from 219.235.4.22 port 4993 ssh2
复制代码
这时IP地址还有对方的端口都显示出来了,比lastb命令要详细的多。
这时我就把之前写的脚本修改了一下,直接读取/var/log/secure :
#!/bin/bash
bad_ip=` grep 'Failed password for root from' /var/log/secure|awk '{print $11,$1,$2}'|sort|uniq -c|awk '$1>4 {print $2}'|xargs`
for ip in $bad_ip; do
in_iptables=`iptables -nvL|grep $ip |wc -l`
if [ $in_iptables -eq 0 ]; then
iptables -I INPUT -s $ip -j REJECT
service iptables save
fi
done
复制代码
执行一下,成功! |
|