---
title: "2. XSS types and payloads"
date: 2024-10-02
draft: false
---

### stored xss payloads and examples / definition
Also known as persistent
Stored in back-end db and retrieved on visit

- Most critical
- Not easily removable 

#### basic xss payload
```bash
<script>alert(window.origin)</script>
```

- modern browsers may block `alert()` so its handy to know other basic payloads
- i.e. `<plaintext>` 
	- stop rendering html code that comes after it, and display as plain
- another easy payload is `<script>print()</script>` 
	- pop up browser dialogue
	- unlikely to be blocked by browsers
- `document.cookie` contains cookie

to test if xss is stored, we refresh page and determine if we get alert again 

----
### reflected xss payloads + examples
- two types of Non-Persistent XSS
	- Reflected XSS
		- processed by backend
	- DOM-based XSS 
		- completely processed clientside never reaches backend

Unlike persistent - non-persist XSS vulns are temporary, not persisted through page refreshes. Attacks only affect targeted user (instead of all users)

**Reflected XSS** vulnerabilities occur when input reaches backend and returns without filtering/sanitization 
- Many cases where entire input may be returned 
	- error messages
	- confirmation messages
- in these cases we can attempt XSS payloads to see whether they execute
- Usually temporary 

![](/img/Pasted image 20241002173742.png)
in the above example we inserted `test` in the input box - our input is `Reflected` back to us 
we can try the same basic payloads above 
as before if we view page source we can see our payload in the source 

revisiting the page the error message does not appear meaning it is non-persistent 

**TO TARGET A USER SPECIFICALLY** with a reflected xss payload we craft our HTTP request 
- view network traffic 
- see request being sent
- to target a user, SEND THEM URL CONTAINING OUR PAYLOAD 
```bash
http://94.237.49.214:47518/index.php?task=<script>alert(document.cookie)</script>
```

---
### DOM XSS 
Another non-persistent 

Difference between reflected and dom xss: 
- Reflected XSS sends input data to backend server through HTTP requests
- DOM XSS is completely processed on the client-side through Javascript 
- DOM XSS occurs when Javascript is used to change page source through the document object model (DOM) 

![](/img/Pasted image 20241002174546.png)
Adding a test item to the list, and viewing network traffic in browser, we would observe that NO HTTP REQUESTS are being made to make this change to the page. 

Input parameter in the url is using a pound sign (`#`) for the item we've added 
- meaning its a client-side parameter completely processed on the browser
- indicates input being processed client-side through javascript and never reaches the backend thus dom-based xss 

Viewing page source we'll see `test` string is nowhere to be found. 
- indicates javascript code is updating the page when we click the ADD button, and page source is retrieved by browser 
	- "BASE" page source will not show our input 
	- if we refresh page it isn't retained 
	- determine RENDERED page source with ctrl+shift+c

#### Understanding DOM-based XSS 
**Source and Sink**
- Source 
	- Javascript object that takes user input (can be any input param)
- Sink
	- Function that writes the user input to the DOM object on the page
	- If SINK function does not properly sanitize input, it's vulnerable to DOM XSS 

Commonly used JS fxs to write to DOM: 
- `document.write()`
- `DOM.innerHTML`
- `DOM.outerHTML`

Commonly used `jQuery` lib functions to write to DOM object:
- `add()`
- `after()`
- `append()`

If `Sink` fx writes exact input without any sanitization (like the above functions) and no other means of sanitization were used then we know page is likely vulnerable to XSS 

Looking at source of page we can view `script.js` file and see **source** is being taken from `task=` parameter 

```javascript
var pos = document.URL.indexOf("task=");
var task = document.URL.substring(pos + 5, document.URL.length);
```

below these lines we'll see page uses the `innerHTML` function to write `task` variable in the `todo` DOM 

```javascript
document.getElementById("todo").innerHTML = "<b>Next Task:</b> " + decodeURIComponent(task);
```

Thus if we can control input and output isn't sanitized, dom xss

#### DOM attacks
Trying xss payload we used earlier it will not work 
`innerHTML` function does not allow the use of `<script>` tags 
There's many other XSS payloads we can use that do not contain `<script>` tags

```bash
<img src="" onerror=alert(window.origin)>
```
- Creates new HTML img object 
- `onerror` attribute that will execute javascript code when image not found 
- thus we provide empty img link and when code is executed it will always fail 

![](/img/Pasted image 20241002201926.png)
![](/img/Pasted image 20241002201949.png)
