如何计算与Putty显示相同的SSH指纹?

时间:2017-09-20 12:45:03

标签: ssh

首次连接到Putty中的新SSH主机时,它会显示一条消息,要求我验证RSA指纹:

Putty First Connection

在网络上查看相同的信息时(例如通过Wireshark),相同的值不会显示,而是显示为:

enter image description here

如何根据数据包捕获中显示的信息计算指纹?

1 个答案:

答案 0 :(得分:0)

wireshark中显示的值是服务器的完整公钥。该值的指纹(MD5哈希值)以putty显示给用户,因为它更容易(更短)读取,期望用户匹配整个密钥。

要计算公钥指纹,首先需要将Wireshark给出的十六进制流转换为字节流equivilent,然后从此计算MD5哈希并以十六进制格式输出。

在python中的粗略实现是在STDIN上取下wireshark值(HEX DH主机密钥被复制为HEX流)并在STDOUT上输出指纹:

import md5
import sys

# Accepts a wireshark encoded string on STDIN an outputs MD5 fingerprint to STDOUT

# The value copied from the 'HEX DH host Key' as a HEX Stream
wireshark_key = sys.stdin.readline()

# Change the HEX value into the raw byte stream, which will include non-printable characters
hex_string = wireshark_key.strip().decode("hex")

# Calculate the MD5 Hash of the byte stream and output in Hexidecimal     format
md5_fingerprint = md5.new(hex_string).hexdigest()

# Tidy up the output so it matches what Putty displays
putty_fingerprint = ":".join([md5_fingerprint[i:i+2] for i in range(0, len(md5_fingerprint), 2)])

print(putty_fingerprint)

要运行此操作,请将wireshark值(公钥)保存到文件中,然后执行:

cat <key.txt> | python scriptname.py

然后输出应该与Putty在第一次连接和事件日志中显示的内容相匹配。

以下网页非常有用:图中显示了这一点:

http://passionateaboutis.blogspot.co.uk/2015/07/ssh-fingerprint-from-pcap.html

https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Public_Key_Authentication

虽然这个脚本对于一个关闭案例可能很有用,但如果您需要获取大量主机的指纹,那么nmap和其中一个NSE脚本可能会更有效:

https://nmap.org/nsedoc/scripts/ssh-hostkey.html

将NMAP的输出保存为XML将自动存储所有hsots的计算指纹。