Cyber Services / All lessons / CEH — Ethical Hacking / CEH Module 11 Guide Session Hijacking

CEH Module 11 Guide Session Hijacking

Module 71 of 130 · 5 min read · Free to read

MODULES ↓ Command blocks have a COPY button
CEH Module 11 – In-Depth Mastery with Labs, Threat Modeling & Defense

Go beyond theory: Understand, simulate, detect, and defend against session hijacking in enterprise environments.

Legal & Ethical Warning

Session hijacking is a criminal offense without explicit written authorization. These techniques must only be used:

  • On systems you own or have written permission to test (with signed scope)
  • In isolated lab environments (DVWA, OWASP Juice Shop, Hack The Box)
  • During authorized penetration tests under a formal engagement letter
  • For academic research with IRB approval and ethical oversight

Violating computer misuse laws (e.g., CFAA, GDPR, UAE Cybercrime Law) can result in imprisonment, fines, and career termination.

I. Deep Dive: Session Management Architecture

Why Sessions Exist: Solving HTTP's Stateless Problem

HTTP was designed as a stateless protocol—each request is independent. But modern web apps require continuity (e.g., staying logged in while browsing). Session management bridges this gap by creating a "virtual identity" for users across requests.

How It Works Step-by-Step:

  1. User submits credentials via login form (POST /login)
  2. Server validates credentials against database
  3. If valid, server generates a cryptographically secure session ID (e.g., 128-bit random string)
  4. Server stores session data (user ID, permissions, IP, timestamp) in secure storage (Redis, DB)
  5. Server sends Set-Cookie: session_id=abc123...; HttpOnly; Secure; SameSite=Strict
  6. Browser stores cookie and sends it with every subsequent request to the domain
  7. Server validates session ID on each request and retrieves user context
Pro Insight: Session vs Token

Session-based: Stateful (server stores data), shorter-lived, easier to revoke.
Token-based (JWT): Stateless (client stores data), longer-lived, harder to revoke but scales better.

II. Advanced Session Hijacking Techniques

Attack Vector Deep Dive

A. Passive Attacks: Stealing Sessions in Transit

Network Sniffing: On unencrypted HTTP or compromised Wi-Fi, attackers capture raw traffic to extract session cookies.

Activity 1: Simulate Sniffing in Kali

Objective: Capture HTTP traffic and extract session cookies using Wireshark.

  1. Start DVWA on your lab VM
  2. Log in via HTTP (not HTTPS)
  3. In Kali, run: sudo wireshark
  4. Filter with: http.cookie
  5. Identify the PHPSESSID value in captured packets

Reflection: Why is this attack ineffective against HTTPS? What if HSTS is missing?

B. Active Attacks: Forcing or Injecting Session Theft

XSS-Based Theft: Malicious scripts steal cookies if HttpOnly is missing.

Activity 2: XSS Session Theft Lab

Steps:

  1. Use DVWA's "Stored XSS" module
  2. Inject:
  3. Start Python server: python3 -m http.server 8000
  4. Visit page as victim—observe cookie in server logs

Defense Test: Enable HttpOnly—does the attack still work?

III. Kali Linux Practical Labs

Lab 1: Full Session Hijack with ZAP & Burp

Objective: Intercept, steal, and replay a session cookie

  1. Configure ZAP: Set proxy to 127.0.0.1:8080
  2. Intercept Login: Break on request, observe credentials in POST body
  3. Capture Session: Forward request, break on response, note Set-Cookie
  4. Hijack Session: In new browser, use Cookie Editor extension to set stolen cookie
  5. Verify Access: Navigate to /dashboard—should be logged in as victim
Reflection Question

How would you detect this attack in a real enterprise environment? What logs would show anomalies?

Lab 2: MITM with BetterCap (Advanced)

Automated Session Capture on Local Network

# Target: 192.168.1.105 (victim)
sudo bettercap -iface eth0 -eval "
set arp.spoof.targets 192.168.1.105;
arp.spoof on;
set http.proxy.sslstrip true;
http.proxy on;
net.sniff on;
"

Monitor output: BetterCap logs all HTTP requests, including cookies. Look for Cookie: headers in real-time.

Pro Tip

Combine with dns.spoof to redirect victims to phishing pages that harvest sessions.

IV. Real-World Attack Scenarios

Case Study: OAuth Token Hijacking in SaaS Platforms

In 2022, attackers exploited an XSS flaw in a subdomain of a major SaaS provider to steal OAuth tokens. These tokens were then used to access email, files, and HR systems across the enterprise.

Attack Chain:

  1. XSS in support.victim.com (low-severity subdomain)
  2. Script exfiltrates access_token from localStorage
  3. Token replayed to api.victim.com with Authorization: Bearer
  4. Full account compromise across integrated apps (Slack, GSuite, Salesforce)
Defense Lessons
  • Enforce strict CSP headers
  • Use short-lived tokens with refresh rotation
  • Bind tokens to client fingerprints (IP, User-Agent)
  • Monitor for anomalous token usage (geo, device changes)

V. Enterprise Defense-in-Depth

Layered Security Controls

1. Secure Session Configuration (Non-Negotiable)

  • Secure flag: Cookies only over HTTPS
  • HttpOnly: Block JavaScript access
  • SameSite=Strict: Prevent CSRF and some XSS exfiltration
  • Short timeouts: 15-30 min idle, 8 hours absolute
  • Regenerate ID on login/privilege change

2. Advanced Protections

  • IP/User-Agent Binding: Invalidate session if client context changes
  • Token Binding: Cryptographically bind session to TLS channel
  • Behavioral Analytics: Detect impossible travel, rapid privilege escalation
Activity 3: Secure Your Flask App

Modify the following insecure session code to include all critical defenses:

# INSECURE CODE
response.set_cookie('session', session_id)

Your Task: Add Secure, HttpOnly, SameSite, and timeout.

VI. Detection, Forensics & Threat Hunting

Detecting Session Hijacking

Key Indicators of Compromise (IoCs):

  • Same session ID used from multiple geolocations in short time
  • Session active after user logout (if server-side invalidation works)
  • Cookie sent over HTTP when HSTS is enforced
  • Abnormal user-agent strings for a session (e.g., Chrome → Python-requests)

SIEM Query Example (Splunk):

index=web_logs 
| stats dc(src_ip) as ip_count, dc(user_agent) as ua_count by session_id 
| where ip_count > 1 OR ua_count > 1 
| table session_id, ip_count, ua_count

VII. Interactive Knowledge Checks

1. Which cookie attribute prevents JavaScript access to session cookies?
2. What is the primary defense against network sniffing of session cookies?
Modules (130) ← CEH — Ethical Hacking