阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

192次阅读
没有评论

共计 1574 个字符,预计需要花费 4 分钟才能阅读完成。

之前我曾经用 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
正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计1574字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中