MOD: finished updating code to support exceptions in scanDBFile

This commit is contained in:
JohnE 2016-02-24 02:09:39 -08:00
parent b06b5ae4c4
commit 572c40ee86
3 changed files with 32 additions and 27 deletions

24
dbfp.py
View File

@ -28,10 +28,9 @@ def compareFingerprint(file_in, file_json):
# #
def createFingerprint(file_in, app_name, app_ver, notes): def createFingerprint(file_in, app_name, app_ver, notes):
print "Reading database file: {}".format(file_in) print "Reading database file: {}".format(file_in)
try:
db = FingerprintDB() db = FingerprintDB()
retVal = db.scanDBFile(file_in) db.scanDBFile(file_in)
if (retVal > 0):
if app_name: if app_name:
db.setAppName(app_name) db.setAppName(app_name)
if app_ver: if app_ver:
@ -40,8 +39,8 @@ def createFingerprint(file_in, app_name, app_ver, notes):
db.setNotes(notes) db.setNotes(notes)
filename = db.writeFingerprint() filename = db.writeFingerprint()
print "Fingerprint generated: {}".format(filename) print "Fingerprint generated: {}".format(filename)
else: except Exception as ex:
print db.getErrorString(retVal) print ex
# #
def indexFingerprints(fp_dir): def indexFingerprints(fp_dir):
@ -59,26 +58,28 @@ def indexFingerprints(fp_dir):
print " {} files skipped (N/A, not a fingerprint file)".format(retVal[2]) print " {} files skipped (N/A, not a fingerprint file)".format(retVal[2])
print print
except Exception as ex: except Exception as ex:
print "ERROR: issue creating the index\n{}\n".format(ex) print "ERROR: issue creating the index\n{}".format(ex)
# #
def compareFingerprintDir(file_in, fp_dir): def compareFingerprintDir(file_in, fp_dir):
try: try:
db = FingerprintDB() db = FingerprintDB()
db.scanDBFile(file_in) db.scanDBFile(file_in)
logging.info("MD5 DB == {}".format(db.getMD5DB()))
logging.info("MD5 TB == {}".format(db.getMD5Tables()))
fp = FingerprintIndex() fp = FingerprintIndex()
fp.openIndex(fp_dir) fp.openIndex(fp_dir)
# search for fingerprints with exact database match # search for fingerprints with exact database match
# fp_ret = fp.findFP(db.getMD5DB()) fp_ret = fp.findFP(db.getMD5DB())
fp_ret = None
if (fp_ret): if (fp_ret):
print "Database matche(s) found" print "Database matche(s) found"
print "RESULTS:" print "RESULTS:"
for fp_list in fp_ret: for fp_list in fp_ret:
for fp in fp_list[1].split(','): for fp in fp_list[1].split(','):
print fp print fp
print
# search for fingerprints with similar tables # search for fingerprints with similar tables
else: else:
logging.info("Searching for md5 tables: {}".format(db.getMD5Tables())) logging.info("Searching for md5 tables: {}".format(db.getMD5Tables()))
@ -124,7 +125,7 @@ def androidPull():
fin_count += count fin_count += count
print "Fingerprints created: {}".format(str(count)) print "Fingerprints created: {}".format(str(count))
print "\nTotal Fingerprints created: {}\n".format(str(fin_count)) print "\nTotal Fingerprints created: {}".format(str(fin_count))
# #
def androidData(data_dir): def androidData(data_dir):
@ -147,7 +148,7 @@ def androidData(data_dir):
count = __createFingerprint(in_dir, out_dir, ddir) count = __createFingerprint(in_dir, out_dir, ddir)
fin_count += count fin_count += count
print "COMPLETED: created {} fingerprints\n".format(str(fin_count)) print "COMPLETED: created {} fingerprints".format(str(fin_count))
# #
def queryMD5(fp_dir, md5_db): def queryMD5(fp_dir, md5_db):
@ -275,3 +276,4 @@ def parseArgs():
if __name__ == "__main__": if __name__ == "__main__":
main() main()
print

View File

@ -6,6 +6,10 @@ class FingerprintWrite(Exception):
"""Error writing the fingerprint to a file""" """Error writing the fingerprint to a file"""
pass pass
class FingerprintReadNoData(Exception):
"""File contains no data"""
pass
class FingerprintIndexOpen(Exception): class FingerprintIndexOpen(Exception):
"""Error opening an index file""" """Error opening an index file"""
pass pass
@ -21,3 +25,4 @@ class FingerprintIndexWrite(Exception):
class FingerprintMD5(Exception): class FingerprintMD5(Exception):
"""Error creating an MD5 sum""" """Error creating an MD5 sum"""
pass pass

View File

@ -8,7 +8,7 @@ import hashlib
import time import time
import logging import logging
from libs.toolbox import ToolBox from libs.toolbox import ToolBox
from libs.exceptions import FingerprintWrite, FingerprintMD5 from libs.exceptions import FingerprintWrite, FingerprintMD5, FingerprintReadNoData
delimeter = "|" delimeter = "|"
@ -62,7 +62,7 @@ class FingerprintDB:
(self.conn, self.cur) = self.__openDB(filein) (self.conn, self.cur) = self.__openDB(filein)
except Exception, ex: except Exception, ex:
logging.info(ex) logging.info(ex)
return -2 raise
try: try:
# extract file name from path+filename # extract file name from path+filename
@ -76,20 +76,16 @@ class FingerprintDB:
self.__createMD5DB() self.__createMD5DB()
except Exception as ex: except Exception as ex:
logging.error(ex) logging.error(ex)
return -3 raise
# create and index of table hashes # create and index of table hashes
self.table_hashes = {} self.table_hashes = {}
print "*****"
print self.tables
for key in self.tables.keys(): for key in self.tables.keys():
print self.tables[key]
self.table_hashes[key] = self.tables[key].hash() self.table_hashes[key] = self.tables[key].hash()
# flag is used to determine if the class has data # flag is used to determine if the class has data
self.init = True self.init = True
self.filein = filein self.filein = filein
return 1
# #
def writeFingerprint(self): def writeFingerprint(self):
@ -278,22 +274,24 @@ class FingerprintDB:
# read a sqlite database by parsing the create table strings # read a sqlite database by parsing the create table strings
# sqlmaster = "SELECT name, sql FROM sqlite_master WHERE type='table'" # sqlmaster = "SELECT name, sql FROM sqlite_master WHERE type='table'"
def __readDatabase(self): def __readDatabase(self):
flag = False
rows = self.cur.execute(self.sqlmaster) rows = self.cur.execute(self.sqlmaster)
row = self.cur.fetchone() # row = self.cur.fetchone()
if (not row): # newTable = TableSchema()
raise Exception # newTable.loadTable(row[0], row[1])
# self.table_names.append(newTable.name())
newTable = TableSchema() # self.tables[newTable.name()] = newTable
newTable.loadTable(row[0], row[1])
self.table_names.append(newTable.name())
self.tables[newTable.name()] = newTable
for row in rows: for row in rows:
flag = True
newTable = TableSchema() newTable = TableSchema()
newTable.loadTable(row[0], row[1]) newTable.loadTable(row[0], row[1])
self.table_names.append(newTable.name()) self.table_names.append(newTable.name())
self.tables[newTable.name()] = newTable self.tables[newTable.name()] = newTable
if (not flag):
raise FingerprintReadNoData("No data, possible zero byte file")
# #
def debugFingerprint(self): def debugFingerprint(self):
if self.tables: if self.tables: