# VNX-343: Predictable Value Range from Previous Values

Detects secrets that are the previous value plus a small random step (token += random.randint(...), session_id = last + rng.nextInt(500).

| Field | Value |
|---|---|
| Rule ID | `VNX-343` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Python, Java, Kotlin, JavaScript / TypeScript, PHP, Ruby, Go, C, C++ |
| CWE | [CWE-343](https://cwe.mitre.org/data/definitions/343.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: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 | `randomness` `predictable` `predictable-value-range` `cwe-343` |
| Source | [`internal/sast/rules/vnx-343.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-343.rego) |

## Overview

VNX-343 maps to [CWE-343: Predictable Value Range from Previous Values](https://cwe.mitre.org/data/definitions/343.html). A value that moves forward by a small random step from the last one is predictable within a window: anyone who has seen one value knows the next lies just above it, and tries the few hundred candidates. This is the classic weak TCP initial-sequence-number design, and it appears in hand-rolled token and session-id generators.

The rule reports:

- `x += <random draw>` where `x` is named for a secret or an initial sequence number (token, session id, reset code, OTP, CSRF, password, API key, isn), and
- `<previous> + <random draw>` where the previous value is named last/prev/previous/current/old/next or for a secret, and the sum is stored under, returned as, or flows within six lines into a secret.

A random draw is `random.randint`/`randrange`/`getrandbits`/`uniform`, `secrets.randbelow`, `Math.random()` (also inside `Math.floor`), `rand()`/`mt_rand()`/`random_int()`, `nextInt`/`nextLong` (Random, ThreadLocalRandom, kotlin.random.Random), Go `rand.Intn`/`IntN`/`Int63n`/`Int31n` (also inside a numeric conversion), Ruby `rand`/`random_number`, and C `rand()`/`random()`/`arc4random_uniform`. A small step from a CSPRNG is still reported: the range is the weakness, not the generator.

## What gets flagged

```python
# FLAGGED
self.last_token += random.randint(1, 1000)
reset_code = prev_code + random.randrange(10, 99)
session_id = last_session + secrets.randbelow(256)
```

```java
// FLAGGED
long sessionId = previousSessionId + ThreadLocalRandom.current().nextInt(1, 500);
```

```c
/* FLAGGED: initial sequence numbers a small step apart */
isn = last_isn + (rand() % 64000);
tcb->snd_isn += rand() & 0xffff;
```

## What is not flagged

- Jitter, delays, backoff, poll intervals, positions, totals and other values that are not secrets (`self.retry_delay += random.uniform(0, 1)`, `next_poll = last_poll + random.randint(1, 5)`).
- Counts and bookkeeping names (`token_count`, `session_timeout`).
- A draw written before the previous value (`rand() + last`), and an increment by a constant (VNX-342).
- Test files, and the pattern in comments or string literals.

## Remediation

1. Generate each secret independently with a CSPRNG over its full width: `secrets.token_urlsafe()`, `crypto.randomBytes()`, `random_bytes()`, `SecureRandom`, `crypto/rand`.
2. For sequence numbers, use a keyed hash of the connection identifiers plus a clock (RFC 6528) rather than a random step from the previous value.
3. For confirmed false positives, add a `# vulnetix-ignore: VNX-343` comment on the line.

## References

- [CWE-343: Predictable Value Range from Previous Values](https://cwe.mitre.org/data/definitions/343.html)
- [RFC 6528: Defending against Sequence Number Attacks](https://www.rfc-editor.org/rfc/rfc6528)

## 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-343
// nosec vnx-343
```

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