Skip to content

Python - Update Weak PRNG query #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions python/lib/ghsl/cryptography/RandomNumberGenerator.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
private import semmle.python.ApiGraphs
private import semmle.python.Concepts
private import semmle.python.dataflow.new.DataFlow

module RandomNumberGenerator {
abstract class Sinks extends DataFlow::Node { }

class OsRandom extends Sinks {
OsRandom() {
exists(DataFlow::Node call |
// https://docs.python.org/3/library/os.html#os.getrandom
call = API::moduleImport("os").getMember("getrandom").getACall() and
this = call
)
}
}

class PyRandom extends Sinks {
PyRandom() {
exists(DataFlow::Node call |
// TODO: does `random.seed(_)` need to be static?
// https://docs.python.org/3/library/random.html#random.random
call =
API::moduleImport("random")
.getMember(["random", "randrange", "randint", "randbytes"])
.getACall() and
this = call
)
}
}

class PyUuid extends Sinks {
PyUuid() {
exists(DataFlow::Node call |
call = API::moduleImport("uuid").getMember(["uuid1", "uuid3"]).getACall() and
this = call
)
}
}
}
33 changes: 3 additions & 30 deletions python/src/security/CWE-338/WeakPRNG.ql
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,7 @@
*/

import python
import semmle.python.ApiGraphs
import ghsl.cryptography.RandomNumberGenerator

abstract class RandomNumberGeneratorSinks extends DataFlow::Node { }

class OSRandom extends RandomNumberGeneratorSinks {
OSRandom() {
exists(DataFlow::Node call |
// https://docs.python.org/3/library/os.html#os.getrandom
call = API::moduleImport("os").getMember("getrandom").getACall() and
this = call
)
}
}

class PyRandom extends RandomNumberGeneratorSinks {
PyRandom() {
exists(DataFlow::Node call |
(
// https://docs.python.org/3/library/random.html#random.random
call = API::moduleImport("random").getMember("random").getACall()
or
// https://docs.python.org/3/library/random.html#random.randbytes
call = API::moduleImport("random").getMember("randbytes").getACall()
) and
this = call
)
}
}

from RandomNumberGeneratorSinks rngs
select rngs.asExpr(), "Using weak PRNG"
from RandomNumberGenerator::Sinks rngs
select rngs, "Using weak PRNG"
7 changes: 7 additions & 0 deletions python/test/security/CWE-338/WeakPRNG.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| app.py:6:1:6:16 | ControlFlowNode for Attribute() | Using weak PRNG |
| app.py:11:1:11:15 | ControlFlowNode for Attribute() | Using weak PRNG |
| app.py:12:1:12:23 | ControlFlowNode for Attribute() | Using weak PRNG |
| app.py:13:1:13:21 | ControlFlowNode for Attribute() | Using weak PRNG |
| app.py:15:1:15:20 | ControlFlowNode for Attribute() | Using weak PRNG |
| app.py:18:1:18:12 | ControlFlowNode for Attribute() | Using weak PRNG |
| app.py:19:1:19:44 | ControlFlowNode for Attribute() | Using weak PRNG |
1 change: 1 addition & 0 deletions python/test/security/CWE-338/WeakPRNG.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
security/CWE-338/WeakPRNG.ql
21 changes: 21 additions & 0 deletions python/test/security/CWE-338/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import random
import uuid

# os module
os.getrandom(10)

# random module
random.seed("8")

random.random()
random.randrange(0, 10)
random.randint(0, 10)

random.randbytes(10)

# uuid module
uuid.uuid1()
uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
uuid.uuid4()
uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
1 change: 1 addition & 0 deletions python/test/security/CWE-338/options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
semmle-extractor-options: --max-import-depth=0