这几天遇到这样的情况,每到晚上感觉自己的代理服务器隔一段时间就要卡一下,很是烦恼!

迫于无奈我去看了一下探针。

好家伙,cpu占用率几乎是100%
tz

代理服务器就只有我一个人用啊,带着疑惑,去查询了服务器日志

cat /var/log/secure

没错的话就是有人在暴力破解!这老六半夜一点多搁着暴力破解我ssh密码😅
jg

详细查了一下全部的攻击源地址
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}'

gj
挺多的,确认是恶意攻击没错了

应对方法

编写脚本将这些地址全部写入 /etc/hosts.deny 文件中拒绝其连接

vim /root/deny_ssh.sh

#! /bin/bash
# 提取所有的IP到black.list文件中
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /usr/local/bin/black.list
 
# 设定次数
define="5"
for i in `cat  /usr/local/bin/black.list`
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
加入定时自动任务
crontab -e
每5分钟检查一次
*/5 * * * *  sh /root/deny_ssh.sh
重启 crontab使其生效
systemctl restart crond
过五分钟后验证 /etc/hosts.deny 文件
cat /etc/hosts.deny

yz