Skip to content

Host Key Type Selection Should Match OpenSSH's #350

Closed

Description

@bryanr

Hi,

When the remote ssh server supports ssh-rsa and/or ssh-dss plus ecdsa-sha2-nistp256, and the local known_host file(s) include only its ecdsa-sha2-nistp256 key, paramiko will fail to connect with:

paramiko.ssh_exception.SSHException: Server '[example.com]:666' not found in known_hosts

This is because transport.py always picks the 0th mutually-supported method:

self.host_key_type = agreed_keys[0]

with the priority implicitly being ssh-rsa > ssh-dss > ecdsa-sha2-nistp256 due to:

_preferred_keys = ('ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256')

Many OpenSSH servers enable all 3 methods by default now:

HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key

and, when first connecting, recent OpenSSH clients prefer:

ecdsa-sha2-nistp256 > ssh-rsa > ssh-dss

and thus add the server's ecdsa-sha2-nistp256 key (only) into known_hosts.

When paramiko connects to the same server, it instead picks ssh-rsa, searches in vain for an ssh-rsa host key, and then fails with the error above, confusing the user (as the OpenSSH client connects, while paramiko does not, even though it now supports ECDSA).

Disabling ssh-rsa and ssh-dss on the server side will work around this - forcing paramiko to use ecdsa-sha2-nistp256 - but that reduces server interoperability with other SSH clients that may not support ecdsa-sha2-nistp256 yet.

Another option is to make paramiko use the same priority as OpenSSH:

diff --git a/paramiko/transport.py b/paramiko/transport.py
index 406626a..a305017 100644
--- a/paramiko/transport.py
+++ b/paramiko/transport.py
@@ -91,7 +91,8 @@ class Transport (threading.Thread):
     _preferred_ciphers = ('aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-c
                           'aes256-cbc', '3des-cbc', 'arcfour128', 'arcfour256')
     _preferred_macs = ('hmac-sha1', 'hmac-md5', 'hmac-sha1-96', 'hmac-md5-96')
-    _preferred_keys = ('ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256')
+    # host key algos ordered to match openssh 6.6p1's client's most->least preferred:
+    _preferred_keys = ('ecdsa-sha2-nistp256', 'ssh-rsa', 'ssh-dss')
     _preferred_kex = ('diffie-hellman-group1-sha1', 'diffie-hellman-group-excha
     _preferred_compression = ('none',)

however, this change will break one (rarer, but not unusual) case where the known_hosts file contains an ssh-rsa key only, and the OpenSSH Server supports both ecdsa-sha2-nistp256 and ssh-rsa.

The proper fix IMO is to consult the known_hosts db while picking the host_key_type, to try to find one that's mutually-supported, plus already has a known_hosts key available [this is what the OpenSSH client does]. If there are multiple options, then the most secure should be used e.g. ecdsa-sha2-nistp256 > ssh-rsa > ssh-dss.

Please investigate & integrate some fix for this issue, as it's creating confusing connectivity problems on many systems running recent versions of OpenSSH. For example, https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=702571 is actually still mostly-open due to this behavior.

Regards,

Bryan

Activity

  1. jturner314 commented on Jul 6, 2014

    @jturner314

    I ran across this issue earlier today when trying to use the example from the documentation:

    client = SSHClient()
    client.load_system_host_keys()
    client.connect('my-host-url')

    I agree that the proper fix is to account for the known_hosts keys when negotiating the host_key_type.

    An easy workaround until paramiko is fixed is to add another line for the desired host's RSA key into your ~/.ssh/known_hosts file.

  2. gerritsangel commented on Aug 30, 2014

    @gerritsangel

    @jturner314:
    Do you know of any way how to connect explicitly with ssh-rsa? Or how can I manually create a ssh-rsa line if OpenSSH always connects with ecdsa-sha2-nistp256? OpenSSH always connects with ecdsa-sha2-nistp256.

    I think Paramiko needs to fix this. For example, I cannot connect to an Arch Linux system. A CentOS system works, though.

  3. bitprophet commented on Sep 3, 2014

    @bitprophet
    Member

    This feels like a duplicate of #243 - merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions