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

前端之家收集整理的这篇文章主要介绍了shell 自动检查SSH暴力破解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

原文链接:https://www.f2er.com/bash/392722.html

猜你在找的Bash相关文章