xss introduction
xss introduction
Cross-Site Scripting 1
Testing JavaScript Execution Sinks:
Stored XSS, where the malicious script comes from the website's database.
Reflected XSS
Overview
Reflected XSS is a type of cross-site scripting vulnerability that occurs when an
application receives data in an HTTP request and includes it in the response
without proper sanitization.
Key Characteristics:
The attack is reflected rather than stored.
The attack is executed within the user's browser in the context of their
session.
https://insecure-website.com/status?message=All+is+well
Response:
https://insecure-website.com/status?message=<script>/*+Bad+stuff+her
Cross-Site Scripting 2
e...+*/</script>
Response:
Impact:
If a user visits the attacker’s crafted URL, the malicious script executes in
their browser.
The script can perform actions or access sensitive data in the context of
the user’s session.
Key Characteristics:
The attack is stored on the server and affects multiple users.
Impact:
Cross-Site Scripting 3
The malicious script is saved in the database and executed whenever users
view the content.
Key Characteristics:
The attack occurs entirely in the browser.
You searched for: <img src=1 onerror='/* Bad stuff here... */'>
Impact:
This often occurs when data is taken from URL parameters, input fields, or
local storage and inserted into the DOM without sanitization.
Cross-Site Scripting 4
Preventing XSS Attacks
Key Prevention Measures:
Filter Input on Arrival: Validate and sanitize user input as strictly as possible.
Encode Data on Output: Use HTML, URL, JavaScript, and CSS encoding to
prevent execution.
Reflected XSS
Lab
Information
name: Reflected XSS into HTML context with nothing encoded
link: https://portswigger.net/web-security/cross-site-
scripting/reflected/lab-html-context-nothing-encoded
Solution
Enter the following javascript code in the Search box:
<script>alert("test")</script>
Perform any action within the application that the user can perform.
Cross-Site Scripting 5
Initiate interactions with other application users, including malicious attacks,
that will appear to originate from the initial victim user.
4. Test an XSS Payload: Insert a basic XSS script and check if it executes.
Stored XSS
Lab
Information
name: Stored XSS into HTML context with nothing encoded
Link: https://portswigger.net/web-security/cross-site-scripting/stored/lab-html-
context-nothing-encoded
Solution
enter the following javascript code in the comment box
<script>alert(1)</script>
2. Locate Exit Points: Check all responses where user data might appear.
Cross-Site Scripting 6
3. Find Entry-Exit Links: Submit unique values and track where they reappear.
4. Confirm Data Storage: Ensure the input persists across multiple requests.
5. Test XSS Payloads: Insert scripts and verify execution in the stored
context.
DOM-based XSS
Labs
Information (Lab 1)
name: DOM XSS in document.write sink using source location.search
Link: https://portswigger.net/web-security/cross-site-scripting/dom-based/lab-
document-write-sink
Solution (Lab 1)
1. Enter a random alphanumeric string into the search box.
2. Right-click and inspect the element, and observe that your random string
has been placed inside an img src attribute.
"><script>alert(1)</script>
Information (lab 2)
name: Lab: DOM XSS in document.write sink using source location.search inside a
select element
Link: https://portswigger.net/web-security/cross-site-scripting/dom-based/lab-
document-write-sink-inside-select-element
Solution (Lab 2)
1. On the product pages, notice that the dangerous JavaScript extracts
a storeId parameter from the location.search source. It then uses document.write to
create a new option in the select element for the stock checker
functionality.
Cross-Site Scripting 7
2. Add a storeId query parameter to the URL and enter a random alphanumeric
string as its value. Request this modified URL.
3. In the browser, notice that your random string is now listed as one of the
options in the drop-down list.
4. Right-click and inspect the drop-down list to confirm that the value of
your storeId parameter has been placed inside a select element.
Information (Lab 3)
name: DOM XSS in innerHTML sink using source location.search
Link: https://portswigger.net/web-security/cross-site-scripting/dom-based/lab-
innerhtml-sink
Solution (Lab 3)
Enter the following Javascript code the search box:
Information (Lab 4)
name: DOM XSS in innerHTML sink using source location.search
Link: https://portswigger.net/web-security/cross-site-scripting/dom-based/lab-
innerhtml-sink
Solution (Lab 4)
Enter the following Javascript code the search box:
Cross-Site Scripting 8
1. Inject a Random String – Place a unique alphanumeric value into
location.search .
2. Inspect the DOM – Use developer tools ( Ctrl+F / Cmd+F ) to locate the string.
3. Identify Context – If inside quotes, try breaking out with special characters.
Cross-Site Scripting 9