今天一早,登录到跑Docker的一台服务器,一登录就发现如下提示:
WARNING!The SSH server rejected X11 forwarding request.
Last faild login: Sat Apr 24 07:13:15 CST 2021 from 91.196.148.56 on ssh:notty
There were 133 failed login attempts since the last successful login.
心里顿时就一帮草泥马奔腾,没什么对外的服务,实际也就跑了几个自用的容器,一个密码管理,一个RSS流的,其他没啥,怎么就被爆破了呢?百思不得其解。然后就开始慢慢的查询,经过查询得知居然有1W+的爆破攻击。
0x01 爆破记录
#通过lastb 查询登录失败的记录 wc -l 统计一共的条数
lastb | wc -l
13454
# 通过lastb查询,发现应该是通过跑字典爆破的,记录一共1万多条,
lastb
nagios ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
mysql ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
root ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
backup ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
testwebl ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
manager ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
glassfis ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
nagios ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
ftpuser ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
tmc ssh:notty 180.142.130.246 Wed Apr 21 06:18 - 06:18 (00:00)
test01 ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
hp ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
pcap ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
liy ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
tmc ssh:notty 180.142.130.246 Wed Apr 21 06:18 - 06:18 (00:00)
manager ssh:notty ip209.ip-144-217 Wed Apr 21 06:18 - 06:18 (00:00)
截取部分日志,发现总共大概基本通过如下几个地址
0x02 控制策略
基于系统访问控制策略控制,可以通过系统的访问控制策略阻止这几个ip
# 编辑 /etc/hosts.deny文件,将上述几个地址加入
vim /etc/hosts.deny
sshd:45.76.149.9:deny
,编写脚本 vim /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
创建记录登录失败次数的文件 touch /usr/local/black.txt(此处如果有正常访问被拉黑的话,可以将需要正常访问的加入到访问白名单 vim /etc/hosts.allow 将需正常访问的加入此文件即可)
添加定时任务,每5分钟运行一次上述脚本;
*/5 * * * * sh /usr/local/bin/secure_ssh.sh
0x03 修改配置
通过修改ssh服务配置文件,修改远程端口,指定认证类型,建议使用密钥登录,禁止密码登录生成ssh免密登录密钥。
cd /root/.ssh
#生成密钥对,-t代表类型,有RSA和DSA两种
ssh-keygen -t rsa
Generating public/private rsa key pair.
#密钥文件默认存放位置,按Enter即可
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
#输入密钥锁码,或直接按 Enter 留空
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
#生成的私钥
Your identification has been saved in /root/.ssh/id_rsa.
#生成的公钥
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:K1qy9xxkk1FUuzQxsdfsdf6dfdxxvHw9lQ+xxxx
The key's randomart image is:
+---[RSA 2048]----+
| +. |
| o * . |
| . .O + |
| . *. * |
| S =3+ |
| . =... |
| .oo =+ov+ |
| ==o+B*3o. |
| oo.3=XXX. |
+----[SHA256]-----+
# 然后将密钥文件拷贝或分发到其他需要免密登录的电脑,使用终端登录连线的时候需选泽密钥登录,选中密钥文件即可
# 修改sshd配置文件
Port 2202 #修改sshd默认端口,默认22
PubkeyAuthentication yes #启用密钥登录,前提是先生成密钥
PasswordAuthentication no #禁止密码登录
通过上述设置正常可以限制陌生登录,爆破了。没有绝对安全的系统,所以在使用的时候还是要保持正常的使用,不适用弱密码,不安装陌生来历不明的软件,不运行高危脚本等。
基于上述环境设置,辅助的话可以结合其他监控类软件监测 /usr/local/black.txt 当文件变化,当文件发生变化的时候告警通知提醒,即可即时得到通知,知道有非法爆破。