昨晚苦逼加班完后,今早上班继续干活时,SSH链接服务器发现异常的提示,仔细看了一下吓一小跳,昨晚9点钟到如今,一晚上之间被人尝试链接200+,慌~~~web
[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日志文件咋都这么大?原来不止是昨晚被攻击,以前就已经挨无数的暴击了。vim
发现被无数不一样的IP地址和不一样的用户进行SSH尝试链接。bash
随便拿个IP地址,百度一下,全是国外的IP地址,太可恶了:服务器
虽然个人密码很复杂了,可是为了以防万一,仍是速度百度一下,斩草除根app
i. 修改SSH端口为 2298(这个端口建议使用 1024 以上的)ssh
[root@zwlbsweb ~]# vim /etc/ssh/sshd_config ---------------配置以下---------------- Port 2298
ii. 重启SSHtcp
[root@zwlbsweb ~]# systemctl restart sshd
iii. 查看端口是否更改this
[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用户不方便。spa
注:同一个IP地址超过10次的尝试,就加入/etc/hosts.deny。3d
#! /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链接没有提示了,日志也恢复了正常状态,感谢博友们,收工!