XSS Attack and How To Mitigate It
XSS Attack and How To Mitigate It
As the simulated victim in our labs uses Chrome, we've amended the affected
labs so that they can also be solved using print(). We've indicated this in
the instructions wherever relevant.
Reflected XSS, where the malicious script comes from the current
HTTP request.
Stored XSS, where the malicious script comes from the website's
database.
DOM-based XSS, where the vulnerability exists in client-side code
rather than server-side code.
Reflected cross-site scripting
Reflected XSS is the simplest variety of cross-site scripting. It arises when an
application receives data in an HTTP request and includes that data within the
immediate response in an unsafe way.
https://insecure-website.com/status?message=All+is+well.
<p>Status: All is well.</p>
https://insecure-
website.com/status?message=<script>/*+Bad+stuff+here...+*
/</script> <p>Status: <script>/* Bad stuff here...
*/</script></p>
If the user visits the URL constructed by the attacker, then the attacker's script
executes in the user's browser, in the context of that user's session with the
application. At that point, the script can carry out any action, and retrieve any
data, to which the user has access.
The data in question might be submitted to the application via HTTP requests;
for example, comments on a blog post, user nicknames in a chat room, or
contact details on a customer order. In other cases, the data might arrive from
other untrusted sources; for example, a webmail application displaying
messages received over SMTP, a marketing application displaying social
media posts, or a network monitoring application displaying packet data from
network traffic.
Here is a simple example of a stored XSS vulnerability. A message board
application lets users submit messages, which are displayed to other users:
If the attacker can control the value of the input field, they can easily construct
a malicious value that causes their own script to execute:
In a typical case, the input field would be populated from part of the HTTP
request, such as a URL query string parameter, allowing the attacker to
deliver an attack using a malicious URL, in the same manner as reflected
XSS.
Manually testing for reflected and stored XSS normally involves submitting
some simple unique input (such as a short alphanumeric string) into every
entry point in the application, identifying every location where the submitted
input is returned in HTTP responses, and testing each location individually to
determine whether suitably crafted input can be used to execute arbitrary
JavaScript. In this way, you can determine the context in which the XSS
occurs and select a suitable payload to exploit it.
Manually testing for DOM-based XSS arising from URL parameters involves a
similar process: placing some simple unique input in the parameter, using the
browser's developer tools to search the DOM for this input, and testing each
location to determine whether it is exploitable. However, other types of DOM
XSS are harder to detect. To find DOM-based vulnerabilities in non-URL-
based input (such as document.cookie) or non-HTML-based sinks
(like setTimeout), there is no substitute for reviewing JavaScript code,
which can be extremely time-consuming. Burp Suite's web vulnerability
scanner combines static and dynamic analysis of JavaScript to reliably
automate the detection of DOM-based vulnerabilities.
Content security policy
Content security policy (CSP) is a browser mechanism that aims to mitigate
the impact of cross-site scripting and some other vulnerabilities. If an
application that employs CSP contains XSS-like behavior, then the CSP might
hinder or prevent exploitation of the vulnerability. Often, the CSP can be
circumvented to enable exploitation of the underlying vulnerability.
Filter input on arrival. At the point where user input is received, filter
as strictly as possible based on what is expected or valid input.
Encode data on output. At the point where user-controllable data is
output in HTTP responses, encode the output to prevent it from being
interpreted as active content. Depending on the output context, this
might require applying combinations of HTML, URL, JavaScript, and
CSS encoding.
Use appropriate response headers. To prevent XSS in HTTP
responses that aren't intended to contain any HTML or JavaScript, you
can use the Content-Type and X-Content-Type-
Options headers to ensure that browsers interpret the responses in
the way you intend.
Content Security Policy. As a last line of defense, you can use
Content Security Policy (CSP) to reduce the severity of any XSS
vulnerabilities that still occur.