简体   繁体   English

无法交换加密密钥

[英]Unable to exchange encryption keys

I'm facing a hard problem at the moment and I didn't find anything online that can help me.我目前面临一个难题,我没有在网上找到任何可以帮助我的东西。

I want to connect from my server to another one through SSH in order to send instructions (the second server manage Wi-Fi authorizations).我想通过 SSH 从我的服务器连接到另一台服务器以发送指令(第二台服务器管理 Wi-Fi 授权)。

As much as I can say, I think the problem occurred because we updated one server.据我所知,我认为问题的发生是因为我们更新了一台服务器。 (I'm not really sure if the problem has appeared because of it). (我不确定问题是否因此而出现)。

I'm from a Windows Server and I want to call a Linux one.我来自 Windows Server,我想调用 Linux。

Here is the script :这是脚本:

function executeCommand($command) {
    $infoConnection = getInfoConnection();

    $out = '';
    //The Warning occurs here, impossible to go further
    $connection = ssh2_connect($infoConnection["hostname"], 22);

    if ($connection === false) {
        $error = error_get_last();
        throw new Exception("
        Error Type : ".$error["type"]."<br/>
        Message : ".$error["message"]."<br/>
        File : ".$error["file"]."<br/>
        Line : ".$error["line"]."<br/>");
    }

    ssh2_auth_password($connection, $infoConnection["username"], $infoConnection["password"]);

    $stdio_stream = ssh2_shell($connection);
    sleep(2);
    fwrite($stdio_stream,$infoConnection["username"]."\n");
    sleep(1);
    fwrite($stdio_stream,$infoConnection["password"]."\n");
    sleep(1);

    fwrite($stdio_stream, $command."\n");
    sleep(1);
    while($buffer = fgets($stdio_stream)) {

        $out .= $buffer;
    }
    fwrite($stdio_stream, 'exit');
    unset($connection);

    return $out;
}

I had this problem when trying to access a focal ubuntu server from a little old xenial through ssh2_connect.我在尝试通过 ssh2_connect 从一个旧的 xenial 访问焦点 ubuntu 服务器时遇到了这个问题。 The solution was to update libssh2-1.解决方案是更新 libssh2-1。 Even with php showing the old version, it worked normally.即使 php 显示旧版本,它也能正常工作。

In the xenial, I added the focal repository, then installed the latest version of libssh2-1, restarted PHP to apply and removed focal repository.在 xenial 中,我添加了焦点存储库,然后安装了最新版本的 libssh2-1,重新启动 PHP 以应用和删除焦点存储库。

sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse"
sudo apt-get update
sudo apt -y install libssh2-1
sudo add-apt-repository -r "deb http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse"
sudo apt-get update

Warning: ssh2_connect() [function.ssh2-connect]: Error starting up SSH connection(-5): Unable to exchange encryption keys in ../aff_wifi.php on line 203警告:ssh2_connect() [function.ssh2-connect]:启动 SSH 连接时出错(-5):无法在第 203 行的 ../aff_wifi.php 中交换加密密钥

libssh2 0.x only supports Diffie-Hellman SHA1 based key exchange. libssh2 0.x 仅支持基于 Diffie-Hellman SHA1 的密钥交换。 OpenSSH has disabled DH SHA1 by default.默认情况下,OpenSSH 已禁用 DH SHA1。 That leaves libssh2 0.x high and dry.这让 libssh2 0.x 高而枯燥。

Option 1: Update libssh2选项 1:更新 libssh2

libssh2 1.7 and up supports DH SHA256 and ECDH key exchange. libssh2 1.7 及更高版本支持 DH SHA256 和 ECDH 密钥交换。 These will work with the latest OpenSSH.这些将适用于最新的 OpenSSH。 1.x releases require PHP 7. 1.x 版本需要 PHP 7。

Option 2: use phpseclib选项 2:使用 phpseclib

If you're stuck on PHP 5 then libssh2 isn't usable.如果您坚持使用 PHP 5,则 libssh2 不可用。 The highest version available for PHP5 is libssh2 0.13 which still only supports the SHA1 key exchanges. PHP5 可用的最高版本是 libssh2 0.13,它仍然只支持 SHA1 密钥交换。 An alternate library that worked for me was phpseclib .另一个对我有用的库是phpseclib That supports diffie-hellman-group-exchange-sha256 and I was able to connect to updated OpenSSH servers.它支持 diffie-hellman-group-exchange-sha256,我能够连接到更新的 OpenSSH 服务器。

If you have access to the SSH server, and the SSH server is running on a Linux system, the /var/log/messages and /var/log/secure logs on the Linux system might contain events that could be helpful in identifying why "Unable to exchange encryption keys" is being returned.如果您有权访问 SSH 服务器,并且 SSH 服务器在 Linux 系统上运行,则 Linux 系统上的/var/log/messages/var/log/secure日志可能包含有助于确定原因的事件“无法交换加密密钥”正在返回。 For example, the /var/log/secure log could have something like this.例如,/var/log/secure 日志可能有这样的内容。

Jan 29 07:02:46 docker1 sshd[3245780]: Unable to negotiate with 192.168.0.15 port 55736: no matching host key type found. Their offer: ssh-rsa,ssh-dss [preauth]

Notice in this example that the /var/log/secure log captures "no matching host key type found" as the underlying issue.请注意,在此示例中,/var/log/secure 日志将“未找到匹配的主机密钥类型”捕获为潜在问题。 By default, the PHP ssh2_connection functions offers the following host key types.默认情况下,PHP ssh2_connection 函数提供以下主机密钥类型。

  • ssh-rsa SSH-RSA
  • ssh-dss ssh-dss

If the SSH server does not accept ssh-rsa or ssh-dss as host key types, then the /var/log/secure log will capture something like "no matching host key type found" and PHP should log "Unable to exchange encryption keys".如果 SSH 服务器不接受 ssh-rsa 或 ssh-dss 作为主机密钥类型,那么 /var/log/secure 日志将捕获诸如“找不到匹配的主机密钥类型”之类的内容,并且 PHP 应该记录“无法交换加密密钥” ”。 In this scenario, the SSH server would need to be updated to accept the ssh-rsa or ssh-dss host key types.在这种情况下,需要更新 SSH 服务器以接受 ssh-rsa 或 ssh-dss 主机密钥类型。

For example, if the SSH server is OpenSSH, this could mean appending ssh-rsa and ssh-dss to the HostKeyAlgorithms line in the /etc/crypto-policies/back-ends/opensshserver.config on the OpenSSH Linux System.例如,如果 SSH 服务器是 OpenSSH,这可能意味着将 ssh-rsa 和 ssh-dss 附加到 OpenSSH Linux 系统上 /etc/crypto-policies/back-ends/opensshserver.config 中的HostKeyAlgorithms行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM