Session Identifiers: Generation and Properties
You'll learn how to create unpredictable session IDs that attackers can't guess or forge.
When a web application creates a session (as you learned in the previous lesson), it needs to give you a unique ticket—called a session identifier (session ID)—so it can recognize you on future requests. This ID is typically stored in a cookie or URL parameter.
Why Session ID Generation Matters
If an attacker can predict or guess your session ID, they can hijack your session and impersonate you. Imagine a coat check that hands out tickets numbered 1, 2, 3, 4... An attacker could simply try ticket #5 and walk out with someone else's coat. Session IDs work the same way—predictable patterns are catastrophic.
Required Properties
A secure session ID must have three key properties:
-
High Entropy: Enough randomness that guessing is computationally infeasible. A 128-bit random value (32 hexadecimal characters) provides approximately 340 undecillion possibilities—essentially unguessable.
-
Cryptographic Randomness: Generated using a cryptographically secure random number generator (CSRNG), not predictable algorithms like sequential counters or timestamp-based values. Think of a lottery ball machine versus counting 1, 2, 3...
-
Uniqueness: No two users should ever receive the same session ID, even across different time periods.
What Bad Session IDs Look Like
- Sequential:
SESSION_001,SESSION_002(predictable) - Timestamp-based:
20250515143022(guessable) - Low entropy:
abc123(brute-forceable)
What Good Session IDs Look Like
3a7f9c2e4b8d1a6f9e2c5b8a4d7f1e3c(128-bit random hex)- Long, random, impossible to predict
Key Takeaway: Session IDs must be generated with cryptographic randomness and sufficient entropy (minimum 128 bits) to prevent attackers from predicting or brute-forcing valid session tokens.