> For the complete documentation index, see [llms.txt](https://sayonara.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sayonara.gitbook.io/writeups/portswigger/xss/13-stored-dom-xss.md).

# 13) Stored DOM XSS

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Fe5zGe0ceCUa2itoQTJc7%2Fimage.png?alt=media&amp;token=cc2f5cee-4870-4da1-a339-47b4126f3342" alt=""><figcaption></figcaption></figure></div>

#### Locate possible injection points

As usual the first step is to analyse the application, we have a search functionnality so let's search for random string and then open the developer tools and find where the user input is located in the html

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FF1vmEZsUUb3j3KD6fPsu%2Fimage.png?alt=media&amp;token=5694c313-1a20-45ff-823f-e35cc4079071" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Fyx6VasERqmMJneiNGj6X%2Fimage.png?alt=media&amp;token=f434d74a-d4d0-4033-aa84-952192ac5d86" alt=""><figcaption></figcaption></figure></div>

and if we look in the debugger source for javascript files we wil find this

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FOxjwTnUWkRujuXargy1e%2Fimage.png?alt=media&amp;token=9bd2d817-4a34-49c1-9328-b0bd86f50139" alt=""><figcaption></figcaption></figure></div>

so from this we know that the injection will be in the comment field and also we see the protection mechanism (in the escapeHTML() function)

JavaScript `replace()` function to encode angle brackets. However, when the first argument is a string, the function only replaces the first occurrence. We exploit this vulnerability by simply including an extra set of angle brackets at the beginning of the comment. These angle brackets will be encoded, but any subsequent angle brackets will be unaffected, enabling us to effectively bypass the filter and inject HTML.

### <mark style="color:blue;">Exploit</mark>

```
<><img src=x onerror=alert(1)>
```

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FMJrzSVbLJtlKyCP4Yfe5%2Fimage.png?alt=media&amp;token=08ce19e8-0e61-42f2-9dc0-9e695ce0a9f6" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FLWrQ5RVOUCWlBboiUx36%2Fimage.png?alt=media&amp;token=06055cf0-fcb7-4f60-a690-9defaa5a7bb1" alt=""><figcaption></figcaption></figure></div>

and then the alert will pop up once we are back to the blog

and we have solved the lab

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FaXDrLWyn7G3R5m8ZeECi%2Fimage.png?alt=media&amp;token=f1730548-01da-4692-bbd2-f4a46d809e1c" alt=""><figcaption></figcaption></figure></div>
