Expert · updated

DOM-based vulnerabilities

PortSwigger Web Security Academy · Expert

  • #DOM
  • #XSS

Exploiting DOM Clobbering to Enable XSS

The vulnerability

The script uses a global variable called defaultAvatar. If you put elements with that id into a comment, the browser creates the variable for you, with your content in it.

Fix: Don’t use the window.x || {} pattern for global variables, and check that an object is what you expect.

The lab

Open the lab on PortSwigger

This lab contains a DOM-clobbering vulnerability. The comment functionality allows “safe” HTML. To solve this lab, construct an HTML injection that clobbers a variable and uses XSS to call the alert()function. Note

Please note that the intended solution to this lab will only work in Chrome.

Solution

1. Add the First Comment: Go to a blog post and add the following HTML payload as a comment. This payload is designed to clobber the defaultAvatar variable.

<a id=defaultAvatar><a id=defaultAvatar name=avatar href="cid:&quot;onerror=alert(1)//">

2. Add the Second Comment to Trigger the XSS: After posting the first comment, add a second, harmless comment. This action triggers the vulnerable JavaScript code, which uses the clobbered defaultAvatar variable. An alert window will be displayed, and the lab is solved.

Tip

Complete the XSS labs in Google Chrome, as Firefox blocks the alert() function in this context.

Clobbering DOM Attributes to Bypass HTML Filters

The vulnerability

HTMLJanitor goes through the attributes of every element and removes the dangerous ones. If a form contains an input with the id attributes, it looks at the input instead and your event handler stays.

Fix: Use a sanitizer that is protected against DOM clobbering, and keep it up to date.

The lab

Open the lab on PortSwigger

This lab uses the HTMLJanitor library, which is vulnerable to DOM clobbering. To solve this lab, construct a vector that bypasses the filter and uses DOM clobbering to inject a vector that calls the print() function. You may need to use the exploit server in order to make your vector auto-execute in the victim's browser.Note

The intended solution to this lab will not work in Firefox. We recommend using Chrome to complete this lab.

Solution

1. Add the Malicious Comment: Go to a blog post and add the following HTML as a comment. This payload creates a form with an id=x and an onfocus event handler that calls print(). It also includes a hidden input with id=attributes to clobber a variable used by the sanitizer. Post the comment.

<form id=x tabindex=0 onfocus=print()><input id=attributes>

2. Deliver the Exploit: Go to the Exploit Server and add the following HTML to the body. This code creates an iframe that loads the blog post with your malicious comment. The JavaScript code then uses a URL fragment (#x) to automatically focus the form element, triggering the onfocus event.

<iframe src=https://<LAB-ID>.web-security-academy.net/post?postId=3 onload="setTimeout(()=>this.src=this.src+'#x',500)">

Replace <LAB-ID> and postId with the correct values. Click "Store" and "Deliver exploit to victim". The victim's browser will load the page, the onfocus event will be triggered, and a print window will pop up, and the lab is solved.

Tip

Complete the XSS labs in Google Chrome, as the intended solution for this lab will not work in Firefox.