Skip to content
XSS Write-up Beginner

DOM XSS Exploitation via postMessage in an iframe

The target application listens for postMessage events and writes the received data directly into an element's innerHTML without checking the sender's origin or sanitising the content. An attacker could send a crafted message containing HTML with an event handler to execute arbitrary JavaScript in the victim's browser.

medium.com

Published on medium.com

This article explores a specific case of DOM-based Cross-Site Scripting (DOM XSS) that occurs through the misuse of the window.postMessage() API, commonly referred to as web messages.

Bash Overflow, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The application did not validate the origin of incoming postMessage events and passed the message data directly into innerHTML without sanitisation.

Impact

An attacker could perform actions on behalf of authenticated users, such as stealing session cookies or tokens, or redirect users to malicious sites.

What the attacker needed

Victim visits attacker-controlled page; target page loaded in iframe

How it was found

  1. Open the lab and monitor requests with Burp Suite Proxy History to find the JavaScript that listens for message events and sets innerHTML of the element with ID ads to e.data.
  2. In the browser developer console, simulate a message to test whether an alert() can be triggered, confirming code execution.
  3. Craft a payload on the provided exploit server.
  4. Click Store to save the exploit, then View exploit to verify it works.
  5. Adjust the payload to an iframe that, on load, posts a message containing an image with an onerror handler calling print() to the target site.
  6. Trigger the payload; when print() executes, the lab is solved.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

A web page can receive messages from other windows, like a chat window. This site took whatever text arrived in such a message and inserted it directly into the page as HTML, without checking who sent it or cleaning it. That let an attacker send a message containing a small image tag that runs code when the image fails to load, so the attacker's JavaScript ran on the site.

What is XSS?

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.