为防止恶意攻击服务器,特写了如下脚本,并放入crontab中(* */1 * * * root sh /root/hosts_deny.sh),每小时运行一次
#! /bin/bash
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /root/black.txt
DEFINE="100"
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" >> /etc/hosts.deny
echo "vsftpd:$IP" >> /etc/hosts.deny
fi
fi
done
#! /bin/bash
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /root/black.txt
DEFINE="100"
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" >> /etc/hosts.deny
echo "vsftpd:$IP" >> /etc/hosts.deny
fi
fi
done
本文介绍了一个自动化的脚本方案,用于检测并封禁针对服务器的恶意登录尝试。通过定期检查安全日志,该脚本能够统计失败的登录尝试次数,并依据预设阈值将频繁尝试登录的IP地址加入到hosts.deny文件中,有效防止了暴力破解攻击。
3114

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



