41. Session Hijacking – Tokens, Cookies and Defence
41.3 Session Fixation and Predictable IDs
Session fixation: attacker sets (or knows) a session ID before victim logs in; victim authenticates into that same ID; attacker reuses it. Classic weak pattern: accept attacker-supplied session ID and never regenerate after successful login.
Predictable IDs: if session IDs are short, sequential, or weak RNG, Red can guess. Modern frameworks use long random IDs – still regenerate on privilege change.
Blue classics:
session_regenerate_id(true)(PHP) after successful login- Reject / ignore attacker-chosen IDs where framework allows
- Never put session ID in bookmarks / email links if avoidable
| Red team (attacker) does | Blue team (defender) detects / stops |
|---|---|
| Plants known session ID; waits for victim login | Regenerate session ID on login; invalidate old |
| Guesses weak / sequential IDs | Cryptographically strong long IDs from framework |
| Shares "helpful" login URL with embedded SID | Avoid SID in URL; Prefer cookies + regenerate |
Ravindra Bagale's Tip
Students keep the same PHPSESSID even after a successful login. Close the fixation door: regenerate on login. One line for the interview: "I rotate the session identifier at authentication." Don't worry – one function call, a big difference.
Ravindra Bagale's Tip – मराठी
Students login success नंतर पण तोच PHPSESSID ठेवतात. Fixation चा दरवाजा बंद: regenerate on login. Interview साठी एक line: "I rotate the session identifier at authentication." घाबरू नका – एक function call, मोठा फरक.
Ravindra Bagale's Tip – हिंदी
Students login success के बाद भी वही PHPSESSID रखते हैं. Fixation का दरवाज़ा बंद: regenerate on login. Interview के लिए एक line: "I rotate the session identifier at authentication." घबराओ मत – एक function call, बड़ा फ़र्क.
Lab
Deliberate weak PHP on OWN VM: login without regenerate. From Kali, note SID before login (or set cookie), complete login as Rani in browser, replay SID from Kali curl – see authenticated page. Then add session_regenerate_id(true) after password OK. Prove old SID fails. Host-only only.