Session hijacking and cookie theft: takeover with no password

Advanced5 min readUpdated

GET /learn/account-attacks/session-hijacking-and-cookie-theft/ Accept text/html text/markdown

Short answer

Session hijacking is when an attacker takes a valid session token, usually the session cookie, and uses it to act as the logged-in user. Infostealer malware copies cookies from the browser profile and the attacker replays them from another device, so the site never asks for the password or MFA again. Short sessions, re-authentication and device binding limit the damage.

After login, most sites recognise the user by a session cookie and nothing else. Whoever presents the cookie is treated as the user until it expires. That makes the cookie a bearer credential: a long-lived "remember me" cookie is worth as much to an attacker as the password and the second factor together, and it never triggers a login check.

How do attackers steal session cookies?

Infostealer malware is the most direct route. It runs on the victim's computer, often installed from a pirated program or a fake update, reads the browser profile, and copies cookies, saved passwords and autofill data in one pass. The bundle, called a log, is uploaded to the attacker and often resold. MITRE ATT&CK describes this as Steal Web Session Cookie (T1539).

The other routes:

  • Phishing relays that pass the login through to the real site and capture the session cookie it sets, including after MFA.
  • Cross-site scripting, where injected JavaScript reads document.cookie and sends it away. This only works on cookies without the HttpOnly attribute.
  • Interception on connections without HTTPS, or of cookies set without the Secure attribute.
  • Session fixation, where the attacker plants a session ID before login and the site keeps the same ID after the user authenticates.

Why the site does not ask for MFA again

MFA runs at login. The session cookie is the site's record that login, including MFA, already succeeded. When the attacker loads the stolen cookie into their own browser and replays it, every request looks like the continuation of a valid session, so there is nothing to challenge. MITRE notes that stolen session cookies can bypass some MFA protocols, and using them is listed separately as Web Session Cookie (T1550.004). This is the gap that MFA cannot close on its own.

Careful attackers try to look like the victim's device as well, using an antidetect browser that copies the user agent, screen size and time zone from the stolen log. That makes simple header checks less reliable and is why the binding below uses several signals.

What a hijacked session looks like in logs

Compare each request with the device and network the session was issued to:

  • The same session ID with a different device fingerprint, browser family or TLS fingerprint from the one at login.
  • A jump to a different ASN or country mid-session, especially to a hosting provider or a known proxy network.
  • Requests from two devices interleaved on one session.
  • A session still in use after the user logged out on their own device.
  • Straight after the switch, sensitive actions such as an email change, a new payout method or a data export. Signs of account takeover lists the rest.

This PostgreSQL query finds sessions seen with more than one device fingerprint or user agent in the last day, assuming a request_events table with session_id, user_id, event_time, device_fingerprint, user_agent and asn:

sql
SELECT session_id, user_id,
       count(DISTINCT device_fingerprint) AS fingerprints,
       count(DISTINCT user_agent) AS user_agents,
       count(DISTINCT asn) AS networks,
       min(event_time) AS first_seen,
       max(event_time) AS last_seen
FROM request_events
WHERE event_time >= now() - interval '24 hours'
  AND session_id IS NOT NULL
GROUP BY session_id, user_id
HAVING count(DISTINCT device_fingerprint) > 1
    OR count(DISTINCT user_agent) > 1
ORDER BY fingerprints DESC, networks DESC
LIMIT 100;

A browser update during a long session changes the user agent version, so compare browser family and major version, or expect some noise.

How do you defend against session hijacking?

Start with the cookie itself. The OWASP Session Management Cheat Sheet covers the baseline: a random ID, a new ID issued at login to block fixation, server-side expiry and these attributes:

http
Set-Cookie: __Host-sid=Hk3vQ9pZr2LmXw7TnB4s; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=28800

The __Host- prefix makes the browser reject the cookie unless it is Secure, has Path=/ and has no Domain attribute, so a subdomain cannot overwrite it. HttpOnly blocks theft through cross-site scripting. None of these attributes stop malware that reads the cookie store on the device, so the next layers matter more.

Keep sensitive sessions short

Use an idle timeout and an absolute lifetime, and make them shorter for banking, admin and account settings areas. Treat a long-lived "remember me" cookie as good for browsing only. For changes to the email address, phone number, password, MFA settings and payout details, require a recent authentication: as a starting point, ask for the password or a passkey again if the last one was more than 15 minutes ago. A stolen cookie then lets the attacker look around but not lock the owner out.

Bind the session to the device and network

When you issue a session, store what it was issued to: a device fingerprint, the browser family, the TLS fingerprint and the ASN. Check them on every sensitive request, and on a sample of the others. A workable starting rule:

  • Device fingerprint or browser family changes: end the session and ask the user to log in again.
  • ASN changes alone, such as Wi-Fi to mobile data: allow it, but treat the next sensitive action as higher risk.
  • ASN changes to a hosting or proxy network together with any fingerprint change: end the session and notify the user.

Ending a session costs a real user one extra login, so this rule can afford to be strict.

Make revocation work

Keep sessions in a server-side store so you can end them. End every session when the password or MFA settings change, give users a list of active sessions with a way to end each one, and make logout delete the session on the server. For API tokens, the same idea exists in OAuth as sender-constrained tokens, for example DPoP (RFC 9449), which ties a token to a key held by the client.

Device Bound Session Credentials in Chrome

Device Bound Session Credentials (DBSC) move this binding into the browser. At login, the browser creates a key pair for the session and keeps the private key in secure hardware such as a TPM where the device has one. The site issues only short-lived cookies. When one expires, the browser proves it still holds the private key and receives a fresh cookie. A cookie copied to another machine stops working once it expires, because that machine cannot produce the proof. The site has to add endpoints for registration and refresh, and the browser falls back to normal cookies on devices without secure key storage. Support depends on the browser and platform, so check the Chrome documentation for where it works and keep the server-side checks above in place for every other browser.

Tools that help

StopBots is published by Kairal, which is one of the tools listed. The list is not a ranking.

  • MaxMind offers free GeoLite databases for country and ASN lookups, enough to record the network each session was issued on and spot jumps.
  • Spur classifies IP addresses as VPN, residential proxy or hosting, which helps decide whether a session's new network is suspicious.
  • Fingerprint gives each browser a visitor identifier that you can store at login and compare on later requests to the same session.
  • Kairal connects device, network and behavioural signals across the customer journey to detect account takeover, and its console shows the evidence behind each decision with related events, devices and IP addresses linked.

Common questions

Does a VPN protect against session hijacking?

Only against interception on the network path, such as an open Wi-Fi network, and HTTPS already covers most of that. It does nothing against infostealer malware or phishing relays.

Does logging out protect a stolen session?

Only if the site ends the session on the server. If logout just deletes the cookie in the user's browser, a copy held by an attacker keeps working until it expires.

What is the difference between session hijacking and session fixation?

In hijacking, the attacker steals a session the user already has. In fixation, the attacker gets the user to log in with a session ID the attacker chose in advance, then uses that ID once it is authenticated.