码迷,mamicode.com
首页 > 系统相关 > 详细

shell 自动检查SSH暴力破解IP并阻断攻击

时间:2017-06-14 16:37:34      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:ssh在5分钟内尝试密码登录错误超过10次的拒绝这个ip

#!/bin/bash

local_ip="192.168.0.4"   #定义本地IP不会被拒绝

tmp_log=`mktemp`

cat /var/log/secure |grep "Failed password for root from" |awk ‘{print $11}‘ |uniq -c > $tmp_log

cat $tmp_log |while read line

        do

        attack_num=`echo $line |awk ‘{print $1}‘`

        attact_ip=`echo $line |awk ‘{print $2}‘`

        if [ "$attack_num" -gt "10" ] && [ "$local_ip" != "$attack_ip" ]

                then

                echo sshd:"$attact_ip":deny >> /etc/hosts.deny

        fi

        done

sort -u /etc/hosts.deny > $tmp_log     #过滤相同IP

cp -f $tmp_log /etc/hosts.deny

rm -f $tmp_log


每5分钟检测一次,在5分钟内尝试密码登录错误超过10次的拒绝这个IP

crontab -e

*/5 * * * * /sh/sshtool.sh


查看被拒绝的IP

cat /etc/hosts.deny

本文出自 “Shell” 博客,请务必保留此出处http://zhizhimao.blog.51cto.com/3379994/1936556

shell 自动检查SSH暴力破解IP并阻断攻击

标签:ssh在5分钟内尝试密码登录错误超过10次的拒绝这个ip

原文地址:http://zhizhimao.blog.51cto.com/3379994/1936556

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!