Session hijacking is the use of someone else's valid, authenticated session by obtaining or controlling the identifier that represents it. After a user logs in, a web application does not re-check the password on every request. It issues a session token, usually stored in a cookie, and validates that token on later requests. For a bearer session token, possession can be enough to use the session, unless further checks or binding prevent it. An attacker who copies it, or who arranged in advance for the victim to use a token the attacker already knows, can act as the user until the session ends. The attacker need not know the password, which is why hijacking is distinct from credential theft and why defences focus on the token: how it is generated, where it is stored, how it travels, when it changes and how it can be killed.
Sessions, Tokens and Authentication State
HTTP is stateless, so applications create state themselves. On successful login the server records a session, typically a server-side entry keyed by a random identifier, and sends that identifier to the browser in a Set-Cookie header. On each later request the browser returns the cookie, the server looks up the entry and finds the user, permissions and any other data attached to it. Alternatively the token may be a signed structure that carries claims inside it, verified by signature rather than by lookup. In both designs the token is the authentication state: it is what distinguishes a logged-in request from an anonymous one.
Opaque identifiers need enough cryptographically generated randomness to make guessing impractical. Length alone is insufficient: a long timestamp followed by a predictable counter is still predictable. OWASP recommends at least 128 bits when generating a custom session identifier. Its lower minimum entropy discussion is not a reason to shorten established framework defaults. Opaque values should carry no personal information; signed claims are a separate design, and a readable signature-protected token is not automatically confidential. Strong generation reduces guessing risk without protecting copies exposed elsewhere.
Theft, Fixation and Replay Risks
A common hijacking path starts with theft of an already authenticated token. The token is exposed somewhere along its life: a script injected into the page reads it from document.cookie if the HttpOnly attribute is missing; a connection over plain HTTP lets anyone on the network path read it if the Secure attribute is missing; malware or a careless browser extension reads the cookie store directly; a URL carrying the identifier is written to a proxy log, a referrer header or a shared screenshot. The resulting copy may be replayed from another client if it remains valid and the application accepts it. Device binding or a fresh authentication requirement can interrupt that sequence.
Session fixation reverses the order. OWASP describes it as fixing a session identifier on the victim's browser before authentication, inducing the user to log in with that identifier, and then using the now-authenticated session. The identifier may arrive through a URL parameter, a hidden form field or a cookie the attacker was able to set. The attack works only if the application keeps the same identifier across the login boundary, so the defence is simple to state: issue a fresh identifier at authentication and at every privilege change. OWASP calls this regeneration mandatory.
Replay in this context means resubmitting a captured token or request. Where a token stays valid for a long time and nothing binds it to the client, a copy captured once is useful for as long as the session lives. Binding the session to additional properties, such as the client's address or user agent, raises the bar but is not reliable on its own, since the same guidance notes that attackers can share an address through NAT or a proxy and can copy a user agent string.
| Scenario | When the attacker acts | What the attacker needs | Primary defence |
|---|---|---|---|
| Token theft | After login | A copy of the live identifier | HttpOnly, Secure, no identifiers in URLs, short lifetimes |
| Session fixation | Before login | Ability to plant a known identifier | Regenerate the identifier at authentication |
| Token replay | Any time while valid | A captured token still accepted | Expiry, rotation, server-side revocation |
Hijacking vs Password Theft
A stolen password may allow new logins, although multifactor authentication and risk controls can still block them. A hijacked session gives access only to that one session, with its existing scope and remaining lifetime, and typically cannot pass a step that asks for the password again or a second factor. The two failures also leave different traces. Password misuse may produce new login events; token reuse may appear as changed activity within an existing session. Neither pattern proves the cause by itself. Mixing them up leads to the wrong remedy: a password reset does nothing about a session that remains valid unless the application also revokes sessions when the password changes.
Expiration, Rotation and Revocation
Limiting how long any token is useful is the most general defence. OWASP recommends an idle timeout that ends sessions after a period without activity and an absolute timeout that ends them regardless of activity, both enforced on the server rather than trusted to a cookie expiry that the client controls. Rotation shortens the window further: changing the identifier after login, after privilege changes and periodically during long sessions can make a captured value stale, provided older values are invalidated and any transition grace period is limited.
Session revocation is the server's ability to declare a token invalid before it would otherwise expire. For lookup-based sessions this is a matter of deleting or flagging the server-side record. A self-contained signed token needs a server-side revocation check for immediate invalidation. Short-lived access tokens with revocable refresh tokens bound the delay but do not instantly cancel an already issued access token. Logout must trigger server-side invalidation, and OWASP adds that all identifiers belonging to the same logical session should be revoked together. Users benefit from a view of active sessions with the ability to end them, and account recovery after suspected compromise should offer revocation of all sessions. An ordinary password-change flow should clearly explain which sessions it ends.
Detection and Practical Limits
Anomaly monitoring can help identify token reuse that ordinary validity checks miss. Applications can watch for repeated attempts with invalid or expired identifiers, which suggests guessing, and for anomalies such as a cookie that was modified, a session that appears from two distant networks within minutes, or a client that changes mid-session. Logging should record a salted hash of the identifier rather than the identifier itself, so the logs do not become a new source of theft.
No application-side control removes the risk entirely. Malware with sufficient browser or operating-system access may extract cookies despite page-level flags, and a user who pastes a URL containing a token into a public channel has disclosed it themselves. What the controls achieve is to make a token hard to obtain, short-lived once obtained and quick to kill once noticed. For users, the practical equivalents are logging out on shared devices, declining to reuse tokens or cookies across untrusted software, and treating any unexplained session activity as a reason to end all sessions and change the password.