# PS - DomXSS

## Lab: DOM XSS in document.write sink using source location.search

* [Lab on PortSwigger](https://portswigger.net/web-security/cross-site-scripting/dom-based/lab-document-write-sink)

### Instructions

![instruction](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-5b9863f76c9ca4141c864b9594dca366081f57a0%2F2023-02-06-08-22-58.png?alt=media)

* We click on access the lab

### Resolution

* We land on this page

![landing](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-51f59ebc871b7c09f720c9a2e28b5624ecf1d497%2F2023-02-06-07-35-07.png?alt=media)

* It mentions a location.search source in the title so the search box is definitely where we should try our injection.
* Here is the js code for the search

```js
function trackSearch(query) {
    document.write('<img src="/resources/images/tracker.gif?searchTerms='+query+'">');
}
var query = (new URLSearchParams(window.location.search)).get('search');
if(query) {
    trackSearch(query);
}
```

* We can see that it is taking our input and it will be used in the img tag.
* Indeed if we type test for example, we can then if we inspect the element see that our string is in the src of the image tag

![in img tag](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-3fc0edd0b71aa34109059a2e864c2a13383473ee%2F2023-02-06-10-26-33.png?alt=media)

* So to get an alert we can try to close the img tag and put another tag with an alert in it let's try this `"><script>alert(1)</script>`

![alert](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-45787ada4e949f1ffb1e81983d59f13cf55c2628%2F2023-02-06-10-30-38.png?alt=media)

* And it works!

![Solved](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-c47e4b642ce9c4edf0dc8e0e95e51942cf697e93%2F2023-02-06-10-32-08.png?alt=media)
