> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/web-exploitation/secret-box.md).

# Secret Box

While reviewing the source code, we come across a query responsible for storing user secrets:

```
INSERT INTO secrets(owner_id, content) VALUES ('${userId}', '${content}')
```

At first glance, this looks normal, but the issue lies in how the `content` parameter is handled. The user input is directly embedded into the SQL query without any sanitization or parameterization. This opens the door for SQL Injection, as we can break out of the intended query structure and inject our own SQL.

Instead of submitting normal text in the secret field, we craft a payload designed to execute a subquery:

```
'|| (SELECT content FROM secrets LIMIT 1) ||'
```

Under normal conditions, the query would execute like this:

```
VALUES ('user_id', 'input')
```

However, after injecting our payload, the query transforms into:

```
VALUES ('user_id', ''|| (SELECT content FROM secrets LIMIT 1) ||'')
```

Here, the application concatenates the result of the subquery into the `content` field. Instead of storing our input, the database executes the injected SQL and fetches the first entry from the `secrets` table.

As a result, we successfully retrieve sensitive data from the database, which most likely belongs to an admin or another user. This confirms the presence of a SQL Injection vulnerability caused by unsafe handling of user input.

<img src="https://github.com/user-attachments/assets/a6bd0b40-80ce-4b8d-9e4f-64efd2a15b04" alt="image" height="418" width="1191">


---

# 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://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/web-exploitation/secret-box.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.
