Cross-Site Scripting (XSS) is a security vulnerability that occurs when an application includes untrusted data in a web page. Attackers can inject malicious scripts into web pages viewed by other users. These scripts execute in the context of a user's browser, which can lead to a wide range of attacks, such as stealing cookies, session tokens, or sensitive data, defacing websites, or redirecting users to malicious sites.
There are three major types of XSS:
Stored XSS: The injected payload is permanently stored on the server and served to other users when they access the page.
Reflected XSS: The payload is included in the page as a result of a user action, like clicking on a malicious link or submitting a form.
DOM-based XSS: while stored and reflected XSS attacks exploit vulnerabilities in the server-side code, a DOM-based XSS exploits client-side ones (e.g. JavaScript used to help dynamically render a page). DOM-based XSS usually affect user inputs that are temporarily reflected, just like reflected XSS attacks.
Practice
Tools
XSStrike (Python) can be used to scan website for XSS
pythonxsstrike.py-uhttps://target.url/
Dalfox is a powerful open-source XSS scanner and utility focused on automation.
# Scan an URLdalfoxurlhttp://testphp.vulnweb.com/listproducts.php?cat=1# From an URLs filecaturls.txt|dalfoxpipe
XSSer (Python) is a tool that can detect, exploit and report XSS vulnerabilities on a web application
# --Cl : Crawl only local target(s)# -c : Number of urls to crawl on target(s)# --Cw: Deeping level of crawler: 1-5# -s: show advanced statistics output results# --auto: Inject a list of vectors provided by XSSer# --xml: Export to XMLpython xsser -u https://target.url -c 50 --Cw 1 --Cl -s --threads 5 --timeout 30 --retries 1 --delay 0 --auto --xml out.xml
toxssin (Python) is an open-source penetration testing tool that automates the process of exploiting Cross-Site Scripting (XSS) vulnerabilities. It consists of an https server that works as an interpreter for the traffic generated by the malicious JavaScript payload that powers this tool (toxin.js).
We have to identify input vectors that may not be properly sanitized and that are stored or reflected. For example:
URI parameters for reflected and DOM-based XSS
Other user inputs in forums, chats, comments, posts, and other stored content for stored XSS
HTTP headers like Cookies (and even User-Agents in some cases)
We can input special characters and observe the output to determine if any of the special characters return unfiltered. The most common special characters used for this purpose include:
< > ' " { } ;
The following website (or Github) can assist in recognizing the alterations made to user inputs, which can aid in circumventing filters and modifications, enabling the exploitation of XSS vulnerabilities.
Payloads
Generally we will use following payloads as a proof of concept. It will open an alert window.
We may improve our payload for more reliability. First, compress it using this website, and then past it as the following encode_to_javascript function argument
CSP is a browser security mechanism that aims to mitigate XSS and some other attacks. It works by restricting the resources (such as scripts and images) that a page can load and restricting whether a page can be framed by other pages. To enable CSP, a response needs to include an HTTP response header called Content-Security-Policy with a value containing the policy. The policy itself consists of one or more directives, separated by semicolons.
CSPBypass.com, is an open-source tool designed to help ethical hackers bypass restrictive Content Security Policies (CSP) and exploit XSS (Cross-Site Scripting) vulnerabilities on sites where injections are blocked by CSPs that only allow certain whitelisted domains.