The Wayback Machine - https://web.archive.org/web/20240808114518/https://www.geeksforgeeks.org/dom-based-cross-site-scripting-attack-in-depth/
Open In App

DOM-based Cross-Site Scripting Attack in Depth

Last Updated : 21 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

In this article, we will be understanding one of the types of Cross-Site Scripting in-depth i.e DOM-based XSS. Let’s discuss it one by one as follows.

DOM-based Cross Site Scripting :

  • DOM XSS stands for Document Object Model-based Cross-site Scripting. DOM-based vulnerabilities occur in the content processing stage performed on the client, typically in client-side JavaScript.
  • DOM-based XSS works similar to reflected XSS one — attacker manipulates client’s browser environment (Document Object Model) and places payload into page content. The main difference is, that since the malicious payload is stored in the browser environment, it may be not sent on the server-side. This way all protection mechanisms related to traffic analysis will fail.
  • In reflective and stored Cross-site scripting attacks you can see the vulnerability malicious script in the response page but in DOM-based cross-site scripting, the HTML source code and the response of the attack will be the same, i.e. the malicious script cannot be found in the response from the web server.
  • In a DOM-based XSS attack, the malicious string is not parsed by the victim’s browser until the website’s legitimate JavaScript is executed. To perform a DOM-based XSS attack, you need to place data into a source so that it is propagated to a sink and causes the execution of arbitrary JavaScript code.

Breakdown of a DOM-based XSS attack :
The following is a breakdown of a DOM-based XSS attack as follows.

  1. Attacker discovers the DOM-based XSS vulnerability
  2. The hacker or attacker crafts a malicious script and sends the URL to the target(Email, social media, etc)
  3. Victim clicks on the URL
  4. Victims browser sends a request to the vulnerable site (note: the request does not contain the XSS malicious script)
  5. The web server responds with the web page (note: this response does not contain the XSS malicious script)
  6. Victims web browser renders the page, with the hackers or attackers XSS malicious script

Impact :

  1. Steal another client’s cookies or sessions.
  2. Modify another client’s cookies or sessions.
  3. Steal another client’s submitted form information or some sensitive credentials.
  4. Modify another client’s submitted form data or information by intercepting the request (before it reaches the server).

Note –
Submit a form to your application on the user’s behalf which modifies passwords or sensitive data on server or other application data.

Finding DOM-based Cross Site Scripting :

  1. Most DOM XSS vulnerabilities can be found rapidly and efficiently using Burp Suite’s tool scanner or some other scripts which are available on GitHub.
  2. To test for DOM-based cross-site scripting manually, you generally need to use a web browser with developer tools, such as Chrome or Firefox.
  3. You need to work through each available source or input field in turn and test each one individually.

Understanding DOM-based Attack via Diagram:

DOM XSS Steps

Diagram Description –
From the above fig, “Consider diagram arrow numbers (Step 1 to Step 6) as steps” as follows.

  • Step-1: An attacker crafts the URL and sends it to a victim.
  • Step-2: The victim clicks on it and the request goes to the server.
  • Step-3: The server response contains the hard-coded JavaScript.
  • Step-4: The attacker’s URL is processed by hard-coded JavaScript, triggering his payload.
  • Step-5: The victim’s browser sends the cookies to the attacker.
  • Step-6: Attacker hijacks user’s session.

Example :
Example of a DOM-based XSS Attack as follows.

<HTML>
<TITLE>Hello!</TITLE>
<SCRIPT>
var pos=document.URL.indexOf("name=")+5;
document.write(document.URL.substring(pos,document.URL.length));
</SCRIPT>
<BR>
Welcome To Our Website
â€Ķ
</HTML>

Explanation –
Normally, this HTML page would be used for welcoming the user, e.g –

http://www.victim.site/hello.html?name=Gaurav

However, a request such as the one below would result in an XSS condition as follows.

http://www.victim.site/hello.html?name=alert(document.domain)

Remediation from DOM-based XSS:

  • Detecting DOM XSS is hard using purely server-side detection (i.e. HTTP requests), which is why providers like Acunetix leverages DeepScan to do it.
  • These malicious scripts or payloads are never sent to the web server due to being behind an HTML fragment (everything behind the # symbol).
  • As a result, the root issue is in the code (i.e. JavaScript) that is on the source page. This means that you should always sanitize or filter user input, irrespective of whether it is client-side
  • To remediate DOM-based XSS, data must not be dynamically written from any un-trusted source into the HTML document. Security controls must be in place if the functionality requires it.
  • It may involve a combination of JavaScript escaping, HTML encoding, and URL encoding.
  • Frameworks like AngularJS and React use templates that make the construction of ad-hoc HTML an explicit (and rare) action. This will boost your development team towards best practices, and make unsafe operations easier to detect.

Note –   
The following source attributes should be avoided like URL, document URI, location, href, search, hash.



Similar Reads

Difference Between Stored Cross Site Scripting and Reflected Cross Site Scripting
In today’s online world, keeping web applications secure is crucial to protect both businesses and their users. One common threat is Cross-Site Scripting (XSS), where attackers insert harmful code into websites or web applications. This can cause to serious issues like stealing passwords, redirecting users to dangerous sites, and changing web conte
3 min read
Cross Site Scripting (XSS) Prevention Techniques
XSS or Cross-Site Scripting is a web application vulnerability that allows an attacker to inject vulnerable JavaScript content into a website. An attacker exploits this by injecting on websites that doesn't or poorly sanitizes user-controlled content. By injecting vulnerable content a user can perform (but not limited to), Cookie Stealing.Defacing
5 min read
"Script error" reported in window.onerror without cross-site scripting
Sometimes, when developing a JavaScript application, you might encounter an issue where a "Script error" is reported in the window.onerror method, even though there is no cross-site scripting (XSS) involved. This can be frustrating because it can be difficult to determine the cause of the error and how to fix it. The problem you may encounter: Here
2 min read
Same Site Scripting
Same Site Scripting is type of vulnerability that is caused by common DNS misconfiguration. But before we dive deep into concepts, let's make sure we have basic understanding of how Internet works. Working of the web : When you try to visit website, your browser communicates with web server where that website's hosted by using IP (internet protocol
3 min read
Cross-Site Request Forgery (CSRF) Protection Methods and Bypasses
Cross-Site Request Forgery is a vulnerability found in web applications that lets a third party attacker perform sensitive actions on a user's behalf. The exploitation of this bug can target normal users as well as site administrators, sometimes leading to a full compromise of a website. Modern websites tend to deploy some protection mechanisms fro
4 min read
Difference between Active Attack and Passive Attack
Active Attacks: Active attacks are the type of attacks in which, The attacker efforts to change or modify the content of messages. Active Attack is dangerous to Integrity as well as availability. Due to active attack system is always damaged and System resources can be changed. The most important thing is that, In an active attack, Victim gets info
2 min read
If we delete cookies of a site, we can still logged in without logging again
The facebook is coded in PHP and PHP was launched in 1994. But in PHP, there is a disadvantage i.e if we disable cookies (say in Facebook) the Facebook server will forget the person who has logged in previously and it will take to me that page when that application was launched for the first time. But in Java (JEE Edition, launched in 1999) we have
6 min read
HTTP headers | Clear-Site-Data
The HTTP header Clear-Site-Header is a response-type header. This header is used in deleting the browsing data which is in the requesting website. These browsing data includes cache, cookies, storage and executionContents. It helps the web developers to have an improved level of control over data stored by the browser locally. Syntax: Clear-Site-Da
2 min read
How to uniquely identify computers visiting web site in JavaScript?
If you want to identify computers visiting your website, you will have to store the cookies of your website on the visiting computers. Cookies: In simple words, HTTP cookies are small notes which store the user's preferences about a particular website. For example, when a user shops online without logging in, he may put some items in the cart. Even
4 min read
How to Remove Site URLs from Google Search?
In this article, we'll see how we can remove indexed URL's on google search results using the Google Search Console tool. As a note, this process will work only for the google indexed URLs.Need to urgently remove content from Google Search? Use or Setup a Google Search Console to remove unwanted, outdated, duplicated, discarded or deleted pages by
1 min read
three90RightbarBannerImg