Skip to content

Instantly share code, notes, and snippets.

@ihebski
Last active September 22, 2020 18:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihebski/c499c7346a7bd71157aa555bc8309f54 to your computer and use it in GitHub Desktop.
Save ihebski/c499c7346a7bd71157aa555bc8309f54 to your computer and use it in GitHub Desktop.
store subdomains into sqlite db
#!/usr/bin/env python3
# @ih3bski
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import uuid
from loguru import logger
import sys
app = Flask(__name__)
# Change the default db path
dbname = "bb_record"
dbpath = "/tmp"
# SQLAlchemy config/Disable warnings for a clean output $_$
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{dbpath}/{dbname}.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
stats = logger.level("STATS", no=38, color="<yellow>", icon="📈")
Counter = 0
# Model -> T.B changed
class Target(db.Model):
__tablename__ = 'Target'
id = db.Column(db.Text, primary_key=True)
subdomain = db.Column(db.Text)
def __init__(self,id,subdomain):
self.subdomain = subdomain
self.id = id
db.create_all()
def save(subdomain):
global Counter
if db.session.query(Target.id).filter_by(subdomain=subdomain).scalar() is None :
db.session.add(Target(str(uuid.uuid4()),subdomain))
db.session.commit()
Counter += 1
logger.log('INFO',f'[+] {subdomain} added to database')
else:
logger.log('ERROR',f'[-] {subdomain} already exists')
def search(subdomain):
sub = db.session.query(Target).filter(Target.subdomain.like(f"%{subdomain}%")).all()
if sub :
for row in sub:
logger.log('INFO',row.subdomain)
logger.log("STATS", f'We have found {len(sub)} subdomains ! Happy Hacking $_$')
else:
logger.log('WARNING',f'[-] {subdomain} NOT FOUND.')
def main():
# use stdin if it's full
if not sys.stdin.isatty():
for subdomains in sys.stdin:
save(subdomains.strip())
logger.log("STATS", f'{Counter} Bulk added ^_^ !')
# read subdomain as argument
else:
if len(sys.argv) > 1:
search(sys.argv[1])
else:
print("Usage:\n Store new subdomains : cat targets | python3 db.py \n Search subdomains: python3 db.py <domain>")
if __name__ == '__main__':
main()
@ihebski
Copy link
Author

ihebski commented Sep 20, 2020

overview

Mini utility to store the list of the enumerated subdomains into an sqlite3 db.

Prod

Install Requirements

sudo pip3 install Flask flask_sqlalchemy loguru uuid

Add it as a bash command

cp db /usr/bin
chmod +x /usr/bin/db

Usage

  • Add list of subdomains to database

cat subdomains.txt | db

or

echo example.com | db

  • Search for domain

db <domain.com>

@remonsec
Copy link

remonsec commented Sep 21, 2020

That's amazing bro. Thanks a lot for your contribution to the community
is there any way to output the result of db

as example

db site.com -o site.txt

@ihebski
Copy link
Author

ihebski commented Sep 21, 2020

Welcome 👍 thrilled to hear that, you can use tee

db site.com 2>&1 | tee -a db-results.txt

For a cleaner results, to extract subdomains only without the debug output

db site.com 2>&1 | cut -d' ' -f 12 | tee -a db-results.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment