# XSS (DOM)

> #### Objective
>
> Run your own JavaScript in another user's browser, use this to steal the cookie of a logged in user.

##

## Security Level: Low

> Low level will not check the requested input, before including it to be used in the output text. Spoiler: /vulnerabilities/xss\_d/?default=Englishalert(1).

* Let's select the first option i.e. `English` and click `Submit`.

<figure><img src="https://1586847736-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtSZ40gLWhBDTzPEgHsVB%2Fuploads%2FuJ7r4Xev9TCYU7JBxCCT%2F1.png?alt=media&#x26;token=8831f633-30c2-4849-bb58-c019dc0bd7c7" alt=""><figcaption></figcaption></figure>

* If we look at the URL, we can see that our input has been set as a URL parameter.
* DOM-based XSS vulnerabilities usually arise when JavaScript takes data from an attacker-controllable source, such as the URL, and passes it to a sink that supports dynamic code execution.
* Let's change the URL to the following:

```
10.0.4.5/DVWA/vulnerabilities/xss_d/?default=<script>alert();</script>
```

<figure><img src="https://1586847736-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtSZ40gLWhBDTzPEgHsVB%2Fuploads%2Fd1dIaFknIEam6O53QxLz%2F2.png?alt=media&#x26;token=93895306-0849-4e93-b2f6-488172dd2be0" alt=""><figcaption></figcaption></figure>

##

## Security Level: Medium

> The developer has tried to add a simple pattern matching to remove any references to "\<script" to disable any JavaScript. Find a way to run JavaScript without using the script tags. Spoiler: You must first break out of the select block then you can add an image with an onerror event:\
> /vulnerabilities/xss\_d/?default=English>/option>.

* Let's check the source code.&#x20;

<figure><img src="https://1586847736-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtSZ40gLWhBDTzPEgHsVB%2Fuploads%2FLprlQmFDm8IXSYKShvxt%2F3.png?alt=media&#x26;token=0378bfdb-21b4-4bab-b8bc-a5b091f768e0" alt=""><figcaption></figcaption></figure>

* So our input is being stripped of `<script` tags.
* Let's inspect the code in the web page as well.&#x20;

<figure><img src="https://1586847736-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtSZ40gLWhBDTzPEgHsVB%2Fuploads%2FYJ3JpvhiCUxAXMxZe1Hd%2F4.png?alt=media&#x26;token=8064c0a6-035a-4f4c-bc57-49580003010f" alt=""><figcaption></figcaption></figure>

* We can see that we first need to escape the `<select>` tag that we are in.
* Once we have done that we can use the `img onerror` attribute to trigger an alert.

```
10.0.4.5/DVWA/vulnerabilities/xss_d/?default=</select><img src=1 onerror=alert(document.cookie)>
```

<figure><img src="https://1586847736-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtSZ40gLWhBDTzPEgHsVB%2Fuploads%2FPlBlqt0Q1Pz8fOmwpgfW%2F5.png?alt=media&#x26;token=cd39f58e-3c55-40cf-a2e6-49fcf0ae46b3" alt=""><figcaption></figcaption></figure>

##

## Security Level: High

> The developer is now white listing only the allowed languages, you must find a way to run your code without it going to the server. Spoiler: The fragment section of a URL (anything after the # symbol) does not get sent to the server and so cannot be blocked. The bad JavaScript being used to render the page reads the content from it when creating the page.\
> /vulnerabilities/xss\_d/?default=English#alert(1).

* Let's check the source code first.

<figure><img src="https://1586847736-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtSZ40gLWhBDTzPEgHsVB%2Fuploads%2FMNYd7YAxQlaHay6pDW6O%2F6.png?alt=media&#x26;token=48f957c5-4346-44cb-9538-e014cf7c6e0f" alt=""><figcaption></figcaption></figure>

* In this case we can use the `#` character so that our URI is fragmented and it satisfies the checks.

```
10.0.4.5/DVWA/vulnerabilities/xss_d/#?default=<script>alert(document.cookie);</script>
```

<figure><img src="https://1586847736-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FtSZ40gLWhBDTzPEgHsVB%2Fuploads%2F0pLBoFajqgGroCTUdUgf%2F7.png?alt=media&#x26;token=1928c1ad-f522-4694-aa06-dd26ca57526f" alt=""><figcaption></figcaption></figure>
