手机版
你好,游客 登录 注册
背景:
阅读新闻

查看fail2ban日志代替lastb查看登录失败记录

[日期:2016-06-17] 来源:Linux社区  作者:cmzsteven [字体: ]

之前我曾经用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

执行一下,成功!
 
本文永久更新链接地址http://www.linuxidc.com/Linux/2016-06/132400.htm
linux
相关资讯       Fail2ban  lastb 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款