41. Session Hijacking – Tokens, Cookies and Defence
41.8 Defence Hardening Checklist
SME / lab checklist (print for Rani):
- HTTPS everywhere on auth and cookies; HSTS
- Cookie flags: Secure, HttpOnly, SameSite=Lax (or Strict where UX allows)
- Regenerate session ID on login and privilege elevation
- Short idle / absolute TTL; "remember me" = separate hardened design
- Logout destroys server-side session, not only browser cookie clear
- MFA for admin / payout / email-change
- CSRF tokens on state-changing requests
- XSS hygiene + CSP on app pages
- Avoid SID in URL; avoid tokens in localStorage for high-value sessions when cookie pattern fits
- Monitor concurrent sessions; admin "kill sessions" button
- WAF rules for common session abuse patterns (support, not sole control)
- Framework updates – session libs have CVEs historically
<?php
// OWN lab PHP sketch – after password verified (illustrative)
session_start();
// ... verify password against DB (prepared statements) ...
session_regenerate_id(true);
$_SESSION['user'] = 'rani';
$_SESSION['login_at'] = time();
// php.ini / ini_set ideas for lab:
// session.cookie_httponly = 1
// session.cookie_secure = 1 // needs HTTPS
// session.cookie_samesite = Lax
?>
| Red team (attacker) does | Blue team (defender) detects / stops |
|---|---|
| Hopes logout only cleared client cookie | Server-side session destroy + new ID |
| Uses week-old stolen cookie | Short TTL + idle timeout + absolute timeout |
| Targets admin without MFA | MFA + regenerate + alert on admin UA/IP change |
Ravindra Bagale's Tip
Make the checklist your wallpaper – but prove it with curl before/after in the lab. A paper policy without regenerate = false comfort. Interview: walk through 5 controls without reading your notes.
Ravindra Bagale's Tip – मराठी
Checklist wallpaper बनवा – पण lab मध्ये curl before/after ने prove करा. Regenerate शिवाय paper policy = खोटे समाधान. Interview: notes न वाचता 5 controls सांगा.
Ravindra Bagale's Tip – हिंदी
Checklist को wallpaper बनाओ – पर lab में curl before/after से prove करो. Regenerate के बिना paper policy = झूठी तसल्ली. Interview: notes पढ़े बिना 5 controls बताओ.
Practice task
Score Sahyadri Traders current fictional portal 0–12 on the checklist. Pick worst 3 gaps; write owner email in professional English proposing fix order (HTTPS → flags → regenerate → TTL → MFA).