Day 6 of 2160 minShell WalkerHands-on lab

Day 6: XSS in Context: Attributes, the DOM and Where AI Helps

Taught by Mr Elite Updated

By the end of today you can

  • Name the injection context (HTML body, attribute, or DOM) from a response or script
  • Break out of a quoted attribute without angle brackets
  • Find a DOM XSS source and sink by reading client-side JavaScript
  • Ask an assistant to explain code rather than guess payloads, then prove the result yourself

Warm-up

Answer from memory first, then tap to check. Recalling yesterday is what makes it stick.

On Day 5, what counts as evidence of XSS: reflection or script execution?

Script execution. Reflection is only a lead.

Where in Burp do you find the request, response and your reflected input?

HTTP history in the Proxy tab.

Last time you found a reflection and made a script run. That was the easy case, where your input landed in plain HTML and angle brackets worked. Real applications are fussier. The same input might land inside a quoted attribute, inside a JavaScript string, or never touch the server at all.

Today I will teach you to ask one question before you type any payload: where did my input land? Answer that and the payload nearly chooses itself. We will use that on two labs, and I will show you how to use an assistant to explain code, which is a far better job for it than guessing payloads.

Context decides the payload

Context means the spot in the page where your input ends up. The browser parses each spot by different rules, so a payload that works in one place is dead text in another. Your job is to read the surrounding characters, then escape whatever you are inside.

Injection context
The part of the page your input lands in, such as HTML body, attribute value or JavaScript string.
Breaking out
Closing the quote or tag you are inside so your own code is read as code.
Event handler
An attribute like onfocus or onmouseover that runs JavaScript when something happens to the element.

Here are three contexts and what each needs. Compare the surrounding characters, not the payload.

html
<p>Hello INPUT</p>
<input value="INPUT">
<script>var name = 'INPUT';</script>
Three contexts. Body needs a new tag, the attribute needs a closing double quote, the script needs a closing single quote.

Quick check

Your input appears as and the server escapes < and >. What do you need to escape first?

Attribute context: breaking out of quotes

When angle brackets are escaped you cannot open a new tag. You do not need to. If you close the quote, you are back inside the existing tag and can add an attribute of your own. An input element with an event handler is enough.

html
<input value="" autofocus onfocus="alert(1)" x="">
What the page looks like after injecting: " autofocus onfocus="alert(1)" x=" . The final x=" soaks up the original closing quote.

autofocus makes the browser focus the field on load, which fires onfocus with no click. The trailing x=" is tidy-up so the leftover quote does not break the tag. If the page uses single quotes, swap your quote to match. Always match the quote you see in the source.

DOM XSS: reading the client-side code

DOM XSS happens entirely in the browser. JavaScript takes data from a source, such as the URL fragment (the part after #), and writes it to a sink that treats it as HTML. The server never sees a fragment, so Burp history will show nothing odd. You have to read the page's JavaScript.

Source
Where attacker-controlled data enters the script, for example location.hash or location.search.
Sink
A function or property that turns data into live content, for example innerHTML, document.write or eval.
javascript
var term = decodeURIComponent(location.hash.slice(1));
document.getElementById('out').innerHTML = term;
A textbook vulnerable pattern. Source: location.hash. Sink: innerHTML. Nothing sits between them.

Note that innerHTML will not run a plain script tag inserted this way, so event-handler payloads such as an image with a bad source and an onerror handler are the usual choice. Read what the code actually does, then pick.

Quick check

Why will Burp's HTTP history not show your DOM XSS payload being sent?

Watch me do it: find the context with an assistant's help

I will walk through the method on a practice page of your own, so you do not touch anything real. This builds the habit you will use on both labs.

html
<html><body>
<div id="out"></div>
<script>
var t = decodeURIComponent(location.hash.slice(1));
document.getElementById('out').innerHTML = 'You searched: ' + t;
</script></body></html>
ctx.html, a deliberately vulnerable page that exists only on your machine.
text
Explain what this JavaScript does line by line. Name the data source, the sink, and whether any sanitising happens between them. Do not suggest exploits yet.
The prompt. Asking for a line-by-line explanation gives you something you can verify against the code.

Lab practice: two contexts, two labs

Now you apply it. Both labs are SecurityElites practice targets, so testing them is allowed. Do not use these techniques on anything else without written permission. For each lab write down the context before you try any payload.

Your turn

Reflected XSS

We start with the ProfileMe lab because it forces you to stop relying on angle brackets. It is the attribute context from section two, in a real page. Then move to xss-dom-1 (QuickDocs) for the DOM case, which uses the source and sink reading from section three.

Open the lab in a new tab

What to try

What to notice

  • Which characters the server escapes and which it leaves alone
  • Whether the payload ran with or without user interaction
  • That the DOM lab never shows your payload in Burp's request line
  • How reading the code told you the payload shape before you tried it

If it did not work

Pasting a payload before checking the context

View the source first and look at the characters either side of your marker. Choose the escape that matches.

Mismatching quote types

If the attribute uses single quotes, close with a single quote. Copy what the source shows.

Expecting a script tag to run through innerHTML

Use an element with an event handler, such as an image with onerror.

Trusting the assistant's payload without reading the code

Use the assistant to explain the code, then confirm with your own test. Only execution counts.

Recap

  • Find the injection context before choosing any payload
  • Escaped angle brackets do not stop attribute breakouts
  • Close the quote, then add an event handler such as onfocus with autofocus
  • DOM XSS runs from a source to a sink in browser code, and the server never sees the fragment
  • Ask an assistant to explain code, then verify with your own test
  • Execution is the evidence, as on Day 5

Cheat sheet

View source (Ctrl+U)
Shows the raw HTML the server sent, so you can read your context
" autofocus onfocus="alert(1)" x="
Attribute breakout pattern for a double-quoted value
location.hash
Common DOM source: the URL fragment
innerHTML
Common DOM sink that parses strings as HTML
<img src=x onerror=alert(1)>
Event-handler payload that works through innerHTML
python3 -m http.server 8000
Serves the current folder locally for safe testing

Before the next day

Mission: Two labs, two notes

Solve both labs and write a short note for each. Naming the context in writing forces you to understand it, and these notes become raw material for your reports on Day 19.

About 40 min

You are done when: A folder with two screenshots and a notes.txt naming context, payload and reason for each lab.

Want more? Fix your local ctx.html by replacing innerHTML with textContent, reload with your payload, and write one sentence on why it is now safe.

Quiz

4 questions. Your score is saved with your progress.

1. A page escapes < and > but your input lands inside value="...". What is the best next move?

2. In DOM XSS, what is a sink?

3. What is the best way to use an assistant on today's labs?

4. Your DOM payload shows no alert after you edit only the fragment in the address bar. What should you try first?

Finish the day

Mark the day complete to save your progress.

Go to Day 7

Next: Day 7 moves from the browser to the server's decisions: broken access control, where you change identifiers and request fields to reach data that is not yours. The habit of reading what the app really does carries straight over.

Tell someone you are doing this:WhatsAppXLinkedIn

Your notes

Private to this device. Write what you want to remember from today.