Rule Definition
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to access sensitive information.
Remediation
Do not use pseudo-random generators from the "random" module.
Use more secured alternatives such as "os.urandom()" and methods from "secrets" module.
Violation Code Sample
import random
def unsafe_random():
return random.randint(0, 100) # unsafe random number
Fixed Code Sample
import secrets
def secure_random():
return secrets.randbelow(100) # cryptographically secure random number
Reference
CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)
https://cwe.mitre.org/data/definitions/338.html
CWE-330: Use of Insufficiently Random Values
https://cwe.mitre.org/data/definitions/330.html
Related Technologies
Technical Criterion
CWE-338 - Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG) [Base]
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.