再过5分钟你就能了解如何简单防治暴力破解ssh登陆啦

396 阅读1分钟

1. centos7

centos7用的是firewall添加单个黑名单只需要把ip添加到/etc/hosts.deny, vi /etc/hosts.deny 添加你要禁止的ip就可以了。 示例:多次失败登录即封掉IP,超过5次的就加到黑名单。

  1. 创建脚本 vi /usr/local/bin/secure_ssh.sh
#! /bin/bash
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /usr/local/bin/black.txt
for i in `cat  /usr/local/bin/black.txt`
do
  IP=`echo $i |awk -F= '{print $1}'`
  NUM=`echo $i|awk -F= '{print $2}'`
   if [ $NUM -gt 5 ];then
      grep $IP /etc/hosts.deny > /dev/null
    if [ $? -gt 0 ];then
      echo "sshd:$IP:deny" >> /etc/hosts.deny
    fi
  fi
done
  1. 创建文件  touch /usr/local/bin/black.txt
  2. 添加定时任务 crontab -e

*/5 * * * *  sh /usr/local/bin/secure_ssh.sh

  1. 检查 crontab -l -u root
  2. 启动cron服务
/bin/systemctl start crond.service   //启动
/bin/systemctl stop crond.service    //停止
/bin/systemctl restart crond.service //重启
/bin/systemctl reload crond.service  //重新加载配置