Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| modul:m183:learningunits:lu10:lu10a [2025/12/27 19:07] – angelegt dgaravaldi | modul:m183:learningunits:lu10:lu10a [2026/01/28 16:30] (aktuell) – dgaravaldi | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== LU10a - Cross-Site Scripting | + | ====== LU10a - Cross-Site Scripting ====== |
| + | < | ||
| + | \\ | ||
| + | ===== Einleitung ===== | ||
| + | Cross-Site Scripting (kurz XSS) ist eine Form der Cyberattacke, | ||
| - | **Cross-site scripting (XSS)** is a very common type of web application attack vector | + | Bei XSS wird Script-Code von außen |
| - | A successful XSS attack can cause serious damage — including compromised user accounts, activation of Trojan code, manipulation of page content to trick users into sharing sensitive data, or exposure of session cookies that allow attackers to impersonate valid users. | + | \\ |
| + | ===== Beispiel ===== | ||
| + | Ein kleines Beispiel soll dies untermauern. Stellen Sie sich eine simple Gästebuch-Anwendung vor, wie Sie sie in diesem Buch öfters finden. Hier zunächst eine Gästebuch-Datenbank (mit MySQL). | ||
| - | ===== Types of XSS Attacks ===== | + | {{: |
| - | There are **two major types** of cross-site scripting attacks commonly discussed: | + | Was passiert aber, wenn Sie HTML-Code eingeben? Dieser Code wird dann ungefiltert ausgegeben. Sie können das Layout des Gästebuches verschandeln, |
| - | * **Stored XSS (Persistent)** – A malicious script is permanently injected into an application and served to all users. | + | {{: |
| - | * **Reflected XSS (Non-Persistent)** – A malicious script is embedded in a URL or request, then reflected off the web server back to the user’s browser when the link is visited. | + | |
| + | {{: | ||
| + | Das allein ist ja schon schlimm genug, doch noch übler wird es, wenn statt HTML-Code JavaScript-Code eingeschleust wird. Da gibt es verschiedene Stufen der Grausamkeiten | ||
| + | * Öffnen von modalen Warnfenstern mit '' | ||
| + | * unendliches Neuladen der Seite mit '' | ||
| + | * die Umleitung des Benutzers mit '' | ||
| + | * das Auslesen aller Cookies, beispielsweise mit '' | ||
| - | ===== What is Stored Cross-Site Scripting ===== | + | Aus guten Gründen wird dies nicht weiter ausgeführt, |
| + | {{: | ||
| - | To execute a **stored XSS attack**, the attacker must find a vulnerability in a web application where user input is stored without proper validation or escaping. A common example is when a comment field or form accepts HTML input and embeds it directly into pages viewed by other users. | + | {{: |
| - | {{stored_xss_example.png? | ||
| - | **Example**: | + | \\ |
| + | ===== XSS-Auditor ===== | ||
| + | Die Gefahr für '' | ||
| + | {{: | ||
| - | ===== Stored XSS Attack — Step by Step ===== | + | \\ |
| - | 1. The attacker discovers a page with an input field that allows HTML. | + | ===== Gegenmassnahmen |
| - | 2. They insert malicious JavaScript code into that field. | + | Wirkungsvoll gegen XSS ist |
| - | 3. The application stores and later serves that code as part of normal content. | + | * gründliche Input-Validierung |
| - | 4. When other users visit the page, their browser executes the attack script. | + | * Output-Escaping (in PHP mit der Funktion '' |
| + | \\ | ||
| + | ===== Output-Escaping ===== | ||
| + | Ziel des Output-Escaping ist, dass im Webbrowser die Benutzereingaben niemals als Teil der Seite (d.h. mit deren HTML-Code) ausgeführt werden. Stattdessen werden die Benutzereingaben sozusagen entschärft und wie gewöhnliche Texte als ungefährlicher statischer Inhalt angezeigt. | ||
| + | In PHP wird h | ||
| - | ===== How Stored XSS Endangers Users ===== | + | \\ |
| - | Stored XSS is especially dangerous because: | + | ===== Beispiel |
| + | Im folgenden Beispiel wird die eigene Funktion '' | ||
| - | * It can impact | + | **Code-01: ohne Escaping -> unsicherer PHP-Code** |
| - | * Attacker-controlled scripts can steal session credentials or redirect users to phishing sites. | + | < |
| - | * Malicious payloads can embed external JavaScript that reports user data back to the attacker. | + | ... |
| + | /** | ||
| + | * Liest ein einfaches Textfeld aus dem Formular aus | ||
| + | * @param $feld - Datenfeld, welches ausgelesen wird | ||
| + | | ||
| + | */ | ||
| + | function leseFeld($feld) | ||
| + | { | ||
| + | if (!isset($_POST[$feld])) { | ||
| + | return LEER_STRING; | ||
| + | } | ||
| + | if (!is_string($_POST[$feld])) { | ||
| + | return LEER_STRING; | ||
| + | } | ||
| + | return $_POST[$feld]; | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | **Code-02: mit Escaping -> sicherer PHP-Code** | ||
| + | < | ||
| + | ... | ||
| + | /** | ||
| + | * Liest ein einfaches Textfeld aus dem Formular aus | ||
| + | * @param $feld - Datenfeld, welches ausgelesen wird | ||
| + | * @return string - ausgelesenes Datenfeld als String | ||
| + | */ | ||
| + | function leseFeld($feld) | ||
| + | { | ||
| + | if (!isset($_POST[$feld])) { | ||
| + | return LEER_STRING; | ||
| + | } | ||
| + | if (!is_string($_POST[$feld])) { | ||
| + | return LEER_STRING; | ||
| + | } | ||
| + | return htmlspecialchars($_POST[$feld]); | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| - | ===== Related Topics ===== | + | ---- |
| - | [1]: https:// | + | |
| - | [2]: https://www.imperva.com/learn/application-security/ | + | [[https://creativecommons.org/licenses/by-nc-sa/4.0/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] Daniel Garavaldi |
| - | [3]: https://developer.mozilla.org/docs/Web/Security/Attacks/ | + | |