检查远程主机发送的 ECDSA 密钥的指纹

Wat*_*ker 31 ssh fingerprint

尝试ssh进入服务器时,我收到了众所周知的警告消息:

$ ssh whateverhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxx/xxxxxxx.
Please contact your system administrator.
Add correct host key in /home/user/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/user/.ssh/known_hosts:10
ECDSA host key for ipofmyhost has changed and you have requested strict checking.
Host key verification failed.
Run Code Online (Sandbox Code Playgroud)

我知道为什么,因为我更改了此类服务器的 ip。但如果不是这样,我如何检查远程主机发送的 ECDSA 密钥的指纹?

我尝试通过以下方式这样做:

echo -n ipofthehost | sha256sum
Run Code Online (Sandbox Code Playgroud)

但我没有得到相同的指纹。我也尝试过类似AWS 中的“hostname,ip” ,但没有得到任何匹配。

如果我从我的 known_hosts 文件中删除入口并ssh再次尝试,它会成功并告诉以下内容:

ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxx/xxxxxxx.
Are you sure you want to continue connecting (yes/no)?
Run Code Online (Sandbox Code Playgroud)

那么什么是应用 sha256sum 来获取指纹以及我如何检查它?

Xav*_*cas 19

公钥指纹不是 IP 地址字符串的简单哈希。

要检索远程主机公钥,您可以使用ssh-keyscan <IP address>,然后您可以使用常用工具提取其指纹 ( ssh-keygen -lf <public_key_file>)。

最后,您可以将known_hosts文件中的当前指纹与ssh-keygen -l -F <domain_or_IP_address>.

  • 我很困惑为什么当第一次通过 SSH 连接并强制使用 ecdsa 密钥时(`ssh -oHostKeyAlgorithms='ecdsa-sha2-nistp256' william@my.server`)它给了我一个 43 位字母数字指纹(`ECDSA密钥指纹是 SHA256:sBKcTiQ5V .... 等`)但是当我运行 `ssh-keygen -l -f /etc/ssh/ssh_host_ecdsa_key.pub` 时,我得到 32 个字符的十六进制?? (2认同)
  • @WilliamTurrell 发生这种情况是因为你的服务器必须有一个较旧的(可能是 openSSH 6.8 之前的)版本的 `ssh-keygen` (或者你的服务器提供商没有跟上时代的步伐,仍然只提供 md5 哈希值而不是新的) SHA256)。这里列出了解决方法:http://superuser.com/questions/929566/ (2认同)

Wat*_*ker 10

更详细一点:因为警告消息是指远程主机发送的ECDSA密钥的指纹,所以我们收集有关主机的公共 (ECDSA) 密钥的信息:

ssh-keyscan -t ecdsa <IP_address_or_hostname> ECDSA_file_to_compare
Run Code Online (Sandbox Code Playgroud)

然后我们可以在known_hosts文件中找出公共 (ECDSA) 密钥的位置:

ssh-keygen -l -F ipofhost
Run Code Online (Sandbox Code Playgroud)

如果我们要比较指纹,我们必须将其放入known_hosts文件的内容中(仅与该主机相关的条目)。我们可以称其为 ecdsa_file_from_known_hosts,然后将它们进行如下比较:

ssh-keygen -lf ecdsa_file_to_compare
ssh-keygen -lf ecdsa_file_from_known_hosts
Run Code Online (Sandbox Code Playgroud)

并检查它们是否显示相同的哈希值。

当然它们不匹配,这就是我收到警告消息的原因(ssh内部检查此匹配)。如果我们确定 IP 地址更改(因此我们没有遭受中间人攻击),我们可以删除该主机在我们known_hosts文件中的条目,下次我们ssh进入它时,一个新的条目因为它将被添加到这样的文件中。