What Is DOM-based XSS (Cross-Site Scripting) - Tutorial & Examples - Web Security Academy
What Is DOM-based XSS (Cross-Site Scripting) - Tutorial & Examples - Web Security Academy
DOM-based XSS
Track your progress
Learning materials: View al
In this section, we'll describe DOM-based cross-site scripting (DOM XSS), explain how to find DOM XSS
vulnerabilities, and talk about how to exploit DOM XSS with different sources and sinks. 0%
The most common source for DOM XSS is the URL, which is typically accessed with the window.location object.
An attacker can construct a link to send a victim to a vulnerable page with a payload in the query string and fragment
4 3 1
of 46 of 119 of 26
portions of the URL. In certain circumstances, such as when targeting a 404 page or a website running PHP, the
payload can also be placed in the path. Apprentice Practitioner Expert
For a detailed explanation of the taint flow between sources and sinks, please refer to the DOM-based vulnerabilities
page.
Your level:
https://portswigger.net/web-security/cross-site-scripting/dom-based 1/4
10/18/2020 What is DOM-based XSS (cross-site scripting)? Tutorial & Examples | Web Security Academy
In principle, a website is vulnerable to DOM-based cross-site scripting if there is an executable path via which data XXE
can propagate from source to sink. In practice, different sources and sinks have differing properties and behavior that SSRF
can affect exploitability, and determine what techniques are necessary. Additionally, the website's scripts might Request smuggling
perform validation or other processing of data that must be accommodated when attempting to exploit a vulnerability. Command injection
There are a variety of sinks that are relevant to DOM-based vulnerabilities. Please refer to the list below for details. Server-side template injection
The document.write sink works with script elements, so you can use a simple payload, such as the one below: Insecure deserialization
Directory traversal
document.write('... <script>alert(document.domain)</script> ...'); Access control
Authentication
Business logic vulnerabilities
LAB DOM XSS in document.write sink using source location.search Not solved Web cache poisoning
HTTP Host header attacks
WebSockets
Note, however, that in some situations the content that is written to document.write includes some surrounding Information disclosure
context that you need to take account of in your exploit. For example, you might need to close some existing elements
before using your JavaScript payload.
The innerHTML sink doesn't accept script elements on any modern browser, nor will svg onload events fire.
Find XSS
This means you will need to use alternative elements like img or iframe. Event handlers such as onload and vulnerabilities using
onerror can be used in conjunction with these elements. For example: Burp Suite
element.innerHTML='... <img src=1 onerror=alert(document.domain)> ...'
If a JavaScript library such as jQuery is being used, look out for sinks that can alter DOM elements on the page. For
instance, the attr() function in jQuery can change attributes on DOM elements. If data is read from a user-
controlled source like the URL and then passed to the attr() function, then it may be possible to manipulate the
value sent to cause XSS. For example, here we have some JavaScript that changes an anchor element's href
attribute using data from the URL:
$(function(){
$('#backLink').attr("href",(new
URLSearchParams(window.location.search)).get('returnUrl'));
});
You can exploit this by modifying the URL so that the location.search source contains a malicious JavaScript
URL. After the page's JavaScript applies this malicious URL to the back link's href, clicking on the back link will
execute it:
?returnUrl=javascript:alert(document.domain)
LAB DOM XSS in jQuery anchor href attribute sink using location.search source Not solved
If a framework like AngularJS is used, it may be possible to execute JavaScript without angle brackets or events.
When a site uses the ng-app attribute on an HTML element, it will be processed by AngularJS. In this case,
AngularJS will execute JavaScript inside double curly braces that can occur directly in HTML or inside attributes.
DOM XSS in AngularJS expression with angle brackets and double quotes HTML-
LAB Not solved
encoded
https://portswigger.net/web-security/cross-site-scripting/dom-based 2/4
10/18/2020 What is DOM-based XSS (cross-site scripting)? Tutorial & Examples | Web Security Academy
In a reflected+DOM vulnerability, the server processes data from the request, and echoes the data into the response.
The reflected data might be placed into a JavaScript string literal, or a data item within the DOM, such as a form field.
A script on the page then processes the reflected data in an unsafe way, ultimately writing it to a dangerous sink.
eval('var data = "reflected string"');
Websites may also store data on the server and reflect it elsewhere. In a stored+DOM vulnerability, the server
receives data from one request, stores it, and then includes the data in a later response. A script within the later
response contains a sink which then processes the data in an unsafe way.
element.innerHTML = comment.author
https://portswigger.net/web-security/cross-site-scripting/dom-based 3/4