Session hijacking

Also known as: cookie hijacking

On this page 7

Session hijacking is an attack in which an adversary obtains or forges a valid session identifier and uses it to impersonate a user who has already authenticated to an application. In application security, it is significant because it bypasses the login step entirely: passwords, multi-factor authentication and CAPTCHA checks are irrelevant once the attacker holds a token the server already trusts.

Because most web sessions are carried in browser cookies, the attack is also called cookie hijacking or sidejacking. The same principle applies to bearer tokens in APIs, mobile apps and single sign-on systems: whoever presents the token is treated as the user.

How session hijacking exploits stateful authentication

HTTP is stateless, so after a successful login the server issues a session token, usually a random identifier stored in a cookie or a signed token such as a JWT. The browser sends this token with every request, and the server maps it to the authenticated identity. The token therefore functions as a temporary credential equivalent to the password for the lifetime of the session.

Session hijacking succeeds when an attacker can read, predict or plant that token. Once obtained, the attacker replays it from another browser or script, and the application processes requests as if they came from the victim. Unless the server binds the session to additional context or detects anomalies, nothing distinguishes the attacker from the legitimate user.

Attack techniques and variants

Attackers obtain session tokens through several distinct routes, and each requires a different defense. The table below groups the main techniques by the weakness they exploit.

TechniqueHow the token is obtainedWeakness exploited
Cross-site scripting theftInjected script reads document.cookie and sends it outXSS flaw plus cookie without HttpOnly
Network sniffingToken captured from unencrypted trafficMissing HTTPS or absent Secure flag
Session fixationAttacker plants a known ID before the victim logs inID not regenerated after authentication
Token predictionIdentifier guessed from a patternLow-entropy or sequential IDs
Infostealer malwareCookies exported from the browser profileCompromised endpoint
Adversary-in-the-middle phishingReverse proxy relays login and captures the post-MFA cookiePhishable authentication
Token leakageID exposed in URLs, logs or Referer headersTokens placed in query strings
Main session hijacking techniques

In recent years the dominant real-world vectors have shifted from network sniffing toward infostealer malware and adversary-in-the-middle phishing kits, because these defeat multi-factor authentication. The attacker does not need the password at all, only the authenticated session cookie issued after MFA.

Session hijacking is often confused with neighboring application security attacks that also abuse sessions. The decisive difference is whether the attacker possesses the token or merely causes the victim’s browser to use it.

AttackDoes the attacker hold the tokenTypical goal
Session hijackingYes, the token is stolen or forgedFull account takeover
CSRFNo, the victim’s browser sends itTrigger one unwanted action
XSSNot necessarily; it is often the delivery vectorRun script in the victim’s origin
Credential stuffingNo, the attacker logs in with reused passwordsCreate a new session
ClickjackingNo, the victim clicks a hidden frameTrick the user into an action
Session hijacking and neighboring attacks

These attacks frequently chain together. A stored XSS flaw, for example, can deliver a script that exfiltrates the session cookie, turning a client-side injection bug into a full hijack. For that reason, incident responders who find XSS in an authenticated area should assume that active sessions may be compromised and force their invalidation.

Defending sessions in practice

Effective defense combines strong token generation, strict cookie attributes, transport security and server-side session control. No single measure is sufficient, because the techniques above attack different points in the token’s life cycle.

ControlRecommended settingThreat mitigated
Token entropyAt least 64 bits from a secure random generatorPrediction and brute force
HttpOnly attributeSet on all session cookiesScript-based theft through XSS
Secure attribute and HSTSHTTPS only, HSTS with long max-ageNetwork sniffing and downgrade
SameSite attributeLax or StrictCSRF and some cross-site leaks
ID regenerationNew ID at login and privilege changeSession fixation
TimeoutsIdle 15 to 30 minutes for sensitive apps, absolute limitLong-lived stolen tokens
Server-side revocationInvalidate on logout and password resetReuse of captured tokens
Session protection controls

Beyond cookie hygiene, mature applications add anomaly detection that flags a session suddenly used from a new country, device fingerprint or autonomous system. Re-authentication before high-risk actions such as changing the email address or payout details limits the damage of a stolen session. Emerging token binding approaches, such as Device Bound Session Credentials and DPoP for OAuth, tie the token to a key held on the client device so that a copied cookie is useless elsewhere.

Common mistakes and limitations of defenses

Many implementations fail in predictable ways. The most frequent errors concern token storage, lifetime and revocation.

  • Storing tokens in localStorage makes them readable by any script running in the origin, so a single XSS flaw exposes every session.
  • Stateless JWTs without revocation remain valid until expiry even after logout or a password change.
  • Binding sessions to IP addresses breaks legitimate mobile users and does little against attackers on the same network.
  • Logout that only deletes the cookie on the client leaves the server-side session usable by anyone holding a copy.
  • Relying on MFA alone does not help once the post-authentication cookie is stolen.

The fundamental limitation is that bearer tokens are transferable by design. Until binding to device-held keys becomes universal, defenders depend on short lifetimes, rapid revocation and behavioral detection.

In simple terms

When a person logs in to a website, the site gives the browser a kind of digital wristband, much like the band a festival gives visitors after checking their tickets. Staff no longer check the ticket, only the wristband.

Session hijacking means someone steals or copies that wristband. The thief walks in without ever showing a ticket, and staff assume it is the original visitor. Good protection makes wristbands hard to copy, expire quickly and get canceled the moment something looks wrong.

A practical example

An accountant at a mid-sized firm receives a convincing email about a shared invoice and signs in through a link to what looks like the company’s cloud email portal. The page is a reverse proxy phishing kit that forwards everything to the real service. The accountant completes the push-based MFA prompt, and the proxy captures the authenticated session cookie issued afterward.

Within ten minutes the attacker imports the cookie into a browser in another country and creates a mailbox rule that hides replies from the finance department. The security team detects an impossible-travel alert, revokes all of the user’s sessions, resets credentials and removes the rule. The firm then moves to phishing-resistant FIDO2 security keys and shortens session lifetimes for administrative roles, which closes this particular route.