JSch:当主机密钥在 known_hosts 和添加 JCE 后出现 UnknownHostKey 异常

Posted

技术标签:

【中文标题】JSch:当主机密钥在 known_hosts 和添加 JCE 后出现 UnknownHostKey 异常【英文标题】:JSch: UnknownHostKey exception when Host Key is in known_hosts AND after adding JCE 【发布时间】:2016-04-13 13:28:08 【问题描述】:

我一直在与 JSch 合作一个项目,连接到 Unix 服务器。使用 ecdsa-sha2-nistp256 进行密钥交换连接到服务器时遇到一些问题。

奇怪的是,我已经从this post 启用了 JCE(使用 JDK 1.8 65),但仍然没有骰子。

如果我正确读取日志,则客户端确实启用了 ecdsa-sha2-nistp256。但它又回到了 ssh-rsa。也许我错过了一步?

错误:

UnknownHostKey: *********. RSA key fingerprint is 1d:ac:f7:50:10:07:51:4b:17:9c:cd:b5:be:25:75:28

这是日志:

INFO: Connecting to ******* port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_6.6.1
INFO: Local version string: SSH-2.0-JSCH-0.1.53
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO: kex: server: ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: 
INFO: kex: server: 
INFO: kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
INFO: kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: none
INFO: kex: client: none
INFO: kex: client: 
INFO: kex: client: 
INFO: kex: server->client aes128-ctr hmac-md5 none
INFO: kex: client->server aes128-ctr hmac-md5 none
INFO: SSH_MSG_KEX_ECDH_INIT sent
INFO: expecting SSH_MSG_KEX_ECDH_REPLY
INFO: ssh_rsa_verify: signature true
INFO: Disconnecting from ******* port 22
INFO: Connecting to ******* port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_6.6.1
INFO: Local version string: SSH-2.0-JSCH-0.1.53
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO: kex: server: ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: 
INFO: kex: server: 
INFO: kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
INFO: kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: none
INFO: kex: client: none
INFO: kex: client: 
INFO: kex: client: 
INFO: kex: server->client aes128-ctr hmac-md5 none
INFO: kex: client->server aes128-ctr hmac-md5 none
INFO: SSH_MSG_KEX_ECDH_INIT sent
INFO: expecting SSH_MSG_KEX_ECDH_REPLY
INFO: ssh_rsa_verify: signature true
INFO: Disconnecting from ******* port 22

编辑: 戴夫的回答是正确的。 Jsch默认请求ssh-rsa,但是我的一些key使用ecdsa-sha2-nistp256。

为了解决这个问题,我通过 HostKeyRepository 循环查找匹配的主机并获取它的类型,然后用它告诉会话请求什么类型的密钥,如下所示:

HostKeyRepository hkr = this.jschConn.getHostKeyRepository();
    for(HostKey hk : hkr.getHostKey())
        if(hk.getHost().equals(this.serverIp))
            String type = hk.getType();
            session.setConfig("server_host_key",type);
        
    

谢谢戴夫!

【问题讨论】:

【参考方案1】:

正如Q you linked 所说,您的日志显示,ecdsa-sha2-nistp256支持,但ssh-rsa首选,并且由于您连接的服务器显然支持RSA,就是用的。而且您的 known_hosts 中显然没有 RSA 密钥。

除了在链接的 Q 中使用ssh 获取 RSA 密钥的简单选项,或者使用ssh-keyscan 更容易获取 RSA 密钥之外,如果您这样做,请快速查看源代码建议

Session.setConfig("server_host_key","ecdsa-sha2-nistp256")

在连接之前它应该只允许该主机密钥,并假设这是您在 known_hosts 中为该服务器拥有的密钥,它应该可以工作,但我还没有测试过。

除此之外:“启用 JCE”我假设您的意思是安装 JCE Unlimited Strength Jurisdiction Policy Files,如JSch Algorithm negotiation fail 的答案中所述。自 2005 年左右以来,JCE 本身已包含在每个 JRE 中。无限制策略仅在您需要超过 128 位的对称密码时才有帮助,如 Q,而您的日志显示您的服务器同意 AES-128 .

【讨论】:

感谢 Dave,我所说的“启用 JCE”是正确的。这里更大的问题是已知的 hosts 文件已经包含几个密钥,这些密钥可以(理论上)使用基于服务器的不同算法。我不想要求特定的密钥类型。相反,我想确保 Jsch 请求的密钥类型与我们已经在文件中拥有的密钥类型相同。也许我需要在尝试连接之前对已知的主机文件进行一些解析 好的,我得到了这个工作。我抓取了 HostKeyRepository 并搜索了与我匹配的 HostKey 对象,然后从对象中抓取了它的类型并在 Session.setConfig() 中使用了它,再次感谢 Dave! @JacobSchwartz ,技术上可以告诉我你的方式吗?例如,您的代码session.setConfig() 有人可以帮忙吗? ***.com/questions/55033909/…【参考方案2】:

这个解决方案对我有用......直到它没有。 HostKey.getHost() 返回的服务器名称 IP 地址都在 known_hosts 行中。使用 String.contains() 而不是 equals() 修复了一些问题。

【讨论】:

以上是关于JSch:当主机密钥在 known_hosts 和添加 JCE 后出现 UnknownHostKey 异常的主要内容,如果未能解决你的问题,请参考以下文章

使用 Paramiko 更改主机密钥时自动更新 known_hosts 文件

使用Paramiko在主机密钥更改时自动更新known_hosts文件

无法将 IP 地址“xxx”的 RSA 主机密钥添加到已知主机列表 (/home/webapp/.ssh/known_hosts)

使用 pysftp 针对使用自定义端口的 known_hosts 文件验证主机密钥

pysftp 忽略禁用的主机密钥检查

Docker,如何处理 ssh 密钥、known_hosts 和 authorized_keys