Skip to content

Commit 70282f9

Browse files
committed
convert paramiko query to SecondaryServerCmdInjection query, Add inline tests
1 parent d234a53 commit 70282f9

File tree

10 files changed

+52
-26
lines changed

10 files changed

+52
-26
lines changed

python/ql/src/experimental/Security/CWE-074/paramiko/paramiko.qhelp renamed to python/ql/src/experimental/Security/CWE-074/secondaryCommandInjection/SecondaryServerCmdInjection.qhelp

File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name RCE with user provided command with paramiko ssh client
3+
* @description user provided command can lead to execute code on a external server that can be belong to other users or admins
4+
* @kind path-problem
5+
* @problem.severity error
6+
* @security-severity 9.3
7+
* @precision high
8+
* @id py/paramiko-command-injection
9+
* @tags security
10+
* experimental
11+
* external/cwe/cwe-074
12+
*/
13+
14+
import python
15+
import experimental.semmle.python.security.SecondaryServerCmdInjection
16+
import ParamikoFlow::PathGraph
17+
18+
from ParamikoFlow::PathNode source, ParamikoFlow::PathNode sink
19+
where ParamikoFlow::flowPath(source, sink)
20+
select sink.getNode(), source, sink, "This code execution depends on a $@.", source.getNode(),
21+
"a user-provided value"

python/ql/src/experimental/Security/CWE-074/paramiko/paramikoBad.py renamed to python/ql/src/experimental/Security/CWE-074/secondaryCommandInjection/paramikoBad.py

File renamed without changes.

python/ql/src/experimental/Security/CWE-074/paramiko/paramiko.ql renamed to python/ql/src/experimental/semmle/python/security/SecondaryServerCmdInjection.qll

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
/**
2-
* @name RCE with user provided command with paramiko ssh client
3-
* @description user provided command can lead to execute code on a external server that can be belong to other users or admins
4-
* @kind path-problem
5-
* @problem.severity error
6-
* @security-severity 9.3
7-
* @precision high
8-
* @id py/paramiko-command-injection
9-
* @tags security
10-
* experimental
11-
* external/cwe/cwe-074
12-
*/
13-
141
import python
15-
import semmle.python.dataflow.new.DataFlow
162
import semmle.python.dataflow.new.TaintTracking
173
import semmle.python.dataflow.new.RemoteFlowSources
184
import semmle.python.ApiGraphs
5+
import semmle.python.dataflow.new.internal.DataFlowPublic
6+
import codeql.util.Unit
7+
8+
module SecondaryCommandInjection {
9+
/**
10+
* The additional taint steps that need for creating taint tracking or dataflow.
11+
*/
12+
class AdditionalTaintStep extends Unit {
13+
/**
14+
* Holds if there is a additional taint step between pred and succ.
15+
*/
16+
abstract predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ);
17+
}
18+
19+
/**
20+
* A abstract class responsible for extending new decompression sinks
21+
*/
22+
abstract class Sink extends DataFlow::Node { }
23+
}
1924

2025
private API::Node paramikoClient() {
2126
result = API::moduleImport("paramiko").getMember("SSHClient").getReturn()
2227
}
2328

24-
private module ParamikoConfig implements DataFlow::ConfigSig {
29+
module ParamikoConfig implements DataFlow::ConfigSig {
2530
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
2631

2732
/**
@@ -50,10 +55,3 @@ private module ParamikoConfig implements DataFlow::ConfigSig {
5055

5156
/** Global taint-tracking for detecting "paramiko command injection" vulnerabilities. */
5257
module ParamikoFlow = TaintTracking::Global<ParamikoConfig>;
53-
54-
import ParamikoFlow::PathGraph
55-
56-
from ParamikoFlow::PathNode source, ParamikoFlow::PathNode sink
57-
where ParamikoFlow::flowPath(source, sink)
58-
select sink.getNode(), source, sink, "This code execution depends on a $@.", source.getNode(),
59-
"a user-provided value"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
missingAnnotationOnSink
2+
testFailures
3+
failures
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import python
2+
import experimental.dataflow.TestUtil.DataflowQueryTest
3+
import experimental.semmle.python.security.SecondaryServerCmdInjection
4+
import FromTaintTrackingConfig<ParamikoConfig>

python/ql/test/experimental/query-tests/Security/CWE-074-paramiko/paramiko.expected renamed to python/ql/test/experimental/query-tests/Security/CWE-074-SecondaryServerCmdInjection/SecondaryServerCmdInjection.expected

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE-074/secondaryCommandInjection/SecondaryServerCmdInjection.ql

python/ql/test/experimental/query-tests/Security/CWE-074-paramiko/paramiko.py renamed to python/ql/test/experimental/query-tests/Security/CWE-074-SecondaryServerCmdInjection/paramiko.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
@app.get("/bad1")
1515
async def read_item(cmd: str):
16-
stdin, stdout, stderr = paramiko_ssh_client.exec_command(cmd)
16+
stdin, stdout, stderr = paramiko_ssh_client.exec_command(cmd) # $ result=BAD
1717
return {"success": stdout}
1818

1919
@app.get("/bad2")
2020
async def read_item(cmd: str):
21-
stdin, stdout, stderr = paramiko_ssh_client.exec_command(command=cmd)
21+
stdin, stdout, stderr = paramiko_ssh_client.exec_command(command=cmd) # $ result=BAD
2222
return {"success": "OK"}
2323

2424
@app.get("/bad3")
2525
async def read_item(cmd: str):
26-
stdin, stdout, stderr = paramiko_ssh_client.connect('hostname', username='user',password='yourpassword',sock=paramiko.ProxyCommand(cmd))
26+
stdin, stdout, stderr = paramiko_ssh_client.connect('hostname', username='user',password='yourpassword',sock=paramiko.ProxyCommand(cmd)) # $ result=BAD
2727
return {"success": "OK"}

python/ql/test/experimental/query-tests/Security/CWE-074-paramiko/paramiko.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)