如何统计出系统中谁在破解你的密码及破解的次数(分析/var/log/secure)
分析/var/log/secure的IP地址 计算次数 并格式化输出
[root@template ~]# cat /var/log/secure|awk -F "[ :]+" '{ip[$14]=ip[$14]+1} END{for (item in ip) {printf "%-20s %-s \n",item,ip[item] }}'
一、系统:CentOS 6.3 64位
二、方法:读取/var/log/secure,查找关键字 Failed,例如(注:文中的IP地址特意做了删减):
Sep 17 09:08:09 localhost sshd[29087]: Failed password for root from 13.7.3.6 port 44367 ssh2 Sep 17 09:08:20 localhost sshd[29087]: Failed password for root from 13.7.3.6 port 44367 ssh2 Sep 17 09:10:02 localhost sshd[29223]: Failed password for root from 13.7.3.6 port 56482 ssh2 Sep 17 09:10:14 localhost sshd[29223]: Failed password for root from 13.7.3.6 port 56482 ssh2
从这些行中提取IP地址,如果次数达到5次则将该IP写到 /etc/hosts.deny中。
三、步骤:
1、先把始终允许的IP填入 /etc/hosts.allow ,这很重要!比如: sshd:19.16.18.1:allow sshd:19.16.18.2:allow
2、脚本 /root/secure_ssh.sh
#! /bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /root/black.txt DEFINE="5" for i in `cat /root/black.txt` do IP=`echo $i |awk -F= '{print $1}'` NUM=`echo $i|awk -F= '{print $2}'` if [ $NUM -gt $DEFINE ];then grep $IP /etc/hosts.deny > /dev/null if [ $? -gt 0 ];then echo "sshd:$IP:deny" >> /etc/hosts.deny fi fi done

本文介绍了如何通过分析/var/log/secure日志文件,统计并阻止尝试破解root密码的IP地址。使用awk命令提取IP,达到5次失败登录即添加到/etc/hosts.deny,同时使用iptables策略进一步防护。

5401

被折叠的 条评论
为什么被折叠?



