我想编写一个监控插件来检查网络上的各个主机,以确保未启用密码或交互式 SSH 身份验证。也就是说,我需要编写以下代码:
- 连接到 SSH 端口。
- 枚举可用的身份验证方法。
- 验证是否只能进行基于密钥的身份验证。
使用 python 或 bourne sh 代码(使用 ssh
)的方法对我来说是最有趣的,但其他语言、库或提示也很受欢迎。
I'd like to write a monitoring plugin that checks various hosts on my network to make sure that password or interactive SSH authentication is not enabled. That is, I need to write code that:
- Connects to an SSH port.
- Enumerates available authentication methods.
- Verifies that only key based authentication is possible.
Methods using either python or bourne sh code (using ssh
) is most interesting to me, but other languages, libraries or hints are appreciated too.
发布评论
评论(5)
我目前正在自己构建一个,但是,您可以使用 PreferredAuthentications 选项强制 ssh 输出(到 STDERR)支持的方法。这可以使用 grep/python/选择的语言轻松解析。
I'm currently building one myself, however, you can force ssh to output (to STDERR) the supported methods by using the PreferredAuthentications option. This can easily be parsed with grep/python/language of choice.
RFC 4252 定义了 SSH 中的身份验证,内容如下:
因此,您可以发送无身份验证请求来获取支持的列表。
然而,身份验证本身发生在某些较低级别的操作发生之后(密钥交换就是其中之一),因此您可能需要在 sh 脚本中编写 SSH 协议的一部分,这可能是一项艰巨的任务。
RFC 4252, which defines authentication in SSH, says the following:
So you can send a request for none authentication to get the list of supported ones.
However, authentication itself occurs after certain lower-level actions take place (key exchange is one of them) so you might need to write a part of SSH protocol in sh script, which is probably a non-trivial task.
您现在可以使用名为 nmap 内置 NSE 脚本>ssh-auth-methods 来做到这一点:
另外,有人做了一个类似的python3 脚本。
You can now use the
nmap
built-in NSE script called ssh-auth-methods to do this:In addition, someone has made a similar python3 script.
这是基于 Eadwacer 答案的完整解决方案:
重试是为了避免由于间歇性连接失败(例如超时等)而出现误报。
示例运行如下:
调试信息发送到 stdout > 并且可以轻松静音以进行非交互式运行:
您必须将
1.1.1.1
和2.2.2.2
替换为 SSH 服务器的 IP 地址或主机名。如果您想接受“publickey”之外的其他内容,还应该编辑fgrep
中的字符串。Here is a complete solution based on Eadwacer's answer:
The retries are there to avoid false-positives due to intermittent connection failures like timeouts, etc.
Here is how a sample run looks like:
Debug info is sent to
stdout
and can easily be muted for non-interactive runs:You have to replace
1.1.1.1
and2.2.2.2
with the IP addresses or hostnames of your SSH servers. The string infgrep
should also be edited if you want to accept something else than just "publickey".如果您需要 *nix 解决方案,您还可以连接到 OpenSSH 源。
如果 Windows 适合您 - 您还可以尝试一些 .NET 商业库,它们比 OpenSSH 源方便得多:)
If you need *nix solution, you can also hook into OpenSSH sources.
If Windows suitable for you - you can also try some .NET commercial libraries, they are much handier than OpenSSH sources :)