查询linux服务器事物传输失败日志,查看fail2ban日志代替lastb查看登录失败记录
之前我曾經用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
執行一下,成功!
總結
以上是生活随笔為你收集整理的查询linux服务器事物传输失败日志,查看fail2ban日志代替lastb查看登录失败记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven 对于java的要求_如何在M
- 下一篇: 强行更改linux服务器时间,加强Lin