-
Notifications
You must be signed in to change notification settings - Fork 2
/
almonwatch
executable file
·757 lines (711 loc) · 26 KB
/
almonwatch
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
#!/bin/bash
# almonwatch
# version 1.0 beta 5 rev 2
# set -x
# PS4=':$LINENO+'
export LANG=en_US.UTF-8
export PATH=$PATH:/usr/local/bin
if [[ $(id -u) != 0 ]] ; then
echo "Error: $0 needs to be run as root!" >&2
exit 1
fi
supportdir="/Library/Application Support/ALM"
dbloc="$supportdir/alm.db"
if [[ $1 == "remove" ]] || [[ $1 == "blacklist" ]] ; then
if ! [[ -f "$dbloc" ]] ; then
echo "Error: database does not exist!" >&2
exit
fi
if [[ $2 != "path" ]] && [[ $2 != "name" ]] && [[ $2 != "id" ]] ; then
echo "Error: wrong option - use 'path', 'name', or 'id'" >&2
fi
if [[ $1 == "remove" ]] ; then
shift
if [[ $1 == "path" ]] ; then
shift
echo "Removing: $@"
oldentry=$(sqlite3 "$dbloc" "select exists(select * from ALM where path = \"$@\");" 2>/dev/null)
if [[ $oldentry == "0" ]] ; then
echo "Error: no record found!"
exit
fi
sqlite3 "$dbloc" "delete from ALM where path=\"$@\";" 2>/dev/null
oldentry=$(sqlite3 "$dbloc" "select exists(select * from ALM where path = \"$@\");" 2>/dev/null)
[[ $oldentry == "0" ]] && echo "Success" || echo "Error!" >&2
elif [[ $1 == "name" ]] ; then
shift
echo "Removing: $@"
oldentry=$(sqlite3 "$dbloc" "select exists(select * from ALM where name=\"$@\");" 2>/dev/null)
if [[ $oldentry == "0" ]] ; then
echo "Error: no record found!"
exit
fi
sqlite3 "$dbloc" "delete from ALM where name=\"$@\";" 2>/dev/null
oldentry=$(sqlite3 "$dbloc" "select exists(select * from ALM where name=\"$@\");" 2>/dev/null)
[[ $oldentry == "0" ]] && echo "Success" || echo "Error!" >&2
elif [[ $1 == "id" ]] ; then
shift
echo "Removing: $1"
oldentry=$(sqlite3 "$dbloc" "select exists(select * from ALM where cfbid = \"$1\");" 2>/dev/null)
if [[ $oldentry == "0" ]] ; then
echo "Error: no record found!"
exit
fi
sqlite3 "$dbloc" "delete from ALM where cfbid=\"$1\";" 2>/dev/null
oldentry=$(sqlite3 "$dbloc" "select exists(select * from ALM where cfbid = \"$1\");" 2>/dev/null)
[[ $oldentry == "0" ]] && echo "Success" || echo "Error!" >&2
fi
elif [[ $1 == "blacklist" ]] ; then
shift
if [[ $1 == "path" ]] ; then
shift
echo "Blacklisting: $@"
oldentry=$(sqlite3 "$dbloc" "select exists(select * from ALM where path = \"$@\");" 2>/dev/null)
if [[ $oldentry == "0" ]] ; then
echo "Error: no record found!"
exit
fi
sqlite3 "$dbloc" "update ALM set whitelist = \"X\" where path=\"$@\";" 2>/dev/null
blstatus=$(sqlite3 "$dbloc" "select whitelist from ALM where path=\"$@\";" 2>/dev/null)
[[ $blstatus == "X" ]] && echo "Success" || echo "Error!" >&2
elif [[ $1 == "name" ]] ; then
shift
echo "Blacklisting: $@"
oldentry=$(sqlite3 "$dbloc" "select exists(select * from ALM where name=\"$@\");" 2>/dev/null)
if [[ $oldentry == "0" ]] ; then
echo "Error: no record found!"
exit
fi
sqlite3 "$dbloc" "update ALM set whitelist = \"X\" where name=\"$@\";" 2>/dev/null
blstatus=$(sqlite3 "$dbloc" "select whitelist from ALM where name=\"$@\";" 2>/dev/null)
[[ $blstatus == "X" ]] && echo "Success" || echo "Error!" >&2
elif [[ $1 == "id" ]] ; then
shift
echo "Blacklisting: $1"
if [[ $oldentry == "0" ]] ; then
echo "Error: no record found!"
exit
fi
sqlite3 "$dbloc" "update ALM set whitelist = \"X\" where cfbid=\"$1\";" 2>/dev/null
blstatus=$(sqlite3 "$dbloc" "select whitelist from ALM where cfbid=\"$1\";" 2>/dev/null)
[[ $blstatus == "X" ]] && echo "Success" || echo "Error!" >&2
fi
fi
exit
fi
# activate SystemEvents once to avoid missing notifications (also added in almhelper)
osascript -e 'tell application "System Events" to activate' 2>/dev/null
echo "************************************************"
echo "*** Application Launch Monitor (ALM) Watcher ***"
echo "************************************************"
localdate=$(date)
echo "Local date: $localdate"
account=$(id -un)
process="Application Launch Monitor"
# beep function
_beep () {
osascript -e 'beep'
}
# notify function
_notify () {
osascript &>/dev/null << EOT
tell application "System Events"
activate
display notification "$2" with title "$process [" & "$account" & "]" subtitle "$1"
end tell
EOT
}
# certificate & signature scan function
_certscan () {
if $unsigned ; then # no warning here (is in main section)
team="-"
anchor="-"
skid="-"
certid="-"
certsec="-"
else # create temp directory and read/export code signature information
posixdate=$(date +%s)
tmpdir="/tmp/alm-$appname-$posixdate"
mkdir "$tmpdir"
cd "$tmpdir"
csign=$(codesign -dvvvv --extract-certificates --requirements - "$filepath" 2>&1) # OUT: --entitlements -
cd /
designated=$(echo "$csign" | grep "^designated =>")
# anchor apple (incl. apple generic)
anchor_aag=$(echo "$designated" | grep "anchor apple generic")
if [[ $anchor_aag ]] ; then
anchor="apple generic"
else
anchor_aa=$(echo "$designated" | grep "anchor apple$")
if [[ $anchor_aa ]] ; then
anchor="apple"
else
anchor="-"
fi
fi
if [[ $anchor == "-" ]] ; then
if $dbentry ; then
oldanchor=$(sqlite3 "$dbloc" "select anchor from ALM where cfbid=\"$bundleid\";")
if [[ $anchor != $oldanchor ]] ; then # anchor has changed
osaissue="$osaissue\nrequirements: not signed by Apple"
errors=true
else
if $hashchange || [[ $wlstatus == 2 ]] ; then # executable change without apple anchor needs warning
osaissue="$osaissue\nrequirements: not signed by Apple"
errors=true
fi
fi
else # initial run: needs warning
osaissue="$osaissue\nrequirements: not signed by Apple"
errors=true
fi
fi
# team identifier (initial)
team=$(echo "$csign" | awk -F= '/^TeamIdentifier=/{print $2}')
! [[ $team ]] && team="-" # warnings for team "not set" or "-" will be dealt with further down
$dbentry && oldteam=$(sqlite3 "$dbloc" "select teamid from ALM where cfbid=\"$bundleid\";")
# subject key identifier & ad-hoc-signed & unsigned
certfsize=$(stat -f%z "$tmpdir/codesign0" 2>/dev/null)
if [[ -f "$tmpdir/codesign0" ]] && [ $certfsize -gt 0 ] ; then # certificate(s) found
# list of extracted certificates
certlist=$(find "$tmpdir" -mindepth 1)
leafcert=$(echo "$certlist" | head -1)
rootcert=$(echo "$certlist" | tail -r | head -1)
if [[ $rootcert == $leafcert ]] ; then # no certificate chain: probably self-issued/signed
if ! $dbentry || $hashchange || [[ $wlstatus == 2 ]] ; then # warning for initial scan or executable change
osaissue="$osaissue\nchain: no root certificate"
errors=true
fi
fi
# verify code signing using the root certificate: Apple System leafs might throw an error
certsecall=$(security verify-cert -c "$rootcert" 2>&1)
if ! [[ $certsecall ]] ; then # no security info
certsec="-"
if $dbentry ; then
oldcertsec=$(sqlite3 "$dbloc" "select certsec from ALM where cfbid=\"$bundleid\";")
if [[ $certsec != $oldcertsec ]] ; then # security info has changed
osaissue="$osaissue\nsecurity: new assessment"
errors=true
else
if $hashchange || [[ $wlstatus == 2 ]] ; then # executable change without security info needs warning
osaissue="$osaissue\nsecurity: no assessment available"
errors=true
fi
fi
else # initial run needs warning if no security info
osaissue="$osaissue\nsecurity: no assessment available"
errors=true
fi
else # security info printed
if [[ $certsecall != "...certificate verification successful." ]] ; then # there were errors
if [[ $(echo "$certsecall" | grep "Cert Verify Result:") != "" ]] ; then
certsec=$(echo "$certsecall" | awk -F": " '{print $2}')
else
certsec="$certsecall" # non-standard security error
fi
if $dbentry ; then
oldcertsec=$(sqlite3 "$dbloc" "select certsec from ALM where cfbid=\"$bundleid\";")
if [[ $certsec != $oldcertsec ]] ; then # security info changed
osaissue="$osaissue\ncertificate: $certsec"
errors=true
else
if $hashchange || [[ $wlstatus == 2 ]] ; then # executable change with security error needs warning
osaissue="$osaissue\ncertificate: $certsec"
errors=true
fi
fi
else # initial run needs warning on security error
osaissue="$osaissue\ncertificate: $certsec"
errors=true
fi
else # all good
certsec="success"
# read Root CA subject to check for Apple Root CA in tandem with successful verification
rootca=$(openssl x509 -in "$rootcert" -inform DER -noout -text -fingerprint | grep "Subject: " | xargs | awk -F"CN=" '{print $2}')
if [[ $rootca == "Apple Root CA" ]] && [[ $anchor == "apple" ]] ; then
if [[ $team == "not set" ]] ; then
team="Apple System" # change team ID to distinguish from self-issued certificates with team ID "not set"
fi
fi
fi
fi
# subject key identifier (SKID)
skid=$(openssl x509 -in "$leafcert" -inform DER -noout -text -fingerprint | grep -A1 "Subject Key Identifier" | tail -1 | xargs | sed s/://g)
if ! [[ $skid ]] ; then
noskid=true
if [[ $(echo "$csign" | grep "^Signature=adhoc") != "" ]] ; then
skid="adhoc"
else
skid="-"
fi
else
noskid=false
fi
if $dbentry ; then # database entry present
oldskid=$(sqlite3 "$dbloc" "select skid from ALM where cfbid=\"$bundleid\";")
if [[ $skid == $oldskid ]] ; then # SKID unchanged
if $hashchange || [[ $wlstatus == 2 ]] ; then # executable change with no SKID or adhoc needs warning
if [[ $skid == "-" ]] ; then
osaissue="$osaissue\ncertificate: no SKID"
errors=true
elif [[ $skid == "adhoc" ]] ; then
osaissue="$osaissue\nsignature: ad-hoc"
errors=true
fi
fi
else # SKID has changed
if $noskid ; then
if [[ $skid == "adhoc" ]] ; then
osaissue="$osaissue\nsignature: ad-hoc"
errors=true
else
osaissue="$osaissue\ncertificate: no SKID"
errors=true
fi
else
if [[ $wlstatus == 2 ]] ; then # warn of SKID change in manually whitelisted apps
if [[ $team == $oldteam ]] ; then
osaissue="$osaissue\ncertificate: SKID has changed"
errors=true
else
osaissue="$osaissue\ncertificate: SKID has changed\ncertificate: Team ID has changed"
errors=true
fi
else
if [[ $team == $oldteam ]] ; then # same team ID info in db
if [[ $team == "-" ]] || [[ $team == "not set" ]] ; then # SKID change with no Team ID needs warning
osaissue="$osaissue\ncertificate: SKID has changed"
errors=true
fi
else # team ID info in db is different
osaissue="$osaissue\ncertificate: SKID has changed\ncertificate: Team ID has changed"
errors=true
fi
fi
fi
fi
else # no db entry: warnings for adhoc & no SKID
if [[ $skid == "adhoc" ]] ; then
osaissue="$osaissue\nsignature: ad-hoc"
errors=true
elif [[ $skid == "-" ]] ; then
osaissue="$osaissue\ncertificate: no SKID"
errors=true
fi
fi
else # no certificate(s) found
skid="-"
certsec="-"
if $hashchange || [[ $wlstatus == 2 ]] ; then # executable change with no certificate(s) needs warning
osaissue="$osaissue\ncertificate: no SKID\ncertificate: no Root CA"
errors=true
fi
fi
# team ID (final)
if $dbentry ; then
if [[ $team != $oldteam ]] ; then # team ID change
osaissue="$osaissue\ncertificate: Team ID has changed"
errors=true
else # no team ID change
if $hashchange || [[ $wlstatus == 2 ]] ; then # executable change with no team ID needs warning
if [[ $team == "not set" ]] || [[ $team == "-" ]] ; then
osaissue="$osaissue\nsignature: no Team ID"
errors=true
fi
fi
fi
else
if [[ $team == "not set" ]] || [[ $team == "-" ]] ; then # excluding Apple System "not set"
osaissue="$osaissue\nsignature: no Team ID"
errors=true
fi
fi
# certificate Bundle ID
certid=$(echo "$csign" | awk -F"=" '/^Identifier=/{print $2}') # ID from default signature output
reqid=$(echo "$designated" | awk -F"identifier" '{print $2}' | awk -F\" '{print $2}') # ID from requirements
if ! [[ $certid ]] || ! [[ $reqid ]] ; then # at least one ID is missing
certid="missing"
idmatch=false
else
if [[ $bundleid == $certid ]] && [[ $bundleid == $reqid ]] ; then # both signature IDs match bundle ID
idmatch=true
else # there's a mismatch
certid="mismatch"
idmatch=false
fi
fi
if ! $idmatch ; then
if $dbentry ; then # database entry detected
oldcid=$(sqlite3 "$dbloc" "select certid from ALM where cfbid=\"$bundleid\";")
if [[ $certid != $oldcid ]] ; then # do not warn if current $certid is not missing or mismatch
if [[ $certid == "missing" ]] ; then
osaissue="$osaissue\nsignature: at least one Bundle ID missing"
errors=true
elif [[ $certid == "mismatch" ]] ; then
osaissue="$osaissue\nsignature: Bundle ID mismatch"
errors=true
fi
else
if $hashchange || [[ $wlstatus == 2 ]] ; then # executable change with bundle ID errors needs warning
if [[ $certid == "missing" ]] ; then
osaissue="$osaissue\nsignature: at least one Bundle ID missing"
errors=true
elif [[ $certid == "mismatch" ]] ; then
osaissue="$osaissue\nsignature: Bundle ID mismatch"
errors=true
fi
fi
fi
else # initial run needs warning
if [[ $certid == "missing" ]] ; then
osaissue="$osaissue\nsignature: at least one Bundle ID missing"
errors=true
elif [[ $certid == "mismatch" ]] ; then
osaissue="$osaissue\nsignature: Bundle ID mismatch"
errors=true
fi
fi
fi
# remove extracted certificates
rm -rf "$tmpdir" 2>/dev/null
fi
}
# other scans
_otherscan () {
oldpath=$(sqlite3 "$dbloc" "select path from ALM where cfbid=\"$bundleid\";")
if [[ $filepath != $oldpath ]] ; then
if $hashchange || $gkerror ; then # allow for user manually moving the application
osaissue="$osaissue\nbundle: filepath has changed"
errors=true
fi
fi
oldname=$(sqlite3 "$dbloc" "select name from ALM where cfbid=\"$bundleid\";")
if [[ $oldname != $appname ]] ; then
osaissue="$osaissue\nbundle: name has changed"
errors=true
fi
}
# AppleScript prompt function
_prompt () {
osascript 2>/dev/null << EOT
tell application "System Events"
activate
set theUserChoice to button returned of (display dialog "$1" & return & return & "$2" ¬
buttons {"Abort Launch", "Launch Once", "Whitelist"} ¬
default button 1 ¬
cancel button "Abort Launch" ¬
with title "Application Launch Monitor" ¬
with icon alias ((path to system folder as text) & "Library:PreferencePanes:Security.prefPane:Contents:Resources:FileVault.icns") ¬
giving up after 300)
end tell
EOT
}
# whitelist function
_whitelist () {
if [[ $1 == add ]] ; then
sqlite3 "$dbloc" "update ALM set whitelist=\"1\" where cfbid=\"$bundleid\";"
elif [[ $1 == remove ]] ; then
sqlite3 "$dbloc" "update ALM set whitelist=\"0\" where cfbid=\"$bundleid\";"
elif [[ $1 == manual ]] ; then
sqlite3 "$dbloc" "update ALM set whitelist=\"2\" where cfbid=\"$bundleid\";"
fi
}
# set app to frontmost
_frontmost () {
osascript &>/dev/null << EOT
tell application "System Events"
tell process "$1"
set frontmost to true
end tell
end tell
EOT
}
! [[ -d "$supportdir" ]] && mkdir "$supportdir"
! [[ -d "$supportdir/bin" ]] && mkdir "$supportdir/bin"
if ! [[ -f "$dbloc" ]] ; then
sqlite3 "$dbloc" "create table ALM(cfbid TEXT, path TEXT, name TEXT, teamid TEXT, anchor TEXT, skid TEXT, certid TEXT, hash TEXT, whitelist TEXT, spctl TEXT, certsec TEXT, misc1 TEXT, misc2 TEXT, misc3 TEXT);"
chown root:wheel "$dbloc"
fi
_almonwatch () {
while read notification ; do
if [[ $notification == "Listening..." ]] ; then
echo "almon is running..."
afplay "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/payment_success.aif" &
_notify "🔎 ALM has started…"
continue
elif [[ $notification == "Stopping..." ]] ; then
echo "almon has exited."
_beep &
_notify "⁉️ ALM has exited!" "Something went wrong…"
break
else
echo -e "\n*** Launch detected ***"
# blank settings
errors=false
unsigned=false
pid=""
bundleid=""
filepath=""
appname=""
teamid=""
skid=""
exehash=""
wlstatus=""
# parse input
pid=$(echo "$notification" | awk -F":::" '{print $2}')
appname=$(echo "$notification" | awk -F":::" '{print $5}')
bundleid=$(echo "$notification" | awk -F":::" '{print $4}')
nsworkspace=$(echo "$notification" | awk -F":::" '{print $1}')
nsworkspace2=$(echo "$notification" | awk -F":::" '{print substr($0, index($0,$3))}')
# parse for filepath & check
filepath=$(echo "$notification" | awk -F":::" '{print $3}')
filename=$(basename "$filepath")
[[ -h "$filepath" ]] && filepath=$(perl -MCwd -e 'print Cwd::abs_path shift' "$filepath")
mdls_output=$(mdls -name kMDItemContentTypeTree "$filepath")
if ! [[ -d "$filepath" ]] || ! [[ $(echo "$mdls_output" | grep "\"com.apple.package\",") ]] ; then
filelaunch=true
echo "$filename: error - no com.apple.package"
if [[ $(echo "$mdls_output" | grep "\"public.executable\",") ]] ; then
echo "Executable launched: $filepath"
else
echo "Other file launched: $filepath"
fi
executable="$filename"
exepath="$filepath"
originalname="$appname"
appname="$filename"
else
filelaunch=false
echo "$filename: com.apple.package"
# determine executable (package)
plistloc="$filepath/Contents/Info.plist"
executable=$(plutil -p "$plistloc" | awk -F\" '/CFBundleExecutable/{print $4}')
exepath="$filepath/Contents/MacOS/$executable"
fi
# initial log
if ! $filelaunch ; then
echo -e "Name: $appname\nBundle ID: $bundleid\nPath: $filepath\nProcess ID: $pid\nExecutable: $exepath\nNSWorkspace: $nsworkspace\nNSWorkspace: $nsworkspace2"
else
echo -e "Original name: $originalname\nName: $appname\nBundle ID: $bundleid\nPath: $filepath\nProcess ID: $pid\nExecutable: $exepath\nNSWorkspace: $nsworkspace\nNSWorkspace: $nsworkspace2"
fi
# check whitelist status
wlstatus=$(sqlite3 "$dbloc" "select whitelist from ALM where cfbid=\"$bundleid\";" 2>/dev/null)
if ! [[ $wlstatus ]] ; then
dbentry=false
whitelisted=false
echo "$appname: no database entry found!"
else
echo "$appname: found database entry"
if [[ $wlstatus == "X" ]] ; then
echo "$appname is blacklisted: aborting launch..."
_beep &
_notify "❌ Blacklisted! Aborting launch…" "$appname"
osascript -e 'tell application "$appname" to quit' && { sleep 3 ; kill -s KILL $pid 2>/dev/null ; }
continue
fi
dbentry=true
if [[ $wlstatus == 1 ]] ; then
whitelisted=true
echo "$appname: set to auto-whitelist"
elif [[ $wlstatus == 2 ]] ; then
whitelisted=true
wloverride=true
echo "$appname: set to manual whitelist"
else
whitelisted=false
echo "$appname: not whitelisted"
fi
fi
# executable hashes
exehash=$(shasum -a 256 "$exepath" | awk '{print $1}')
if $dbentry ; then
oldhash=$(sqlite3 "$dbloc" "select hash from ALM where cfbid=\"$bundleid\";" 2>/dev/null)
if [[ $exehash != $oldhash ]] ; then
hashchange=true
wloverride=false
wlstatus="0"
echo "Executable: $oldhash > $exehash"
else
hashchange=false
echo "Executable: hashes match"
fi
else
hashchange=false
fi
# Gatekeeper assessment
physize=$(mdls -name kMDItemPhysicalSize "$filepath" | awk -F" = " '{print $2}')
if ! $dbentry ; then
if [[ $physize ]] && [[ $physize -gt 3500000000 ]] ; then
spctl_deep="skipped"
else
spctl_deep=$(codesign --verify --deep "$filepath" 2>&1 | head -1)
fi
else
if [[ $physize ]] && [[ $physize -gt 3500000000 ]] ; then
if $hashchange ; then
spctl_deep=$(codesign --verify --deep "$filepath" 2>&1 | head -1)
else
spctl_deep="skipped"
fi
else
spctl_deep=$(codesign --verify --deep "$filepath" 2>&1 | head -1)
fi
fi
if [[ $spctl_deep ]] ; then
if [[ $spctl_deep != "skipped" ]] ; then
[[ $(echo "$spctl_deep" | grep "code object is not signed at all") ]] && unsigned=true || unsigned=false
gk_issue=$(echo "$spctl_deep" | awk -F": " '{print substr($0, index($0,$2))}')
gkerror=true
if ! $dbentry ; then
osaissue="codesign: $gk_issue"
errors=true
fi
else
gkerror=true
gk_issue="skipped"
if ! $dbentry ; then
osaissue="codesign: $gk_issue"
errors=true
fi
fi
else
spctl_deep="-"
gk_issue="-"
gkerror=false
fi
# qualitative assessment
if $dbentry ; then
oldspctl=$(sqlite3 "$dbloc" "select spctl from ALM where cfbid=\"$bundleid\";" 2>/dev/null)
! [[ $oldspctl ]] && oldspctl="-"
if [[ $oldspctl == "skipped" ]] ; then
### if [[ $gk_issue != $oldspctl ]] ; then # different GK assessment, previously skipped, not skipped now, e.g. due to hashchange
echo "do something" ###
else
if [[ $gk_issue == $oldspctl ]] ; then # same GK assessment
if ! $hashchange ; then # executable unchanged
if [[ $gk_issue != "-" ]] ; then # not a benign bundle
osaissue="codesign: $gk_issue"
errors=true
if $whitelisted && [[ $wlstatus == 2 ]] ; then
wloverride=true
fi
fi
else # executable modified
if [[ $gk_issue != "-" ]] ; then # not a bening bundle; no whitelisting due to executable change
osaissue="codesign: $gk_issue\nbundle: executable has been modified"
errors=true
wloverride=false
wlstatus="0"
fi
fi
else # different GK assessment
if [[ $gk_issue != "-" ]] ; then # possible malware
wloverride=false
wlstatus="0"
osaissue="codesign: $gk_issue"
$hashchange && osaissue="$osaissue\nbundle: executable has been modified"
errors=true
fi
fi
fi
else
wloverride=false
wlstatus="0"
fi
# run additional scans
_certscan
if $dbentry ; then
_otherscan
if $errors ; then
if $wloverride && [[ $wlstatus == 2 ]] ; then
sqlite3 "$dbloc" "update ALM set path = \"$filepath\", name = \"$appname\", teamid = \"$team\", anchor = \"$anchor\", skid = \"$skid\", certid = \"$certid\", hash = \"$exehash\", whitelist = \"2\", spctl = \"$gk_issue\", certsec = \"$certsec\" where cfbid = \"$bundleid\";"
else
sqlite3 "$dbloc" "update ALM set path = \"$filepath\", name = \"$appname\", teamid = \"$team\", anchor = \"$anchor\", skid = \"$skid\", certid = \"$certid\", hash = \"$exehash\", whitelist = \"0\", spctl = \"$gk_issue\", certsec = \"$certsec\" where cfbid = \"$bundleid\";"
fi
else
if $wloverride && [[ $wlstatus == 2 ]] ; then
wloverride=false
wlstatus="1"
fi
sqlite3 "$dbloc" "update ALM set path = \"$filepath\", name = \"$appname\", teamid = \"$team\", anchor = \"$anchor\", skid = \"$skid\", certid = \"$certid\", hash = \"$exehash\", whitelist = \"1\", spctl = \"$gk_issue\", certsec = \"$certsec\" where cfbid = \"$bundleid\";"
fi
else
if $errors ; then
sqlite3 "$dbloc" "insert into ALM (cfbid,path,name,teamid,anchor,skid,certid,hash,whitelist,spctl,certsec) values (\"$bundleid\",\"$filepath\",\"$appname\",\"$team\",\"$anchor\",\"$skid\",\"$certid\",\"$exehash\",\"0\",\"$gk_issue\",\"$certsec\");"
else
sqlite3 "$dbloc" "insert into ALM (cfbid,path,name,teamid,anchor,skid,certid,hash,whitelist,spctl,certsec) values (\"$bundleid\",\"$filepath\",\"$appname\",\"$team\",\"$anchor\",\"$skid\",\"$certid\",\"$exehash\",\"1\",\"$gk_issue\",\"$certsec\");"
fi
fi
# notification & user prompt
export supportdir
export notification
if $errors ; then
osaissue=$(echo -e "$osaissue" | grep -v "^$" | awk '!seen[$0]++')
echo -e "$appname: there are issues!\n---"
echo -e "$osaissue\n---"
if $wloverride ; then
echo "Whitelist override: true"
if [[ $gk_issue != "skipped" ]] ; then
_notify "☑️ Whitelisted with issues" "$appname"
else
_notify "☑️ Whitelisted: skipped" "$appname"
fi
kill -s CONT $pid && _frontmost "$appname"
if ! $hashchange ; then
bash -c '"$supportdir"/bin/run "$notification:::2:::samehash"' 2>/dev/null &
else
bash -c '"$supportdir"/bin/run "$notification:::2:::newhash"' 2>/dev/null &
fi
else
echo "Whitelist override: false"
osaprompt="The application '$appname' with the Bundle ID '$bundleid' has the following issues:"
_beep &
_notify "⚠️ There are issues!" "$appname"
userchoice=$(_prompt "$osaprompt" "$osaissue")
if [[ $userchoice == "Whitelist" ]] ; then
echo "Whitelisting: $appname"
_whitelist manual
kill -s CONT $pid && _frontmost "$appname"
if ! $hashchange ; then
bash -c '"$supportdir"/bin/run "$notification:::2:::samehash"' 2>/dev/null &
else
bash -c '"$supportdir"/bin/run "$notification:::2:::newhash"' 2>/dev/null &
fi
elif [[ $userchoice == "Launch Once" ]] ; then
_whitelist remove
echo "Launching once: $appname"
kill -s CONT $pid && _frontmost "$appname"
if ! $hashchange ; then
bash -c '"$supportdir"/bin/run "$notification:::0:::samehash"' 2>/dev/null &
else
bash -c '"$supportdir"/bin/run "$notification:::0:::newhash"' 2>/dev/null &
fi
else
_whitelist remove
echo "Aborting launch: $appname"
osascript -e 'tell application "$appname" to quit' && { sleep 3 ; kill -s KILL $pid 2>/dev/null ; }
if ! $hashchange ; then
bash -c '"$supportdir"/bin/run "$notification:::0:::samehash"' 2>/dev/null &
else
bash -c '"$supportdir"/bin/run "$notification:::0:::newhash"' 2>/dev/null &
fi
fi
fi
else
_whitelist add
echo "$appname: no issues"
_notify "✅ Verified" "$appname"
kill -s CONT $pid && _frontmost "$appname"
if ! $hashchange ; then
bash -c '"$supportdir"/bin/run "$notification:::1:::samehash"' 2>/dev/null &
else
bash -c '"$supportdir"/bin/run "$notification:::1:::newhash"' 2>/dev/null &
fi
fi
fi
done
}
echo "Listening for application launch notifications..."
_almonwatch
echo "*** Exiting... ***"
exit 0