hacktricks/network-services-pentesting/27017-27018-mongodb.md

198 lines
10 KiB
Markdown
Raw Normal View History

2022-04-28 23:27:22 +00:00
# 27017,27018 - Pentesting MongoDB
2022-04-28 16:01:33 +00:00
<details>
2024-02-10 13:03:23 +00:00
<summary><strong>Impara l'hacking di AWS da zero a eroe con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-10 13:03:23 +00:00
Altri modi per supportare HackTricks:
2024-01-03 11:42:55 +01:00
2024-02-10 13:03:23 +00:00
* Se vuoi vedere la tua **azienda pubblicizzata in HackTricks** o **scaricare HackTricks in PDF** Controlla i [**PIANI DI ABBONAMENTO**](https://github.com/sponsors/carlospolop)!
* Ottieni il [**merchandising ufficiale di PEASS & HackTricks**](https://peass.creator-spring.com)
* Scopri [**The PEASS Family**](https://opensea.io/collection/the-peass-family), la nostra collezione di esclusive [**NFT**](https://opensea.io/collection/the-peass-family)
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo Telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Condividi i tuoi trucchi di hacking inviando PR a** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2023-12-04 16:45:05 +01:00
<figure><img src="../../.gitbook/assets/image (1) (3) (1).png" alt=""><figcaption></figcaption></figure>
2023-02-27 10:28:45 +01:00
2024-02-10 13:03:23 +00:00
Unisciti al server [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) per comunicare con hacker esperti e cacciatori di bug bounty!
2023-02-27 10:28:45 +01:00
2024-02-10 13:03:23 +00:00
**Insight sull'hacking**\
Interagisci con contenuti che approfondiscono l'emozione e le sfide dell'hacking
2023-07-14 17:03:41 +02:00
2024-02-10 13:03:23 +00:00
**Notizie sull'hacking in tempo reale**\
Resta aggiornato sul mondo dell'hacking frenetico attraverso notizie e approfondimenti in tempo reale
2023-07-14 17:03:41 +02:00
2024-02-10 13:03:23 +00:00
**Ultime notizie**\
Rimani informato sul lancio delle nuove bug bounty e sugli aggiornamenti cruciali della piattaforma
2022-11-05 10:07:43 +01:00
2024-02-10 13:03:23 +00:00
**Unisciti a noi su** [**Discord**](https://discord.com/invite/N3FrSbmwdy) e inizia a collaborare con i migliori hacker oggi stesso!
2024-02-10 13:03:23 +00:00
## Informazioni di base
2024-02-10 13:03:23 +00:00
**MongoDB** è un **sistema di gestione di database open source** che utilizza un **modello di database orientato ai documenti** per gestire diverse forme di dati. Offre flessibilità e scalabilità per la gestione di dati non strutturati o semi-strutturati in applicazioni come l'analisi dei big data e la gestione dei contenuti.
**Porta predefinita:** 27017, 27018
```
PORT STATE SERVICE VERSION
27017/tcp open mongodb MongoDB 2.6.9 2.6.9
```
2024-02-10 13:03:23 +00:00
## Enumerazione
2024-02-10 13:03:23 +00:00
### Manuale
```python
from pymongo import MongoClient
client = MongoClient(host, port, username=username, password=password)
client.server_info() #Basic info
#If you have admin access you can obtain more info
admin = client.admin
admin_info = admin.command("serverStatus")
cursor = client.list_databases()
for db in cursor:
2024-02-10 13:03:23 +00:00
print(db)
print(client[db["name"]].list_collection_names())
#If admin access, you could dump the database also
```
2024-02-10 13:03:23 +00:00
**Alcuni comandi MongoDB:**
```plaintext
- `show dbs`: Mostra tutti i database presenti nel server MongoDB.
- `use <database>`: Seleziona il database specificato per eseguire le operazioni successive.
- `show collections`: Mostra tutte le collezioni presenti nel database selezionato.
- `db.<collection>.find()`: Restituisce tutti i documenti presenti nella collezione specificata.
- `db.<collection>.findOne()`: Restituisce il primo documento trovato nella collezione specificata.
- `db.<collection>.insertOne(<document>)`: Inserisce un nuovo documento nella collezione specificata.
- `db.<collection>.updateOne(<filter>, <update>)`: Aggiorna il primo documento che soddisfa il filtro specificato nella collezione specificata.
- `db.<collection>.deleteOne(<filter>)`: Elimina il primo documento che soddisfa il filtro specificato nella collezione specificata.
```
2024-02-10 13:03:23 +00:00
Per ulteriori informazioni sui comandi MongoDB, consulta la documentazione ufficiale.
```bash
show dbs
use <db>
show collections
db.<collection>.find() #Dump the collection
db.<collection>.count() #Number of records of the collection
db.current.find({"username":"admin"}) #Find in current db the username admin
```
2024-02-10 13:03:23 +00:00
### Automatico
MongoDB is a NoSQL database that is commonly used in web applications. It runs on port 27017 by default. When performing a network service pentest, it is important to check if MongoDB is running on this port.
#### Checking for MongoDB
To check if MongoDB is running on port 27017, you can use the following command:
```
nmap -p 27017 <target_ip>
```
If MongoDB is running, you will see an open port result.
#### Exploiting MongoDB
2024-02-10 13:03:23 +00:00
Once you have confirmed that MongoDB is running, you can proceed to exploit it. There are several techniques that can be used to exploit MongoDB, including:
2024-02-10 13:03:23 +00:00
1. Default Credentials: MongoDB often has default credentials set, such as username: `admin` and password: `admin`. You can try these credentials to gain unauthorized access.
2. Injection Attacks: MongoDB is vulnerable to injection attacks, similar to SQL injection. You can try injecting malicious code to manipulate the database.
3. Unauthorized Access: MongoDB may have misconfigured access controls, allowing unauthorized users to access the database. You can try accessing the database without proper authentication.
4. File System Access: MongoDB allows users to store files in the database. If file system access is enabled, you can try accessing sensitive files stored in the database.
#### Prevention and Mitigation
To prevent MongoDB exploitation, it is important to follow security best practices, such as:
- Change default credentials and use strong, unique passwords.
- Implement proper access controls and authentication mechanisms.
- Regularly update and patch MongoDB to fix any known vulnerabilities.
- Monitor and log MongoDB activity for any suspicious behavior.
- Encrypt sensitive data stored in MongoDB to protect it from unauthorized access.
By following these practices, you can reduce the risk of MongoDB exploitation and protect your data from unauthorized access.
```bash
nmap -sV --script "mongo* and default" -p 27017 <IP> #By default all the nmap mongo enumerate scripts are used
```
2022-05-01 13:25:53 +00:00
### Shodan
2024-02-10 13:03:23 +00:00
* Tutti i mongodb: `"informazioni sul server mongodb"`
* Cerca i server mongodb completamente aperti: `"informazioni sul server mongodb" -"parzialmente abilitato"`
* Solo abilita parzialmente l'autenticazione: `"informazioni sul server mongodb" "parzialmente abilitato"`
2024-02-10 13:03:23 +00:00
## Accesso
2024-02-10 13:03:23 +00:00
Di default, mongo non richiede una password.\
**Admin** è un database mongo comune.
```bash
mongo <HOST>
mongo <HOST>:<PORT>
mongo <HOST>:<PORT>/<DB>
mongo <database> -u <username> -p '<password>'
```
2024-02-10 13:03:23 +00:00
Lo script nmap: _**mongodb-brute**_ verificherà se sono necessarie le credenziali.
```bash
nmap -n -sV --script mongodb-brute -p 27017 <ip>
```
2024-02-10 13:03:23 +00:00
### [**Forza bruta**](../generic-methodologies-and-resources/brute-force.md#mongo)
2024-02-10 13:03:23 +00:00
Guarda dentro _/opt/bitnami/mongodb/mongodb.conf_ per sapere se sono necessarie delle credenziali:
```bash
grep "noauth.*true" /opt/bitnami/mongodb/mongodb.conf | grep -v "^#" #Not needed
grep "auth.*true" /opt/bitnami/mongodb/mongodb.conf | grep -v "^#\|noauth" #Not needed
```
2024-02-10 13:03:23 +00:00
## Previsione degli Object ID di Mongo
2024-02-10 13:03:23 +00:00
Esempio [da qui](https://techkranti.com/idor-through-mongodb-object-ids-prediction/).
2024-02-08 22:36:15 +01:00
2024-02-10 13:03:23 +00:00
Gli Object ID di Mongo sono stringhe esadecimali di **12 byte**:
2024-02-08 22:36:15 +01:00
![http://techidiocy.com/_id-objectid-in-mongodb/](../.gitbook/assets/id-and-objectids-in-mongodb.png)
2024-02-10 13:03:23 +00:00
Ad esempio, ecco come possiamo analizzare un vero Object ID restituito da un'applicazione: 5f2459ac9fa6dc2500314019
2024-02-10 13:03:23 +00:00
1. 5f2459ac: 1596217772 in decimale = venerdì 31 luglio 2020 17:49:32
2. 9fa6dc: Identificatore della macchina
3. 2500: ID del processo
4. 314019: Un contatore incrementale
2024-02-10 13:03:23 +00:00
Degli elementi sopra, l'identificatore della macchina rimarrà lo stesso finché il database eseguirà la stessa macchina fisica/virtuale. L'ID del processo cambierà solo se il processo di MongoDB viene riavviato. Il timestamp verrà aggiornato ogni secondo. L'unico problema nel tentativo di indovinare gli Object ID semplicemente incrementando il contatore e i valori del timestamp, è il fatto che Mongo DB genera gli Object ID e assegna gli Object ID a livello di sistema.
2024-02-10 13:03:23 +00:00
Lo strumento [https://github.com/andresriancho/mongo-objectid-predict](https://github.com/andresriancho/mongo-objectid-predict), dato un Object ID di partenza (puoi creare un account e ottenere un ID di partenza), restituisce circa 1000 Object ID probabili che potrebbero essere stati assegnati agli oggetti successivi, quindi devi solo forzarli.
2022-05-01 13:25:53 +00:00
## Post
2024-02-10 13:03:23 +00:00
Se sei root puoi **modificare** il file **mongodb.conf** in modo che non siano necessarie le credenziali (_noauth = true_) e **effettuare il login senza credenziali**.
2022-04-28 16:01:33 +00:00
2024-02-08 22:36:15 +01:00
***
2023-12-04 16:45:05 +01:00
<figure><img src="../../.gitbook/assets/image (1) (3) (1).png" alt=""><figcaption></figcaption></figure>
2023-07-14 17:03:41 +02:00
2024-02-10 13:03:23 +00:00
Unisciti al server [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) per comunicare con hacker esperti e cacciatori di bug!
2023-03-05 19:54:13 +00:00
2023-12-04 16:45:05 +01:00
**Hacking Insights**\
2024-02-10 13:03:23 +00:00
Interagisci con contenuti che approfondiscono l'emozione e le sfide dell'hacking
2023-02-27 10:28:45 +01:00
2024-02-10 13:03:23 +00:00
**Notizie sull'hacking in tempo reale**\
Resta aggiornato sul mondo dell'hacking frenetico attraverso notizie e approfondimenti in tempo reale
2023-02-27 10:28:45 +01:00
2024-02-10 13:03:23 +00:00
**Ultime notizie**\
Rimani informato sui nuovi bug bounty in lancio e sugli aggiornamenti cruciali della piattaforma
2023-02-27 10:28:45 +01:00
2024-02-10 13:03:23 +00:00
**Unisciti a noi su** [**Discord**](https://discord.com/invite/N3FrSbmwdy) e inizia a collaborare con i migliori hacker oggi stesso!
2022-11-05 10:07:43 +01:00
2022-04-28 16:01:33 +00:00
<details>
2024-02-10 13:03:23 +00:00
<summary><strong>Impara l'hacking di AWS da zero a eroe con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-10 13:03:23 +00:00
Altri modi per supportare HackTricks:
2024-01-03 11:42:55 +01:00
2024-02-10 13:03:23 +00:00
* Se vuoi vedere la tua **azienda pubblicizzata in HackTricks** o **scaricare HackTricks in PDF** Controlla i [**PACCHETTI DI ABBONAMENTO**](https://github.com/sponsors/carlospolop)!
* Ottieni il [**merchandising ufficiale di PEASS & HackTricks**](https://peass.creator-spring.com)
* Scopri [**The PEASS Family**](https://opensea.io/collection/the-peass-family), la nostra collezione di esclusive [**NFT**](https://opensea.io/collection/the-peass-family)
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo Telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Condividi i tuoi trucchi di hacking inviando PR ai** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>