Skip to content

Commit 52278b2

Browse files
committed
Python: Add query for insecure SSH host key policies in Paramiko.
1 parent 285f8b0 commit 52278b2

File tree

8 files changed

+106
-0
lines changed

8 files changed

+106
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
In the Secure Shell (SSH) protocol, host keys are used to verify the identity of
9+
remote hosts. Accepting unknown host keys may leave the connection open to
10+
man-in-the-middle attacks.
11+
</p>
12+
</overview>
13+
14+
<recommendation>
15+
<p>
16+
Do not accept unknown host keys. For the Paramiko library in particular, avoid
17+
setting the missing host key policy to either <code>AutoAddPolicy</code> or
18+
<code>WarningPolicy</code>, as both of these will continue even when the host
19+
key is unknown. The default <code>RejectPolicy</code> throws an exception when
20+
unknown host keys are encountered.
21+
</p>
22+
</recommendation>
23+
24+
<example>
25+
<p>
26+
The following example opens a connection to <code>example.com</code> with the
27+
missing host key policy set to <code>AutoAddPolicy</code>. If the host key
28+
verification fails, the client will continue to interact with the server, even
29+
though the connection may be compromised.
30+
</p>
31+
<sample src="examples/paramiko_host_key.py" />
32+
</example>
33+
34+
<references>
35+
<li>
36+
Paramiko documentation: <a href="http://docs.paramiko.org/en/2.4/api/client.html?highlight=set_missing_host_key_policy#paramiko.client.SSHClient.set_missing_host_key_policy">set_missing_host_key_policy</a>.
37+
</li>
38+
</references>
39+
</qhelp>
40+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @name Accepting unknown host keys.
3+
* @description Accepting unknown host keys can allow man-in-the-middle attacks.
4+
* @kind problem
5+
* @problem.severity error
6+
* @precision high
7+
* @id py/missing-host-key-validation
8+
* @tags security
9+
* external/cwe/cwe-295
10+
*/
11+
12+
import python
13+
14+
private ModuleObject theParamikoClientModule() { result = ModuleObject::named("paramiko.client") }
15+
16+
private ClassObject theParamikoSSHClientClass() {
17+
result = theParamikoClientModule().attr("SSHClient")
18+
}
19+
20+
private ClassObject unsafe_paramiko_policy(string name) {
21+
(name = "AutoAddPolicy" or name = "WarningPolicy") and
22+
result = theParamikoClientModule().attr(name)
23+
}
24+
25+
from CallNode call, string name
26+
where
27+
call = theParamikoSSHClientClass()
28+
.declaredAttribute("set_missing_host_key_policy")
29+
.(FunctionObject)
30+
.getACall() and
31+
call.getAnArg().refersTo(unsafe_paramiko_policy(name))
32+
select call, "Setting missing host key policy to " + name + " may be unsafe."
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from paramiko.client import SSHClient, AutoAddPolicy
2+
3+
client = SSHClient()
4+
client.set_missing_host_key_policy(AutoAddPolicy)
5+
client.connect("example.com")
6+
7+
# ... interaction with server
8+
9+
client.close()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| paramiko_host_key.py:5:1:5:49 | ControlFlowNode for Attribute() | Setting missing host key policy to AutoAddPolicy may be unsafe. |
2+
| paramiko_host_key.py:7:1:7:49 | ControlFlowNode for Attribute() | Setting missing host key policy to WarningPolicy may be unsafe. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE-295/MissingHostKeyValidation.ql
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from paramiko.client import AutoAddPolicy, WarningPolicy, RejectPolicy, SSHClient
2+
3+
client = SSHClient()
4+
5+
client.set_missing_host_key_policy(AutoAddPolicy) # bad
6+
client.set_missing_host_key_policy(RejectPolicy) # good
7+
client.set_missing_host_key_policy(WarningPolicy) # bad

python/ql/test/query-tests/Security/lib/paramiko/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class SSHClient(object):
2+
def __init__(self, *args, **kwargs):
3+
pass
4+
5+
def set_missing_host_key_policy(self, *args, **kwargs):
6+
pass
7+
8+
class AutoAddPolicy(object):
9+
pass
10+
11+
class WarningPolicy(object):
12+
pass
13+
14+
class RejectPolicy(object):
15+
pass

0 commit comments

Comments
 (0)