問題發現
登錄雲主機,根據提示消息,發現正遭受SSH爆破攻擊,IP地址為159.65.230.189
查看登錄相關安全日志:tail -f /var/log/secure
,發現其他嘗試爆破IP106.12.183.6
、182.61.166.179
、220.88.40.41
百度搜索IP地址為國外IP,有2個IP查詢顯示百度,不知道是啥原因。
另外發現該IP在其他地方也存在SSH攻擊,打開鏈接網址為:http://antivirus.neu.edu.cn/scan/ssh.php ,為東北大學網絡中心-網絡威脅黑名單系統中存在攻擊記錄
該網站給出了封鎖這些黑IP的方法,可以參考使用,本系統未采用,而是使用了下文的“防止 SSH 暴力破解腳本”。
#==========開始復制========== ldd `which sshd` | grep libwrap # 確認sshd是否支持TCP Wrapper,輸出類似:libwrap.so.0 => /lib/libwrap.so.0 (0x00bd1000) cd /usr/local/bin/ wget antivirus.neu.edu.cn/ssh/soft/fetch_neusshbl.sh chmod +x fetch_neusshbl.sh cd /etc/cron.hourly/ ln -s /usr/local/bin/fetch_neusshbl.sh . ./fetch_neusshbl.sh #=========結束復制==========
防止 SSH 暴力破解腳本
注:同一個IP地址超過10次的嘗試,就加入/etc/hosts.deny。
vi ssh_pervent.sh
#! /bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /root/sshPrevent/black.txt DEFINE="10" for i in `cat /root/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@VM_0_11_centos ~]# crontab -e */5 * * * * /bin/bash /root/sshPrevent/ssh_pervent.sh # 每五分鍾檢查一次 # 重啟crontab [root@VM_0_11_centos ~]# systemctl restart crond
五分鍾后,查看是否成功:
[root@VM_0_11_centos sshPrevent]# cat black.txt 101.36.138.61=1 103.133.109.143=4 103.15.62.69=42 103.21.228.3=8 103.23.100.87=50 104.131.113.106=1 104.131.37.34=51 104.131.83.45=51 104.200.110.184=49 104.210.60.66=14 104.211.79.54=50 104.244.76.201=6 104.244.79.242=6 ...... ------------------我是分割線------------------
[root@VM_0_11_centos sshPrevent]# 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.15.62.69 sshd:103.21.228.3 sshd:103.23.100.87 sshd:104.131.37.34 sshd:104.131.83.45 sshd:104.200.110.184 sshd:104.210.60.66 sshd:104.211.79.54 sshd:106.12.119.148 sshd:106.12.127.183 sshd:106.12.130.235 sshd:106.12.13.138 sshd:106.12.17.107 sshd:106.12.183.6 sshd:106.12.199.98 sshd:106.12.24.1 sshd:106.12.241.109 sshd:106.12.30.229 sshd:106.12.60.137 sshd:106.12.80.204 sshd:106.13.52.234 sshd:106.13.56.45 ...... ------------------我是分割線------------------
爆破源IP成功加入阻攔列表,查看secure日志,可以看到加入阻攔列表的IP被拒絕連接,阻斷爆破成功。
參考鏈接
- 【Linux CentOS 防止SSH暴力破解】 https://www.cnblogs.com/l-hh/p/11204251.html
- 【Centos下防止ssh暴力破解】 https://www.jianshu.com/p/fc41ad4399d9
本文首發於【路飛小站】http://www.noofi.cn
長按二維碼關注我們