def migrate(): """Update the database to the current schema version""" is_curr = False db_ver = 0 with gfyp_db.DatabaseConnection() as db_con: is_curr = db_con.is_db_current() db_ver = db_con.get_version() if not is_curr: dst = "db.bak.%s" % str(datetime.now()) msg = "Updated database to most recent version - Existing database stored as: %s" % dst shutil.move("db.db", dst) build() if db_ver == 0: # Case db_ver == 0: Needs to be modified to account for UNIQUE monitor domains with gfyp_db.DatabaseConnection() as db_con: with gfyp_db.DatabaseConnection(filename=dst) as old_db_con: existing_watch_entries = old_db_con.get_watch_entries() for entry in existing_watch_entries: db_con.add_watch_entry(entry[1], entry[0]) existing_found_entries = old_db_con.get_all_found_domains() entries_iter = existing_found_entries.fetchall() for entry in entries_iter: db_con.add_discovered_domain(entry[0], entry[1]) else: msg = "Database is currently at the most recent schema version. No changes necessary." print(msg) log(msg, logging.INFO)
def main(): """Description: Search for new domain variants and email alerts for new ones. """ args = get_args() #Get configuration from env variables or fallback to hard-coded values smtp_auth = dict() smtp_auth['server'] = EMAIL_SMTPSERVER smtp_auth['from'] = EMAIL_FROM for key, value in smtp_auth.iteritems(): if value is None: msg = "Fatal error: Email setting '%s' has not been set." % key log(msg, logging.ERROR) sys.exit(msg) with gfyp_db.DatabaseConnection() as db_con: domain_entries = db_con.get_watch_entries() if len(domain_entries) == 0: msg = ("No domains have been added for watching/alerts. Use " "util.py to add domains.") print msg log(msg) for row in domain_entries: alert_email = row[0] domain = row[1] check_and_send_alert(smtp_auth, alert_email, domain, escape_alert=args['escape_alert'], db_con=db_con)
def build(): """Create tables.""" with gfyp_db.DatabaseConnection() as db_con: is_err = db_con.table_init() err_msg = ", but with errors" msg = "Database is initalized%s." % (err_msg if is_err else '') print msg log_level = logging.ERROR if is_err else logging.INFO log(msg, log_level)
def dump(): """Write database to CSV file.""" filename = sys.argv[2] with gfyp_db.DatabaseConnection() as db_con: with open(filename, 'wb') as csvfile: csvoutput = csv.writer(csvfile) found_entries = db_con.get_all_found_domains() entries_iter = found_entries.fetchall() for entry in entries_iter: csvoutput.writerow(entry) print "Wrote %d entries to '%s'." % (len(entries_iter), filename)
def check_and_send_alert(smtp_auth, alert_email, domain, escape_alert=False, db_con=None): """Consult DB whether an alert needs to be sent for domain, and send one. Args: smtp_auth (dict): Credentials for SMTP server, including 'username', 'password', and 'server'. alert_email (str) domain (str) escape_alert (bool): Whether or not to escape periods in the email body in order to avoid spam filtering. (Default: False) db_con (None or `gfyp_db.DatabaseConnection`): This can optionally provide a database connection to reuse. Otherwise, a new one will be created. """ msg = "Now checking %s - %s" % (alert_email, domain) print(msg) log(msg) close_db = False if db_con is None: db_con = gfyp_db.DatabaseConnection() close_db = True body = "" dns_check = dnslib() entries = dns_check.checkDomain(domain) msg = "DNSTwist found %d variant domains from %s." % (len(entries), domain) print(msg) log(msg) num_new_entries = 0 for domain_found, domain_info in entries: found_entries = db_con.get_matching_found_domains(domain_found) entries_iter = found_entries.fetchall() if len(entries_iter) == 0: db_con.add_discovered_domain(domain_found, domain_info) body += "\r\n\r\n%s - %s" % (domain_found, domain_info) num_new_entries += 1 if body != "": recipient = alert_email subject = 'GFYP - New Entries for %s' % domain if escape_alert: body = body.replace('.', '[.]') send_email(smtp_auth, recipient, subject, body) msg = "Found %d new domain variants from %s" % (num_new_entries, domain) print(msg) log(msg) if close_db: db_con.conn.close()
def add_domain(): """Inserts a new domain to monitor Todos: * Should not add another record if <domain, email> pair is already present. Can do this by checking in Python or SQL constraint. """ if len(sys.argv) != 4: log("Incorrect number of arguments for adding domain: %s" % sys.argv, logging.ERROR) usage() domain_name = sys.argv[2] email_notif_addr = sys.argv[3] with gfyp_db.DatabaseConnection() as db_con: db_con.add_watch_entry(domain_name, email_notif_addr)
def add_domain(): """Inserts a new domain to monitor""" if len(sys.argv) != 4 and len(sys.argv) != 5: log("Incorrect number of arguments for adding domain: %s" % sys.argv, logging.ERROR) usage() email_notif_addr = sys.argv[3] domain_list = [] domain_list.append(sys.argv[2]) if len(sys.argv) == 5: #Looks like a TLD file is present, add them as well baseName = ((sys.argv[2]).rsplit('.'))[0] with open(sys.argv[4], 'rb') as csvfile: csvreader = csv.reader(csvfile) for tld in csvreader: domain_list.append(baseName + "." + tld[0]) with gfyp_db.DatabaseConnection() as db_con: for domain in domain_list: db_con.add_watch_entry(domain, email_notif_addr)
def main(): """Description: Search for new domain variants and email alerts for new ones. """ args = get_args() #Get configuration from env variables or fallback to hard-coded values smtp_auth = dict() smtp_auth['username'] = os.getenv('GFYP_EMAIL_USERNAME', EMAIL_USERNAME) smtp_auth['password'] = os.getenv('GFYP_EMAIL_PASSWORD', EMAIL_PASSWORD) smtp_auth['server'] = os.getenv('GFYP_EMAIL_SMTPSERVER', EMAIL_SMTPSERVER) for key, value in list(smtp_auth.items()): if value is None: msg = "Fatal error: Email setting '%s' has not been set." % key log(msg, logging.ERROR) sys.exit(msg) if any([EMAIL_USERNAME, EMAIL_PASSWORD, EMAIL_SMTPSERVER]): msg = ("WARNING: You have hard-coded credentials into a code file. Do " "not commit it to a public Git repo!") print(msg) log(msg, logging.WARNING) with gfyp_db.DatabaseConnection() as db_con: if db_con.is_db_current(): domain_entries = db_con.get_watch_entries() if len(domain_entries) == 0: msg = ("No domains have been added for watching/alerts. Use " "util.py to add domains.") print(msg) log(msg) for row in domain_entries: alert_email = row[0] domain = row[1] check_and_send_alert(smtp_auth, alert_email, domain, escape_alert=args['escape_alert'], db_con=db_con) else: msg = "GFYP database is not current. Please run 'python util.py migrate' to update to the current schema" print(msg) log(msg, logging.ERROR)
def remove_entry(): """Removes an identified domain from the list of found entries""" domain_name = sys.argv[2] with gfyp_db.DatabaseConnection() as db_con: db_con.delete_found_domain(domain_name)
def remove_domain(): """Removes a domain from being monitored""" domain_name = sys.argv[2] with gfyp_db.DatabaseConnection() as db_con: db_con.delete_watch_entry(domain_name)
def addnote(): """Add a note for a found domain""" domain_name = sys.argv[2] note = sys.argv[3] with gfyp_db.DatabaseConnection() as db_con: db_con.add_note(domain_name, note)