-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
executable file
·1272 lines (1160 loc) · 47.2 KB
/
index.js
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
'use strict';
const pkg = require('./package.json');
const program = require('commander');
const fs = require('fs');
const async = require('async');
const childProcess = require('child_process');
const logger = require('./logger.js');
const plist = require('plist');
const path = require('path');
const handlebars = require('handlebars');
const copydir = require('copy-dir');
const split = require('split');
const deepdiff = require('deep-diff').diff;
const readChunk = require('read-chunk');
const iOSversions = require('./ios-versions.js');
const __base = path.join(__dirname, '/');
program
.version(pkg.version)
.description('Incident response tool for iPhone or iPad')
.option('--debug', 'Display debugging output');
program
.command('extract')
.arguments('<dir>')
.description('Extract IR artifacts from iPhone or iPad')
.option('-b, --backup', 'Backup iOS device')
.option('--syslog-timeout <seconds>', 'Optional timeout for how long to collect syslong, e.g. 86400 to collect for a day')
.action(function (dir, options) {
if (program.debug) { logger.transports.console.level = 'debug'; }
extractArtifacts(dir, options, function (err, runStatus) {
if (err) {
logger.error(err);
} else {
logger.info(runStatus);
}
});
});
program
.command('process')
.arguments('<dir>')
.description('Process extracted artifacts in <dir>')
.action(function (dir) {
if (program.debug) { logger.transports.console.level = 'debug'; }
async.series({
processArtifacts: function (callback) {
// process device info
logger.info('executing processArtifacts now');
processArtifacts(dir, function (err, results) {
if (err) {
logger.warn('error in processArtifacts: %s', err);
} else {
logger.info(results);
}
callback(null, 'done procesing all artifacts');
});
},
findIssues: function (callback) {
logger.info('executing findIssues now');
findIssues(dir, function (err, results) {
if (err) {
logger.warn(err);
} else {
logger.info(results);
}
callback(null, 'done finding issues');
});
}
}, function (err, results) {
if (err) { logger.warn(err); }
logger.info('done processing artifacts and finding issues');
});
});
program
.command('report')
.arguments('<dir> [diffdir]')
.description('Generate iOS IR reports from <dir>')
.action(function (dir, diffdir) {
if (program.debug) { logger.transports.console.level = 'debug'; }
generateReport(dir, diffdir, function (err, runStatus) {
if (err) {
logger.error(err);
} else {
logger.info(runStatus);
}
});
});
program.parse(process.argv);
// if program was called with no arguments, show help.
if (program.args.length === 0) {
program.help();
} else {
// Checking to sure passed command (rawArgs[2]) is valid.
// Adpated from https://github.com/tj/commander.js/issues/57#issue-4481445
const validCommands = program.commands.map(function (cmd) {
return cmd._name;
});
if (validCommands.indexOf(program.rawArgs[2]) === -1) {
logger.error('Invalid command "%s". Run `ios-triage --help`', program.rawArgs[2]);
}
}
function setWorkingDirectory (userOutputDir, udid, currentEpoch) {
let workingDir = '';
if (userOutputDir) {
workingDir = userOutputDir;
} else {
workingDir = __dirname;
}
const udidDir = path.join(workingDir, udid);
const udidEpochDir = path.join(udidDir, currentEpoch);
const artifactDir = path.join(udidEpochDir, 'artifacts');
if (!fs.existsSync(udidDir)) {
fs.mkdirSync(udidDir);
}
if (!fs.existsSync(udidEpochDir)) {
fs.mkdirSync(udidEpochDir);
}
if (!fs.existsSync(artifactDir)) {
fs.mkdirSync(artifactDir);
}
logger.info('output directory set to %s', udidEpochDir);
return (udidEpochDir);
}
function extractArtifacts (dir, options, callback) {
// let's first get the UDID...if we can't do this successfully, we have a problem
getUDID(function (err, udid) {
if (err) {
return callback(new Error(err));
}
// no error getting UDID so time to fetch data
// first we'll setup the working directory, saving data in unique dir each time based on epoch time
const currentEpoch = new Date().getTime();
const wd = setWorkingDirectory(dir, udid, currentEpoch.toString());
const idevicesyslog = getDeviceSyslog(udid, wd, options.syslogTimeout);
async.parallel({
backup: function (callback) {
if (options.backup) {
doDeviceBackup(udid, wd, callback);
} else {
logger.info('Skipping device backup');
// this callback() is critical so async.parallel can return
callback();
}
},
deviceInfo: function (callback) {
getDeviceInfo(udid, wd, callback);
},
installedApps: function (callback) {
getInstalledApps(udid, wd, callback);
},
provisioningProfiles: function (callback) {
copyProvisioningProfiles(udid, wd, callback);
},
crashReports: function (callback) {
getCrashReports(udid, wd, callback);
}
}, function (err, results) {
// handle any errors from extraction functions
if (err) { logger.error('errors encountered during extraction. error: %s\nresults: %s', err, results); }
if (options.syslogTimeout === undefined) {
logger.info("completed all extraction functions so we'll now kill deviceSyslog");
idevicesyslog.kill('SIGINT');
} else {
logger.info('waiting %d seconds for syslog to execute', options.syslogTimeout);
}
callback(null, 'extract complete');
});
});
}
function getUDID (callback) {
var retval = '';
const udid = childProcess.spawn('idevice_id', ['-l']);
udid.stdout.on('data', (chunk) => {
// FIXME: if idevice_id fires the data event more than once the this would overwrite
// retval which is likley problematic
retval = chunk;
});
udid.on('close', code => {
/*
Unfortunately idevice_id returns a 0 in all situations I checked
which differs from how ideviceinfo works. If you call idevice_id with
an invalid parameter or no device is attached, it still returns a 0.
I'm going to keep this return code != 0 in here for now in case they fix
in the future. The work around is to test the length for retval and if it is
41, then we have a UDID returned!
*/
let udidStr = '';
if (retval.length === 82) {
// when an idevice has been configured for WiFi sync, idevice_id will
// return the UDID twice. Handling this sitaution with hardcoding :-)
udidStr = String.fromCharCode.apply(null, retval.slice(40));
logger.info('Authorized iDevice found, UDID: %s', udidStr.trim());
callback(null, udidStr.trim());
} else if (retval.length === 41) {
// found a valid udid so return null error and uuid value
// first let's make this a string and trim any newlines
udidStr = String.fromCharCode.apply(null, retval);
logger.info('Authorized iDevice found, UDID: %s', udidStr.trim());
callback(null, udidStr.trim());
} else {
// encountered some sort of error. If 0 len then no device attached, otherwise something else
if (retval.length === 0) {
return callback(new Error('No authorized iDevice found. Plug in and authorize a device first.'));
} else {
return callback(new Error(retval));
}
}
});
}
function getDeviceSyslog (udid, wd, syslogTimeout) {
const filename = 'syslog.txt';
const file = fs.createWriteStream(wd + '/artifacts/' + filename);
let userTimeout = 0;
// check to see if user specified a timeout
if (syslogTimeout) {
// syslog timeout was specified so run with that user setting
// FIX: add check to make sure syslogTimeout is an int or catch the conversion error
userTimeout = Number(syslogTimeout) * 1000;
}
// execFile maxBuffer is set to 200k but I've overode it here to 5MB. I'll probably
// need to change this call to exec or fork, need to research a little more
// originally chose execFile so I could control the timeout...but we could also do
// that in the calling function if we wanted.
const opts = {
timeout: userTimeout,
maxBuffer: 5000 * 1024
};
// call idevicesyslog binary
const idevicesyslog = childProcess.execFile('idevicesyslog', [], opts);
logger.info('capturing device syslog...');
// on data events, write chunks to file
idevicesyslog.stdout.on('data', (chunk) => {
file.write(chunk);
});
// after Stream ends, close the file, inform user of saved file
idevicesyslog.stdout.on('end', () => {
file.end();
logger.debug('in getDeviceSyslog, end event fired');
});
idevicesyslog.on('close', function (code) {
if (code !== 0) {
logger.error('idevicesyslog returned error code ' + code);
// return callback(new Error('idevicesyslog returned error code ' + code));
} else {
logger.debug('in getDeviceSyslog, close event triggered without error');
logger.info('iOS Device syslog saved');
// callback(null, idevicesyslog);
}
});
// for syslog, we call back immediately and return the childProcess so the calling program
// has control over deciding when to kill the process. Could be immediately after other
// extraction is complete or after a timeout value
// callback(null, idevicesyslog);
return (idevicesyslog);
}
function getDeviceInfo (udid, wd, callback) {
// idevice info can be run with no "domains" or domains supplied, netting more info
// we'll build an array of domains to call and do this in a loop
const domains = [
'',
'com.apple.disk_usage',
'com.apple.disk_usage.factory',
'com.apple.mobile.battery',
'com.apple.iqagent',
'com.apple.purplebuddy',
'com.apple.PurpleBuddy',
'com.apple.mobile.chaperone',
'com.apple.mobile.third_party_termination',
'com.apple.mobile.lockdownd',
'com.apple.mobile.lockdown_cache',
'com.apple.xcode.developerdomain',
'com.apple.international',
'com.apple.mobile.data_sync',
'com.apple.mobile.tethered_sync',
'com.apple.mobile.mobile_application_usage',
'com.apple.mobile.backup',
'com.apple.mobile.nikita',
'com.apple.mobile.restriction',
'com.apple.mobile.user_preferences',
'com.apple.mobile.sync_data_class',
'com.apple.mobile.software_behavior',
'com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands',
'com.apple.mobile.iTunes.accessories',
'com.apple.mobile.internal',
'com.apple.mobile.wireless_lockdown',
'com.apple.fairplay',
'com.apple.iTunes',
'com.apple.mobile.iTunes.store',
'com.apple.mobile.iTunes'
];
const baseFilename = 'ideviceinfo';
domains.forEach(function (domain) {
let domainAddl = '';
if (domain !== '') {
domainAddl = '-' + domain;
}
const filename = baseFilename + domainAddl + '.xml';
const file = fs.createWriteStream(wd + '/artifacts/' + filename);
// call ideviceinfo binary with domain extension if present
let opts = ['--xml'];
if (domain !== '') {
opts.push('--domain');
opts.push(domain);
}
logger.debug('calling ideviceinfo with opts: %s', opts);
const ideviceinfo = childProcess.spawn('ideviceinfo', opts);
// on data events, write chunks to file
ideviceinfo.stdout.on('data', (chunk) => {
file.write(chunk);
});
// after Stream ends, close the file, inform user of saved file
ideviceinfo.stdout.on('end', () => {
file.end();
logger.debug('iOS Device info saved, domain: %s', domain);
// callback(null, ideviceinfo);
});
// should this event be on exit or on close?
// per documentation, not all Streams emit a close event
// https://nodejs.org/api/stream.html#stream_event_close
ideviceinfo.on('close', function (code) {
if (code !== 0) {
// return callback(new Error('Error: ideviceinfo (domain: ' + domain + ') returned error code ' + code));
logger.error('Error: ideviceinfo (domain: %s) returned error code %s', domain, code);
}
});
});
// does this callback immediately?
callback(null, 'complete device info extraction');
}
function getInstalledApps (udid, wd, callback) {
const filename = 'installed-apps.xml';
const file = fs.createWriteStream(wd + '/artifacts/' + filename);
// call ideviceinstaller binary
const ideviceinstaller = childProcess.spawn('ideviceinstaller', ['--list-apps', '-o', 'list_all', '-o', 'xml']);
// on data events, write chunks to file
ideviceinstaller.stdout.on('data', (chunk) => {
file.write(chunk);
});
// after Stream ends, close the file, inform user of saved file
ideviceinstaller.stdout.on('end', () => {
file.end();
logger.info('iOS Device installed apps saved');
callback(null, ideviceinstaller);
});
ideviceinstaller.on('close', function (code) {
if (code !== 0) {
callback(new Error('ideviceinstaller returned error code ' + code));
}
});
}
function copyProvisioningProfiles (udid, wd, callback) {
// ideviceprovision writes any pprofiles to disk vs. returning to stdout
// creating a directory to store this data and putting stdout into log file
const pprofilesDir = path.join(wd, 'artifacts', 'pprofiles');
if (!fs.existsSync(pprofilesDir)) {
fs.mkdirSync(pprofilesDir);
}
const filename = 'ideviceprovision.log';
const file = fs.createWriteStream(pprofilesDir + filename);
// call ideviceprovision binary
const ideviceprovision = childProcess.spawn('ideviceprovision', ['copy', pprofilesDir]);
// on data events, write chunks to file
ideviceprovision.stdout.on('data', (chunk) => {
file.write(chunk);
});
// after Stream ends, close the file, inform user of saved file
ideviceprovision.stdout.on('end', () => {
file.end();
logger.info('Installed provisioning profiles saved');
callback(null, ideviceprovision);
});
ideviceprovision.on('close', function (code) {
if (code !== 0) {
callback(new Error('ideviceprovision returned error code ' + code));
}
});
}
function getCrashReports (udid, wd, callback) {
// idevicecrashreport writes multiple files vs. returning to stdout
// creating a directory to store this data and putting stdout into log file
const crashreportsDir = wd + '/artifacts/crash_reports/';
if (!fs.existsSync(crashreportsDir)) {
fs.mkdirSync(crashreportsDir);
}
const filename = 'crashlogs.txt';
const file = fs.createWriteStream(crashreportsDir + filename);
// call ideviceprovision binary
const idevicecrashreport = childProcess.spawn('idevicecrashreport', ['--extract', '--keep', crashreportsDir]);
// on data events, write chunks to file
idevicecrashreport.stdout.on('data', (chunk) => {
file.write(chunk);
});
// after Stream ends, close the file, inform user of saved file
idevicecrashreport.stdout.on('end', () => {
file.end();
logger.info('Crash reports and log saved');
callback(null, idevicecrashreport);
});
idevicecrashreport.on('close', function (code) {
if (code !== 0) {
return callback(new Error('idevicecrashreport returned error code ' + code));
}
});
}
function doDeviceBackup (udid, wd, callback) {
// idevicebackup2 backup --full .
// idevicebackup2 writes many files and directories vs. returning to stdout
// creating a directory to store this data and putting stdout into log file
const backupDir = wd + '/artifacts/backup/';
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir);
}
const filename = 'backup_log.txt';
const file = fs.createWriteStream(backupDir + filename);
// call ideviceprovision binary
const idevicebackup2 = childProcess.spawn('idevicebackup2', ['backup', '--full', backupDir]);
// on data events, write chunks to file
idevicebackup2.stdout.on('data', (chunk) => {
file.write(chunk);
});
// after Stream ends, close the file, inform user of saved file
idevicebackup2.stdout.on('end', () => {
file.end();
logger.info('Device backup and log saved');
callback(null, idevicebackup2);
});
idevicebackup2.on('close', function (code) {
if (code !== 0) {
callback(new Error('idevicebackup2 returned error code ' + code));
}
});
}
function processArtifacts (dir, callback) {
const processedPath = path.join(dir, 'processed');
const artifactPath = path.join(dir, 'artifacts');
// if no artifact dir exists, err.
if (!fs.existsSync(artifactPath)) {
logger.error('No artifact directory found at %s', artifactPath);
process.exit(1);
} else {
// see if processed dir exists, if so alert but continue. otherwise, create
if (!fs.existsSync(processedPath)) {
fs.mkdirSync(processedPath);
} else {
logger.warn('Processed path already exists, overwriting data in %s', path.resolve(processedPath));
}
async.parallel({
artifacts: function (callback) {
// device info
processDeviceInfo(dir, function (err, results) {
if (err) {
logger.warn(err);
} else {
logger.info(results);
}
callback();
});
},
apps: function (callback) {
// installed apps
processInstalledAppsXML(dir, function (err, results) {
if (err) {
logger.warn(err);
} else {
logger.info(results);
}
callback();
});
},
pprofiles: function (callback) {
// provisioning profiles
processProvisioningProfiles(dir, function (err, results) {
if (err) {
logger.warn(err);
} else {
logger.info(results);
}
callback();
});
},
syslog: function (callback) {
// process syslog
processSyslog(dir, function (err, results) {
if (err) {
logger.warn(err);
} else {
logger.info(results);
}
callback();
});
},
crashreport: function (callback) {
// process crash reports
processCrashReports(dir, function (err, results) {
if (err) {
logger.warn(err);
} else {
logger.info(results);
}
callback();
});
},
backup: function (callback) {
// process backup
processBackup(dir, function (err, results) {
if (err) {
logger.warn(err);
} else {
logger.info(results);
}
callback();
});
}
}, function (err, results) {
if (err) {
logger.debug('in processArtifact async.parallel call final function: %s', err);
} else {
logger.debug('in processArtifact async.parallel call final function: %s', results);
}
callback('null', 'completed processArtifact async.parallel execution');
});
} // else
}
function processInstalledAppsXML (dir, callback) {
const artifactPath = path.join(dir, 'artifacts');
const processedPath = path.join(dir, 'processed');
const installedAppsXML = path.join(artifactPath, 'installed-apps.xml');
const apps = {};
apps.summary = {};
apps.summary.entitlements = {};
apps.summary.privacySensitiveDataAccess = {};
apps.summary.UIBackgroundModes = {};
let totalApps = 0;
let userApps = 0;
let systemApps = 0;
let nonAppleSigner = 0;
let appsWithEntitlements = 0;
let usePersistentWifi = 0;
let requestsForPrivacySensitiveDataAccess = 0;
let allowArbitraryLoads = 0;
let allowArbitraryLoadsInWebContent = 0;
let domainsAllowedArbitraryLoads = 0;
// let domainsForceTLSLoads = 0;
async.parallel({
processApps: function (callback) {
try {
// read and parse plist file
const obj = plist.parse(fs.readFileSync(installedAppsXML, 'utf8'));
// full app details for inspection and comparision
apps.details = obj;
for (let prop in obj) {
// every prop in array is properties for an app
const app = obj[prop];
totalApps++;
for (let attrib in app) {
switch (attrib) {
case 'Entitlements':
appsWithEntitlements++;
for (let entitlement in app[attrib]) {
if (!(entitlement in apps.summary.entitlements)) {
apps.summary.entitlements[entitlement] = 0;
}
apps.summary.entitlements[entitlement]++;
}
break;
case 'NSAppTransportSecurity':
for (let transportProperty in app[attrib]) {
logger.debug('examining %s: ', transportProperty);
switch (transportProperty) {
case 'NSAllowsArbitraryLoads':
if (app[attrib][transportProperty] === true) {
allowArbitraryLoads++;
}
break;
case 'NSAllowsArbitraryLoadsInWebContent':
if (app[attrib][transportProperty] === true) {
allowArbitraryLoadsInWebContent++;
}
break;
case 'NSExceptionDomains':
for (let domain in app[attrib][transportProperty]) {
logger.debug('found exception for domain %s: ', domain);
/*
need to test to see if domain (normally and object) is set to null, e.g.
{
"NSExceptionDomains": {
"http://guide.2demo.net/": null
},
"NSAllowsArbitraryLoads": true
}
*/
/*
let _allowInsecureHTTPLocal = null;
if ("NSExceptionAllowsInsecureHTTPLoads" in app[attrib][transportProperty][domain]) {
_allowInsecureHTTPLocal = app[attrib][transportProperty][domain].NSExceptionAllowsInsecureHTTPLoads;
};
logger.info("found exception for domain (%s, %s): ", domain, _allowInsecureHTTPLocal);
if ((_allowsInsecureHTTPLoads !== null) && (_allowsInsecureHTTPLoads === true)) {
*/
domainsAllowedArbitraryLoads++;
/*
} else {
domainsForceTLSLoads++;
};
*/
}
break;
default:
// found an unknown NSAppTransportSecurity property
logger.debug('found an unknown NSAppTransportSecurity property: %s', transportProperty);
break;
}
} // for loop on NSAppTransportSecurity
break; // NSAppTransportSecurity
case 'UIRequiresPersistentWiFi':
if (app[attrib] === true) {
usePersistentWifi++;
}
break;
case 'UIBackgroundModes':
for (let i = 0; i < app[attrib].length; i++) {
if (!(app[attrib][i] in apps.summary.UIBackgroundModes)) {
apps.summary.UIBackgroundModes[app[attrib][i]] = 0;
}
apps.summary.UIBackgroundModes[app[attrib][i]]++;
}
break;
case 'SignerIdentity':
if (app[attrib] !== 'Apple iPhone OS Application Signing') {
nonAppleSigner++;
}
break;
case 'ApplicationType':
switch (app[attrib]) {
case 'User':
userApps++;
break;
case 'System':
systemApps++;
break;
default:
break;
}
break;
default:
// otherwise ignore property for now
if (attrib.endsWith('UsageDescription')) {
logger.debug('found an app request access to privacy-sensitive data');
requestsForPrivacySensitiveDataAccess++;
if (!(attrib in apps.summary.privacySensitiveDataAccess)) {
apps.summary.privacySensitiveDataAccess[attrib] = 0;
}
apps.summary.privacySensitiveDataAccess[attrib]++;
}
break;
}
}
}
callback(null, 'finished processing app plist');
} catch (err) {
// could not read apps xml or hit plist parse error
logger.error('hit error processing app data: %s', err);
callback(err);
}
}
}, function (error, results) {
if (error) { logger.warn('could not read or parse app data'); }
// object for summary app data
apps.summary.totalApps = totalApps;
apps.summary.userApps = userApps;
apps.summary.systemApps = systemApps;
apps.summary.nonAppleSigner = nonAppleSigner;
apps.summary.appsWithEntitlements = appsWithEntitlements;
apps.summary.usePersistentWifi = usePersistentWifi;
apps.summary.requestsForPrivacySensitiveDataAccess = requestsForPrivacySensitiveDataAccess;
apps.summary.allowArbitraryLoads = allowArbitraryLoads;
apps.summary.allowArbitraryLoadsInWebContent = allowArbitraryLoadsInWebContent;
apps.summary.domainsAllowedArbitraryLoads = domainsAllowedArbitraryLoads;
// apps.summary.domainsForceTLSLoads = domainsForceTLSLoads;
logger.debug('installed apps xml processed, writing to %s', path.join(processedPath, 'installedApps.json'));
const parsedAppsJSON = JSON.stringify(apps);
fs.writeFile(processedPath + path.sep + 'apps.json', parsedAppsJSON, 'utf8', function (err) {
if (err) {
callback(null, 'error writing parsed app data to disk');
} else {
callback(null, 'wrote app data to disk');
}
});
});
}
function processDeviceInfo (dir, callback) {
const artifactPath = path.join(dir, 'artifacts');
const processedPath = path.join(dir, 'processed');
const deviceInfoXML = path.join(artifactPath, 'ideviceinfo.xml');
const device = {};
device.details = {};
async.parallel({
processDevice: function (callback) {
try {
// read and parse plist file
const obj = plist.parse(fs.readFileSync(deviceInfoXML, 'utf8'));
device.details.standard = obj;
callback(null, 'Processed main ideviceinfo plist');
} catch (err) {
// could not read device xml or hit plist parse error
callback(null, 'Hit snag processing main ideviceinfo plist');
}
},
processDomains: function (callback) {
fs.readdir(artifactPath, function (err, files) {
if (err) {
logger.error(err);
callback(null, 'Hit snags processing some/all ideviceinfo domain files');
} else {
async.each(files, function (file, callback) {
if (file.startsWith('ideviceinfo-')) {
try {
let domainName = file.slice(12, (file.length - 4)); // trim ideviceinfo- and .xml
let domainInfo = plist.parse(fs.readFileSync(path.join(artifactPath, file), 'utf8'));
logger.debug('for domain %s , data: %s', domainName, JSON.stringify(domainInfo));
device.details[domainName] = domainInfo;
callback(null, 'successful cb from async.each on domain ' + file); // cb for async.each
} catch (err) {
logger.error(err);
callback(null, 'error in cb from async.each on domain ' + file + '. Error: ' + err);
}
} else {
callback(null, 'not a domain file so skip');
}
}, function (err, results) {
if (err) { logger.err(err); }
logger.debug('results from ideviceinfo async.each cbs: %s', results);
callback(null, 'processed ideviceinfo domain files');
});
}
});
}
}, function (error, results) {
if (error) { logger.error(error); }
logger.debug('device info xml processed, writing to %s', path.join(processedPath, 'deviceInfo.json'));
const deviceJSON = JSON.stringify(device);
fs.writeFile(path.join(processedPath, 'device.json'), deviceJSON, 'utf8', function (err) {
if (err) {
callback(null, 'error writing parsed device and domain data to disk');
} else {
callback(null, 'wrote device info and domains to disk');
}
});
});
}
function processProvisioningProfiles (dir, callback) {
const artifactPath = path.join(dir, 'artifacts');
const processedPath = path.join(dir, 'processed');
const pprofilePath = path.join(artifactPath, 'pprofiles');
// const ideviceprovisionLog = path.join(pprofilePath, 'ideviceprovision.log');
const pprofiles = {};
pprofiles.details = [];
// now let's find all files in pprofilePath ending with .mobileprovision
// and then call `ideviceprovision --xml dump` on each to get details
fs.readdir(pprofilePath, function (err, files) {
if (err) {
return callback(new Error('Provisioning profiles data not processed: ' + err));
} else {
let count = 0;
async.each(files, function (file, callback) {
if (file.endsWith('.mobileprovision')) {
logger.debug('filename: %s', file);
count++;
// now let's parse the pprofile xml file into a json object
logger.debug('parsing %s', path.join(pprofilePath, file));
let obj = {};
try {
obj = plist.parse(fs.readFileSync(path.join(pprofilePath, file), 'utf8'));
pprofiles.details.push(obj);
} catch (err) {
/*
sometimes it appears ideviceprovision won't be able to list a pprofile
in this instance, let's surface to the user so addl inspection. sample output:
profile_get_embedded_plist: unexpected profile data (0)
(unknown id) - (no name)
*/
logger.debug('error reading a pprofile from the device, filename: %s', file);
obj.AppIDName = 'error reading pprofile ' + file + ' from device';
pprofiles.details.push(obj);
}
}
callback();
}, function (err) {
if (err) {
logger.error(err);
} else {
logger.debug('pprofiles found: %d', count);
pprofiles.summary = {
'pprofilesFound': count
};
logger.debug('pprofiles processed, writing to %s', path.join(processedPath, 'pprofiles.json'));
const pprofilesJSON = JSON.stringify(pprofiles);
fs.writeFile(path.join(processedPath, 'pprofiles.json'), pprofilesJSON, 'utf8', function (err) {
if (err) {
callback(null, 'error writing pprofile data to disk');
} else {
callback(null, 'wrote pprofile data to disk');
}
});
}
});
}
});
}
function processSyslog (dir, callback) {
const artifactPath = path.join(dir, 'artifacts');
const processedPath = path.join(dir, 'processed');
const syslogFile = path.join(artifactPath, 'syslog.txt');
try {
let count = 0;
fs.createReadStream(syslogFile)
.pipe(split())
.on('data', function (chunk) {
count++;
})
.on('end', function () {
const syslog = {};
syslog.summary = {
'lines': count
};
logger.debug('syslog processed, writing to %s', path.join(processedPath, 'syslog.json'));
logger.debug('syslog object: %s', JSON.stringify(syslog));
const syslogJSON = JSON.stringify(syslog);
fs.writeFile(path.join(processedPath, 'syslog.json'), syslogJSON, 'utf8', function (err) {
if (err) {
callback(null, 'error writing syslog data to disk');
} else {
callback(null, 'wrote syslog data to disk');
}
});
});
} catch (err) {
return new Error('Syslog data not processed: ' + err);
}
}
function processCrashReports (dir, callback) {
const artifactPath = path.join(dir, 'artifacts');
const processedPath = path.join(dir, 'processed');
const crashreportPath = path.join(artifactPath, 'crash_reports');
const crashreportLog = path.join(crashreportPath, 'crashlogs.txt');
try {
let count = 0;
const filenames = [];
fs.createReadStream(crashreportLog)
.pipe(split())
.on('data', function (line) {
if (line.startsWith('Copy: ')) {
count++;
// example line: Copy: DiagnosticLogs/security.log.20170119T084705Z
// split on ' ' and push the 2nd field to an array
filenames.push(line.split('Copy: ')[1]);
}
})
.on('end', function () {
const crashreports = {};
crashreports.summary = {
'reports': count,
'filenames': filenames
};
crashreports.details = [];
// read each log file to get properties and
for (let i = 0; i < crashreports.summary.filenames.length; i++) {
let filename = path.join(crashreportPath, crashreports.summary.filenames[i]);
logger.debug('time to get details on %s', filename);
let fileStats = fs.statSync(filename);
let preview = 'Empty file';
if (fileStats.size > 0) {
if (fileStats.size < 500) {
preview = readChunk.sync(filename, 0, fileStats.size);
} else {
preview = readChunk.sync(filename, 0, 500);
}
}
let fileDetails = {};
fileDetails.filename = crashreports.summary.filenames[i];
fileDetails.size = fileStats.size;
fileDetails.preview = preview.toString().split('\n');
crashreports.details.push(fileDetails);
}
// write processed artifact data
logger.debug('crash report data processed, writing to %s', path.join(processedPath, 'crashreports.json'));
logger.debug('crashreports object: %s', JSON.stringify(crashreports));
const crashreportsJSON = JSON.stringify(crashreports);
fs.writeFile(path.join(processedPath, 'crashreports.json'), crashreportsJSON, 'utf8', function (err) {
if (err) {
callback(null, 'error writing crash report data to disk');
} else {
callback(null, 'wrote crash report data to disk');
}
});
});
} catch (err) {
return new Error('Crash report data not processed: ' + err);
}
}
function processBackup (dir, callback) {
const artifactPath = path.join(dir, 'artifacts');
const processedPath = path.join(dir, 'processed');
const backupPath = path.join(artifactPath, 'backup');
const backupFile = path.join(backupPath, 'backup_log.txt');
const backup = {};
let backupFileCount = 0;
fs.createReadStream(backupFile)
// handled the error event before pipe, I guess order matters here
.on('error', function () {
// not flagging as error, just going to write a blank backup object
logger.info('Backup dir not found, skipping processing');
const backupJSON = JSON.stringify(backup);
fs.writeFile(path.join(processedPath, 'backup.json'), backupJSON, 'utf8', function (err) {
if (err) {
callback(null, 'error writing parsed backup data to disk');