CWE-341, Predictable from Observable State, is when a number or object can be predicted from observations an attacker can make about the system or network, such as the time or a process ID.
If the inputs to a value are public, the value is not secret. This hits authentication code, file naming, and anything that mixes a clock into an identifier.
How it happens
A developer wants a unique value and reaches for the state at hand. The state is observable, so the output is reproducible. Observed examples:
- CVE-2024-48445: a chain in which an e-commerce app relied on an easily guessable timestamp in a weak authentication algorithm.
- CVE-2002-0389: a mail server stored private mail messages with predictable filenames in a world-executable directory.
- CVE-2001-1141: a PRNG allowed attackers to use the output of small requests to determine internal state.
# Vulnerable: token from time and pid, both observable
token = hashlib.sha1(f"{time.time()}{os.getpid()}".encode()).hexdigest()
Hashing does not help. The attacker repeats the same computation over a small range of times.
What an attacker gains
The record lists impact as varying by context, and notes this can be exploited in a number of ways. If a predictable number is used to generate a session or authentication token, it may let an attacker impersonate users.
How to find it
Static analysis can find some uses. Search for time, pid and counters flowing into tokens, filenames or keys.
How to fix it
MITRE's mitigations:
- Increase the entropy used to seed a PRNG.
- Use products or modules that conform to FIPS 140-2 to avoid obvious entropy problems, and consult its approved generator list.
- Use a PRNG that periodically re-seeds itself from high quality sources such as hardware devices with high entropy, without re-seeding so often that the source blocks.
For tokens, simply use your platform's secure random function.
token = secrets.token_hex(32)
Quick check
Does hashing the time make it unpredictable?
No. If an attacker can guess the inputs, the attacker can compute the hash.
Why do predictable filenames matter?
If the directory is reachable, an attacker can request files by guessing names, as in the mail server case.