Session Hijacking - How Attackers Steal Logins
About 2 min read
Session hijacking is an attack in which an attacker steals a user's session ID and impersonates that user to access a service. A session ID is an identifier issued by the server after login, and if it is stolen, the service can be used in an authenticated state without even knowing the password. In the 2021 edition of the OWASP Top 10, improper session management is included in category A07 (Identification and Authentication Failures).
Not Carrying an Identifier Across the Moment Privileges Change
In many implementations an identifier is assigned to a visitor before any login takes place, so that display settings or partially entered input can be kept across requests. If the same value continues to be used after authentication, only the state that the value points to changes; the value itself does not. Anyone who managed to learn that value beforehand therefore holds, the moment the legitimate user finishes logging in, a value that now refers to a state carrying the privileges of that user. Whether such a value can be learned in advance is not a question of attacker skill but of whether the application still allows a value supplied from outside to be adopted as the session. This is a different problem from replacing the identifier at fixed intervals. Rotating on a timer shortens how long any single value stays exposed, but it does nothing about a value being carried across the point at which privileges change. What acts on that point is issuing a new value immediately after any operation that changes privileges, and invalidating the old one at the same time. To check it, compare the value before and after authentication. It is the kind of omission that ordinary functional testing does not surface, because login succeeds either way. One oversight is issuing the new value while leaving the old one valid, so that two identifiers point at the same state and the switch loses its effect.
The Session Theft Flow
Attack Techniques
These include stealing the session ID from cookies using XSS (cross-site scripting), intercepting network communication to steal the session ID, and session fixation attacks that force the use of a session ID prepared in advance.
Concrete Damage Scenarios
Logging out does not necessarily make you safe. In reality, a logout process that does not invalidate the session on the server side may leave a stolen session ID still valid. For example, if you log in to a site that does not use HTTPS over public Wi-Fi and your session ID is intercepted through a man-in-the-middle attack, the attacker can use that session to act in a logged-in state. Likewise, if malware extracts cookies stored in the browser, deleting the cookie on the device alone does not invalidate the copied session ID.
Countermeasures
The basic countermeasures are using HTTPS, setting the Secure and HttpOnly attributes on cookies, and regenerating the session ID immediately after any operation that changes privileges, such as login. On services designed to require re-authentication before important operations, for example through two-factor authentication, those operations still require additional authentication even if a session is hijacked.
Was this article helpful?
Related Terms
- Session Tokens - Temporary Authentication CredentialsA session token is a temporary credential issued after login to identify the user. Managed as cookies or JWTs, a stolen token can lead to session hijacking attacks.
- CSRF - Cross-Site Request Forgery ExplainedCSRF tricks your browser into sending unauthorized requests while you are logged in. Learn how anti-CSRF tokens and SameSite cookies prevent this attack.
- What Is Credential Stuffing?Credential stuffing automates stolen username-password pairs against multiple sites. Discover why reusing passwords puts every account at risk.
- Identity Providers - Centralized Authentication ServicesAn identity provider (IdP) centrally manages user credentials and issues assertions via SAML or OIDC. Okta, Microsoft Entra ID (formerly Azure AD), and Google Workspace are examples in enterprise environments.