What is Cross-site scripting (XSS)?

What is cross-site scripting?

Cross-site Scripting (XSS) is a security vulnerability usually found in websites and/or web applications that accept user input. Examples of these include search engines, login forms, message boards and comment boxes.

Cybercriminals exploit this vulnerability by inputting strings of executable malicious code into these functions. This injects the malicious code into the targeted website’s content, making it a part of the website and thus allowing it to affect victims who may visit or view that website. The code may also present itself as transient content that isn’t actually part of the website but only appears to be to the visitor. This makes it look like the website is indeed compromised by cybercriminals.

Cybercriminals may also use this vulnerability to take control or directly compromise a website, as well as exploit other existing vulnerabilities on the website's server or software.

How XSS Works

  • Cybercriminals choose their target website, and scope out its vulnerable functions that accept user input. Examples are search bars, login forms, and comment boxes.
  • They input malicious code into the function they choose. The malicious code could be written using programming language such as HTML or JavaScript, among others.
  • The malicious code gets injected on the web page that results from the function (search results or a comment in a comments page). It then becomes part of that web page, making it compromised.
  • Depending on how the code is injected, the malicious content may not even be on the actual web page itself, but rather as a transient element that only appears to be part of the website during the particular instance of the exploitation. This can create the illusion that the actual website is indeed compromised, when it is actually not.
  • Users may become victims of the compromised web page by either getting linked to it by the cybercriminals responsible, or stumbling upon it, depending on which function the culprits decided to abuse.
  • The routines that the injected code could run on a victim’s system may vary from the harmlessly annoying to the outright malicious. It could be as harmless as an unexpected image being displayed among the legitimately-published content on that website, to something that redirects the user to a malicious website and/or downloads malicious files onto their system automatically. It can also be used to steal critical personal information from the victim, such as login information.

Why Is XSS Dangerous?

With XSS, cybercriminals can turn trusted websites into malicious ones, thus causing inordinate harm and damage not only to the victims but also to the reputation of the trusted website's owner.

Websites that are compromised by XSS can cause any number of threats to attack a user's system. This can involve anything from inappropriate content being displayed to malware being downloaded onto the system without the user knowing.

What Can Users Do?

As dangerous as XSS is, there are ways in order to patch such a vulnerability. Website owners must ensure that all their web applications that accept user input do so in such a way that they will sanitize the inputted strings first before creating the resulting page of the input. This prevents any code injection from taking place. Users, on the other hand, should disable scripting on their browsers, as well as avoid clicking on links from suspicious parties or senders.

Website developers/owners should:

  • Ensure that any page on their website that accepts user input filters out code inputs, such as HTLM and JavaScript.
  • Scan for any web application vulnerabilities and patch them accordingly
  • Update their website and server software to prevent future exploitation of vulnerabilities that may also be targeted through an XSS attack.

Users should:

  • Disable scripting on pages where they’re not required, or disable them completely.
  • Avoid clicking on links from suspicious email or posts on messageboards, as they may lead to compromised pages.
  • Access desired websites directly through their address, and not through a third-party source or link.
  • Update their system software and applications regularly to prevent secondary vulnerability exploitation. 

Types of XSS attacks

According to the Open Web Application Security Project (OWASP), XSS attacks fall into one of three categories: reflected XSS, stored XSS, and Document Object Model (DOM) XSS. These are detailed below.

Reflected cross-site scripting attack (non-persistent)

A reflected XSS attack occurs when a hacker delivers a malicious script to a vulnerable web application, which the server then returns in the HTTP response. The victim’s browser executes the malicious script as part of the HTTP response, compromising the legitimate user and sending private information back to the hacker.

Reflected XSS attacks typically target error messages or search engine result pages, as it’s easy to send a malicious email with a link that many users will click. When the user clicks the link, the server receives the request containing the malicious script, and since it’s not stored, it responds by sending a code back to the user. When user inputs aren’t adequately validated and sanitized, or when data is unsafely duplicated from a request, there’s a risk of reflected XSS vulnerabilities.

The first line of defense against XSS attacks is to filter content and verify user inputs. You can use script suppliers’ safelists and blocklists to reject risky data patterns.

Additionally, you can implement a stringent Content Security Policy (CSP) to help you identify the source of inline scripts, reducing the risk of reflected XSS attacks. A strong CSP gives you control of scripts and the web page locations where they may be loaded and run. 

Stored cross-site scripting attack (persistent)

In a stored XSS attack, a malicious script saves user input to the target server. Unlike a reflected XSS attack, which executes on the server, a stored XSS attack executes on the user’s browser. Attackers then use modern HTML5 applications, typically employing HTML databases, to permanently store harmful scripts on the browser.

In a stored XSS attack, the script is saved and executed on the server every time the user accesses the affected website. It’s easy for an attacker to target a large number of victims, and the result is persistent. Stored XSS attacks can also occur when untrained users try to extract data from the software without taking any sanitization or validation precautions.

Stored XSS attacks aim to reflect a malicious script to a user, so the easiest way to prevent them is to sanitize user data and handle inputs carefully—and the best way to prevent them is to use appropriate parameter binding.

You can sanitize data with an auto-escaping template system or HTML encoding. You should encode data intended for output to prevent the server from interpreting it as active content. This means the application will handle special characters in its saved data as HTML tag content, rather than plain HTML. 

Data parameter (data) binding varies according to the vector, but you can always pass variables as additional values outside the normal functionality of the function. You can also use appropriate response headers to prevent attacks, typically by just adding a few lines of code.

Another technique to stop XSS attacks from occurring in real time is to employ dynamic security that actively searches for exploitation attempts. By blocking known patterns, you can stop attackers from exploiting existing loopholes.

Finally, you can use Web Application Firewalls (WAFs) for real-time XSS attack detection and mitigation.

Document Object Model (DOM) cross-site scripting attack

The DOM interface enables the processing and manipulation of web page contents by reading and modifying HTML and XML documents. DOM-based XSS attacks introduce malicious changes to the DOM context of the victim’s browser, causing the client-side code to be executed in unintended ways.

DOM-based XSS attacks, unlike reflected and stored XSS attacks, do not store the malicious script or deliver it to the server. In this attack, the victim's browser is the sole vulnerability. Since they’re more difficult to understand than other categories, DOM-based vulnerabilities are uncommon, sophisticated, and challenging to overcome. Moreover, automated vulnerability scanners and web application firewalls can’t easily identify them.

You can use the same techniques to prevent this attack as those for the other two, but you must take extra care to sanitize the client-side code. Two effective solutions are to prevent user-controlled sources from changing potentially dangerous JavaScript functions (known as sinks) or to allow only trustworthy content by using a safelist. With these precautions, strings that might endanger the DOM won't be sent to sinks. You can also sanitize the data using built-in browser functionality, reducing the risk of parser change-related problems.

A novel defense against this type of attack is to use trusted types. This is a browser security mechanism that ensures that all risky parts of the DOM can only be used by data that has passed a predefined policy. It prevents arbitrary strings from being passed to potentially dangerous sinks, which helps the browser differentiate between code and data—removing the main source of vulnerability. 

Server-side XSS versus client-side XSS

XSS attacks are categorized as either server XSS or client XSS. Client-side programs run on the client’s device or browser and take care of the user interface and any other processing that takes place on the client’s device. Server-side programs operate on servers and create the contents of a webpage.

Server-side XSS occurs when all the server-side code is vulnerable and the browser renders the response and runs any legitimate scripts embedded in it. On the other hand, client-side XSS executes on the user’s device and modifies a webpage after it has loaded.

An XSS attack is possible anywhere there is HTML. Whether they’re stored, reflected, or DOM-based, all XSS attacks have the same effect: An attacker gains complete control of a web session.

These XSS attacks can also overlap, and a website may be vulnerable to all three simultaneously. In the case of a single website or an offline application, all three attack types might present themselves directly in the browser. However, their behavior may differ when the data is saved on the server compared to when it’s reflected from the server. 

Related Research