Cross Site Scripting
A collection of XSS inputs
Basic XSS Injection
Create a popup saying "Hello":
<script>alert("ALERT")</script>
Create a popup printing the host IP address:
alert(window.location.hostname)
Create a popup printing the document cookies:
alert(document.cookie)
XSS using an image:
<img src="#" onerror=alert(1) />
Console Actions
Changes the content of an element titled "title":
document.getElementById("title").innerText="hacked";
Cause an alert to happen:
alert()
ORalert(1)
ORalert('XSS')
ORalert("XSS")
Log to JS console:
console.log("test")
Encode a string using base64:
btoa("$PLAINTEXT")
Decode base64 to a string:
atob("$BASE64")
Get document cookie:
document.cookie
XSS Hacking
Sometimes XSS will allow you to do unintended actions on the target. This can include "Session Manipulation and Hijacking", "Local File Inclusion and Reading", and "Remote Code Execution".
LFI
The following code can be used to read an internal page and send it to a listening server on the attack box, it uses the "fetch" keyword to get the html data on a page, read it as text, and then forward it as a GET parameter called data to a listening server:
Another example that has more error handling and uses the "img onerror" attribute to bypass checks for the script tag is as follows:
Other methodologies could be used as well to get page data such as using the method "xhr":
Flask App Usage
If your standard netcat listener isn't up to the task, a Flask server can be used to examine and control the flow of traffic between your server and the target box. An example of the Flask server can look like this:
Juice Shop
Using an iframe element with a javascript alert tag in a search bar would be considered DOM XSS:
This type of XSS is called XFS (Cross-Frame Scripting), it is one of the most common forms of detecting XSS within web applications. When the request is made to the server, it will send back an alert due to incorrect input sanitation.
If this was done using Burp to change the header of a page request or continuous chat, persistent XSS could be created to have it execute every time the page is accessed.
Reflected XSS
Reflected XSS is a type of XSS vulnerability where a malicious script is reflected to the user's browser, often via a crafted URL or form submission. The following search query is a simple example of reflected XSS:
<script>alert(document.cookie)</script>
PHP
Suppose the following PHP code is presented:
The $_GET
function retrieves a PHP array containing values from the URL query string. The command $_GET['q']
refers to a query string parameter q
such that the URL would look like http://website.com/search.php?q=term
. Withut any sanitization, this piece of code would allow malicious scripts such as XSS or MYSQL injection to name a few.
JavaScript (Node.js)
Given the following Node.js code:
This code uses Express, a popular web application framework for Node.js. The req.query.q
will extract the value of q
similar to the above PHP code. The following proof of concept can then be used to cause reflected XSS:
Stored XSS
Also known as persistent XSS, it's a vulnerability that occurs when the application stores user-supplied input and later embeds it in web pages served to other users without proper sanitization or escaping. Examples include web forum posts, product reviews, user comments, and other data stores.
PHP
The code below has multiple vulnerabilities:
Although this is vulnerable to SQL injection in the section $_POST['comment']
, it is also vulnerable to XSS since the comment is displayed without sanitization with the line echo $row['comment']
. These vulnerabilities can be fixed with mysqli_real_escape_string()
and htmlspecialchars()
functions respectively.
Javascript (Node.js)
The following is an example of another stored XSS vulnerability:
The main issue here is with the section ${comment}
and how it is displayed directly as part of the HTML code. If a user views this comment, any scripts that were injected in it would be executed by the visiting user. This can be fixed using sanitizeHTML()
as well as prevent unsafe elements like <script>
and <onload>
.
DOM Based XSS
The following "Person" field is where the vulnerability lies:
The v-html
tag allows html input and does only basic sanitizing versus the second td
element which translates the input to text. The application itself requires a secret key in local storage to request a delete operation. The idea was to create a stored DOM XSS to capture other users secret keys and send them to a listening server on an attack box. The following list is the process of crafting a payload to execute sending a message containing the captured secret key:
The sent messages can then be captured on a simple Python server.
DOM Clobbering
To test if DOM clobbering can be used, try the following payload
Then in the console, try typing:
If this returns the item containing the href value from the payload, then it is likely vulnerable to DOM clobbering.
Last updated