Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LANG-SEC-071 --project .About This Rule
Understanding the vulnerability and how it is detected
paramiko.AutoAddPolicy() and paramiko.WarningPolicy() automatically accept SSH host keys for servers not in the known_hosts file. With AutoAddPolicy, unknown keys are silently accepted and added to known_hosts; with WarningPolicy, a warning is logged but the connection proceeds.
Both policies undermine SSH's protection against man-in-the-middle attacks. SSH host key verification ensures the client is connecting to the genuine server and not an attacker impersonating it. Without this verification, an attacker who can intercept or redirect the TCP connection can perform an MITM attack on the SSH session, capturing credentials and all session data.
paramiko.RejectPolicy() (the default if no policy is set) rejects connections to servers with unknown host keys and is the secure choice. Alternatively, pre-populate known_hosts with the expected host keys.
Security Implications
Potential attack scenarios if this vulnerability is exploited
SSH Session MITM via Key Substitution
An attacker who can intercept or redirect the SSH connection can present their own host key instead of the server's. With AutoAddPolicy, the client accepts and uses the attacker's key, decrypting all session data and enabling command injection into the session.
Credential Theft
During the SSH authentication phase, the client sends its username and authenticates (password or key challenge-response). With an MITM SSH session, the attacker captures the authentication credentials and can reuse them to authenticate directly to the real server.
Command Injection in Automated Workflows
Python scripts that automate SSH commands (deployment, configuration management, monitoring) with AutoAddPolicy trust the connection unverified. An MITM attacker can inject malicious commands into the session, executing them with the SSH user's privileges on the target server.
Known_hosts Pollution
AutoAddPolicy permanently adds attacker-controlled keys to known_hosts. Future connections using RejectPolicy will then trust the attacker's key, enabling ongoing MITM attacks even after the original vulnerability is noticed.
How to Fix
Recommended remediation steps
- 1Replace AutoAddPolicy() and WarningPolicy() with RejectPolicy(), which rejects connections to servers with unknown host keys.
- 2Pre-populate known_hosts with expected server host keys using ssh-keyscan in your deployment pipeline or configuration management.
- 3Load known host keys using ssh.load_system_host_keys() and ssh.load_host_keys(known_hosts_path) before connecting.
- 4For automated infrastructure where host keys change (auto-scaling), use SSH certificate authorities instead of individual host keys.
- 5Never suppress host key warnings in production code; treat unknown host keys as a potential MITM attack indicator.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects calls to paramiko.AutoAddPolicy() and paramiko.WarningPolicy() used as arguments to SSHClient.set_missing_host_key_policy(). Both policies bypass SSH host key verification and are flagged as security concerns.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Unverified SSL Context Created
ssl._create_unverified_context() disables certificate verification entirely, making TLS connections vulnerable to man-in-the-middle attacks.
Certificate Validation Disabled (verify=False)
TLS certificate validation is explicitly disabled via verify=False or CERT_NONE, making connections vulnerable to man-in-the-middle attacks.
Paramiko exec_command() Usage
paramiko exec_command() runs commands on a remote host. Audit that command arguments are not derived from untrusted input to prevent command injection.
Frequently Asked Questions
Common questions about Paramiko Implicit Host Key Trust (AutoAddPolicy)
New feature
Get these findings posted directly on your GitHub pull requests
The Paramiko Implicit Host Key Trust (AutoAddPolicy) rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.