41. Session Hijacking – Tokens, Cookies and Defence
41.7 Detection – Logs, Concurrent Sessions, SIEM Signals
Blue cannot prevent every steal – must detect odd use:
- Same session ID from two far cities in minutes
- Sudden User-Agent change mid-session
- Privilege action after long idle
- Many 401/403 then success from new IP
- Logout failed but cookie still accepted (invalidate bug)
Log what matters: session create, regenerate, destroy, login MFA result, IP, UA (privacy policy aware). SIEM / Wazuh / ELK – correlate; alert SOC (fictional Shahrukh) with runbook.
| Red team (attacker) does | Blue team (defender) detects / stops |
|---|---|
| Replays cookie from Kali while user still online | Concurrent session policy; step-up MFA; kill session |
| Spoofs UA to match victim | Combine IP+UA+behaviour; don't trust UA alone |
| Uses stolen session only for quiet data read | DLP / access logs on exports; anomaly baselines |
Ravindra Bagale's Tip
Students keep just one "login success" log line. Log the session lifecycle events – create, regenerate, destroy. These are what save you in an investigation. Got it?
Ravindra Bagale's Tip – मराठी
Students फक्त "login success log" ची एक line ठेवतात. Session lifecycle events log करा – create, regenerate, destroy. Investigation मध्ये हेच वाचवतात. समजले का?
Ravindra Bagale's Tip – हिंदी
Students सिर्फ़ "login success log" की एक line रखते हैं. Session lifecycle events log करो – create, regenerate, destroy. Investigation में यही बचाते हैं. समझ आया?
Lab
Add PHP/nginx log lines on OWN app: login, regenerate, logout. From Kali replay cookie while browser still logged in. Show two IPs in log for one SID. Write alert rule pseudo: same sid + distinct /24 within 5 minutes → ticket.
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.
Real incident: Microsoft 365 token spray – stolen sign-in sessions replayed into 79 organisations, no password needed (Oct 2026)
1. What happened
In this lesson you learnt that after you log in, the website gives you a session token (like a cookie). Whoever holds that token is "logged in". If an attacker copies it, they need no password and no MFA code.
- On 22 October 2026, the security company Huntress reported a campaign against Microsoft 365 email accounts.
- Between 14 July and 22 September 2026, Huntress responded to incidents at 79 organisations. It saw at least 497 successful sign-ins and more than 200,000 failed attempts from the attacker.
- The attacker did not guess passwords. It replayed sign-in tokens stolen from victims. Huntress calls this "token spraying": like password spraying, but with stolen tokens.
- Inside the accounts, the attacker registered at least 48 fake computers in the company directory (Microsoft Entra ID), wiped and replaced the victims' MFA methods in 19 organisations, and made hidden inbox rules.
- In one case, the attacker sent a payroll-related email from a hacked mailbox to an outside accountant, and hid the replies. This is how business email compromise (BEC) fraud starts.
- Strict Conditional Access rules worked: in one wave, 1,052 sign-ins were blocked, all at just four organisations. In the other organisations nothing stopped the attacker.
2. How the hack happened (public reporting only – high-level)
- The victim got a phishing link to a fake sign-in page. The page was a relay (adversary-in-the-middle, AiTM). It showed the real Microsoft login and the real MFA prompt.
- The victim typed the password and approved MFA. Microsoft gave a session token after MFA, and the relay kept a copy.
- Some victims were instead tricked into typing a code on Microsoft's real page (device-code phishing). That also handed tokens to the attacker.
- Within minutes (4 to 12 minutes in two cases Huntress timed), the attacker reused the token from rented servers. The token was made after MFA, so no MFA prompt appeared.
- To stay in after the token expired, the attacker added its own authenticator app and registered fake devices.
- It made inbox rules with short, forgettable names that deleted mail or moved it to quiet folders like RSS Feeds or Conversation History, so the owner would not see replies or warnings.
- It changed IP addresses between waves, so a block on one address did not stop the next wave.
3. How to stop it / defend (this lesson: detection – logs, concurrent sessions, SIEM signals)
Why: MFA protects the login. It does not protect the token made after login. So you must watch how each session is used, and kill a bad session fast.
Signals to alert on in your SIEM:
- A user's session used from a new country or a hosting/VPN network minutes after a normal login.
- The operating system or browser changes in the middle of one session (Huntress saw macOS change to Windows).
- MFA methods deleted and a new one added within minutes.
- A new device registered from a hosting network.
- A new inbox rule with a 1–3 character name, a rule that moves mail to RSS Feeds or Conversation History, or a rule that matches words like payroll or bank.
- Many Conditional Access blocks for one user. Treat this as "the password or token is valid", not as "we are safe".
Steps when you find one:
- Revoke all sessions of the user. A password reset alone is not enough: Huntress says an access token can still work for about 90 minutes.
- Reset the password.
- Remove unknown MFA methods and devices. A password reset leaves them in place.
- Delete the hidden inbox rules and warn contacts (accountant, bank) about any fake email.
The same "log out everywhere" idea in your own web app:
- PHP: keep sessions in a database or Redis with the user id, and delete all rows for that user on password reset.
- Node.js (express-session): use a Redis store, keep a list of session ids per user, and destroy them all on reset.
- Java / Spring Security: use
SessionRegistry.getAllSessions(user, false)and callexpireNow()on each one. - .NET (ASP.NET Core Identity): call
UpdateSecurityStampAsync(user), so old login cookies stop working at the next security-stamp check. - Python / Django: a password change already logs out other sessions; for a full kill, delete that user's rows from the session table.
- JWT apps: keep access tokens short (5–15 minutes) and revoke the user's refresh tokens on the server.
4. How to take care so it won't happen again (checklist)
- [ ] Use phishing-resistant MFA (passkeys or FIDO2 security keys) for email and admin accounts. They do not work on a fake site.
- [ ] Turn on Conditional Access that requires MFA for all users, all apps and all client types, and allow only managed devices for staff where you can.
- [ ] Block device-code sign-in unless a team really needs it.
- [ ] Let only admins register devices, and stop normal users from approving new apps.
- [ ] Every week, review new MFA methods, new devices, new inbox rules and forwarding rules.
- [ ] Teach staff: never sign in or type a code from a link in an email. Open the app yourself.
- [ ] Confirm any payment or bank-detail change by calling a number you already know, not one from the email.
- [ ] Write a short runbook: revoke sessions → reset password → remove MFA methods and devices → remove rules → tell contacts.
Source: Huntress (Andrea Hatcher), "M365 Token Spray and Replay Campaign Compromises Business Email Accounts", Huntress Blog, 22 October 2026; Help Net Security, "Microsoft 365 accounts compromised at 79 organizations, no passwords needed", 23 October 2026; Infosecurity Magazine, "Widespread Token-Spraying Attacks Target Microsoft 365 Accounts", 23 October 2026.
1. काय झालं
या lesson मध्ये तुम्ही शिकलात की login केल्यावर website तुम्हाला एक session token देते (cookie सारखं). ज्याच्याकडे तो token आहे, तो "logged in" असतो. Attacker ने तो copy केला तर त्याला password पण लागत नाही आणि MFA code पण नाही.
- 22 ऑक्टोबर 2026 ला Huntress या security कंपनीने Microsoft 365 email accounts वरच्या एका campaign बद्दल report दिला.
- 14 जुलै ते 22 सप्टेंबर 2026 या काळात Huntress ने 79 organisations मधल्या incidents ला response दिला. Attacker चे किमान 497 successful sign-ins आणि 2,00,000 पेक्षा जास्त failed attempts दिसले.
- Attacker ने passwords guess केले नाहीत. त्याने victims कडून चोरलेले sign-in tokens परत वापरले (replay). Huntress याला "token spraying" म्हणते: password spraying सारखंच, पण चोरलेल्या tokens ने.
- Accounts मध्ये घुसल्यावर attacker ने company directory (Microsoft Entra ID) मध्ये किमान 48 खोटे computers register केले, 19 organisations मध्ये victims च्या MFA methods पुसून नवीन टाकल्या, आणि लपवलेले inbox rules बनवले.
- एका case मध्ये attacker ने hack झालेल्या mailbox मधून बाहेरच्या accountant ला payroll बद्दल email पाठवला, आणि त्याचे replies लपवले. Business email compromise (BEC) fraud असाच सुरू होतो.
- कडक Conditional Access rules कामाला आले: एका wave मध्ये 1,052 sign-ins block झाले, आणि ते सगळे फक्त चार organisations मध्ये. बाकीच्या organisations मध्ये attacker ला कोणी अडवलं नाही.
2. Hack कसा झाला (फक्त public reporting – high-level)
- Victim ला एका खोट्या sign-in page ची phishing link आली. ते page एक relay होतं (adversary-in-the-middle, AiTM). त्यावर खरा Microsoft login आणि खरा MFA prompt दिसत होता.
- Victim ने password टाकला आणि MFA approve केलं. MFA नंतर Microsoft ने session token दिला, आणि relay ने त्याची copy ठेवली.
- काही victims ना वेगळ्या पद्धतीने फसवलं: Microsoft च्या खऱ्या page वर एक code टाकायला लावलं (device-code phishing). त्यानेही tokens attacker ला मिळाले.
- काही मिनिटांत (Huntress ने मोजलेल्या दोन cases मध्ये 4 ते 12 मिनिटं) attacker ने भाड्याच्या servers वरून तो token परत वापरला. Token MFA नंतर बनला होता, म्हणून MFA prompt आलाच नाही.
- Token expire झाल्यावरही आत राहता यावं म्हणून attacker ने स्वतःचं authenticator app add केलं आणि खोटे devices register केले.
- त्याने छोट्या, लक्षात न राहणाऱ्या नावांचे inbox rules बनवले, जे mail delete करायचे किंवा RSS Feeds, Conversation History सारख्या शांत folders मध्ये हलवायचे, म्हणजे owner ला replies किंवा warnings दिसू नयेत.
- त्याने प्रत्येक wave मध्ये IP addresses बदलले, म्हणून एका address वरचा block पुढची wave थांबवू शकला नाही.
3. हे कसं थांबवायचं / defend कसं करायचं (हा lesson: detection – logs, concurrent sessions, SIEM signals)
का: MFA login ला protect करतं. ते login नंतर बनलेल्या token ला protect करत नाही. म्हणून प्रत्येक session कसा वापरला जातोय यावर लक्ष ठेवा, आणि वाईट session लगेच kill करा.
SIEM मध्ये या signals वर alert लावा:
- Normal login नंतर काही मिनिटांत user चा session नवीन देशातून किंवा hosting/VPN network वरून वापरला जाणं.
- एकाच session च्या मध्येच operating system किंवा browser बदलणं (Huntress ला macOS चं Windows झालेलं दिसलं).
- MFA methods delete होणं आणि काही मिनिटांत नवीन add होणं.
- Hosting network वरून नवीन device register होणं.
- 1–3 अक्षरी नावाचा नवीन inbox rule, mail RSS Feeds किंवा Conversation History मध्ये हलवणारा rule, किंवा payroll, bank सारख्या शब्दांवर चालणारा rule.
- एकाच user साठी खूप Conditional Access blocks. याचा अर्थ "password किंवा token valid आहे" असा घ्या, "आपण safe आहोत" असा नाही.
असं काही सापडलं तर steps:
- User चे सगळे sessions revoke करा. फक्त password reset पुरेसा नाही: Huntress नुसार access token साधारण 90 मिनिटं चालू राहू शकतो.
- Password reset करा.
- ओळखीच्या नसलेल्या MFA methods आणि devices काढून टाका. Password reset त्यांना तसंच ठेवतो.
- लपवलेले inbox rules delete करा आणि contacts ना (accountant, bank) कोणत्याही खोट्या email बद्दल सावध करा.
हीच "सगळीकडून logout" idea तुमच्या स्वतःच्या web app मध्ये:
- PHP: sessions database किंवा Redis मध्ये user id सोबत ठेवा, आणि password reset झाल्यावर त्या user च्या सगळ्या rows delete करा.
- Node.js (express-session): Redis store वापरा, प्रत्येक user च्या session ids ची list ठेवा, आणि reset वेळी त्या सगळ्या destroy करा.
- Java / Spring Security:
SessionRegistry.getAllSessions(user, false)वापरा आणि प्रत्येक session वरexpireNow()call करा. - .NET (ASP.NET Core Identity):
UpdateSecurityStampAsync(user)call करा, म्हणजे पुढच्या security-stamp check ला जुन्या login cookies चालणं बंद होतं. - Python / Django: password बदलला की बाकीचे sessions आपोआप logout होतात; पूर्ण kill साठी session table मधून त्या user च्या rows delete करा.
- JWT apps: access tokens छोटे ठेवा (5–15 मिनिटं) आणि server वर त्या user चे refresh tokens revoke करा.
4. पुन्हा असं होऊ नये म्हणून काय काळजी घ्यायची (checklist)
- [ ] Email आणि admin accounts साठी phishing-resistant MFA (passkeys किंवा FIDO2 security keys) वापरा. ते खोट्या site वर चालतच नाहीत.
- [ ] सगळे users, सगळे apps आणि सगळे client types साठी MFA लागेल असा Conditional Access चालू करा, आणि शक्य असेल तिथे staff साठी फक्त managed devices allow करा.
- [ ] एखाद्या team ला खरंच गरज नसेल तर device-code sign-in block करा.
- [ ] Devices फक्त admins ना register करू द्या, आणि normal users ना नवीन apps approve करू देऊ नका.
- [ ] दर आठवड्याला नवीन MFA methods, नवीन devices, नवीन inbox rules आणि forwarding rules review करा.
- [ ] Staff ला शिकवा: email मधल्या link वरून कधीच sign in करू नका किंवा code टाकू नका. App स्वतः उघडा.
- [ ] Payment किंवा bank details बदलाची कोणतीही request, email मधल्या नंबरवर नाही तर तुमच्याकडे आधीपासून असलेल्या नंबरवर call करून confirm करा.
- [ ] छोटं runbook लिहा: sessions revoke → password reset → MFA methods आणि devices काढा → rules काढा → contacts ना कळवा.
स्रोत: Huntress (Andrea Hatcher), "M365 Token Spray and Replay Campaign Compromises Business Email Accounts", Huntress Blog, 22 ऑक्टोबर 2026; Help Net Security, "Microsoft 365 accounts compromised at 79 organizations, no passwords needed", 23 ऑक्टोबर 2026; Infosecurity Magazine, "Widespread Token-Spraying Attacks Target Microsoft 365 Accounts", 23 ऑक्टोबर 2026.
1. क्या हुआ
इस lesson में आपने सीखा कि login करने के बाद website आपको एक session token देती है (cookie जैसा). जिसके पास वह token है, वह "logged in" है. अगर attacker उसे copy कर ले, तो उसे न password चाहिए, न MFA code.
- 22 अक्टूबर 2026 को security कंपनी Huntress ने Microsoft 365 email accounts पर चले एक campaign की report दी.
- 14 जुलाई से 22 सितंबर 2026 के बीच Huntress ने 79 organisations के incidents पर response दिया. उसे attacker के कम से कम 497 successful sign-ins और 2,00,000 से ज़्यादा failed attempts दिखे.
- Attacker ने passwords guess नहीं किए. उसने victims से चुराए गए sign-in tokens दोबारा इस्तेमाल किए (replay). Huntress इसे "token spraying" कहती है: password spraying जैसा, लेकिन चुराए गए tokens से.
- Accounts के अंदर attacker ने company directory (Microsoft Entra ID) में कम से कम 48 नकली computers register किए, 19 organisations में victims के MFA methods मिटाकर नए डाले, और छिपे हुए inbox rules बनाए.
- एक case में attacker ने hack हुए mailbox से बाहर के accountant को payroll वाला email भेजा, और उसके replies छिपा दिए. Business email compromise (BEC) fraud ऐसे ही शुरू होता है.
- सख़्त Conditional Access rules काम आए: एक wave में 1,052 sign-ins block हुए, और वे सब सिर्फ चार organisations में थे. बाकी organisations में attacker को किसी ने नहीं रोका.
2. Hack कैसे हुआ (सिर्फ public reporting – high-level)
- Victim को एक नकली sign-in page की phishing link मिली. वह page एक relay था (adversary-in-the-middle, AiTM). उस पर असली Microsoft login और असली MFA prompt दिखता था.
- Victim ने password डाला और MFA approve किया. MFA के बाद Microsoft ने session token दिया, और relay ने उसकी copy रख ली.
- कुछ victims को दूसरे तरीके से फँसाया गया: Microsoft के असली page पर एक code डलवाया गया (device-code phishing). इससे भी tokens attacker को मिल गए.
- कुछ ही मिनटों में (Huntress के नापे दो cases में 4 से 12 मिनट) attacker ने किराए के servers से वह token दोबारा इस्तेमाल किया. Token MFA के बाद बना था, इसलिए कोई MFA prompt आया ही नहीं.
- Token expire होने के बाद भी अंदर रहने के लिए attacker ने अपना authenticator app add किया और नकली devices register किए.
- उसने छोटे, याद न रहने वाले नामों के inbox rules बनाए, जो mail delete करते थे या RSS Feeds, Conversation History जैसे शांत folders में भेज देते थे, ताकि owner को replies या warnings न दिखें.
- उसने हर wave में IP addresses बदले, इसलिए एक address पर लगा block अगली wave को नहीं रोक पाया.
3. इसे कैसे रोकें / defend कैसे करें (यह lesson: detection – logs, concurrent sessions, SIEM signals)
क्यों: MFA login को protect करता है. वह login के बाद बने token को protect नहीं करता. इसलिए हर session कैसे इस्तेमाल हो रहा है, इस पर नज़र रखें, और खराब session तुरंत kill करें.
SIEM में इन signals पर alert लगाएँ:
- Normal login के कुछ मिनट बाद user का session नए देश से या hosting/VPN network से इस्तेमाल होना.
- एक ही session के बीच में operating system या browser बदल जाना (Huntress को macOS से Windows बनता दिखा).
- MFA methods delete होना और कुछ मिनट में नया add होना.
- Hosting network से नया device register होना.
- 1–3 अक्षर वाले नाम का नया inbox rule, mail को RSS Feeds या Conversation History में भेजने वाला rule, या payroll, bank जैसे शब्दों पर चलने वाला rule.
- एक ही user के लिए बहुत सारे Conditional Access blocks. इसका मतलब "password या token valid है" समझें, "हम safe हैं" नहीं.
ऐसा कुछ मिले तो steps:
- User के सारे sessions revoke करें. सिर्फ password reset काफी नहीं: Huntress के अनुसार access token लगभग 90 मिनट तक चल सकता है.
- Password reset करें.
- अनजान MFA methods और devices हटाएँ. Password reset उन्हें वैसे ही छोड़ देता है.
- छिपे हुए inbox rules delete करें और contacts (accountant, bank) को किसी भी नकली email के बारे में सावधान करें.
यही "हर जगह से logout" idea आपके अपने web app में:
- PHP: sessions को database या Redis में user id के साथ रखें, और password reset होने पर उस user की सारी rows delete करें.
- Node.js (express-session): Redis store इस्तेमाल करें, हर user के session ids की list रखें, और reset पर उन सबको destroy करें.
- Java / Spring Security:
SessionRegistry.getAllSessions(user, false)इस्तेमाल करें और हर session परexpireNow()call करें. - .NET (ASP.NET Core Identity):
UpdateSecurityStampAsync(user)call करें, ताकि अगले security-stamp check पर पुरानी login cookies काम करना बंद कर दें. - Python / Django: password बदलते ही बाकी sessions अपने आप logout हो जाते हैं; पूरा kill करने के लिए session table से उस user की rows delete करें.
- JWT apps: access tokens छोटे रखें (5–15 मिनट) और server पर उस user के refresh tokens revoke करें.
4. दोबारा ऐसा न हो, इसके लिए क्या ध्यान रखें (checklist)
- [ ] Email और admin accounts के लिए phishing-resistant MFA (passkeys या FIDO2 security keys) इस्तेमाल करें. वे नकली site पर काम ही नहीं करते.
- [ ] सभी users, सभी apps और सभी client types के लिए MFA ज़रूरी करने वाला Conditional Access चालू करें, और जहाँ हो सके staff के लिए सिर्फ managed devices allow करें.
- [ ] अगर किसी team को सच में ज़रूरत नहीं है तो device-code sign-in block करें.
- [ ] Devices सिर्फ admins को register करने दें, और normal users को नए apps approve न करने दें.
- [ ] हर हफ्ते नए MFA methods, नए devices, नए inbox rules और forwarding rules review करें.
- [ ] Staff को सिखाएँ: email की link से कभी sign in न करें और code न डालें. App खुद खोलें.
- [ ] Payment या bank details बदलने की कोई भी request, email वाले नंबर पर नहीं, बल्कि पहले से आपके पास मौजूद नंबर पर call करके confirm करें.
- [ ] एक छोटा runbook लिखें: sessions revoke → password reset → MFA methods और devices हटाएँ → rules हटाएँ → contacts को बताएँ.
स्रोत: Huntress (Andrea Hatcher), "M365 Token Spray and Replay Campaign Compromises Business Email Accounts", Huntress Blog, 22 अक्टूबर 2026; Help Net Security, "Microsoft 365 accounts compromised at 79 organizations, no passwords needed", 23 अक्टूबर 2026; Infosecurity Magazine, "Widespread Token-Spraying Attacks Target Microsoft 365 Accounts", 23 अक्टूबर 2026.
Check if you're safe: Password Strength Checker