312-50 ยท Session hijacking
Session hijacking countermeasures
HTTPS and HSTS, Secure/HttpOnly/SameSite cookie flags, token regeneration, short timeouts, IPsec, and detection tools.
Key points
- HTTPS everywhere, pinned with HSTS.
- Cookie flags: Secure (HTTPS only), HttpOnly (hidden from JavaScript), SameSite (limits cross-site sending).
- New session ID at login and privilege change; long random IDs.
- Invalidate on the server at logout; idle and absolute timeouts.
- Network: IPsec or VPNs; watch ARP anomalies with IDS or ARP monitoring.
Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies
Practice questions
What does the HttpOnly cookie attribute prevent?
- Sending the cookie over plain HTTP
- Reading the cookie from JavaScript
- Sending the cookie on cross-site requests
- Caching the cookie on the server
Show answer
Answer: B. Reading the cookie from JavaScript
HttpOnly hides the cookie from document.cookie, limiting theft through XSS. It does not encrypt transport (that is Secure/HTTPS) or stop CSRF (that is SameSite).
Which measures reduce the risk of session hijacking?
- Regenerate the session ID after authentication
- Include the session ID in URLs for convenience
- Apply short idle timeouts
- Set SameSite on session cookies
- Reuse one ID for a user across all logins
Show answer
Answer: A. Regenerate the session ID after authentication, C. Apply short idle timeouts, D. Set SameSite on session cookies
New IDs at login stop fixation, short idle timeouts shrink the window, and SameSite limits cross-site use. Putting IDs in URLs or reusing one ID forever does the opposite.
True or false: setting the Secure attribute on a cookie also stops JavaScript from reading it.
Show answer
Answer: False
False. Secure only restricts the cookie to HTTPS requests. Hiding it from JavaScript is the job of HttpOnly.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
