草船借箭:通过ssh蜜罐来获取常用密码本

很早之前就看过类似的方法:由于具有公网 IP 的云服务器几乎每一刻都会有人尝试登录,将 TCP/22 端口背后的 SSH 服务端替换为 SSH 蜜罐,记录所有登录尝试,就可以收集大量的用户名和密码。前两天在知乎看到一个实操:https://zhuanlan.zhihu.com/p/659197095 效仿了一下,效果不错,于是记录一下操作过程。

本文使用的操作系统是 Ubuntu 22.04,云服务器供应商是微软 Azure。

迁移默认 sshd 监听端口

SSH 协议的端口是 TCP/22,因为扫描都是走默认端口的,所以我们必须把原有的 sshd 服务换到别的端口。

编辑 /etc/ssh/sshd_config 文件,找到 Port 22 这一行,默认情况下它可能是被注释的。取消注释,将 22 改成其他端口。1024-65535 均可,改之前最好搜索一下,确保端口不是常用的。

然后在云服务器控制台的安全组(或者防火墙,或者其他类似的地方)放通你刚刚修改的端口,规则是 TCP 入站。注意不要删掉 SSH 默认的 22 端口,稍后会用到。

重启服务器,尝试使用 SSH 连接服务器,注意使用新端口,确保能够正常建立连接。

安装 fakessh 服务

https://github.com/fffaraz/fakessh

这是一个用 Go 写的 SSH 蜜罐程序,能够记录所有的 SSH 登录尝试,并记录到日志文件。作者提供了两种安装方法:Go 和 Docker。我不会用 Docker,恰好服务器上也安装有 Go 工具链,所以用 Go 安装。

安装服务:

go install github.com/fffaraz/fakessh@latest

编译好的 fakessh 可执行文件位于 ~/go/bin/ 中。

然后执行

sudo setcap 'cap_net_bind_service=+ep' ~/go/bin/fakessh

然后使用 screen 或者 tmux 这样的终端复用工具开一个新的 session,在里面运行 fakessh

~/go/bin/fakessh .

传入的参数是一个目录,指示 fakessh 将日志文件输出到何处。此处指定为当前目录。

于是这个蜜罐就在 TCP/22 端口上跑起来了。

登录日志的解析

fakessh 程序产生的日志以 .log 作为后缀名,每次运行生成一个新文件。其中的内容大致是这样:

2024/06/17 09:55:45.473027 185.191.79.16:11216
2024/06/17 09:55:46.210441 185.191.79.16:11216 SSH-2.0-Go root Ab123456
2024/06/17 10:04:53.554819 194.169.175.35:63966
2024/06/17 10:04:55.812057 194.169.175.35:63966 SSH-2.0-libssh_0.10.5 sshd admin
2024/06/17 10:08:47.796645 167.94.146.56:59164
2024/06/17 10:09:14.218134 85.209.11.227:21208
2024/06/17 10:09:15.381395 85.209.11.227:21208 SSH-2.0-Go root broadguam1
2024/06/17 10:12:20.369670 185.191.79.16:6452
2024/06/17 10:12:21.095747 185.191.79.16:6452 SSH-2.0-Go root !QAZ2wsx
2024/06/17 10:14:08.683929 34.70.10.201:48278
2024/06/17 10:19:09.792239 170.187.181.93:44270
2024/06/17 10:19:10.629006 170.187.181.93:44270 SSH-2.0-libssh_0.9.6 ercico ercico
2024/06/17 10:20:03.410491 43.153.100.227:50694
2024/06/17 10:20:04.028143 43.153.100.227:50694 SSH-2.0-libssh_0.9.6 swlivart swlivart
2024/06/17 10:20:12.920081 43.134.104.206:40570
2024/06/17 10:20:13.362721 43.134.104.206:40570 SSH-2.0-libssh_0.9.6 liangdw liangdw
2024/06/17 10:20:33.221698 183.131.84.38:52628
2024/06/17 10:20:33.602245 183.131.84.38:52628 SSH-2.0-libssh_0.9.6 yanpeimin yanpeimin

可以看到用户名和密码已经记录下来了。之后我们可以使用一些简单的工具来提取用户名和密码。我这里直接让 LLM 写了一段 Python,简单改改得到:

import re
from glob import glob
 
 
input_file_paths = glob('fakessh-*.log')
 
pair_file_path = 'pair.txt'
account_file_path = 'account.txt'
password_file_path = 'password.txt'
 
string_pairs = []
account = []
password = []
 
pattern = re.compile(r'SSH-2\.0-\S+ (\S+) (\S+)')
 
for input_file_path in input_file_paths:
    with open(input_file_path, 'r', encoding='utf-8') as input_file:
        for line in input_file:
            match = pattern.search(line)
            if match:
                string1, string2 = match.groups()
                string_pairs.append((string1, string2))
                account.append(string1)
                password.append(string2)
 
# 去除重复并按字典顺序排序
unique_sorted_pairs = sorted(set(string_pairs))
unique_sorted_account = sorted(set(account))
unique_sorted_password = sorted(set(password))
 
with open(pair_file_path, 'w', encoding='utf-8') as output_file:
    for pair in unique_sorted_pairs:
        output_file.write(f"{pair[0]} {pair[1]}\n")
with open(account_file_path, 'w', encoding='utf-8') as output_file:
    for account in unique_sorted_account:
        output_file.write(f"{account}\n")
with open(password_file_path, 'w', encoding='utf-8') as output_file:
    for password in unique_sorted_password:
        output_file.write(f"{password}\n")

运行一下,就可以得到去重并排序后的用户名和密码了。

密码本效果如下:

!
!!^*$%^
!!admaxim!2015
!123$
!123qwe
!123qweasd
!1qazxsw2
!2#4%6&
!@
!@#$%67890
!@#$%^
!@#$%^&*()
!@#$1234
!@#$1234qwer
!@#$Qwer
!@#.abc
!@#123
!@#123.com
!@#123abc
!@#123admin
!@#123qwe
!@#123qweASD

效率

服务器在日本东部,有一个 IPv4 公网地址,挂了10天左右,收集到2万多个密码。压缩了一下放出来了:
https://eslzzyl.lanzouv.com/iFTXo230byab

posted @ 2024-06-22 09:54  Eslzzyl  阅读(69)  评论(0编辑  收藏  举报