Skip to content

Use of cryptographically weak pseudo-random number generator (PRNG)

Description

Depending on the context, generating weak random numbers may expose cryptographic functions, which rely on these numbers, to be exploitable. When generating numbers for sensitive values such as tokens, nonces, and cryptographic keys, it is recommended that the `secrets` module be used instead.

Examples

Insecure Code

python
import random
random_bytes = random.random()

Secure Code

python
import secrets
random_bytes = secrets.token_bytes(64)

Remediation

Use the `secrets` module instead of the `random` module for generating cryptographically secure random numbers.

Rule Details

FieldValue
IDCODE-0158
CategoryCrypto
SeverityLOW
CWECWE-338
ConfidenceHIGH
ImpactLOW
LikelihoodMEDIUM
ExploitabilityMODERATE
Tagsrandom, secrets, crypto
OWASPA3:2017-Sensitive Data Exposure, A02:2021-Cryptographic Failures