Centos下防止ssh暴力破解密码

 参考文章地址:https://yq.aliyun.com/ziliao/48446

                         https://www.cnblogs.com/lsdb/p/7095288.html

1、收集 /var/log/secure 里面的信息,若是某个IP 链接次数超过一定次数 ,则把此ip记录到/etc/hosts.deny里面。

通过crontab来执行,每天的1点1分执行一次。

59 23 * * * /usr/bin/sh /root/bin/Denyhosts.sh

  

脚本内容如下:

#!/bin/bash
#Denyhosts SHELL SCRIPT

cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"=" $1;}' >/root/bin/Denyhosts.txt
DEFINE="10"
for i in `cat /root/bin/Denyhosts.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
                fi
        fi
done

 

说明:

1.一个IP请求连入,linux的检查策略是先看/etc/hosts.allow中是否允许,如果允许直接放行;如果没有,则再看/etc/hosts.deny中是否禁止,如果禁止那么就禁止连入。

2.实验发现对/etc/hosts.allow和/etc/hosts.deny的配置不用重启就立即生效,但不管重启不重启当前已有会话都不会受影响;也就是说对之前已经连入的,即便IP已配置为禁止登录会话仍不会强制断开。

3.网上发现有些教程写成不是sshd而是in.sshd不是in.telnetd而是telnetd的。

 

原文地址:https://www.cnblogs.com/huangyanqi/p/9023822.html