Fixed sqlite3 dependency. If sqlite3 module doesnt exist, logger will downgrade deduplication to file search.

This commit is contained in:
Gifts 2017-01-12 16:43:40 +03:00
commit a20452128c

View file

@ -45,8 +45,8 @@ def HTTPCurrentDate():
try: try:
import sqlite3 import sqlite3
except: except:
print "[!] Please install python-sqlite3 extension." sqlite3 = False
sys.exit(0) print "[!] Please install python-sqlite3 extension. Logging to database will be unavailable"
def color(txt, code = 1, modifier = 0): def color(txt, code = 1, modifier = 0):
if txt.startswith('[*]'): if txt.startswith('[*]'):
@ -159,6 +159,7 @@ def DumpConfig(outfile, data):
def SaveToDb(result): def SaveToDb(result):
# Creating the DB if it doesn't exist # Creating the DB if it doesn't exist
if sqlite3:
if not os.path.exists(settings.Config.DatabaseFile): if not os.path.exists(settings.Config.DatabaseFile):
cursor = sqlite3.connect(settings.Config.DatabaseFile) cursor = sqlite3.connect(settings.Config.DatabaseFile)
cursor.execute('CREATE TABLE responder (timestamp varchar(32), module varchar(16), type varchar(16), client varchar(32), hostname varchar(32), user varchar(32), cleartext varchar(128), hash varchar(512), fullhash varchar(512))') cursor.execute('CREATE TABLE responder (timestamp varchar(32), module varchar(16), type varchar(16), client varchar(32), hostname varchar(32), user varchar(32), cleartext varchar(128), hash varchar(512), fullhash varchar(512))')
@ -179,10 +180,20 @@ def SaveToDb(result):
logfile = os.path.join(settings.Config.ResponderPATH, 'logs', fname) logfile = os.path.join(settings.Config.ResponderPATH, 'logs', fname)
if sqlite3:
cursor = sqlite3.connect(settings.Config.DatabaseFile) cursor = sqlite3.connect(settings.Config.DatabaseFile)
cursor.text_factory = sqlite3.Binary # We add a text factory to support different charsets cursor.text_factory = sqlite3.Binary # We add a text factory to support different charsets
res = cursor.execute("SELECT COUNT(*) AS count FROM responder WHERE module=? AND type=? AND client=? AND LOWER(user)=LOWER(?)", (result['module'], result['type'], result['client'], result['user'])) res = cursor.execute("SELECT COUNT(*) AS count FROM responder WHERE module=? AND type=? AND client=? AND LOWER(user)=LOWER(?)", (result['module'], result['type'], result['client'], result['user']))
(count,) = res.fetchone() (count,) = res.fetchone()
else:
logf = open(logfile, 'rb')
data = logf.read()
logf.close()
# What could possibly go wrong. Checking existence of hash in respective log file
user_to_find = result['user'].encode('utf8', 'replace').split('\\', 1)
user_to_find = '%s::%s' % (user_to_find[1], user_to_find[0]) # Username::domain
count = len(re.findall('(?msi)^' + re.escape(user_to_find), data))
if not count: if not count:
outf = open(logfile,"a") outf = open(logfile,"a")
@ -192,6 +203,7 @@ def SaveToDb(result):
outf.write(result['fullhash'].encode('utf8', 'replace') + '\n') outf.write(result['fullhash'].encode('utf8', 'replace') + '\n')
outf.close() outf.close()
if sqlite3:
cursor.execute("INSERT INTO responder VALUES(datetime('now'), ?, ?, ?, ?, ?, ?, ?, ?)", (result['module'], result['type'], result['client'], result['hostname'], result['user'], result['cleartext'], result['hash'], result['fullhash'])) cursor.execute("INSERT INTO responder VALUES(datetime('now'), ?, ?, ?, ?, ?, ?, ?, ?)", (result['module'], result['type'], result['client'], result['hostname'], result['user'], result['cleartext'], result['hash'], result['fullhash']))
cursor.commit() cursor.commit()
@ -231,8 +243,10 @@ def SaveToDb(result):
else: else:
print color('[*] Skipping previously captured hash for %s' % result['user'], 3, 1) print color('[*] Skipping previously captured hash for %s' % result['user'], 3, 1)
text('[*] Skipping previously captured hash for %s' % result['user']) text('[*] Skipping previously captured hash for %s' % result['user'])
if sqlite3:
cursor.execute("UPDATE responder SET timestamp=datetime('now') WHERE user=? AND client=?", (result['user'], result['client'])) cursor.execute("UPDATE responder SET timestamp=datetime('now') WHERE user=? AND client=?", (result['user'], result['client']))
cursor.commit() cursor.commit()
if sqlite3:
cursor.close() cursor.close()