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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...