# VNX-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)

Detects a value drawn from a non-cryptographic PRNG (Math.random, rand/mt_rand/str_shuffle, Ruby rand/sample, Go math/rand) that is stored under.

| Field | Value |
|---|---|
| Rule ID | `VNX-338` |
| Severity | High |
| SARIF level | `error` |
| Kind | `sast` |
| Languages | JavaScript / TypeScript, PHP, Ruby, Go |
| CWE | [CWE-338](https://cwe.mitre.org/data/definitions/338.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:H/AP:A/AL:A/IC:N/FC:LT/RP:N/RL:A/AV:I/AS:N/IN:A/SC:A/BI:H/DI:H/EX:H/EC:N/P:H` |
| Tags | `prng` `security` `cwe-338` |
| Source | [`internal/sast/rules/vnx-338.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-338.rego) |

## Overview

VNX-338 maps to [CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)](https://cwe.mitre.org/data/definitions/338.html). General-purpose generators (Mersenne Twister, xorshift, LCGs) are built for speed and statistical spread, not unpredictability: a few observed outputs are enough to recover their state and compute every later value.

The rule reports JavaScript/TypeScript `Math.random()`; PHP `rand()`, `mt_rand()`, `lcg_value()`, `str_shuffle()` and `array_rand()`; Ruby `Kernel#rand`, `Random.rand`, `Random.new.rand`/`bytes` and `Array#sample`/`#shuffle`; and Go `math/rand` and `math/rand/v2` draws (under the package's import alias), when the draw becomes a secret: the name it is assigned to or keyed by (token, password, secret, nonce, salt, OTP, PIN, CSRF/XSRF, session id, API/signing key, reset/verification/invite code, session cookie), the string key of a setter or cookie call, a return from a function named for a secret, or a variable assigned from the draw that reaches such a name within six lines.

## What gets flagged

```javascript
// FLAGGED: Math.random() as a reset token and a session id
const resetToken = Math.random().toString(36).slice(2);
const sessionId = Math.floor(Math.random() * 1e16).toString(16);
```

```php
// FLAGGED: Mersenne Twister and str_shuffle as secrets
$token = md5(mt_rand());
$_SESSION['csrf_token'] = substr(str_shuffle('abcdef0123456789abcdef0123456789'), 0, 32);
```

```ruby
# FLAGGED: Kernel#rand as a reset token
self.reset_token = rand(36**20).to_s(36)
```

## What is not flagged

- `crypto.randomBytes`/`randomUUID`/`getRandomValues`, `random_bytes()`/`random_int()`, `SecureRandom` (including `.sample(random: SecureRandom)`) and `crypto/rand`.
- Draws for jitter, sleeps, retries, colours, sampling and counts (`token_count`, `max_tokens`, `token_expiry`).
- Test files, and the pattern in comments or string literals.

Python, Java, C# and C/C++ are covered by VNX-PY-005, VNX-JAVA-016, VNX-CS-006 and VNX-CRYPTO-009; VNX-GO-008 covers Go in more depth.

## Remediation

1. Draw every secret from a CSPRNG: `crypto.randomBytes()`/`crypto.randomUUID()`, `random_bytes()`/`random_int()`, `SecureRandom.hex`/`urlsafe_base64`, `crypto/rand`.
2. For confirmed false positives, add a `# vulnetix-ignore: VNX-338` comment on the line.

## References

- [CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)](https://cwe.mitre.org/data/definitions/338.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-338
// nosec vnx-338
```

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