Accepting unknown SSH host keys

Accepting unknown SSH host keys with Paramiko

Description

Paramiko’s AutoAddPolicy accepts the host key of an unknown SSH server. Without independent verification, the client can trust someone impersonating the intended server.

Potential impact

A server impersonating the intended host can read credentials and commands sent to it, alter session data and supply false responses.

Remediation

  1. Verify the host key’s fingerprint through a separate trusted channel before adding it to known_hosts. If a key changes, investigate and replace it only with an approved key.
  2. Read the trusted file with load_host_keys() and call set_missing_host_key_policy(RejectPolicy). The client will reject a host with an unknown key.

Examples

Before

python
from paramiko.client import SSHClient, AutoAddPolicy

client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy) # Accepts an unknown host key.
client.connect("example.com")

After

python
from paramiko.client import SSHClient, RejectPolicy

client = SSHClient()
client.load_system_host_keys() # Load trusted host keys from known_hosts.
client.set_missing_host_key_policy(RejectPolicy) # Reject unknown host keys.
client.connect("example.com")

The example uses load_system_host_keys() to look for keys in the user’s ~/.ssh/known_hosts. Add the verified server entry before connecting with RejectPolicy.

References