@@ -35,6 +35,30 @@ def get_credz(self, filterTerm=None, credz_type=None):
3535 results = cur .fetchall ()
3636 return results
3737
38+ def get_token (self , filterTerm = None , credz_type = None ):
39+ """
40+ Return credentials from the database.
41+ """
42+ if credz_type :
43+ with self .conn :
44+ cur = self .conn .cursor ()
45+ cur .execute (f"SELECT * FROM token WHERE type='{ credz_type } '" )
46+
47+ # if we're filtering by username
48+ elif filterTerm and filterTerm != '' :
49+ with self .conn :
50+ cur = self .conn .cursor ()
51+ cur .execute ("SELECT * FROM users WHERE LOWER(username) LIKE LOWER(?)" , ['%{}%' .format (filterTerm )])
52+
53+ # otherwise return all credentials
54+ else :
55+ with self .conn :
56+ cur = self .conn .cursor ()
57+ cur .execute ("SELECT * FROM token" )
58+
59+ results = cur .fetchall ()
60+ return results
61+
3862 @staticmethod
3963 def db_schema (db_conn ):
4064 db_conn .execute ('''CREATE TABLE "computers" (
@@ -89,6 +113,19 @@ def db_schema(db_conn):
89113 FOREIGN KEY(pillaged_from_userid) REFERENCES users(id)
90114 )''' )
91115
116+ db_conn .execute ('''CREATE TABLE "token" (
117+ "id" integer PRIMARY KEY,
118+ "file_path" text,
119+ "username" text,
120+ "password" text,
121+ "target" text,
122+ "type" text,
123+ "pillaged_from_computerid" integer,
124+ "pillaged_from_userid" integer,
125+ FOREIGN KEY(pillaged_from_computerid) REFERENCES computers(id),
126+ FOREIGN KEY(pillaged_from_userid) REFERENCES users(id)
127+ )''' )
128+
92129 db_conn .execute ('''CREATE TABLE "certificates" (
93130 "id" integer PRIMARY KEY,
94131 "pfx_file_path" text,
@@ -788,6 +825,123 @@ def add_credz(self, credz_type, credz_username, credz_password, credz_target, cr
788825
789826 return None
790827
828+ def add_token (self , credz_type , credz_username , credz_password , credz_target , credz_path , pillaged_from_computerid = None ,
829+ pillaged_from_userid = None , pillaged_from_computer_ip = None , pillaged_from_username = None ):
830+ """
831+ Check if this credential has already been added to the database, if not add it in.
832+ """
833+ user_rowid = None
834+ try :
835+ credz_username = self .clear_input (credz_username )
836+ credz_password = self .clear_input (credz_password )
837+ credz_target = self .clear_input (credz_target )
838+ credz_path = self .clear_input (credz_path )
839+ self .logging .debug (f"{ credz_username } - { binascii .hexlify (credz_username .encode ('utf-8' ))} " )
840+ self .logging .debug (f"{ credz_password } - { binascii .hexlify (credz_password .encode ('utf-8' ))} " )
841+ self .logging .debug (f"{ credz_target } - { binascii .hexlify (credz_target .encode ('utf-8' ))} " )
842+ self .logging .debug (f"{ credz_path } - { binascii .hexlify (credz_path .encode ('utf-8' ))} " )
843+ self .logging .debug (
844+ f"pillaged_from_computer_ip { pillaged_from_computer_ip } - { binascii .hexlify (pillaged_from_computer_ip .encode ('utf-8' ))} " )
845+ self .logging .debug (f"pillaged_from_username { pillaged_from_username } " )
846+
847+ if pillaged_from_computer_ip != None :
848+ with self .conn :
849+ cur = self .conn .cursor ()
850+ cur .execute (f"SELECT * FROM computers WHERE LOWER(ip)=LOWER('{ pillaged_from_computer_ip } ')" )
851+ results = cur .fetchall ()
852+ if len (results ) > 0 :
853+ result = results [0 ]
854+ pillaged_from_computerid = result [0 ]
855+ self .logging .debug (f"[+] Resolved { pillaged_from_computer_ip } to id : { pillaged_from_computerid } " )
856+ except Exception as ex :
857+ self .logging .error (f"Exception in add_token 1" )
858+ self .logging .debug (ex )
859+
860+ try :
861+ if pillaged_from_username != None :
862+ with self .conn :
863+ cur = self .conn .cursor ()
864+ cur .execute (
865+ f"SELECT * FROM users WHERE LOWER(username)=LOWER('{ pillaged_from_username } ') AND pillaged_from_computerid={ pillaged_from_computerid } " )
866+ results = cur .fetchall ()
867+ if len (results ) > 0 :
868+ result = results [0 ]
869+ pillaged_from_userid = result [0 ]
870+ self .logging .debug (
871+ f"[+] Resolved { pillaged_from_username } on machine { pillaged_from_computerid } to id : { pillaged_from_userid } " )
872+ except Exception as ex :
873+ self .logging .error (f"Exception in add_token 2" )
874+ self .logging .debug (ex )
875+ pass
876+ if pillaged_from_computerid == None or pillaged_from_userid == None :
877+ self .logging .debug (
878+ f"[-] Missing computerId or UserId to register token { credz_username } { credz_password } - { credz_target } " )
879+ # return None
880+ try :
881+ if pillaged_from_userid == None :
882+ query = "SELECT * FROM token WHERE LOWER(username)=LOWER(:credz_username) AND LOWER(password)=LOWER(:credz_password) AND LOWER(type)=LOWER(:credz_type) AND LOWER(target)=LOWER(:credz_target) AND pillaged_from_computerid=:pillaged_from_computerid"
883+ parameters = {
884+ "credz_username" : credz_username ,
885+ "credz_password" : credz_password ,
886+ "credz_type" : credz_type , "credz_target" : credz_target ,
887+ "pillaged_from_computerid" : int (pillaged_from_computerid ),
888+ }
889+ else :
890+ query = "SELECT * FROM token WHERE LOWER(username)=LOWER(:credz_username) AND LOWER(password)=LOWER(:credz_password) AND LOWER(type)=LOWER(:credz_type) AND LOWER(target)=LOWER(:credz_target) AND pillaged_from_computerid=:pillaged_from_computerid AND pillaged_from_userid=:pillaged_from_userid"
891+ parameters = {
892+ "credz_username" : credz_username ,
893+ "credz_password" : credz_password ,
894+ "credz_type" : credz_type , "credz_target" : credz_target ,
895+ "pillaged_from_computerid" : int (pillaged_from_computerid ),
896+ "pillaged_from_userid" : int (pillaged_from_userid )
897+ }
898+ self .logging .debug (query )
899+ with self .conn :
900+ cur = self .conn .cursor ()
901+ cur .execute (query , parameters )
902+ results = cur .fetchall ()
903+ except Exception as ex :
904+ self .logging .error (f"Exception in add_token 3" )
905+ self .logging .debug (ex )
906+ try :
907+ if not len (results ):
908+ if pillaged_from_userid == None :
909+ query = " INSERT INTO token (username, password, target, type, pillaged_from_computerid, file_path) VALUES (:credz_username, :credz_password, :credz_target, :credz_type, :pillaged_from_computerid, :credz_path)"
910+ parameters = {
911+ "credz_username" : credz_username ,
912+ "credz_password" : credz_password ,
913+ "credz_target" : credz_target ,
914+ "credz_type" : credz_type ,
915+ "pillaged_from_computerid" : int (pillaged_from_computerid ),
916+ "credz_path" : credz_path ,
917+ }
918+ else :
919+ query = "INSERT INTO token (username, password, target, type, pillaged_from_computerid,pillaged_from_userid, file_path) VALUES (:credz_username, :credz_password, :credz_target, :credz_type, :pillaged_from_computerid, :pillaged_from_userid, :credz_path)"
920+ parameters = {
921+ "credz_username" : credz_username ,
922+ "credz_password" : credz_password ,
923+ "credz_type" : credz_type ,
924+ "credz_target" : credz_target ,
925+ "pillaged_from_computerid" : int (pillaged_from_computerid ),
926+ "pillaged_from_userid" : int (pillaged_from_userid ),
927+ "credz_path" : credz_path ,
928+ }
929+ self .logging .debug (query )
930+ with self .conn :
931+ cur = self .conn .cursor ()
932+ cur .execute (query , parameters )
933+ user_rowid = cur .lastrowid
934+ self .logging .debug (
935+ f'added_token(credtype={ credz_type } , target={ credz_target } , username={ credz_username } , password={ credz_password } ) => { user_rowid } ' )
936+ else :
937+ self .logging .debug (
938+ f'added_token(credtype={ credz_type } , target={ credz_target } , username={ credz_username } , password={ credz_password } ) => ALREADY IN DB' )
939+
940+ except Exception as ex :
941+ self .logging .error (f"Exception in add_token 4" )
942+ self .logging .debug (ex )
943+
944+ return None
791945
792946 def add_cookies (self , credz_type , credz_name , credz_value , credz_expires_utc , credz_target , credz_path ,
793947 pillaged_from_computerid = None , pillaged_from_userid = None , pillaged_from_computer_ip = None ,
0 commit comments