Linux CentOS 防止SSH暴力破解

一. 问题的发现

昨晚苦逼加班完后,今早上班继续干活时,SSH连接服务器发现异常的提示,仔细看了一下吓一小跳,昨晚9点钟到现在,一夜之间被人尝试连接200+,慌~~~

1. 速度查一下log

[root@zwlbsweb ~]# cd /var/log
[root@zwlbsweb log]# ll -h
-------------------------省略部分信息------------------------------
-rw-------  1 root  root  4.9M Jul 17 10:10 secure
-rw-------  1 root  root   38M Jun 24 03:29 secure-20190624
-rw-------  1 root  root   64M Jun 30 03:10 secure-20190630
-rw-------  1 root  root   46M Jul  7 03:37 secure-20190707
-rw-------  1 root  root   14M Jul 15 03:41 secure-20190715
-------------------------省略部分信息------------------------------

 发现secure日志文件咋都这么大?原来不止是昨晚被攻击,之前就已经挨无数的暴击了。

2. 打开日志文件瞧瞧

发现被无数不同的IP地址和不同的用户进行SSH尝试连接。

随便拿个IP地址,百度一下,全是国外的IP地址,太可恶了:

二. 防范办法

虽然我的密码很复杂了,但是为了以防万一,还是速度百度一下,斩草除根

1. 修改 SSH 默认端口

  • 注:修改之前,记得请把对应端口在防火墙添加规则。

i. 修改SSH端口为 2298(这个端口建议使用 1024 以上的)

[root@zwlbsweb ~]# vim /etc/ssh/sshd_config
---------------配置如下----------------
Port 2298

ii. 重启SSH

[root@zwlbsweb ~]# systemctl restart sshd

iii. 查看端口是否更改

[root@zwlbsweb ~]# netstat -ntlp | grep 2298
tcp    0   0 0.0.0.0:2298   0.0.0.0:*   LISTEN   15156/sshd          
tcp6   0   0 :::2298        :::*        LISTEN   15156/sshd

注:还有个步骤就是“禁止root登录”,我这里就不禁止root登录了,因为密码太复杂记不住,切换root用户不方便。

2. 防止 SSH 暴力破解脚本

注:同一个IP地址超过10次的尝试,就加入/etc/hosts.deny。

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

添加计划任务:

[root@zwlbsweb ~]# crontab -e
*/5 * * * * /bin/bash /sshPrevent/ssh_pervent.sh
# 每五分钟检查一次

# 重启crontab
[root@zwlbsweb ~]# systemctl restart crond

五分钟后,查看是否成功:

[root@zwlbsweb ~]# cat /sshPrevent/black.txt 
103.101.232.208=1
103.108.187.4=2
103.248.220.249=15
104.131.93.33=1
104.236.122.193=2
104.236.186.24=2
104.236.246.16=1
104.244.79.33=4
104.248.211.180=2
......
-------------------------------我是分割线----------------------------------
[root@zwlbsweb ~]# cat /etc/hosts.deny
#
# hosts.deny    This file contains access rules which are used to
#        deny connections to network services that either use
#        the tcp_wrappers library or that have been
#        started through a tcp_wrappers-enabled xinetd.
#
#        The rules in this file can also be set up in
#        /etc/hosts.allow with a 'deny' option instead.
#
#        See 'man 5 hosts_options' and 'man 5 hosts_access'
#        for information on rule syntax.
#        See 'man tcpd' for information on tcp_wrappers
#
sshd:103.248.220.249
sshd:104.248.88.106
sshd:106.12.18.37
sshd:106.51.230.186
sshd:106.75.17.91
sshd:112.21.188.183
sshd:112.221.179.133
......

小小的防范措施就到此完成了!

三. 隔一段时间查看效果如何

半天已经过去了,我们再次查看 secure 日志文件。

SSh连接没有提示了,日志也恢复了正常状态,感谢博友们,收工!

posted @ 2019-07-17 23:11  神奇二进制  阅读(4300)  评论(0编辑  收藏  举报