2222 \ /\ / / _____ \ | `----.| . \ | |____ | |\ \----.
2323 \__/ \__/ /__/ \__\ |_______||__|\__\ |_______|| _| `._____|
2424
25- SQLiteWalker v0.0.1
25+ SQLiteWalker v0.0.2
2626 https://github.com/stark4n6/SQLiteWalker
2727 @KevinPagano3 | @stark4n6 | startme.stark4n6.com
2828 '''
@@ -49,15 +49,15 @@ def main():
4949 base = "SQLiteWalker_Out_"
5050 data_list = []
5151 error_list = []
52- data_headers = ('File Name' ,'File Path' ,'Tables' )
53- error_headers = ('File Name' ,'File Path' ,'Error' )
52+ data_headers = ('File Name' ,'Export Path' ,'Tables' )
53+ error_headers = ('File Name' ,'Export Path' ,'Error' )
5454 count = 0
5555 error_count = 0
5656
5757 start_time = time .time ()
5858
5959 #Command line arguments
60- parser = argparse .ArgumentParser (description = 'SQLiteWalker v0.0.1 by @KevinPagano3 | @stark4n6 | https://github.com/stark4n6/SQLiteWalker' )
60+ parser = argparse .ArgumentParser (description = 'SQLiteWalker v0.0.2 by @KevinPagano3 | @stark4n6 | https://github.com/stark4n6/SQLiteWalker' )
6161 parser .add_argument ('-i' , '--input_path' , required = True , type = str , action = "store" , help = 'Input file/folder path' )
6262 parser .add_argument ('-o' , '--output_path' , required = True , type = str , action = "store" , help = 'Output folder path' )
6363 parser .add_argument ('-q' , '--quiet_mode' , required = False , action = "store_true" , help = 'Turns off console path output' )
@@ -121,99 +121,123 @@ def main():
121121 output_ts = time .strftime ("%Y%m%d-%H%M%S" )
122122 out_folder = output_path + base + output_ts
123123 for file in files :
124- with my_zip .open (file ) as f :
125- header = f .read (100 )
126- if header .startswith (b'\x53 \x51 \x4c \x69 \x74 \x65 \x20 \x66 \x6f \x72 \x6d \x61 \x74 \x20 \x33 \x00 ' ):
127-
128- my_zip .extract (file ,(out_folder + splitter + 'db_out' ))
129- try :
130- db_connect = sqlite3 .connect (out_folder + splitter + 'db_out' + splitter + file )
131-
132- sql_query = """SELECT name FROM sqlite_master
133- WHERE type='table';"""
134-
135- cursor = db_connect .cursor ()
136- cursor .execute (sql_query )
137-
138- # printing all tables list
139- tables = cursor .fetchall ()
140- tables_list = []
141-
142- entries = len (tables )
143- if entries > 0 :
144- for row in tables :
145- tables_list .append (row [0 ])
146-
147- file_name = file .rsplit ("/" ,1 )
148-
149- data_list .append ((file_name [1 ], file , tables_list ))
150- count += 1
151- if not quiet_mode :
152- print ('DB ' + str (count ) + ': ' + file )
153-
154- except sqlite3 .Error as error :
155- if not quiet_mode :
156- print ("Failed to open the database: " , error )
157- print (file )
158- error_list .append ((file_name [1 ], file , error ))
159- error_count += 1
160-
161- finally :
162- if db_connect :
163- db_connect .close ()
164- else :
165- print ('File is not a .zip, please try again. Exiting......' )
166- sys .exit ()
167-
168- else :
169- output_ts = time .strftime ("%Y%m%d-%H%M%S" )
170- out_folder = output_path + base + output_ts
171- os .mkdir (out_folder )
172- for root , dirs , files in os .walk (input_path ):
173- for name in files :
174- file = os .path .join (root , name )
175- file_name = name
176- if os .path .isfile (file ):
177- if os .path .getsize (file ) > 100 :
178- with open (file ,'r' , encoding = "ISO-8859-1" ) as f :
124+ file_name = file .rsplit ("/" ,1 )
125+ if file .endswith (('-shm' ,'-wal' )):
126+ my_zip .extract (file ,(out_folder + splitter + 'db_out' ))
127+
128+ new_path = out_folder + splitter + 'db_out' + file
129+ if platform :
130+ new_path = new_path .replace ('/' ,'\\ ' )
131+
132+ data_list .append ((file_name [1 ], new_path [4 :], '' ))
133+ else :
134+ with my_zip .open (file ) as f :
179135 header = f .read (100 )
180- if header .startswith ('SQLite format 3' ):
136+ if header .startswith (b'\x53 \x51 \x4c \x69 \x74 \x65 \x20 \x66 \x6f \x72 \x6d \x61 \x74 \x20 \x33 \x00 ' ):
137+ my_zip .extract (file ,(out_folder + splitter + 'db_out' ))
181138 try :
182- db_connect = open_sqlite_db_readonly (file )
183-
139+ new_path = out_folder + splitter + 'db_out' + file
140+ if platform :
141+ new_path = new_path .replace ('/' ,'\\ ' )
142+
143+ db_connect = sqlite3 .connect (new_path )
144+
184145 sql_query = """SELECT name FROM sqlite_master
185146 WHERE type='table';"""
186147
187148 cursor = db_connect .cursor ()
188149 cursor .execute (sql_query )
189150
151+ # printing all tables list
190152 tables = cursor .fetchall ()
191153 tables_list = []
192154
193- for row in tables :
194- tables_list .append (row [0 ])
195-
196- data_list .append ((file_name , file , tables_list ))
197- count += 1
198- if not quiet_mode :
199- print ('DB ' + str (count ) + ': ' + file )
200-
201- if root in file :
202- relative_path = os .path .relpath (root ,input_path )
203- new_path = os .path .join (out_folder + splitter + 'db_out' + splitter ,relative_path )
204- if not os .path .exists (new_path ):
205- shutil .copytree (root ,new_path )
155+ entries = len (tables )
156+ if entries > 0 :
157+ for row in tables :
158+ tables_list .append (row [0 ])
159+
160+ data_list .append ((file_name [1 ], new_path [4 :], tables_list ))
161+ count += 1
162+ if not quiet_mode :
163+ print ('DB ' + str (count ) + ': ' + file )
206164
207165 except sqlite3 .Error as error :
208166 if not quiet_mode :
209- print ("Failed to execute the above query " , error )
167+ print ("Failed to open the database: " , error )
210168 print (file )
211- error_list .append ((file_name [1 ], file , error ))
169+ error_list .append ((file_name [1 ], new_path [ 4 :] , error ))
212170 error_count += 1
213171
214172 finally :
215173 if db_connect :
216- db_connect .close ()
174+ db_connect .close ()
175+ else :
176+ print ('File is not a .zip, please try again. Exiting......' )
177+ sys .exit ()
178+
179+ else :
180+ output_ts = time .strftime ("%Y%m%d-%H%M%S" )
181+ out_folder = output_path + base + output_ts
182+ os .mkdir (out_folder )
183+
184+ for root , dirs , files in os .walk (input_path ):
185+ for file in files :
186+ if file .endswith (('-shm' ,'-wal' )):
187+ src_file_path = os .path .join (root , file )
188+
189+ dest_folder_path = os .path .join (out_folder , os .path .relpath (root , input_path ))
190+ dest_file_path = os .path .join (dest_folder_path , file )
191+ os .makedirs (dest_folder_path , exist_ok = True )
192+
193+ shutil .copy2 (src_file_path , dest_file_path )
194+ data_list .append ((file , dest_file_path [4 :], '' ))
195+
196+ else :
197+ src_file_path = os .path .join (root , file )
198+ if os .path .isfile (src_file_path ):
199+ if os .path .getsize (src_file_path ) > 100 :
200+ with open (src_file_path ,'r' , encoding = "ISO-8859-1" ) as f :
201+ header = f .read (100 )
202+ if header .startswith ('SQLite format 3' ):
203+ try :
204+ db_connect = open_sqlite_db_readonly (src_file_path )
205+
206+ sql_query = """SELECT name FROM sqlite_master
207+ WHERE type='table';"""
208+
209+ cursor = db_connect .cursor ()
210+ cursor .execute (sql_query )
211+
212+ tables = cursor .fetchall ()
213+ tables_list = []
214+
215+ for row in tables :
216+ tables_list .append (row [0 ])
217+
218+ count += 1
219+ if not quiet_mode :
220+ print ('DB ' + str (count ) + ': ' + src_file_path )
221+
222+ src_file_path = os .path .join (root , file )
223+
224+ dest_folder_path = os .path .join (out_folder , os .path .relpath (root , input_path ))
225+ dest_file_path = os .path .join (dest_folder_path , file )
226+ os .makedirs (dest_folder_path , exist_ok = True )
227+
228+ shutil .copy2 (src_file_path , dest_file_path )
229+ data_list .append ((file , dest_file_path [4 :], tables_list ))
230+
231+ except sqlite3 .Error as error :
232+ if not quiet_mode :
233+ print ("Failed to execute the above query" , error )
234+ print (file )
235+ error_list .append ((file , dest_file_path [4 :], error ))
236+ error_count += 1
237+
238+ finally :
239+ if db_connect :
240+ db_connect .close ()
217241
218242 with open (out_folder + splitter + 'db_list.tsv' , 'w' , newline = '' ) as f_output :
219243 tsv_writer = csv .writer (f_output , delimiter = '\t ' )
@@ -237,4 +261,4 @@ def main():
237261 print ('Check error file error_list.tsv for details' )
238262
239263if __name__ == '__main__' :
240- main ()
264+ main ()
0 commit comments