Session hijacking: taking over a signed-in session

Session hijacking is taking over a user's signed-in session by getting hold of the token that identifies it. After sign-in, a site recognises the user by a session token, usually a cookie, on every request. Whoever presents that token is treated as the user: no password and no second factor are asked again. Stealing the token, or planting one before the user signs in, is enough.

Section Web attacks Updated Weaknesses CWE-384, CWE-613

Quick facts

Also called
Cookie hijacking, session theft, sidejacking when tokens are sniffed on a network
What is taken
The session token, usually a cookie, that stands in for the user after sign-in
Weaknesses
CWE-384 session fixation; CWE-613 insufficient session expiration
OWASP category
A07:2025 Authentication Failures (A07 page); see authentication failures
Does a WAF stop it?
Rarely. A request with a stolen token is identical to the owner's

Session handling advice follows the OWASP Session Management Cheat Sheet.

What a session token is

HTTP does not remember anyone between requests. After a successful sign-in, the site creates a session on the server and hands the browser a long random value that names it: the session token, or session ID. The browser sends it back with every request, almost always in a cookie, and the site looks it up to know who is asking.

The token is a bearer credential: holding it is the whole proof. That is why it has to be unpredictable, kept out of places others can read, and short-lived. Tokens in other forms, such as signed tokens sent in an Authorization header, carry the same risk once they are stolen.

How session hijacking works

  • Stealing the cookie with script. A cross-site scripting flaw lets attacker script read a cookie that is not marked HttpOnly and send it away.
  • Sniffing it on the network. On a connection without TLS, anyone on the same network can read the cookie in transit; this is the old sidejacking attack on public Wi-Fi.
  • Taking it from where it leaked. Tokens in URLs end up in logs, browser history and Referer headers sent to other sites.
  • Stealing it from the device. Malware that copies browser cookies is now a common way in, and it bypasses multi-factor authentication because the session already passed it.
  • Guessing or predicting it. Rare today, but possible when a token is short or built from predictable values.

In every case the attacker then sends requests with the token, and the site serves them as if the user had.

Session fixation attacks

Session fixation works the other way round. Instead of stealing a token after sign-in, the attacker sets a token before it: by sending the victim a link that carries a session ID, or by planting a cookie through a related subdomain. The victim signs in, the site attaches the signed-in session to the token the attacker already knows, and the attacker uses it.

The fix is simple and decisive: issue a new session token at sign-in, and at every change of privilege, and never accept a session ID from a URL. OWASP lists reusing the same identifier after sign-in as an A07 failure.

WHICH DEFENCE STOPS WHICH THEFT HTTPONLY SECURE + TLS NEW TOKEN AT SIGN-IN SHORT LIFETIME XSS COOKIE THEFT NETWORK SNIFFING TOKEN IN URL SESSION FIXATION MALWARE ON DEVICE // a short lifetime is the only control that limits every row
// five ways to take a session, and what limits each

Protecting session cookies

Cookie attributes, defined in RFC 6265 and its successors, do most of the work:

  • HttpOnly keeps the cookie away from page script, which blunts cookie theft through XSS.
  • Secure sends it only over HTTPS, so it never crosses the network in clear text.
  • SameSite=Lax or Strict limits when the browser attaches it to cross-site requests, which also helps against CSRF.
  • A narrow Domain and Path, so sibling subdomains cannot read or set it.

Session hijacking prevention

  1. Generate tokens with a proven session manager. Long, random, created on the server; never build your own.
  2. Rotate the token at sign-in and privilege changes. This defeats fixation outright.
  3. Set idle and absolute timeouts, and end sessions at logout. On the server, not only by deleting the cookie; this is CWE-613.
  4. Keep tokens out of URLs. Cookies or headers only.
  5. Re-check identity for sensitive actions. Asking for the password or a second factor before changing the email address or paying limits what a stolen session can do.
  6. Let users see and end their sessions. A list of active sessions turns an unnoticed takeover into a visible one.

Some sites also tie a session loosely to the client, for example by noticing a sudden change of country or device. Such checks catch some theft and annoy some travellers; they belong in the application, next to the session itself. When a hijack succeeds, the result is account takeover.

What a WAF can do about session hijacking

Little directly. A request carrying a stolen token is byte for byte what the owner would send, so there is nothing to match. A filter helps one step earlier, by refusing requests that carry the script injection used to steal cookies, and limits can slow attempts to guess tokens.

Everything that decides whether a stolen token still works, its lifetime, its rotation, its cookie attributes, sits in the application.

Questions

What is session hijacking?

Session hijacking is taking over a user's signed-in session by stealing or fixing the session token that identifies it, so the attacker is treated as the user without knowing the password.

What is the difference between session hijacking and session fixation?

In hijacking, the attacker steals a token after the user signs in. In fixation, the attacker sets a token before sign-in and waits for the user to sign in with it. Rotating the token at sign-in stops fixation.

What is a session token?

A long random value a site gives the browser after sign-in, usually in a cookie, and uses on every request to know who is asking. Whoever holds it is treated as the user.

How do you prevent session hijacking?

Use HttpOnly, Secure and SameSite cookies over HTTPS, issue a new token at sign-in, keep tokens out of URLs, set timeouts, end sessions at logout, and ask again before sensitive actions.

Does multi-factor authentication stop session hijacking?

No. It protects the sign-in, and a hijacked session was created after the sign-in passed. Short session lifetimes and re-checks for sensitive actions limit the damage.