root@sparrow:~/writeups$ cat mongodb.md
// web
MongoDB
22 Jul 2026
· 2 min read
MongoDB is a document-oriented NoSQL database (databases → collections → BSON/JSON documents, instead of tables and rows). Very relevant in pentesting because it often ships without authentication enabled and listens on port 27017/tcp, allowing full database reads without a password.
| Type | CLI |
| Default port | 27017 |

Connecting
| Client | When to use it |
|---|---|
mongo <IP>:27017 | Legacy client — recommended for old machines (supports MongoDB 3.x, typical on HTB) |
mongosh <IP>:27017 | Modern client — does NOT work with MongoDB 3.x (wire protocol version error) |
mongo | Local connection, no host |
Basic shell commands
show dbs // list databases
use <db_name> // switch database
show collections // list collections (tables)
db.<collection>.find() // view documents
db.<collection>.find().pretty() // view documents formatted
db.<collection>.count() // count documents
db.<collection>.find({ "field": "value" }) // filtered search
Advanced commands
db // current database
db.stats() // DB statistics
db.serverStatus() // server info
db.getCollectionInfos() // collections with stats
Dump / export
mongoexport --host <IP> --db <db> --collection <collection> --out file.json
mongodump --host <IP> # saved into dump/
Standard HTB procedure
nmap -sC -sV -p- <IP>
# identify MongoDB on 27017
mongo <IP>:27017
show dbs
use sensitive_information
show collections
db.flag.find()
🔥 This path solves 90% of MongoDB machines on HTB.
Common errors
| Error | Fix |
|---|---|
| "mongo: command not found" | The client isn't installed — use manually downloaded binaries |
| "mongosh requires wire version 7" | The server is too old (3.x) — use legacy mongo, NOT mongosh |
| "Authentication failed" | The server has auth enabled — you need valid credentials |
| "Connection refused" | MongoDB isn't listening or is filtered — check with nmap -p27017 <IP> |
Responsible use
These commands are for systems you own or have explicit written authorisation to test (labs, CTFs, contracted engagements). Using them against third-party infrastructure without permission is illegal in most countries.