forked from mac4n6/APOLLO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartemis.py
521 lines (444 loc) · 18.7 KB
/
artemis.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import argparse
from argparse import RawTextHelpFormatter
from six.moves.configparser import RawConfigParser
import xml.etree.ElementTree as ET
import glob, os, sqlite3, os, sys, re, json, subprocess
from datetime import datetime, timezone
parser = argparse.ArgumentParser(description="\
Automated Review Timeline Events Module Integrated Solution\
\n Process selected non-SQLite data sources in Android and iOS for Apollo.py ingestion \n\n Supporting: \n -Android XML Usagestats \n -iOS Mobile Installation logs"
, prog='artemis.py'
, formatter_class=RawTextHelpFormatter)
parser.add_argument('-o', choices=['sql','csv'], required=True, action="store", help="Output: sql=SQLite or csv=CSV (required)")
parser.add_argument('-p', choices=['ios','yolo','android'], required=True, action="store", help="Platform: ios=iOS [supported] or android=Android [supported] (required).")
parser.add_argument('-v', choices=['8','9','10','11','12','yolo','android'], required=True, action="store",help="iOS Version or Android (required).")
parser.add_argument('modules_directory',help="Path to Module Directory")
parser.add_argument('data_dir_to_analyze',help="Path to Data Directory. It can be full file system dump or directory of extracted databases, it is recursive.")
args = parser.parse_args()
format = args.o
mod_dir = args.modules_directory
data_dir = args.data_dir_to_analyze
platform = args.p
version = args.v
print("\n--------------------------------------------------------------------------------------")
print("Artemis: Automated Review Timeline Events Module Integrated Solution")
print("Android objective: Parse Android Usagestats XML to SQLite + Android specific Apollo modules")
print("iOS objective: Parse iOS Mobile Installation logs to SQLite + iOS specific Apollo module")
print("By: Alexis Brignoni | @AlexisBrignoni | abrignoni.com")
print("Data Directory: " + data_dir)
print("Modules Directory: " + mod_dir)
print("--------------------------------------------------------------------------------------")
print("All credit goes to Sarah Edwards for her Apollo framework which Artemis leverages")
print("Apollo Author: Sarah Edwards | @iamevltwin | mac4n6.com")
#Month to numeric with leading zero when month < 10 function
#Function call: month = month_converter(month)
def month_converter(month):
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
month = months.index(month) + 1
if (month < 10):
month = f"{month:02d}"
return month
#Day with leading zero if day < 10 function
#Functtion call: day = day_converter(day)
def day_converter(day):
day = int(day)
if (day < 10):
day = f"{day:02d}"
return day
#create function for usagestats
def parse_usagestat():
pathfound = 0
for root, dirs, filenames in os.walk(data_dir):
for f in dirs:
if f == "usagestats":
pathfound = os.path.join(root, f)
if pathfound == 0:
print("No UsageStats XML directory located")
else:
print("--------------------------------------------------------------------------------------")
print("Locating Usagestats XML files + processing")
print("Please wait...")
#Create sqlite databases
db = sqlite3.connect(data_dir+r'\usagestats.db')
cursor = db.cursor()
#Create table usagedata.
cursor.execute('''
CREATE TABLE data(usage_type TEXT, lastime TEXT, timeactive TEXT, package TEXT, types TEXT, classs TEXT,
source TEXT, fullatt TEXT)
''')
db.commit()
err=0
for filename in glob.iglob(pathfound+'\**', recursive=True):
if os.path.isfile(filename): # filter dirs
file_name = os.path.splitext(os.path.basename(filename))[0]
#Test if xml is well formed
if file_name == 'version':
continue
else:
if 'daily' in filename:
sourced = 'daily'
elif 'weekly' in filename:
sourced = 'weekly'
elif 'monthly' in filename:
sourced = 'monthly'
elif 'yearly' in filename:
sourced = 'yearly'
try:
file_name_int = int(file_name)
except:
print('Invalid filename: ')
print(filename)
print('')
err = 1
try:
ET.parse(filename)
except ET.ParseError:
print('Parse error - Non XML file? at: ')
print(filename)
print('')
err = 1
#print(filename)
if err == 1:
err = 0
continue
else:
tree = ET.parse(filename)
root = tree.getroot()
print('Processing: '+filename)
for elem in root:
#print(elem.tag)
usagetype = elem.tag
#print("Usage type: "+usagetype)
if usagetype == 'packages':
for subelem in elem:
#print(subelem.attrib)
fullatti_str = json.dumps(subelem.attrib)
#print(subelem.attrib['lastTimeActive'])
time1 = subelem.attrib['lastTimeActive']
time1 = int(time1)
if time1 < 0:
finalt = abs(time1)
else:
finalt = file_name_int + time1
#print('final time: ')
#print(finalt)
#print(subelem.attrib['package'])
pkg = (subelem.attrib['package'])
#print(subelem.attrib['timeActive'])
tac = (subelem.attrib['timeActive'])
#print(subelem.attrib['lastEvent'])
#insert in database
cursor = db.cursor()
datainsert = (usagetype, finalt, tac, pkg, '' , '' , sourced, fullatti_str,)
#print(datainsert)
cursor.execute('INSERT INTO data (usage_type, lastime, timeactive, package, types, classs, source, fullatt) VALUES(?,?,?,?,?,?,?,?)', datainsert)
db.commit()
elif usagetype == 'configurations':
for subelem in elem:
fullatti_str = json.dumps(subelem.attrib)
#print(subelem.attrib['lastTimeActive'])
time1 = subelem.attrib['lastTimeActive']
time1 = int(time1)
if time1 < 0:
finalt = abs(time1)
else:
finalt = file_name_int + time1
#print('final time: ')
#print(finalt)
#print(subelem.attrib['timeActive'])
tac = (subelem.attrib['timeActive'])
#print(subelem.attrib)
#insert in database
cursor = db.cursor()
datainsert = (usagetype, finalt, tac, '' , '' , '' , sourced, fullatti_str,)
cursor.execute('INSERT INTO data (usage_type, lastime, timeactive, package, types, classs, source, fullatt) VALUES(?,?,?,?,?,?,?,?)', datainsert)
db.commit()
elif usagetype == 'event-log':
for subelem in elem:
#print(subelem.attrib['time'])
time1 = subelem.attrib['time']
time1 = int(time1)
if time1 < 0:
finalt = abs(time1)
else:
finalt = file_name_int + time1
#time1 = subelem.attrib['time']
#finalt = file_name_int + int(time1)
#print('final time: ')
#print(finalt)
#print(subelem.attrib['package'])
pkg = (subelem.attrib['package'])
#print(subelem.attrib['type'])
tipes = (subelem.attrib['type'])
#print(subelem.attrib)
fullatti_str = json.dumps(subelem.attrib)
#add variable for type conversion from number to text explanation
#print(subelem.attrib['fs'])
#classy = subelem.attrib['class']
if 'class' in subelem.attrib:
classy = subelem.attrib['class']
cursor = db.cursor()
datainsert = (usagetype, finalt, '' , pkg , tipes , classy , sourced, fullatti_str,)
cursor.execute('INSERT INTO data (usage_type, lastime, timeactive, package, types, classs, source, fullatt) VALUES(?,?,?,?,?,?,?,?)', datainsert)
db.commit()
else:
#insert in database
cursor = db.cursor()
datainsert = (usagetype, finalt, '' , pkg , tipes , '' , sourced, fullatti_str,)
cursor.execute('INSERT INTO data (usage_type, lastime, timeactive, package, types, classs, source, fullatt) VALUES(?,?,?,?,?,?,?,?)', datainsert)
db.commit()
#create function for mobile installation logs
def parse_mobile_installation_logs():
#initialize counters
counter = 0
filescounter = 0
goto = 0
#Create sqlite databases
db = sqlite3.connect(data_dir+r'\mib.db')
cursor = db.cursor()
#Create table fileds for destroyed, installed, moved and made identifiers.
cursor.execute('''
CREATE TABLE dimm(time_stamp text, action TEXT, bundle_id TEXT,
path TEXT)
''')
db.commit()
print("--------------------------------------------------------------------------------------")
print("Locating Mobile Installation Logs + processing")
print("Please wait...")
for root, dirs, filenames in os.walk(data_dir):
for f in filenames:
if f.endswith("mobile_installation.log.0"):
goto = 1
log = (os.path.join(root, f))
if f.endswith("mobile_installation.log.1"):
goto = 1
log = (os.path.join(root, f))
if goto == 1:
goto = 0
file = open(log, 'r', encoding='utf8' )
filescounter = filescounter + 1
for line in file:
counter = counter+1
matchObj = re.search( r"(Install Successful for)", line) #Regex for installed applications
if matchObj:
actiondesc = "Install successful"
#print(actiondesc)
matchObj = re.search( r"(?<=for \()(.*)(?=\))", line) #Regex for bundle id
if matchObj:
bundleid = matchObj.group(1)
#print ("Bundle ID: ", bundleid )
matchObj = re.search( r"(?<=^)(.*)(?= \[)", line) #Regex for timestamp
if matchObj:
timestamp = matchObj.group(1)
weekday, month, day, time, year = (str.split(timestamp))
day = day_converter(day)
month = month_converter(month)
inserttime = str(year)+ '-'+ str(month) + '-' + str(day) + ' ' + str(time)
#print(inserttime)
#print(month)
#print(day)
#print(year)
#print(time)
#print ("Timestamp: ", timestamp)
#print(inserttime, actiondesc, bundleid)
#insert to database
cursor = db.cursor()
datainsert = (inserttime, actiondesc, bundleid, '' ,)
cursor.execute('INSERT INTO dimm (time_stamp, action, bundle_id, path) VALUES(?,?,?,?)', datainsert)
db.commit()
#print()
matchObj = re.search( r"(Destroying container with identifier)", line) #Regex for destroyed containers
if matchObj:
actiondesc = "Destroying container"
#print(actiondesc)
#print("Destroyed containers:")
matchObj = re.search( r"(?<=identifier )(.*)(?= at )", line) #Regex for bundle id
if matchObj:
bundleid = matchObj.group(1)
#print ("Bundle ID: ", bundleid )
matchObj = re.search( r"(?<=^)(.*)(?= \[)", line) #Regex for timestamp
if matchObj:
timestamp = matchObj.group(1)
weekday, month, day, time, year = (str.split(timestamp))
day = day_converter(day)
month = month_converter(month)
inserttime = str(year)+ '-'+ str(month) + '-' + str(day) + ' ' + str(time)
#print(inserttime)
#print(month)
#print(day)
#print(year)
#print(time)
#print ("Timestamp: ", timestamp)
matchObj = re.search( r"(?<= at )(.*)(?=$)", line) #Regex for path
if matchObj:
path = matchObj.group(1)
#print ("Path: ", matchObj.group(1))
#print(inserttime, actiondesc, bundleid, path)
#insert to database
cursor = db.cursor()
datainsert = (inserttime, actiondesc, bundleid, path ,)
cursor.execute('INSERT INTO dimm (time_stamp, action, bundle_id, path) VALUES(?,?,?,?)', datainsert)
db.commit()
#print()
matchObj = re.search( r"(Data container for)", line) #Regex Moved data containers
if matchObj:
actiondesc = "Data container moved"
#print(actiondesc)
#print("Data container moved:")
matchObj = re.search( r"(?<=for )(.*)(?= is now )", line) #Regex for bundle id
if matchObj:
bundleid = matchObj.group(1)
#print ("Bundle ID: ", bundleid )
matchObj = re.search( r"(?<=^)(.*)(?= \[)", line) #Regex for timestamp
if matchObj:
timestamp = matchObj.group(1)
weekday, month, day, time, year = (str.split(timestamp))
day = day_converter(day)
month = month_converter(month)
inserttime = str(year)+ '-'+ str(month) + '-' + str(day) + ' ' + str(time)
#print(inserttime)
#print(month)
#print(day)
#print(year)
#print(time)
#print ("Timestamp: ", timestamp)
matchObj = re.search( r"(?<= at )(.*)(?=$)", line) #Regex for path
if matchObj:
path = matchObj.group(1)
#print ("Path: ", matchObj.group(1))
#print(inserttime, actiondesc, bundleid, path)
#insert to database
cursor = db.cursor()
datainsert = (inserttime, actiondesc, bundleid, path ,)
cursor.execute('INSERT INTO dimm (time_stamp, action, bundle_id, path) VALUES(?,?,?,?)', datainsert)
db.commit()
#print()
matchObj = re.search( r"(Made container live for)", line) #Regex for made container
if matchObj:
actiondesc = "Made container live"
#print(actiondesc)
#print("Made container:")
matchObj = re.search( r"(?<=for )(.*)(?= at)", line) #Regex for bundle id
if matchObj:
bundleid = matchObj.group(1)
#print ("Bundle ID: ", bundleid )
matchObj = re.search( r"(?<=^)(.*)(?= \[)", line) #Regex for timestamp
if matchObj:
timestamp = matchObj.group(1)
weekday, month, day, time, year = (str.split(timestamp))
day = day_converter(day)
month = month_converter(month)
inserttime = str(year)+ '-'+ str(month) + '-' + str(day) + ' ' + str(time)
#print(inserttime)
#print(month)
#print(day)
#print(year)
#print(time)
#print ("Timestamp: ", timestamp)
matchObj = re.search( r"(?<= at )(.*)(?=$)", line) #Regex for path
if matchObj:
path = matchObj.group(1)
#print ("Path: ", matchObj.group(1))
#print(inserttime, actiondesc, bundleid, path)
#insert to database
cursor = db.cursor()
datainsert = (inserttime, actiondesc, bundleid, path ,)
cursor.execute('INSERT INTO dimm (time_stamp, action, bundle_id, path) VALUES(?,?,?,?)', datainsert)
db.commit()
matchObj = re.search( r"(Uninstalling identifier )", line) #Regex for made container
if matchObj:
actiondesc = "Uninstalling identifier"
#print(actiondesc)
#print("Uninstalling identifier")
matchObj = re.search( r"(?<=Uninstalling identifier )(.*)", line) #Regex for bundle id
if matchObj:
bundleid = matchObj.group(1)
#print ("Bundle ID: ", bundleid )
matchObj = re.search( r"(?<=^)(.*)(?= \[)", line) #Regex for timestamp
if matchObj:
timestamp = matchObj.group(1)
weekday, month, day, time, year = (str.split(timestamp))
day = day_converter(day)
month = month_converter(month)
inserttime = str(year)+ '-'+ str(month) + '-' + str(day) + ' ' + str(time)
#print(inserttime)
#print(month)
#print(day)
#print(year)
#print(time)
#print ("Timestamp: ", timestamp)
#insert to database
cursor = db.cursor()
datainsert = (inserttime, actiondesc, bundleid, '' ,)
cursor.execute('INSERT INTO dimm (time_stamp, action, bundle_id, path) VALUES(?,?,?,?)', datainsert)
db.commit()
matchObj = re.search( r"(main: Reboot detected)", line) #Regex for reboots
if matchObj:
actiondesc = "Reboot detected"
#print(actiondesc)
matchObj = re.search( r"(?<=^)(.*)(?= \[)", line) #Regex for timestamp
if matchObj:
timestamp = matchObj.group(1)
weekday, month, day, time, year = (str.split(timestamp))
day = day_converter(day)
month = month_converter(month)
inserttime = str(year)+ '-'+ str(month) + '-' + str(day) + ' ' + str(time)
#print(inserttime)
#print(month)
#print(day)
#print(year)
#print(time)
#print ("Timestamp: ", timestamp)
#insert to database
cursor = db.cursor()
datainsert = (inserttime, actiondesc, '', '' ,)
cursor.execute('INSERT INTO dimm (time_stamp, action, bundle_id, path) VALUES(?,?,?,?)', datainsert)
db.commit()
matchObj = re.search( r"(Attempting Delta patch update of )", line) #Regex for Delta patch
if matchObj:
actiondesc = "Attempting Delta patch"
#print(actiondesc)
#print("Made container:")
matchObj = re.search( r"(?<=Attempting Delta patch update of )(.*)(?= from)", line) #Regex for bundle id
if matchObj:
bundleid = matchObj.group(1)
#print ("Bundle ID: ", bundleid )
matchObj = re.search( r"(?<=^)(.*)(?= \[)", line) #Regex for timestamp
if matchObj:
timestamp = matchObj.group(1)
weekday, month, day, time, year = (str.split(timestamp))
day = day_converter(day)
month = month_converter(month)
inserttime = str(year)+ '-'+ str(month) + '-' + str(day) + ' ' + str(time)
#print(inserttime)
#print(month)
#print(day)
#print(year)
#print(time)
#print ("Timestamp: ", timestamp)
matchObj = re.search( r"(?<= from )(.*)", line) #Regex for path
if matchObj:
path = matchObj.group(1)
#print ("Path: ", matchObj.group(1))
#print(inserttime, actiondesc, bundleid, path)
#insert to database
cursor = db.cursor()
datainsert = (inserttime, actiondesc, bundleid, path ,)
cursor.execute('INSERT INTO dimm (time_stamp, action, bundle_id, path) VALUES(?,?,?,?)', datainsert)
db.commit()
print ('Logs processed: ', filescounter)
print ('Lines processed: ', counter)
if platform == "android":
parse_usagestat()
platform = "yolo"
version = "yolo"
elif platform == "ios":
parse_mobile_installation_logs()
elif platform == "yolo":
parse_usagestat()
parse_mobile_installation_logs()
version = "yolo"
print("--------------------------------------------------------------------------------------")
print("Calling Apollo for database processing...")
print("Artemis is done. Good bye")
p = subprocess.run(['python', 'apollo.py', '-o', format, '-p', platform, '-v', version, mod_dir, data_dir])