PROMOTION
See our Fast Start promotion and start your first pentest on The Cobalt Offensive Security Testing Platform for only $4,950.
PROMOTION
See our Fast Start promotion and start your first pentest on The Cobalt Offensive Security Testing Platform for only $4,950.

Testing for Reflective XSS - Part 1

Reflected XSS, also known as Reflected Cross-Site Scripting, is a dangerous vulnerability that occurs when a web application includes user input in its output without proper validation or encoding. Reflected XSS allows attackers to inject malicious scripts that are then executed in the context of the victim's browser.

Identifying and understanding how reflected input parameters are vulnerable is crucial in ensuring the security of web applications. By following a systematic approach and using tools like Arjun, Param Miner, and Paramspider, security researchers can uncover hidden parameters and potential points of exploitation.

Preventing XSS requires proper input validation, output encoding, and awareness of where user input is reflected within the HTML context. Today, we'll cover best practices to prevent a reflective XSS attack to help developers protect their applications from the risks posed by this attack.

What is Reflected XSS?

Reflected XSS is a type of Cross-Site Scripting where the malicious script is injected via user input and immediately reflected back in the web page’s response. This occurs when input is improperly validated and included in the output without proper encoding.

Finding the Reflected Input Parameter

To begin, understand the application's functionality and identify all input vectors, such as URL parameters, forms, and headers. Test these points using simple, unique strings to determine if the input is reflected in the response. Use browser developer tools to inspect the responses and confirm reflections. Once you have identified the points where inputs are reflected, you can proceed to test for XSS vulnerabilities.

GET /search?query=test1234 HTTP/1.1
Host: example.com
User-Agent: test1234
Referer: https://example.com/previous-page
Cookie: session=test1234; user=test1234

Manual Identification

To identify reflected input parameters in a web application, follow these steps:

1. Identify Input Vectors

  • Locate Input Fields: Identify all the places in the application where user input can be provided. This includes URL parameters, form fields, and HTTP headers.

2. Choose a Unique String

  • Simple Test Payload: Use a unique string as a basic payload to help identify reflected parameters. For example, use “test1234”.

3. Inject string into Parameters

  • URL Parameters: Add the test payload to different URL parameters and observe the responses.
    • Example: http://example.com/search?q=test1234
  • Form Fields: Enter the payload in various form fields and submit the forms.
  • HTTP Headers: Modify headers like “User-Agent”, “Referer”, and “Cookie” to include the payload.

4. Analyze Responses

  • View Source Code: Use the browser’s developer tools to view the HTML source code of the response. Look for the injected string in the response.
  • Highlighted Reflection: If the string appears in the HTML, the parameter is likely reflected.

5. Confirm Reflection

  • Context Reflection: In View-Source ensure the reflection of user input “test1234” appears in a context where we can potentially break the HTML context.

Automated Testing

  • Automated Tools: Use tools like OWASP ZAP or Burp Suite to automate the process of injecting payloads and analyzing responses.
    • OWASP ZAP: Use the Active Scan feature to automatically test for reflected parameters.
    • Burp Suite: Use Intruder to send multiple payloads to different parameters and analyze the reflected responses.

Open Source Tools:

1. Arjun

Purpose: Arjun helps find hidden GET and POST parameters in endpoints.

Installation: Install from https://github.com/s0md3v/Arjun

arjun-code-example

Usage:

  • Identify Hidden Parameters:
    arjun -u http://example.com
  • Scan Multiple URLs:
    arjun -i urls.txt -o results.json

2. Param Miner

Purpose: Param Miner, a Burp Suite extension, discovers hidden and unlinked parameters.

Installation: Install Param Miner from the Burp Suite BApp Store.

param-miner-burp-suite-extension

Usage: Right-click on the target in Burp Suite → extensions → param Miner → guess params.

Analyze the requests in extensions → param Miner → output

3. Paramspider

Purpose: Param Spider fetches the parameters from the web archive.

Installation: Install from https://github.com/devanshbatham/ParamSpider

paramspider-example

Usage:

  • Finding Parameters from web archive:
paramspider -d example.com
  • Customize Search:
    • Use options like l to limit the scope and p to specify a particular parameter.

paramspider_example_parameter_list

Paramspider will show you the parameters and where exactly you can fuzz.

Enumeration for Reflection Context

In HTML context, string reflection refers to situations where a web page includes user-supplied input in its HTML output without the proper sanitization or encoding, such as within HTML tags. This lack of procedures can potentially lead to Cross-Site Scripting (XSS) vulnerabilities. Let’s look at some examples.

Reflection: In Between Tags

  • Suppose you have a search form on your website where users can enter a search query. If a user searches for “test1234”, the URL might look like this:
http://example.com/search?q=test1234
  • After processing this input, the web application includes it in the HTML response:
<html>
<head><title>Search Results</title></head>
<body><h1>Search Results for:test1234 </h1></body>
</html>

In this instance, the system reflects the string in the HTML context between HTML <h1> tags. Sometimes, the reflection might occur within a <p> tag, <title> tag, or even a <div> tag, depending on the application.

Reflection: Attribute Value

When an application reflects user input within an HTML attribute without proper sanitization or encoding, it creates XSS vulnerabilities. For instance, if an application directly reflects your input like test1234 in a tag such as <input type="text" value="test1234">, you can inject an XSS payload to execute malicious scripts.

Input: https://abc.com/?search=test1234

Reflected Output:

<html><input type="textbox" id="search" value="test1234"></html>
 

Methodology for Testing XSS

Once you have identified where the string is reflected and how it appears, such as between HTML tags, you need to know how to inject an XSS payload that will work. Simply choosing random payloads from cheat sheets and spamming them in input parameters, or running fuzzing tools, won’t work all the time. This approach can miss potential XSS vulnerabilities if the payloads don't hit. A better approach is to test characters individually or in combination and observe how the application responds. Let’s see how it is done.

Step 1: Enumeration for Reflection Points

In this step, we examine how the user input from the search parameter is reflected within the HTML context.

enumeration-for-reflection-example

  • Input: https://abc.com/?search=test1234
    • The user input from the GET-based “search” parameter is reflected in two places within the HTML context.

We identified two primary reflection points:

  • Reflection 1: The user input is reflected between HTML tags, specifically <h1>. Other tags where input might be reflected include <p>, <strong>, <head>, and more.
  • Reflection 2: The user-supplied input is reflected in the attribute value. There could be different possibilities for exploitation depending on the input, such as if type=hidden.

After this, we proceed to step 2 to understand how the input is being encoded and how to break the context in both reflection cases.

Step 2: Understanding the HTML Context

The purpose of this step is to understand what is being blocked or filtered by the application.

reflective-xss-html-code-example

  • Input: https://abc.com/?search=test1234<
    • This input includes a starting angle bracket < and double quotes ", which are crucial to disrupt the HTML contexts for Reflection 1 and Reflection 2.

We need to observe how the reflection would appear in the application’s “view-source:” if there is no filtration of the user’s potentially harmful input.

Step 3: No Sanitization in Both of the Reflections

In this case, no encoding of special characters is happening. It is noticeable that our second special character’s color is changed in the Reflection 2 which can be considered as the valid syntax of HTML.

  • Input: https://abc.com/?search=test1234<img+src=x></img>"

no-sanitization-code-example

The color change in Reflection 1 indicates that the <img> tag we entered is interpreted as valid HTML. In contrast, the color of the user input in Reflection 2 remains the same, suggesting the input is not interpreted as HTML.

xss-basics-example

After this, we understand how we can break the syntax in these two cases when the input is getting encoded in URL encoding or getting sanitized.

Step 4: Performing XSS in Reflection 1

Input:  https://abc.com/?search=test1234<img+src=x+onerror=alert('Cobalt')></img>"autofocus=""onfocus="alert('Cobalt')"

xss-reflection-code-example-2

In this case, the payload is:

  • <img>: This is an HTML image tag used to embed an image in the web page.
  • src=x: This is an attribute of the image tag. src stands for source, which is used to specify the URL of the image. Here, x is a non-existing source, which will fail to load.
  • onerror: This is an event attribute which executes JavaScript code when an error occurs while loading an external file (in this case, an image).
  • alert('Cobalt'): This is a JavaScript function that displays an alert box with the message ‘Cobalt’. This function executes when the onerror event triggers due to the failure of the image load.

xss-basics-error-example

Step 5: Performing XSS in Reflection 2

  • Input: https://abc.com/?search=test1234<img+src=x></img>"autofocus=""onfocus="alert('Cobalt')"

xss-sanitation-example

In this case, our attributes autofocus and onfocus are successfully injected as a valid HTML syntax.

The payload is broken down as follows:

  • <input type="text" name="search" id="search" value="test1234&lt;img src=x&gt;&lt;/img&gt;"> is an input field of type text with the name and id “search”.
  • autofocus is an HTML attribute that specifies that the input field should automatically get focus when the page loads.
  • "onfocus="alert('Cobalt')"" is an HTML attribute that triggers a JavaScript alert with the message”Cobalt” when the input field gets focus.

xss-basics-example-2

The XSS in Reflection 2 is also successful. We have seen just two very basic cases of enumerating the XSS issue; there are numerous ways to bypass the advanced WAF. It is suggested to learn the basics of JavaScript to understand more about the XSS.

How to Prevent XSS?

1. Sanitize Input:
  • Ensure you remove or escape harmful characters from user input.
  • For example, strip out or encode <, >, &, and ".
2. Encode Output:
  • Properly encode any user-supplied input before reflecting it in the HTML response.
  • Convert special characters to their respective HTML entities. For instance, " should become &quot;.
3. Use Secure Coding Practices:
  • Implement secure frameworks that handle input sanitization and output encoding automatically.
  • Avoid directly inserting user input into the HTML structure.

Final Thoughts

Testing for XSS threats is important. Instead of just throwing a lot of random payloads into an application, take the time to check each piece of input. This way, you’re sure to catch any weak spots.

SANS Application & API Security Survey 2024 CTA

Back to Blog
About Meet Sodha
Meet aka Smilehacker is a seasoned cybersecurity researcher with an impressive four-year track record in the field. His journey began in 2019 when he delved into the world of bug bounties and application security, rapidly honing his skills and expertise. Meet possesses a profound understanding of application security and continually augments his knowledge by devouring articles, engaging in hands-on labs, and conducting extensive research in specialized areas. With a keen eye for vulnerabilities and a passion for safeguarding digital landscapes, Meet is a trusted guardian of online security, dedicated to staying one step ahead of cyber threats. More By Meet Sodha
Hacking Web Cache - Deep Dive in Web Cache Poisoning Attacks
Web cache poisoning is an attack where an attacker takes advantage of flaws in the caching mechanism. They attempt to store an altered and malicious response in the cache entry, forcing the website to serve malicious information to its users.  Core Pentester Harsh Bothra deep dives into these attacks and remediations.
Blog
Jan 31, 2023
Introduction to Command Injection Vulnerability
We've covered code injection attacks in recent blogs, but do you happen to know about command injection attacks? Core Pentester Harsh Bothra walks us through the differences and covers all you need to know to protect yourself against command injection attacks.
Blog
Dec 14, 2022
Secure Software Best Practices: Validate User Input
Protect your systems from bad user input. In this article, we share best practices to validate user input, securely.
Blog
Sep 23, 2022