Skip to content

Commit 77a8cba

Browse files
authored
Merge pull request #22 from GitHubSecurityLab/py-weak-prng
Python - Update Weak PRNG query
2 parents f0d353f + d8b827c commit 77a8cba

File tree

6 files changed

+73
-30
lines changed

6 files changed

+73
-30
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
private import semmle.python.ApiGraphs
2+
private import semmle.python.Concepts
3+
private import semmle.python.dataflow.new.DataFlow
4+
5+
module RandomNumberGenerator {
6+
abstract class Sinks extends DataFlow::Node { }
7+
8+
class OsRandom extends Sinks {
9+
OsRandom() {
10+
exists(DataFlow::Node call |
11+
// https://docs.python.org/3/library/os.html#os.getrandom
12+
call = API::moduleImport("os").getMember("getrandom").getACall() and
13+
this = call
14+
)
15+
}
16+
}
17+
18+
class PyRandom extends Sinks {
19+
PyRandom() {
20+
exists(DataFlow::Node call |
21+
// TODO: does `random.seed(_)` need to be static?
22+
// https://docs.python.org/3/library/random.html#random.random
23+
call =
24+
API::moduleImport("random")
25+
.getMember(["random", "randrange", "randint", "randbytes"])
26+
.getACall() and
27+
this = call
28+
)
29+
}
30+
}
31+
32+
class PyUuid extends Sinks {
33+
PyUuid() {
34+
exists(DataFlow::Node call |
35+
call = API::moduleImport("uuid").getMember(["uuid1", "uuid3"]).getACall() and
36+
this = call
37+
)
38+
}
39+
}
40+
}

python/src/security/CWE-338/WeakPRNG.ql

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,7 @@
1212
*/
1313

1414
import python
15-
import semmle.python.ApiGraphs
15+
import ghsl.cryptography.RandomNumberGenerator
1616

17-
abstract class RandomNumberGeneratorSinks extends DataFlow::Node { }
18-
19-
class OSRandom extends RandomNumberGeneratorSinks {
20-
OSRandom() {
21-
exists(DataFlow::Node call |
22-
// https://docs.python.org/3/library/os.html#os.getrandom
23-
call = API::moduleImport("os").getMember("getrandom").getACall() and
24-
this = call
25-
)
26-
}
27-
}
28-
29-
class PyRandom extends RandomNumberGeneratorSinks {
30-
PyRandom() {
31-
exists(DataFlow::Node call |
32-
(
33-
// https://docs.python.org/3/library/random.html#random.random
34-
call = API::moduleImport("random").getMember("random").getACall()
35-
or
36-
// https://docs.python.org/3/library/random.html#random.randbytes
37-
call = API::moduleImport("random").getMember("randbytes").getACall()
38-
) and
39-
this = call
40-
)
41-
}
42-
}
43-
44-
from RandomNumberGeneratorSinks rngs
45-
select rngs.asExpr(), "Using weak PRNG"
17+
from RandomNumberGenerator::Sinks rngs
18+
select rngs, "Using weak PRNG"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| app.py:6:1:6:16 | ControlFlowNode for Attribute() | Using weak PRNG |
2+
| app.py:11:1:11:15 | ControlFlowNode for Attribute() | Using weak PRNG |
3+
| app.py:12:1:12:23 | ControlFlowNode for Attribute() | Using weak PRNG |
4+
| app.py:13:1:13:21 | ControlFlowNode for Attribute() | Using weak PRNG |
5+
| app.py:15:1:15:20 | ControlFlowNode for Attribute() | Using weak PRNG |
6+
| app.py:18:1:18:12 | ControlFlowNode for Attribute() | Using weak PRNG |
7+
| app.py:19:1:19:44 | ControlFlowNode for Attribute() | Using weak PRNG |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
security/CWE-338/WeakPRNG.ql
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import random
3+
import uuid
4+
5+
# os module
6+
os.getrandom(10)
7+
8+
# random module
9+
random.seed("8")
10+
11+
random.random()
12+
random.randrange(0, 10)
13+
random.randint(0, 10)
14+
15+
random.randbytes(10)
16+
17+
# uuid module
18+
uuid.uuid1()
19+
uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
20+
uuid.uuid4()
21+
uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --max-import-depth=0

0 commit comments

Comments
 (0)