forked from jfarley248/iTunes_Backup_Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.py
178 lines (153 loc) · 8.17 KB
/
writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'''
Copyright (c) 2019 Jack Farley
This file is part of iTunes_Backup_Reader
Usage or distribution of this software/code is subject to the
terms of the GNU GENERAL PUBLIC LICENSE.
writer.py
------------
'''
import os
import sqlite3
import csv
'''Opens a database'''
def OpenDb(inputPath, logger):
try:
conn = sqlite3.connect(inputPath)
logger.debug ("Opened database: " + inputPath + " successfully")
return conn
except Exception as ex:
logger.exception ("Failed to open database: " + inputPath + " Exception was: " + str(ex))
return None
'''Write to a plain ole txt file'''
def writeToTxt(backup_list, application_list, output_file, logger):
output = open(output_file, "w+")
output.write("DEVICE BACKUP INFO\n" +
"Device Name: \t" + backup_list[0] + "\n" +
"Product Name: \t" + backup_list[1] + "\n" +
"Product Model: \t" + backup_list[2] + "\n" +
"Phone Number: \t" + backup_list[3] + "\n" +
"iOS Version: \t" + backup_list[4]+ "\n" +
"Backup Completetion: \t" + str(backup_list[5]) + "\n" +
"Backup Complete Write: \t" + str(backup_list[6]) + "\n" +
"Users & Computers Connected to: \t\n" + backup_list[7] + "\n" +
"Is Passcode Set: \t" + str(backup_list[8]) + "\n" +
"Is Encrypted: \t" + str(backup_list[9]) + "\n" +
"GUID: \t" + backup_list[10] + "\n" +
"ICCID: \t" + backup_list[11] + "\n" +
"IMEI: \t" + backup_list[12] + "\n" +
"MEID: \t" + backup_list[13] + "\n" +
"Serial Number: \t" + backup_list[14] + "\n" +
"Is Full Backup: \t" + str(backup_list[15]) + "\n" +
"Version: \t" + backup_list[16] + "\n" +
"iTunes Version: \t" + backup_list[17] + "\n\n")
output.write("\nAPPLICATIONS INSTALLED\n")
for app in application_list:
output.write("App Name: \t" + app[2] + "\n" +
"Device Installed on: \t" + app[0] + "\n" +
"Purchase Date: \t" + str(app[5]) + "\n" +
"Apple ID: \t" + app[3] + "\n" +
"Apple ID Name: \t" + app[4] + "\n" +
"Device Installed on Serial Number: \t" + app[1] + "\n" +
"Is Possibly Sideloaded: \t" + str(app[6]) + "\n" +
"App Version: \t" + app[7] + "\n" +
"Is Auto Downloaded: \t" + str(app[8]) + "\n" +
"Is Purchased Redownloaded: \t" + str(app[9]) + "\n" +
"Publisher: \t" + app[10] + "\n" +
"Full App Name: \t" + app[11] + "\n\n"
)
output.close()
'''Write to an SQLite database'''
def writeToDb(backup_list, application_list, output_file, logger):
'''Opens database for iTunes_Backups'''
if os.path.exists(output_file):
os.remove(output_file)
conn = OpenDb(output_file, logger)
c = conn.cursor()
'''Creates database table for device data'''
createBackQuery = "CREATE TABLE Device_Data ( Device_Name TEXT, Product_Name TEXT, " \
"Product_Model TEXT, Phone_Number TEXT, " \
"iOS_Version TEXT, Last_Backup_Completion DATE, " \
"Last_Backup_Write_Completed DATE, User_Computers TEXT, Passcode_Set BOOL, Encrypted BOOL, " \
"GUID TEXT, ICCID TEXT, IMEI TEXT, MEID TEXT, Serial_Num TEXT," \
"Full_Backup BOOL, Version TEXT, iTunes_Version TEXT);"
try:
c.execute(createBackQuery)
except Exception as ex:
logger.exception("Failed to execute query: " + createBackQuery + "\nException was: " + str(ex))
try:
'''Inserts for device data'''
conn.executemany('''INSERT INTO Device_Data(Device_Name, Product_Name, Product_Model, Phone_Number,
iOS_Version, Last_Backup_Completion,
Last_Backup_Write_Completed, User_Computers, Passcode_Set, Encrypted,
GUID, ICCID, IMEI, MEID, Serial_Num,
Full_Backup, Version, iTunes_Version) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', (backup_list,))
except Exception as ex:
logger.exception("Error filling Backups table. Error was: " + str(ex))
'''Creates database table for installed apps'''
createAppQuery = "CREATE TABLE Applications (Device_Name TEXT, Device_SN TEXT, App_Name TEXT, AppleID TEXT, " \
"User_Full_Name TEXT, Purchase_Date DATE, Is_Possibly_Sideloaded BOOL, App_Version TEXT, Is_Auto_Download BOOL, " \
"Is_Purchased_Redownload BOOL, Publisher TEXT, Full_App_Name TEXT);"
try:
c.execute(createAppQuery)
except Exception as ex:
logger.exception("Failed to execute query: " + createAppQuery + "\nException was: " + str(ex))
try:
conn.executemany('''INSERT INTO Applications(Device_Name, Device_SN, App_Name, AppleID,
User_Full_Name, Purchase_Date, Is_Possibly_Sideloaded, App_Version, Is_Auto_Download,
Is_Purchased_Redownload, Publisher, Full_App_Name) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)''',
(application_list),)
except Exception as ex:
logger.exception("Error filling Applications table. Error was: " + str(ex))
'''Closes iTunes_Backups.db'''
conn.commit()
conn.close()
'''Write to 2 different CSV files for applications and backup metadata'''
def writeToCsv(backup_list, application_list, output_file, logger):
backup_csv = output_file + "Backups.csv"
app_csv = output_file + "Applications.csv"
with open(backup_csv, 'w', newline='') as backup_csv_handle:
columns = ["Device_Name", "Product_Name", "Product_Model", "Phone_Number",
"iOS_Version", "Last_Backup_Completion",
"Last_Backup_Write_Completed", "User_Computers", "Passcode_Set", "Encrypted",
"GUID", "ICCID", "IMEI", "MEID", "Serial_Num",
"Full_Backup", "Version", "iTunes_Version"]
wr = csv.writer(backup_csv_handle, quoting=csv.QUOTE_ALL)
wr.writerows([columns, backup_list])
with open(app_csv, 'w', newline='') as app_csv_handle:
columns = ["Device_Name", "Device_SN", "App_Name", "AppleID", "User_Full_Name", "Purchase_Date",
"Is_Possibly_Sideloaded", "App_Version", "Is_Auto_Download", "Is_Purchased_Redownload",
"Publisher", "Full_App_Name"]
wr = csv.writer(app_csv_handle, quoting=csv.QUOTE_ALL)
wr.writerow(columns)
wr.writerows(application_list)
def startWrite(backup_list, application_list, output_dir, out_type, logger):
'''Write to TXT'''
if out_type == "txt":
text_file = "Device_" + backup_list[14] + "_Output.txt"
output_file = os.path.join(output_dir, text_file)
logger.debug("Starting output to " + output_file)
try:
writeToTxt(backup_list, application_list, output_file, logger)
logger.debug("Finished output to " + output_file)
except Exception as ex:
logger.exception("Could not write output to " + output_file + " Exception was: " + str(ex))
'''Write to DB'''
if out_type == "db":
text_file = "Device_" + backup_list[14] + "_Output.db"
output_file = os.path.join(output_dir, text_file)
logger.debug("Starting output to " + output_file)
try:
writeToDb(backup_list, application_list, output_file, logger)
logger.debug("Finished output to " + output_file)
except Exception as ex:
logger.exception("Could not write output to " + output_file + " Exception was: " + str(ex))
'''Write to CSV'''
if out_type == "csv":
logger.debug("Starting output")
text_file = "Device_" + backup_list[14] + "_Output_"
output_file = os.path.join(output_dir, text_file)
try:
writeToCsv(backup_list, application_list, output_file, logger)
logger.debug("Finished output to " + output_file)
except Exception as ex:
logger.exception("Could not write CSV output. Exception was: " + str(ex))