D
D
DVoropaev2020-04-19 23:16:28
Python
DVoropaev, 2020-04-19 23:16:28

Why paramiko incorrectly detects ssh connection status?

Here is the function to check if the username/password is correct:

def SSHCheck(target, SSHport, user, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    resultCode = 0
    try:
        ssh.connect(target, port = SSHport, username = user, password = password, timeout=5, banner_timeout = 5, auth_timeout=5)
    except paramiko.AuthenticationException:
        resultCode = 1
    except socket.error:
        resultCode = 2
    except paramiko.ssh_exception.SSHException:
        resultCode = 3
    finally:
        ssh.close()
    return resultCode

Everything works when I connect to a server running OpenSSH.
However, if the server has Dropbear sshd 0.51 (protocol 2.0), then the resultCode is zero (as if the connection was successful) regardless of the login and password.
To distinguish false positives, I decided to try to execute the "ls" command every time and watch the result. To do this, I added an else block
def SSHCheck(target, SSHport, user, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    resultCode = 0
    try:
        ssh.connect(target, port = SSHport, username = user, password = password, timeout=5, banner_timeout = 5, auth_timeout=5)
    except paramiko.AuthenticationException:
        resultCode = 1
    except socket.error:
        resultCode = 2
    except paramiko.ssh_exception.SSHException:
        resultCode = 3
    else:
        channel = ssh.get_transport().open_session()
        channel.get_pty()
        channel.settimeout(5)
        channel.exec_command('ls')
        print(channel.recv(1024)) #Выводим результат команды
        channel.close()
    finally:
        ssh.close()
    return resultCode

However, on every failed login attempt, paramiko tried to "ls" and output something like this:

b"\r\r\nEntering character mode\r\r\nEscape character is '^]'.\r\r\n\r\r\n\r\n\rlogin as: -c\r\n\ rpassword: **"

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question