41. Session Hijacking – Tokens, Cookies and Defence
41.2 Cookie Theft and Insecure Transit
Cookie flags defenders must recite in sleep:
| Flag | Idea |
|---|---|
| Secure | Cookie only on HTTPS – not plain HTTP |
| HttpOnly | JavaScript cannot read document.cookie for that cookie – XSS steal harder |
| SameSite (Strict / Lax / None) | Cross-site send rules – CSRF risk shrinks with Strict/Lax |
Sidejacking era lesson: user logs in over HTTPS, then later hits HTTP pages on same site / same Wi-Fi – session cookie rides cleartext → sniffer copies it. Fix push: HTTPS everywhere + Secure flag + HSTS.
# Concepts – inspect Set-Cookie on YOUR lab only (example)
curl -sI http://192.168.56.40/login.php | tr -d '\r' | grep -i set-cookie
# After you enable HTTPS on lab (self-signed OK):
# curl -skI https://192.168.56.40/login.php | tr -d '\r' | grep -i set-cookie
| Red team (attacker) does | Blue team (defender) detects / stops |
|---|---|
| Sniffs HTTP Set-Cookie / Cookie on open Wi-Fi (classic sidejack) | Force HTTPS; Secure flag; HSTS; no mixed content |
| Reads cookie via XSS if not HttpOnly | HttpOnly; CSP; output encoding |
| Replays stolen Cookie header with curl/Burp | Short TTL; regenerate; anomaly detection |
Ravindra Bagale's Tip
Students set HttpOnly but skip the Secure flag – the cookie still leaks in transit. Use both (Secure + HttpOnly) + a sensible SameSite. Interview: name the three flags with one line each. Keep this in mind.
Ravindra Bagale's Tip – मराठी
Students Secure flag सोडून HttpOnly लावतात – transit मध्ये अजून leak. दोन्ही (Secure + HttpOnly) + योग्य SameSite. Interview: तिन्ही flags ची नावे एक-एक line सह सांगा. लक्षात ठेवा.
Ravindra Bagale's Tip – हिंदी
Students Secure flag छोड़कर HttpOnly लगा देते हैं – transit में फिर भी leak. दोनों (Secure + HttpOnly) + सही SameSite. Interview: तीनों flags के नाम एक-एक line के साथ बताओ. ध्यान रखो.
Lab
On OWN lab PHP site at 192.168.56.40: login, capture Set-Cookie with curl/browser DevTools. Note missing flags. Enable HTTPS (self-signed OK in lab), set session.cookie_secure=1, session.cookie_httponly=1, SameSite=Lax. Re-capture. Document before/after for Rani.
Real incident: Firesheep / sidejacking (2010)
In 2010, security researcher Eric Butler released Firesheep, a Firefox extension demo that showed how easy it was to capture HTTP session cookies on open Wi-Fi and impersonate users of major sites that still served much traffic in cleartext. Public reporting and the ensuing industry push accelerated HTTPS-by-default and Secure cookie adoption. Weakness: session cookies over HTTP on shared networks. Defence: HTTPS everywhere, Secure + HttpOnly cookies, HSTS. Source: contemporary coverage of Firesheep / Eric Butler demo and HTTPS adoption retrospectives (verify; say "reported" for impact anecdotes).
Extended teaching notes
Why this lesson matters
Web flaws stay in the OWASP Top 10 because real apps still ship them. Your job is not to collect tool screenshots. It is to prevent and fix the class of bug on systems you own.
Apply the fix across stacks
Same class of bug, same idea in every language.
Java / Spring Boot
Parameterise queries, encode output, CSRF tokens, limited Actuator, dependency scanning.
ASP.NET Core
EF Core / parameterised SQL, Razor encoding, HTTPS, Data Protection.
Node.js / Express
helmet, parameterised SQL, npm audit, avoid eval and shell concat.
Python / Django / Flask / FastAPI
ORM, autoescape, pip-audit, Bandit; never shell=True with user input.
PHP / Laravel
PDO/Eloquent, Blade escaping, composer audit.
Go / Ruby
database/sql placeholders / ActiveRecord; html/template / ERB escaping; govulncheck / Brakeman.
nginx / Apache / Tomcat
Hide versions, TLS, security headers, least-open ports.
MySQL / PostgreSQL / MongoDB / Redis
Private networks, auth on, least-privilege users, no 0.0.0.0/0.
AWS / Azure / GCP
Roles not long-lived keys, private data stores, cloud audit logs on.
Real examples
Global: injection and XSS still appear in breach reports and bug bounties every year. Indian company (sample): a Bengaluru SaaS team maps each OWASP item to a CI check or a code-review question so new code cannot silently reintroduce it.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.