本人为了学习和使用VPS因此手中长期配置一到两台VPS,没想到这点苍蝇肉也成了黑客眼里的肥肉。
近来一个月发现被人正在暴力猜解ssh登录密码,心想,这准是有人想要拿shell想要把这点苍蝇肉都想占为己有。然而我也不是眼里能揉这种沙子的人。于是就有了分析一下登录失败记录信息的念头。首先我们执行命令:
lastb >>faillog.txt
将所有登录失败信息导出来,然后我们下载faillog.txt文件到本地目录,就地在此目录中开vscode或jupyter并启用python环境。
我们的faillog.txt文件是一个类似csv的一个文件,只是并非用逗号分割,而是用空格分割,格式如下:
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 181.142.251.23.b Tue Apr 28 22:16 - 22:16 (00:00)
xml ssh:notty 212.64.59.227 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
xml ssh:notty 212.64.59.227 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
lxk ssh:notty h-125-44.a400.pr Tue Apr 28 22:16 - 22:16 (00:00)
我们需要令pandas按一个或一个以上空格作为分隔符来读取为DataFrame。我们下一步就要添加第一行作为整个表格的表头:
于是前几行就成了
user channel sourceip weekday month day starttime to endtime during
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 181.142.251.23.b Tue Apr 28 22:16 - 22:16 (00:00)
xml ssh:notty 212.64.59.227 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
xml ssh:notty 212.64.59.227 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
root ssh:notty 218.92.0.206 Tue Apr 28 22:16 - 22:16 (00:00)
注意,表头要和下面的数据列一一对应起来,表头的列数要和数据的列数相同。需要注意的是,我们因为已经决定以空格作为表分隔符,那么只要有空格分割的部分就要当做一个列。比如“-”前后有空格,所以也要当成一个数据列,大不了后面我们去掉一些用不到的列就是了。
安装pandas:
$ pip install pandas
接下来我们开始导入到DataFrame,我们用read_csv或read_table以\s+模式读入:
import pandas as pd
csvfile = pd.read_csv("faillog.txt",sep="\s+")
data = pd.DataFrame(csvfile, columns=['user', 'sourceip'])
data.head(15)
如之前我们所加的列名,我们只需要猜解时登录的用户名和来源IP地址读入表中,我们只需要将这两个列的列名作为DataFrame的columns的参数加进来就可以了。
执行了data.head(15)这句话以后,就会以表格的形式打印前15行数据。
user | sourceip | |
---|---|---|
0 | root | 218.92.0.206 |
1 | root | 181.142.251.23.b |
2 | xml | 212.64.59.227 |
3 | root | 218.92.0.206 |
4 | xml | 212.64.59.227 |
5 | root | 218.92.0.206 |
6 | root | 218.92.0.206 |
7 | root | 218.92.0.206 |
8 | lxk | h-125-44.a400.pr |
9 | root | 218.92.0.206 |
10 | lxk | h-125-44.a400.pr |
11 | root | 120.92.88.227 |
12 | root | 218.92.0.206 |
13 | shashi | 106.54.205.236 |
14 | shashi | 106.54.205.236 |
我们需要统计下,猜解次数最多的前10名IP地址:
ipcounts = data.loc[:,'sourceip'].value_counts()
headip = pd.DataFrame(ipcounts.head(10))
headip
我们通过loc算符读取所有行以及sourceip这一列,使用value_counts()函数进行频次统计,然后输出暴力猜解前10名IP:
sourceip | |
---|---|
218.92.0.206 | 82882 |
218.92.0.207 | 81502 |
49.88.112.73 | 24522 |
112.85.42.72 | 16506 |
112.85.42.94 | 14506 |
49.88.112.74 | 8302 |
49.88.112.70 | 6138 |
58.198.177.40 | 5204 |
49.88.112.71 | 4092 |
112.85.42.195 | 3980 |
接下来我们统计下一共多少IP在猜解攻击:
ipcounts.shape
输出为
5430
再看一下所有的这些IP总共进行了多少次暴力猜解攻击:
csvfile.shape
输出为
783627
也就是说,这一个月里有5430个IP(肉鸡)在尝试分布式暴力猜解我这个小小的VPS的密码,截止到收集数据为止一共猜解了78万余次,按照25天计,平均每天每分钟21.7次猜解尝试,真的是让在下不胜惶恐以至汗颜。这是准备把我的这台苍蝇肉VPS非要拿下不可?
算了,还是换掉SSH端口+安装Fail2ban,顺便改个超强密码以绝后患。
故事就这么结束了……
才怪!
我还是很好奇,这些黑客究竟都尝试了哪些用户名进行密码猜解?
所以我还是手欠的统计了一下被尝试猜解最多的用户名排名:
usernamecounts = data.loc[:,'user'].value_counts()
tb_usernamecounts = pd.DataFrame(usernamecounts.head(60))
tb_usernamecounts
这回我输出了前60名,够在这里阅读的读者做参考了
user | |
---|---|
root | 425035 |
admin | 28574 |
test | 14836 |
ubuntu | 11050 |
postgres | 10580 |
user | 6964 |
oracle | 5108 |
deploy | 4992 |
git | 4568 |
ftpuser | 4310 |
guest | 2160 |
hadoop | 2124 |
test1 | 1522 |
nagios | 1172 |
ubnt | 1024 |
support | 1000 |
teamspea | 988 |
test2 | 946 |
tester | 936 |
server | 898 |
teste | 868 |
minecraf | 720 |
testftp | 718 |
testuser | 708 |
testing | 680 |
tomcat | 676 |
firefart | 674 |
jenkins | 662 |
backup | 648 |
mysql | 641 |
user1 | 632 |
usuario | 618 |
pi | 606 |
develope | 584 |
zabbix | 576 |
administ | 544 |
dev | 544 |
ts | 544 |
www | 544 |
student | 534 |
ts3 | 524 |
apache | 520 |
test3 | 504 |
webmaste | 488 |
web | 488 |
demo | 480 |
info | 472 |
ansible | 464 |
Admin | 448 |
www-data | 444 |
steam | 444 |
sinusbot | 440 |
tom | 432 |
bot | 428 |
odoo | 428 |
Administ | 416 |
admin1 | 410 |
vnc | 400 |
uftp | 388 |
1234 | 384 |
毫无疑问被猜解 的用户名首当其冲的就是root,因为root不但是最高权限也是开发者最常用的ssh登录用户名。
然后就是
root
admin
test
ubuntu
postgres
user
oracle
deploy
git
ftpuser
guest
hadoop
基本上就跟开发者习惯用的用户名都卯上了…
还没说完呢,如果查查看我前面的那个IP排名的话,你会发现很多东西,比如这些IP的归属地等等。
我在查这些IP的时候意外发现了用于记录IP地址数据库的站点,里面记录了很多受害者所受到攻击的来源IP,都在这个网站上记录了。
https://ip-46.com/
查一下,你就知道是哪来的黑客在攻击你的VPS了。
对于企业来说,收集这些数据有个巨大的好处,就是收集安全大数据,为自己的网站服务进行防护可有的放矢的辨认和识别这些黑客用户,虽然算不上是金矿,但是也算是一种黑矿,保护自己企业服务的黑矿。