Run static analysis (SAST) on the codebase looking for the unsafe pattern in the data flow.
Generation of Predictable Numbers or Identifiers
This vulnerability occurs when a system creates numbers or identifiers that are too easy to guess, undermining security mechanisms that rely on unpredictability.
What is CWE-340?
Real-world CVEs caused by CWE-340
-
Product for administering PBX systems uses predictable identifiers and timestamps for filenames (CWE-340) which allows attackers to access files via direct request (CWE-425).
-
PRNG allows attackers to use the output of small PRNG requests to determine the internal state information, which could be used by attackers to predict future pseudo-random numbers.
-
Listening TCP ports are sequentially allocated, allowing spoofing attacks.
Step-by-step attacker path
- 1
This code generates a unique random identifier for a user's session.
- 2
Because the seed for the PRNG is always the user's ID, the session ID will always be the same. An attacker could thus predict any user's session ID and potentially hijack the session.
- 3
This example also exhibits a Small Seed Space (CWE-339).
Vulnerable PHP
This code generates a unique random identifier for a user's session.
function generateSessionID($userID){
srand($userID);
return rand();
} Secure pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
const safe = validateAndEscape(input);
return executeWithGuards(safe);
} How to prevent CWE-340
- Architecture Use safe-by-default frameworks and APIs that prevent the unsafe pattern from being expressible.
- Implementation Validate input at trust boundaries; use allowlists, not denylists.
- Implementation Apply the principle of least privilege to credentials, file paths, and runtime permissions.
- Testing Cover this weakness in CI: SAST rules + targeted unit tests for the data flow.
- Operation Monitor logs for the runtime signals listed in the next section.
How to detect CWE-340
Run dynamic application security testing against the live endpoint.
Watch runtime logs for unusual exception traces, malformed input, or authorization bypass attempts.
Code review: flag any new code that handles input from this surface without using the validated framework helpers.
Plexicus auto-detects CWE-340 and opens a fix PR in under 60 seconds.
Codex Remedium scans every commit, identifies this exact weakness, and ships a reviewer-ready pull request with the patch. No tickets. No hand-offs.
Frequently asked questions
What is CWE-340?
This vulnerability occurs when a system creates numbers or identifiers that are too easy to guess, undermining security mechanisms that rely on unpredictability.
How serious is CWE-340?
MITRE has not published a likelihood-of-exploit rating for this weakness. Treat it as medium-impact until your threat model proves otherwise.
What languages or platforms are affected by CWE-340?
MITRE has not specified affected platforms for this CWE — it can apply across most application stacks.
How can I prevent CWE-340?
Use safe-by-default frameworks, validate untrusted input at trust boundaries, and apply the principle of least privilege. Cover the data-flow signature in CI with SAST.
How does Plexicus detect and fix CWE-340?
Plexicus's SAST engine matches the data-flow signature for CWE-340 on every commit. When a match is found, our Codex Remedium agent opens a fix PR with the corrected code, tests, and a one-line summary for the reviewer.
Where can I learn more about CWE-340?
MITRE publishes the canonical definition at https://cwe.mitre.org/data/definitions/340.html. You can also reference OWASP and NIST documentation for adjacent guidance.
Weaknesses related to CWE-340
Use of Insufficiently Random Values
This vulnerability occurs when an application uses random values that are not sufficiently unpredictable in security-sensitive operations,…
Generation of Weak Initialization Vector (IV)
This vulnerability occurs when software uses a weak or predictable Initialization Vector (IV) for cryptographic operations. Many…
Use of Predictable Algorithm in Random Number Generator
This vulnerability occurs when a device or application relies on a predictable algorithm to generate pseudo-random numbers, making the…
Insufficient Entropy
This vulnerability occurs when a system's random number generator or algorithm lacks sufficient unpredictability, creating patterns or…
Small Space of Random Values
This vulnerability occurs when a system uses a random number generator that produces too few possible values. Attackers can easily predict…
Incorrect Usage of Seeds in Pseudo-Random Number Generator (PRNG)
This vulnerability occurs when a Pseudo-Random Number Generator (PRNG) is used, but its initial seed value is not handled securely or…
Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)
This vulnerability occurs when software uses a pseudo-random number generator (PRNG) that is not cryptographically strong for…
Use of Invariant Value in Dynamically Changing Context
This vulnerability occurs when code uses a fixed, unchanging value (like a hardcoded string, number, or reference) in a situation where…
Session Fixation
Session fixation occurs when an application authenticates a user without first destroying the previous session ID. This allows an attacker…
Don't Let Security
Weigh You Down.
Stop choosing between AI velocity and security debt. Plexicus is the only platform that runs Vibe Coding Security and ASPM in parallel — one workflow, every codebase.