> 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/web/websockets/websocket-handshake-manipulation.md).

# WebSocket Handshake Manipulation

WebSocket Handshake Manipulation notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### Manipulating the WebSocket Handshake to Exploit Vulnerabilities

This online shop has real-time chat functionality based on **WebSockets**. / An XSS filter is present: it is aggressive but has exploitable faults.

A chat field allows you to send messages that are displayed in real time on the support agent side.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7e167899ebc6f6893c92a84b8deca11ad45207f5%2F%7BBD656235-BB53-498B-9FDC-C64F9A2A7784%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Initial XSS Filter Test

When sending the following payload into the chat:

```javascript
<img src=0 onerror=alert(0)>
```

The server responds with the message:

* **Attack detected: Event handler**
* WebSocket connection is immediately **closed**.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-be096d21e9bfaabb72aa5560a09d24d9cec94b57%2F%7B880823DF-F5A4-4802-9978-933D5A8B289D%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

After simply reloading the page, the following message appears:

* **This address is blacklisted**

The IP address is therefore temporarily blocked.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-2e33bd956aec874d584e04f7ace1a80422569d98%2F%7B2816FFE2-3D7D-4A5E-B5D4-067DBDF33F7A%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Contouring Blacklisting

Adding the following HTTP header when establishing the WebSocket handshake:

```http
X-Forwarded-For: 0.0.0.0
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-2fb005e0471d574c87fc426d0dee90538d45a100%2F%7B945882E2-E46A-4509-9A6D-D108717481CB%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

The server again accepts the connection. / To avoid having to add manually each time, this header is configured **permanently in the proxy**.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-952feb57fb13e416a58916a89b9fa5fa47a45a64%2F%7BF1745B76-CF31-4A2D-A7F3-676136BE4FD5%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### XSS Filter Contouring

Once the connection is restored, the payload is slightly modified to bypass the filter:

```javascript
<img src=0 OnErrOR=alert`0`>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-fc0cb7c6ea20369b6bb250a51f13b799dc90ba1f%2F%7B0C6DB5F8-6777-4263-AAFA-086A9424FF9B%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

This change of breakage and syntax allows to bypass the detection.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-516c9a384c0d6a8043d8bc11a7c924d6e6e825de%2F%7B46B6F48D-4B77-4AAC-A4B4-F12D6FD070A6%7D.png?alt=media" alt=""><figcaption></figcaption></figure>


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/web/websockets/websocket-handshake-manipulation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
