The Amnesia Problem
Remember how we said HTTP is "stateless"? Let me paint you a picture of what that actually means:
Imagine going to your favorite coffee shop. You walk in, order a latte, and the barista makes it perfectly. You sit down, enjoy it, and then go back for a refill. But when you reach the counter, the barista looks at you like you're a complete stranger.
"Hi, welcome! What can I get you?"
"Um, I was just here 30 seconds ago. I'd like a refill."
"I'm sorry, who are you? Have we met?"
That's HTTP. Every single request is completely independent. The server has no memory of you between requests. Without cookies and sessions, you'd have to log in again every time you clicked a link. Every. Single. Click.
Why Stateless?
What Are Cookies?
Cookies are small pieces of text data that websites store in your browser. When you make a request, your browser automatically sends the relevant cookies back to the server. It's like the server giving you a name tag, and you showing it every time you come back.
How Cookies Work
Cookie Flow
Setting a Cookie (Server Side)
Sending Cookies (Browser Side)
Cookie Attributes: The Devil's in the Details
Cookies aren't just name=value pairs. They come with attributes that control their behavior. Understanding these is crucial for security.
Let's Break Down Each Attribute
Domain
Which domains can receive this cookie. If set to .example.com, all subdomains (api.example.com, blog.example.com) also get it.
⚠️ Security risk: Subdomain takeover can steal cookies for the whole domain!
Path
Which paths can access the cookie. Path=/admin means only requests to /admin/* will include this cookie.
Commonly set to "/" to include cookie on all paths.
Expires / Max-Age
When the cookie dies. No expiration = "session cookie" (deleted when browser closes).Max-Age=3600 = cookie lives for 1 hour.
Persistent cookies survive browser restarts.
🔒 Secure
Cookie only sent over HTTPS. Without this, attackers on the same network can intercept your session!
✓ ALWAYS use for session cookies
🔒 HttpOnly
JavaScript cannot access this cookie. Prevents XSS attacks from stealing your session via document.cookie.
✓ ALWAYS use for session cookies
🔒 SameSite
Controls when cookies are sent with cross-site requests. Prevents CSRF attacks!
Strict- Never sent cross-site (most secure)Lax- Sent on top-level navigation (default in modern browsers)None- Always sent (requires Secure flag)
A session cookie without HttpOnly can be stolen via XSS.
A session cookie without Secure can be intercepted on public WiFi.
A session cookie without SameSite can be abused for CSRF.
Sessions: The Server's Memory
So we've established that cookies let the browser remember things. But storing sensitive data IN the cookie itself is dangerous - users can read and modify cookies!
Enter sessions. Instead of storing "user=admin, role=superuser" directly in the cookie, the server:
- Creates a random, meaningless session ID (like "x7k2m9n3p5")
- Stores your actual data on the server, linked to that ID
- Gives you just the ID in a cookie
❌ Bad: Data in Cookie
User can change "role=superuser"!
✓ Good: Session ID Only
Server looks up "x7k2m9n3p5q8r1" to find user data
Server-Side Session Storage
Attacking Sessions: The Hacker's Perspective
As a security professional (or aspiring one), you need to understand how attackers target sessions. If you can steal or forge a session, you become that user.
1. Session Hijacking
Steal the session cookie = become the user. Methods include:
- XSS (Cross-Site Scripting): Inject JavaScript to steal document.cookie
- Network sniffing: Intercept cookies over HTTP (not HTTPS)
- Physical access: Copy cookies from browser storage
- Malware: Keyloggers or cookie stealers
2. Session Fixation
Instead of stealing a session, the attacker gives you THEIR session ID. When you log in, you're actually logging into their session.
Session Fixation Attack
3. Brute-Forcing Session IDs
If session IDs are predictable or short, attackers can guess them.
JWTs: Cookies' Stateless Cousin
JSON Web Tokens (JWTs) are an alternative to server-side sessions. Instead of storing session data on the server, ALL the data is in the token itself.
JWT Structure
A JWT has three parts, separated by dots:
JWT vs Server Sessions
| Aspect | Server Sessions | JWTs |
|---|---|---|
| Storage | Server stores data | Client stores data |
| Revocation | Easy - delete session | Hard - can't invalidate until expiry |
| Scalability | Needs shared storage | Stateless, scales easily |
| Size | Small session ID | Large (all data in token) |
JWT Attack Techniques
Viewing and Modifying Cookies
As a security tester, you'll constantly need to view and modify cookies. Here's how:
Browser DevTools
Chrome DevTools Cookie Inspection
JavaScript (If HttpOnly is NOT set)
cURL
Burp Suite
Burp automatically tracks cookies and lets you modify them in the Repeater. It also has a dedicated "Options > Sessions" tab for complex session handling.
Test Your Knowledge
Hands-On Challenge
Interactive Practice
Key Takeaways
- HTTP is stateless - cookies provide memory between requests
- Session IDs in cookies point to server-side data (more secure than storing data in cookies)
- ALWAYS use HttpOnly to prevent XSS cookie theft
- ALWAYS use Secure to prevent interception over HTTP
- Use SameSite=Strict or Lax to prevent CSRF attacks
- Regenerate session IDs after login to prevent session fixation
- Session IDs must be long, random, and unpredictable
- JWTs are stateless alternatives but have their own security considerations