<!-- Copyright (c) 2026 defconxt. All rights reserved. -->
<!-- Licensed under AGPL-3.0 — see LICENSE file for details. -->
---
name: detecting-weak-random-number-generation
description: >-
  Identify weak, predictable, or biased random number generation in
  cryptographic implementations. Covers CSPRNG validation, entropy source
  auditing, statistical randomness testing, and detection of common RNG
  vulnerabilities in TLS, key generation, and token creation.
domain: cybersecurity
subdomain: cryptography
tags:
  - rng
  - csprng
  - entropy
  - randomness-testing
  - nist-sp800-22
  - urandom
  - token-security
version: "1.0"
author: defconxt
license: AGPL-3.0
metadata:
  mitre-attack: ["T1600"]
---

# Detecting Weak Random Number Generation

## Overview

Weak random number generation undermines all cryptographic security built on
top of it. Predictable RNG leads to guessable session tokens, recoverable
private keys, and broken nonces. This skill covers identifying CSPRNG vs
non-CSPRNG usage, testing entropy quality, auditing system entropy sources,
and detecting common RNG implementation flaws.

## Prerequisites

| Requirement | Install |
|---|---|
| Python 3.10+ | For agent tooling and testing |
| ent | `apt install ent` — entropy testing tool |
| rng-tools | `apt install rng-tools` |
| OpenSSL 3.x | `apt install openssl` |

## Key Concepts

### Entropy Source Assessment

```bash
# Check available system entropy
cat /proc/sys/kernel/random/entropy_avail

# Monitor entropy pool in real time
watch -n 1 cat /proc/sys/kernel/random/entropy_avail

# Test /dev/urandom output quality
dd if=/dev/urandom bs=1M count=10 2>/dev/null | ent

# Check for RDRAND/RDSEED CPU instructions
grep -o 'rdrand\|rdseed' /proc/cpuinfo | sort -u
```

### Statistical Randomness Testing

```bash
# Generate random sample for testing
openssl rand -out sample.bin 1000000

# Basic entropy analysis with ent
ent sample.bin
# Expected: entropy ~7.999 bits/byte, chi-square ~50%

# Compression ratio test (random data is incompressible)
ls -la sample.bin && gzip -k sample.bin && ls -la sample.bin.gz
```

### Code-Level RNG Auditing

```bash
# Search for insecure RNG usage in Python
grep -rn 'random\.' --include='*.py' . | grep -v 'secrets\.'

# Detect insecure RNG in JavaScript
grep -rn 'Math.random' --include='*.js' --include='*.ts' .

# Detect insecure RNG in Java
grep -rn 'java.util.Random' --include='*.java' . | grep -v 'SecureRandom'

# Check for time-based seeding (predictable)
grep -rn 'seed.*time\|srand.*time' --include='*.py' --include='*.c' .
```

### Secure RNG by Language

| Language | Secure API | Insecure API |
|---|---|---|
| Python | `secrets`, `os.urandom()` | `random` module |
| JavaScript | `crypto.getRandomValues()` | `Math.random()` |
| Java | `java.security.SecureRandom` | `java.util.Random` |
| C/C++ | `RAND_bytes()` (OpenSSL) | `rand()`, `srand()` |
| Go | `crypto/rand` | `math/rand` |

## Common Failures

| Failure | Cause | Fix |
|---|---|---|
| `Math.random()` for tokens | JS non-CSPRNG in security context | Use `crypto.getRandomValues()` |
| `random.randint()` for secrets | Python stdlib random predictable | Use `secrets` module |
| Low entropy at boot | VMs have limited entropy sources | Install haveged or use virtio-rng |
| Nonce reuse in AES-GCM | Counter reset or RNG collision | Deterministic nonce (AES-GCM-SIV) |

## Workflow

1. **Source Audit** — Identify entropy sources and RNG implementations
2. **Code Review** — Search codebase for non-CSPRNG in security contexts
3. **Statistical Test** — Run ent or NIST tests on generated output
4. **Token Analysis** — Collect and analyze tokens for bias
5. **Report** — Document weak RNG with severity and remediation
6. **Remediate** — Replace with CSPRNG and verify quality

## Verification

| Check | Method |
|---|---|
| CSPRNG everywhere | All security-critical randomness uses CSPRNG |
| Entropy adequate | `/proc/sys/kernel/random/entropy_avail` > 256 |
| Statistical tests pass | `ent` shows entropy > 7.99 bits/byte |
| No code-level issues | No `Math.random`/`random.randint` in security paths |

## References

- [NIST SP 800-90A — DRBG Mechanisms](https://csrc.nist.gov/publications/detail/sp/800-90a/rev-1/final)
- [NIST SP 800-22 — Statistical Test Suite](https://csrc.nist.gov/publications/detail/sp/800-22/rev-1a/final)
- [RFC 4086 — Randomness Requirements for Security](https://datatracker.ietf.org/doc/html/rfc4086)
- [CWE-330 — Insufficiently Random Values](https://cwe.mitre.org/data/definitions/330.html)
