41. Session Hijacking – Tokens, Cookies and Defence
Chala mitrano, Chapter 40 madhe availability baghitla – aata session hijacking. HTTP by default stateless aahe. Login zala ki browser la session token / cookie deto – he token = tumchi identity for that visit. Red jar he token chori kela / guess kela / fix kela, tar website Rani samajte – actually Shahrukh (attacker) boltoy. CIA madhe he confidentiality (गोपनीयता) + integrity (अखंडता) of identity. Ghabru naka – aapan OWN host-only PHP lab var demo; internet Wi-Fi / third-party sites nahi. He khup important aahe, lakshat theva!
What you will learn in this chapter
- What a session is; why hijacking breaks identity trust
- Cookie theft on insecure transit (HTTP vs HTTPS; Secure / HttpOnly / SameSite)
- Session fixation and predictable session IDs
- XSS → cookie steal path (high-level) + CSP / encoding / HttpOnly defence
- Network sniffing of sessions (Wireshark/tcpdump concepts; HSTS)
- Application-level hijack: token in URL, CSRF vs session, JWT pitfalls (concepts)
- Detection: concurrent sessions, UA/IP anomalies, SIEM signals
- Defence checklist: regenerate on login, short TTL, logout invalidate, MFA, WAF
- Lab-safe PHP cookie demo on OWN nginx/Apache + ethics / IT Act
- Project Build-Hack-Fix for Raja-Rani / Sahyadri Traders login session
Lab scope
Practice only against systems you own in host-only / isolated lab (Kali 192.168.56.10, Metasploitable 192.168.56.20, your own PHP/nginx/Apache lab VM). Cookie theft, fixation, and replay demos = OWN PHP app only. Never sniff open café Wi-Fi for strangers' cookies, never steal sessions from live shops, never XSS-inject third-party sites. Tools like Burp / curl / Wireshark stay pointed at host-only IPs. IT Act sections such as 43 and 66 apply if you access others' accounts without authority (verify current text). Lab = learn defence. Production = protect sessions.
Concepts in this chapter
- 41.1What Is a Session – Why Hijacking Matters
- 41.2Cookie Theft and Insecure Transit
- 41.3Session Fixation and Predictable IDs
- 41.4XSS Path to Session Cookies – Defence First
- 41.5Network Sniffing of Sessions – Host-Only Concepts
- 41.6Application-Level Hijack – URL Tokens, CSRF, JWT Concepts
- 41.7Detection – Logs, Concurrent Sessions, SIEM Signals
- 41.8Defence Hardening Checklist
- 41.9Lab-Safe PHP Cookie Demo + Ethics / IT Act
- 41.10Putting It Together – Purple Team Mindset
The chapter recap is at the end of the last concept page.
Chapter 41: Session hijacking and cookies — practice tasks
Open chapter lessons · Full guided lab: cookie flags secure httponly samesite devtools (not repeated here)
41.1 Check cookie flags in DevTools
EasyBankingWhy: Missing flags let XSS steal sessions.
What: Log into a lab app and inspect the session cookie.
How:
- DevTools → Application → Cookies.
- Note Secure, HttpOnly, SameSite.
Expected output: Ideal: Secure + HttpOnly + SameSite=Lax/Strict.
Hint
See linked cookie flags lab.
Answer / how to check
- You wrote Yes/No for each flag.
41.2 Set safe cookies in Express
MediumITWhy: Framework defaults are not always safe.
What: Create a Node cookie with flags.
How:
- res.cookie('sid','x',{httpOnly:true,secure:false,sameSite:'lax'}) for HTTP lab.
- curl -v and read Set-Cookie.
Expected output: HttpOnly and SameSite=Lax present.
Hint
Use secure:true when on HTTPS.
Answer / how to check
- Set-Cookie line shows both flags.
41.3 CSRF defence sketch
MediumRetailWhy: Stolen cookies still need CSRF tokens for state changes.
What: Add a synchronizer token to a lab form.
How:
- Put a random token in the session and a hidden form field.
- Reject POST if they differ.
Expected output: Forged POST without token fails.
Hint
SameSite helps but tokens are still best.
Answer / how to check
- Missing token → 403.
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.