# 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.

| 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](https://cwe.mitre.org/data/definitions/337.html) |
| CAPEC | [CAPEC-97](https://capec.mitre.org/data/definitions/97.html) |
| ATT&CK | [T1557](https://attack.mitre.org/techniques/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`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-337.rego) |

## Overview

VNX-337 maps to [CWE-337: Predictable Seed in Pseudo-Random Number Generator (PRNG)](https://cwe.mitre.org/data/definitions/337.html). 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

```c
// FLAGGED: the seed is the current second
srand(time(NULL));
```

```go
// 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())))
```

```java
// 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

1. Do not seed generators that produce secrets. Use a CSPRNG that seeds itself: `secrets`, `SecureRandom`, `crypto/rand`, `random_bytes()`, `crypto.randomBytes()`.
2. Never call `setSeed()` on a `SecureRandom` with the time; let it seed itself from the operating system.
3. For confirmed false positives, add a `# vulnetix-ignore: VNX-337` comment on the line.

## References

- [CWE-337: Predictable Seed in Pseudo-Random Number Generator (PRNG)](https://cwe.mitre.org/data/definitions/337.html)
- [OWASP Cryptographic Storage Cheat Sheet: Secure random number generation](https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html#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 `;`):

```text
# nosec vnx-337
// nosec vnx-337
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-337 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
