What Is Cross-Site Scripting (XSS)? Working, Types, and Fixes

essidsolutions
  • Cross-site scripting (XSS) is defined as a code injection attack where an attacker inserts malicious code into the content of a web page or application and gets it delivered to the client.
  • In an XSS attack, the attacker does not directly attack the target but instead takes advantage of the vulnerability in a website that the victim often visits.
  • This article explains the fundamentals of XSS, its types, and a few XSS fixes.

What Is Cross-Site Scripting (XSS)?

Cross-site scripting (XSS) is a code injection attack where the attacker inserts malicious code into the content of a web page or application and gets it delivered to the client. The malicious script merges with the website’s dynamic content and is eventually executed on the victim’s browser. Upon the successful execution of such a script, the attacker gains full control over the application’s functionality and data.

In an XSS attack, the attacker does not directly attack the target but instead takes advantage of the vulnerability in a website that the victim often visits. This website then acts as a medium to deliver the malicious script to the end user. User interactions with a vulnerable application can thereby be compromised through XSS attacks. Here, vulnerable applications can include forums, message boards, and websites allowing user comments/feedback.

XSS attacks can cause session hijacking, loss of sensitive data, exfiltration, denial of service attacks, etc. Owing to JavaScript’s support across web browsers and platforms, it is a preferred language of attackers to execute such attacks. However, the malicious executable code can be written in several other languages, including Java, AJAX, HTML, VBScript, ActiveX, Flash, and CSS. Although XSS attacks have been taking place for over a decade, they continue to be rated as one of the most common and viable attack vectors even today. 

Impact of cross-site scripting

When a legitimate website is hit by an XSS attack, a series of issues begin to emerge, such as:

  • Exposure of user’s sensitive data
  • Impersonation of attackers as authorized users
  • Seizure of online user accounts by cyber criminals
  • Vandalizing of website’s presentation by attackers
  • Uploading of trojan horse malware
  • Redirection of a website to dangerous URLs

XSS demands early detection and immediate handling of the event. The malware injection can significantly influence the reputation and relationship of individuals and businesses. For instance, in 2018, a credit card-skimming malware called ‘Magecart’ emerged and affected Ticketmaster, British Airways, and an online retailer Newegg. The malware exploited the web vulnerability by injecting executable code into online check-out sites. It was later identified that users’ credit card details were uploaded to a server and were accessible to the attacker. Moreover, user information was also used to make fraudulent purchases.

How does cross-site scripting work?

XSS is much like SQL injection, where browsers cannot differentiate between legitimate and malicious markup. The browser simply executes the markups received and delivers them to the users requesting them.

These attacks evade the ‘Same Origin Policy (SOP).’ SOPs act as a firewall that does not allow scripts from different websites to interact with each other. This ensures that all webpage content comes from a single source. However, when websites fail to enforce this policy, threat actors tend to inject codes and modify the webpage based on their needs. For example, criminals can impersonate an authenticated user, thereby wreaking havoc to suit their purposes.

Attackers can steal users’ session cookies by pretending to be a victim. Moreover, attackers can spread malware, steal user credentials, influence social networks, and execute social engineering attacks. One such attack was observed in 2022 at Twilio, where a bad actor successfully stole employee passwords through a social engineering attack that involved interacting with Twilio employees through fake IT messages. Another incident took place in October 2022 with Uber’s internal messaging platform Slack, where the threat actor impersonated an employee to gain internal network access.

Typically, cross-site scripting occurs in this manner:

  1. The attacker inserts malicious scripts into web pages that execute on the client’s browser.
  2. Each client’s visit to such a site tends to activate this malicious script.
  3. Upon successful activation, the attacker steals the user’s private credentials or cookies and gains control over the web application.

See More: What Is Cybersecurity? Definition, Importance, Threats, and Best Practices

Types of XSS Attacks

XSS attacks are primarily divided into three categories: reflected XSS, stored/persistent XSS, and Document Object Model (DOM)-based XSS.

1. Reflected XSS

In a reflected XSS attack, the attacker delivers a malicious script to the client using phishing and social engineering techniques. The payload then tricks the victim into making a request to the server that contains the malicious code. Since the request is initiated by the client, it is not stored on the server. Hence, the attack falls under the category of ‘non-persistent attacks.’

Let’s consider a simple example to understand this better.

Initially, the attacker crafts a URL containing the malicious script for a website having a search page. Here, the script is in the form of a query parameter that is vulnerable to XSS attacks. For instance, the URL can be as follows:

http://vulnerbility-site.com?search_term=”<script>(attacker targets search tab)</script>”

Once the URL is ready, the attacker sends it to the target user who would visit the URL through their own web browser. Criminals usually accomplish this by sending an email that contains the harmful URL. Such emails are formatted in a way that tempts the user into clicking on the provided link. In some cases, the attacker can publish the URL on a public website that encourages users to click.

Next, as the user clicks on the URL and the website accepts the query parameter ‘search_term.’ The vulnerable site assumes that the value entered in the search term is exactly what the target user is interested in searching for on the website. However, the site cannot process that the value is actually the malicious script. Consequently, the website’s search page shows the message ‘Searching for <entered search value>….’. However, as the site fails to sanitize the search before starting the search process, the script gets injected into the webpage, loading on the target user’s browser. The script is then executed, and the attacker gains control over the user and the web data.

2. Persistent XSS

Unlike a reflected attack, a persistent XSS attack is stored on the server. As such, users interacting with the vulnerable site or app can be attacked without being sent the script directly.

We’ll look at an example to understand this better.

Let’s say the attacker posts a simple message on a forum hosted on a vulnerable site. The message contains a malicious script. When users visit this forum post, their web browser inevitably loads it, and the script gets executed.

In a persistent XSS attack, users of a vulnerable site/app are considered target victims. This is not the case with a reflected XSS attack.

3. Document Object Model (DOM)-based XSS

Unlike reflected and persistent XSS attacks, harmful scripts are not directly sent to the target user in a DOM-based attack. Such attacks also do not store the script on the vulnerable server. Instead, DOM-based XSS attacks exploit vulnerable client-side scripts to deliver malicious code to the target user’s browser.

Let’s understand it with an example.

The attacker creates a link with a malicious script embedded in it, similar to the one in the above ‘search_term’ example. While the query parameter is processed by the site’s back end, the website itself does not respond to the injected malicious script. Rather, client-side scripts processed locally on the user’s system simulate the search term value. As a result, the target user’s browser loads the attacker’s script and executes it.

Thus, DOM-based XSS attacks reveal that web vulnerabilities aren’t always on the server side.

See More: What Is Vulnerability Management? Definition, Lifecycle, Policy, and Best Practices

Cross-Site Scripting Fixes

With websites becoming more complex by the day, different XSS attack variations continue to emerge. According to Cobalt’s State of Pentesting Report 2022, XSS weaknesses were the second-most common type of vulnerability in 2021, accounting for around 13% of the top vulnerabilities.

As attacks grow in sophistication, organizations should be aware of the practices that can eliminate the security flaws that eventually lead to cross-site scripting attacks. Some of the best practices companies can work on to prevent XSS attacks are listed here.

Cross-Site Scripting Fixes

1. Escape user input

XSS vulnerabilities in web apps can be prevented by escaping user input, which means verifying the application data before rendering it to the user. This practice ensures that the browser does not interpret key characters as executable codes. Escaping helps when the attacker inserts some characters into the webpage to force the browser to interpret them.

For example, attackers can use special characters such as ‘< ‘to paste in the malicious script. When the web browser on the client side comes across such a character, it interprets it as some HTML tag and executes the data following that character. This can compromise the user system. Hence, such characters are escaped to avoid XSS attacks.

2. Sanitize user input

Sanitizing user input refers to changing, deleting, or encoding illegal characters to acceptable ones. It is a process of cleaning data to ensure that any data received from websites or applications aren’t interpreted as malicious executable code. Moreover, encoding halts malicious user data from initiating an automated load-and-execute behavior of a browser. The approach is suitable for web pages that allow HTML markup language.

On failing to sanitize inputs, attackers can inject malicious code into a web page to carry out illicit activities.

3. Validate input

Input validation refers to the process of verifying data to ensure that it is not detrimental to users, websites, or databases. This technique refuses a user’s request to enter special characters into webpage data. As such, it prevents an XSS attack in the initial stages itself. Even if the attackers discover an XSS vulnerability, they may be unable to exploit it as their requests to add executable code to the webpage may be outrightly denied.

Out of the XSS fixes mentioned above, the safest way to prevent XSS attacks is not to trust any user input. User inputs that are part of HTML output should be rated as ‘untrusted’ and undergo a security check before execution. This applies to inputs coming from an authenticated user as well. Such a practice can potentially block XSS vulnerabilities.

4. Use a web application firewall (WAF)

A web application firewall (WAF) is a tool that filters bots and other malicious activities that reveal the characteristics of an attack. The firewall can block XSS attacks before the malicious script gets executed and compromises the network.

5. Implement content security policy (CSP)

A CSP refers to an HTTP response header that specifies the functions a website can potentially perform. These policies deny a website from accepting any kind of in-line scripts. This is the strongest XSS attack prevention method as it can totally block the possibility of an attack since the attackers may not be able to inject any form of malicious script into the webpage.

Actions post an XSS attack

If your website is already hit by an XSS attack, the following steps can be implemented to fix the issue.

  1. Localize the vulnerable code: Identify and locate the exact vulnerability to recover from cross-site scripting.
  2. Remove malicious content and restore: Once the malware is identified, restore the database with clean data by removing any malicious or bad data. Further, cross-check your website and file systems to close the backdoors, if any.
  3. Patch the vulnerability: Upon cleaning the bad data, it is important to apply patches and updates to the vulnerable code. Since cybercriminals often target vulnerabilities in databases, applications, and third-party components, it is equally vital to check for any outdated components and fix them.
  4. Update your credentials: Once the vulnerability is patched, reset all your credentials and application secrets. Moreover, cleaning up your data is essential to prevent reinfection through backdoors or rogue admin users.
  5. Set up a WAF: Once the system is restored, set up a WAF to filter malicious attackers from accessing your website. The firewall comes in handy when new vulnerabilities emerge and patches for it are not made available.

See More: What Is a Firewall? Definition, Key Components, and Best Practices

Takeaway

Cross-site scripting is a multifaceted attack that exploits social engineering methods to target client-side scripts. These attacks are known to steal user credentials, cookie information, and even commercially useful data. It allows attackers to sneak into an organization’s internal network and gain access to a system or web application. From a legal, financial, and reputational standpoint, XSS can damage a company’s image and spread misinformation, impacting its overall business stance.

Did this article help you understand the impact of XSS on businesses? Comment below or let us know on FacebookOpens a new window , TwitterOpens a new window , or LinkedInOpens a new window . We’d love to hear from you!

Image Source: Shutterstock

MORE ON SECURITY VULNERABILITY