← Glossary

What Is Session Hijacking?

Session hijacking is the takeover of a user's existing authenticated session by obtaining or controlling its identifier, so that an attacker is treated as that user without knowing their password. Defences centre on protecting the token, renewing it at login and letting the server revoke it.

Updated:

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.

ScenarioWhen the attacker actsWhat the attacker needsPrimary defence
Token theftAfter loginA copy of the live identifierHttpOnly, Secure, no identifiers in URLs, short lifetimes
Session fixationBefore loginAbility to plant a known identifierRegenerate the identifier at authentication
Token replayAny time while validA captured token still acceptedExpiry, 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.

Frequently asked questions

Is session hijacking the same as password theft?

They compromise different credentials. A password may start a new session, subject to MFA and other checks; a stolen bearer token may reuse an existing session without another login. Changing the password is therefore not a substitute for revoking active sessions when compromise is suspected.

Does logout invalidate every token?

Only if the application implements it that way. OWASP recommends that logout invalidate the session on the server and revoke all identifiers belonging to the same logical session. If logout merely deletes the browser cookie, a copied token can remain valid until it expires or is revoked separately.

How does session fixation differ?

Fixation happens before login. The attacker plants a known identifier on the victim's browser, the victim authenticates with it, and the attacker then uses the elevated session. Theft happens after login by copying a live identifier. Regenerating the identifier at authentication defeats fixation but not theft.

Does HTTPS prevent session hijacking?

It closes one path. Encryption with the Secure cookie attribute stops interception on the network, but tokens can still be exposed through injected scripts when HttpOnly is missing, through malware with access to the cookie store, or through identifiers placed in URLs. Transport security is necessary, not sufficient.

Why should session identifiers be rotated?

Replacing an identifier at authentication closes the classic fixation path, and later rotation can reduce exposure from old copies. The replacement must invalidate the old value after any necessary transition period. Merely issuing another token while leaving the earlier one fully valid does not provide that benefit.