Predictable Session Identifiers
Session identifiers are generated in a way that lets an attacker predict or guess values issued to other users, and so take over their sessions.
| Severity | High |
| CVSS 3.1 | 7.4 CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:N Open in the calculator |
| CVSS 4.0 | 9.1 CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:H/VI:H/VA:N/SC:N/SI:N/SA:N Open in the calculator |
| CWE | CWE-330: Use of Insufficiently Random Values |
| OWASP Top 10 | A04:2025 Cryptographic Failures |
| MITRE ATT&CK | T1606.001 Web Cookies, T1550.004 Web Session Cookie |
| Category | Cryptographic Failures |
Description
The application issues session identifiers that are not generated from a cryptographically secure source of randomness, so values issued to other users can be predicted, or narrowed down to a small set that can be tried in turn. An attacker who can predict a valid identifier can take over another user's session without knowing their password or interacting with them, and act with that user's privileges for as long as the session remains valid. Short session lifetimes narrow the window but do not remove the risk.
Common causes are identifiers built from a counter, a timestamp, the username or user ID, or a hash of such values, and identifiers produced by a general-purpose random number generator (such as java.util.Random in Java, Math.random() in JavaScript or mt_rand() in PHP) whose output can be predicted from values already seen. Where identifiers follow a simple sequence or are derived from known values, every active session can be hijacked, including those of administrators. Identifiers that are too short are a related form: even when random, the range of possible values is small enough to search. Where only part of the identifier is predictable, the attacker must make many guesses, which is slower and more likely to be noticed, but remains practical where requests are not rate limited.
The weakness is usually found in custom session or token schemes rather than in the session management built into mature frameworks. It can be identified by collecting a large sample of identifiers, for example with the Sequencer tool in Burp Suite, and analysing them for low entropy, shared prefixes, sequences or a relationship to the time of issue. Decoding identifiers that look encoded (Base64 or hexadecimal) often reveals the values they were built from. Logging in repeatedly in quick succession, and as different users, and comparing the identifiers issued is a quick manual check.
Remediation
Replace the identifier generation with a cryptographically secure source:
- Use the session management built into the application framework or platform (for example the servlet container's sessions in Java, ASP.NET Core's authentication cookies, or
express-sessionin Node.js) rather than a custom scheme, and keep it up to date. - Where a custom identifier or token is unavoidable, generate at least 128 bits from a cryptographically secure random number generator, such as
SecureRandomin Java, thesecretsmodule in Python,crypto.randomBytes()in Node.js,random_bytes()in PHP orRandomNumberGeneratorin .NET, and encode it as hexadecimal or Base64url. - Do not derive identifiers from usernames, user IDs, timestamps, counters or hashes of them, and do not give the value any meaning.
- Issue a new identifier at login and at any change of privilege, and invalidate identifiers at logout and after a period of inactivity.
As an interim measure, rate limiting and alerting on requests that present invalid session identifiers make guessing slower and more visible. Confirm the fix by repeating the statistical analysis on a fresh sample of identifiers and checking that no pattern, and no relationship to the user or the time of issue, remains.
References
Add This Finding to Your Library
In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.
Free trialYour trial starts when your portal goes live, not when you sign up, so the time you spend setting up does not count. · No card required
Only need the reporting? Our reporting tool is free and open source, with the same library.