VNX-337: Predictable Seed in Pseudo-Random Number Generator (PRNG)
Detects a PRNG seeded from the clock or the process id when its output is security-relevant, and any time-seeded SecureRandom or ChaCha8. Python, Java, Kotlin, Go, C, C++, PHP, Ruby and JavaScript.
| Field | Value |
|---|---|
| Rule ID | VNX-337 |
| Severity | Medium |
| SARIF level | warning |
| Kind | sast |
| Languages | Python, Java, Kotlin, Go, C, C++, PHP, Ruby, JavaScript / TypeScript |
| CWE | CWE-337 |
| CAPEC | CAPEC-97 |
| ATT&CK | T1557 |
| CWSS vector | CWSS:1.0/TI:M/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:M/DI:H/EX:M/EC:N/P:C |
| Tags | prng seed cwe-337 |
| Source | internal/sast/rules/vnx-337.rego |
Overview
VNX-337 maps to CWE-337: Predictable Seed in Pseudo-Random Number Generator (PRNG). A seed taken from the clock or the process id has only a few plausible values: an attacker who knows roughly when the process started, or can read its PID, tries them all and reproduces every value the generator produced.
The rule reports a seed built from time(NULL), time.time(), time.Now(), datetime.now(), System.currentTimeMillis()/nanoTime(), Instant.now(), Date.now()/new Date(), microtime(), hrtime(), Time.now, clock(), gettimeofday/tv_sec/tv_usec, getpid()/os.getpid()/os.Getpid()/getmypid()/Process.pid/process.pid, performance.now() and similar, inline or through a variable assigned one of those within eight lines above, passed to the same seeding APIs as VNX-336.
A time-seeded SecureRandom or ChaCha8 is always reported. A general-purpose PRNG is reported only when its output feeds a secret: the seed call sits in a function named for a secret, the generator is stored under a secret’s name, or the same file draws from a PRNG into a token, password, key, salt, nonce, OTP, CSRF or session value.
What Gets Flagged
// FLAGGED: the seed is the current second
srand(time(NULL));
// FLAGGED: clock and PID seeds in a file that draws session tokens
rand.Seed(time.Now().UnixNano())
var pidRand = rand.New(rand.NewSource(int64(os.Getpid())))
// FLAGGED: a SecureRandom seeded from the clock is predictable
sr.setSeed(System.currentTimeMillis());
What Is Not Flagged
- Clock seeds for jitter, sampling, games and load spreading in files that produce no secret.
- OS-seeded generators (
new SecureRandom(),random.SystemRandom(),crypto/rand). - Test files, and calls in comments or string literals.
Constant seeds are VNX-336 and seeds narrowed to a small range are VNX-339.
Remediation
- Do not seed generators that produce secrets. Use a CSPRNG that seeds itself:
secrets,SecureRandom,crypto/rand,random_bytes(),crypto.randomBytes(). - Never call
setSeed()on aSecureRandomwith the time; let it seed itself from the operating system. - For confirmed false positives, add a
# vulnetix-ignore: VNX-337comment on the line.
References
- CWE-337: Predictable Seed in Pseudo-Random Number Generator (PRNG)
- OWASP Cryptographic Storage Cheat Sheet: Secure random number generation
Suppressing this rule
Put a nosec comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file’s language (#, //, -- or ;):
# nosec vnx-337
// nosec vnx-337
Or record an ignore rule: vulnetix ignore add --rule vnx-337 --reason "...". See Suppressing findings.