BOOK THIS SPACE FOR AD
ARTICLE ADHellow world!
My name is Ali, I am a web application security researcher/enthusiast and today I will be writing my very first blog post in here.
I am also interested in bug bounty hunting and I’ve been following the infosec community on medium for a long time. I have learned a ton here and that’s why I decided to share something back to the community.
If you are also new to bug bounty, you probably too experienced the confusion and pain of not knowing on how to store your recon data properly.
I sure did, and still do to some extent. So my goal here is to figure out a way to solve that issue and share it here with you. For this I will be setting a MongoDB which holds all the recon data (for now it will be only the scope, subdomains, status codes, and update time) and we’ll use Flask and REST Api to be able to push/fetch data easily to our db.
This is by no means a step-by-step guide (but it could be I guess), I am just going to document/journal what I did. I hope you will find it useful.
NOTE: I will be using “booking.com” (wildcard) which is public program on HackerOne to show case the data in my recon database.
After creating a MongoDB cluster in Atalas (https://www.mongodb.com/) I did the following:
Created a database named assets and the first collection booking
Created an admin user with a password, gave it an atlasAdmin role for highest permission
Under the “Network Access” added IP address of “0.0.0.0/0” so that I can access my db from any IP
Connecting to your database
Using Python:
from pymongo.mongo_client import MongoClienturi = "mongodb+srv://admin:<password>@<cluster-name>.fctxhkg.mongodb.net/?retryWrites=true&w=majority"
# Create a new client and connect to the server
client = MongoClient(uri)
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)p
Using Mongo Shell:
mongosh "mongodb+srv://<cluster-name>.fctxhkg.mongodb.net/" --apiVersion 1 --username adminUsing Compass:
mongodb+srv://admin:<password>@<cluster-name>.fctxhkg.mongodb.net/Adding some dummy records to the DB
For start I just wanted to get comfortable with queries so I started with a simple list:
x = [{"subdomain": "www.booking.com",
"org": "booking.com",
"alive": True,
"status": 200,
"updated": ""
},
{
"subdomain": "news.booking.com",
"org": "booking.com",
"alive": True,
"status": 200,
"updated": ""
},
{
"subdomain": "sx2.booking.com",
"org": "booking.com",
"alive": True,
"status": 403,
"updated": ""}
]
The logic is, let’s say for example I’m running a tool like subfinder or amass and there’s an output text file with a list of subs. I wanted to see how I can insert those subs into a separate document each, with specific additional keys.
For now, for the sake of simplicity I will just go with the following keys: subdomain (domain address), org (organization/scope), alive(is the host up?), status (http status code), updated (last updated date/time).
For the list above you can insert it into a connection in 2 ways:
insert_one()
for i in x:db.booking.insert_one(x)
insert_many()
db.booking.insert_many(x)an answer like this means the operation was successful:
InsertManyResult([ObjectId('65b81dff39bcccfcf23d1e6c'), ObjectId('65b81dff39bcccfcf23d1e6d'), ObjectId('65b81dff39bcccfcf23d1e6e')], acknowledged=True)Don’t forget to define your db and collection before anything else:
db = client['assets'] #'client' is defiend earlier in the connection stringcollection = db['booking'] # and 'booking' is obviously the collection name
Next step for me is to figure out a way to insert a huge text file containing a list of subdomains, and only subdomains (no org name, status code, or anything really) into the collection with appropriate keys and pairs. But how…?
NOTE: I guess for now I will just forget about status code and come back to fix this later.
ChatGPT to the rescue:
I have a text file containing a list of subdomains in each separate line. How can I insert that text file into a python list that can be used to insert into a mongo db database?
The code needed a little adjustment but we’re fine.
# Function to read subdomains from a text file and return as a listdef read_subdomains_from_file(filename):
subdomains = []
with open(filename, 'r') as file:
for line in file:
subdomains.append(line.strip()) # Remove newline characters and append to the list
return subdomains
filename = "C:\\Users\\1337\\Desktop\db\\sample.txt"
subdomains_list = read_subdomains_from_file(filename)
for subdomain in subdomains_list:
print(subdomain)
#collection.insert_one({"subdomain": subdomain})
I commented out collection.insert_one({"subdomain": subdomain}) and added print to see the output and it is what I wanted. Now the question is, how can I add “org”, “status”, “update”, etc to the list.
hmm…
After trying a couple of minutes: (no ChatGPT for this one XD)
master_list = []for subdomain in subdomains_list:
x = {}
x['subdomain'] = subdomain
x['org'] = 'booking.com'
x['status'] = ''
x['update'] = time.time()
master_list.append(x)
break
master_list output looks like this:
[{'subdomain': 'wukong-res.booking.com','org': 'booking.com',
'status': '',
'update': 1706567868.2216318}]
→ the update time needs a little work, but it will do for now
and finally:
db.booking.insert_many(master_list)Next step I guess would be to make sure that the subdomain key is also unique, in order to avoid adding the same subdomain into the collection over and over again later in our automation.
Adding the following line will take care of that:
collection_name.create_index([("subdomain", pymongo.ASCENDING)], unique=True)Now if I try to reinsert an existing sub into the collection I will get an error:
#wukong-res.booking.com already existscollection_name.insert_one({"subdomain": "wukong-res.booking.com"})
#duplicate error
DuplicateKeyError: E11000 duplicate key error collection: assets.booking index: subdomain_1 dup key: { subdomain: "wukong-res.booking.com" }, full error: {'index': 0, 'code': 11000, 'errmsg': 'E11000 duplicate key error collection: assets.booking index: subdomain_1 dup key: { subdomain: "wukong-res.booking.com" }', 'keyPattern': {'subdomain': 1}, 'keyValue': {'subdomain': 'wukong-res.booking.com'}}
Nice!
If you read until here, thank you very much for following along. That’s it for part one.
Stay tuned for the second part.