-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathboko.py
1152 lines (1002 loc) · 54.6 KB
/
boko.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
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
# Author: Jesse Nebling (@bashexplode)
# The dylib scanning functions are based off of Patrick Wardle's tool Dylib Hijack Scanner
#
# boko.py is a static application scanner for macOS that searches for and identifies
# potential dylib hijacking and weak dylib vulnerabilities for application executables, as well as
# identifies scripts an application may use that have the potential to be backdoored. It also calls out interesting
# files and lists them instead of manually browsing the file system for analysis.
#
# Dictionary format:
# Filepath: {
# 'writeable': Bool, # indicates whether or not the current user that ran the tool has write permissions on the file
# 'execution': { # dictionary for execution output, only applicable if the --active or --both flag is used
# 'standardoutput': [],
# 'erroroutput' []
# },
# 'load': {
# 'LC_RPATHs': [],
# 'LC_LOAD_WEAK_DYLIBs': [],
# 'LC_LOAD_DYLIBs': []
# },
# 'filetypeh': 'String', # indicates the file type as a string : Executable, Dylib, Bundle, KextBundle, Script, Misc (based on file extension)
# 'filetype': mach_header filetype,
# 'parse': 'String', # indicates if the file was an executable and was parsed for weaknesses
# 'filename': 'String', # File name without full path
# 'vulnerable': {
# 'WeakDylib': [ # list of weak dylibs, each weak dylib has its own dictionary
# {
# 'Certainty': 'String', # indicates how certain the vulnerability exists based on load path ordering and file type : Definite, High, Potential, Low
# 'hijackPath': 'String', # full path a malicious dylib can be placed to hijack the load order of the base file
# 'WriteAccess': Bool, # indicates whether or not the current user that ran the tool can write to the hijackPath
# 'LoadOrder': int, # indicates the order in which the main binary Filename loads the dylib relative path, starts at 0
# 'ReadOnlyPartition': False # indicates whether or not the directory is in a SIP-protected partition
# }
# ],
# 'DylibHijack': [ # list of hijackable dylibs, each dylib has its own dictionary
# {
# 'Certainty': 'String', # indicates how certain the vulnerability exists based on load path ordering and file type : Definite, High, Potential, Low
# 'hijackPath': 'String', # full path a malicious dylib can be placed to hijack the load order of the base file
# 'WriteAccess': Bool, # indicates whether or not the current user that ran the tool can write to the hijackPath
# 'LoadOrder': int, # indicates the order in which the main binary Filename loads the dylib relative path, starts at 0
# 'ReadOnlyPartition': False # indicates whether or not the directory is in a SIP-protected partition
# }
# ],
# 'BackdoorableScript': [ # list of potentially backdoorable scripts, each script has its own dictionary
# {
# 'Certainty': 'String', # indicates how certain the vulnerability exists based on load path ordering and file type : Definite, High, Potential, Low
# 'hijackPath': 'String', # full path a malicious dylib can be placed to hijack the load order of the base file
# 'WriteAccess': Bool, # indicates whether or not the current user that ran the tool can write to the hijackPath
# 'LoadOrder': int, # indicates the order in which the main binary Filename loads the dylib relative path, starts at 0
# 'ReadOnlyPartition': False # indicates whether or not the directory is in a SIP-protected partition
# }
# ]
# }
# }
from __future__ import print_function
import ctypes
import argparse
import os
import sys
import io
import struct
import psutil
import subprocess
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
import threading
screenlock = threading.Semaphore(value=1)
class mach_header(ctypes.Structure):
_fields_ = [
("magic", ctypes.c_uint),
("cputype", ctypes.c_uint),
("cpusubtype", ctypes.c_uint),
("filetype", ctypes.c_uint),
("ncmds", ctypes.c_uint),
("sizeofcmds", ctypes.c_uint),
("flags", ctypes.c_uint)
]
class mach_header_64(ctypes.Structure):
_fields_ = mach_header._fields_ + [('reserved', ctypes.c_uint)]
class load_command(ctypes.Structure):
_fields_ = [
("cmd", ctypes.c_uint),
("cmdsize", ctypes.c_uint)
]
class ExecutableScanner:
def __init__(self, verbose, sipdisabled):
self.verbose = verbose
self.results = {}
self.sipreadonlydefaults = ['//',
'/.fseventsd',
'/.vol',
'/System',
'/System/Applications',
'/System/DriverKit',
'/System/Library',
'/System/Volumes',
'/System/iOSSupport',
'/bin',
'/private/var',
'/sbin',
'/usr',
'/usr/bin',
'/usr/lib',
'/usr/libexec',
'/usr/sbin',
'/usr/share',
'/usr/standalone',
'/Library/Developer/CommandLineTools/SDKs/']
self.uninterestingexts = ['.3pm', '.3', '.3tcl', '.3x', '.3g', '.aiff', '.dll', '.doc', '.dub', '.dylib', '.filters', '.frag', '.gif', '.html',
'.icns', '.idm', '.iqy', '.lab', '.lex', '.manifest', '.meta', '.metainfo',
'.mp4', '.nls', '.node', '.nrr', '.odf', '.olb', '.plist', '.png', '.ppd', '.ppt', '.pst', '.qml',
'.qmltypes', '.qrc', '.svg', '.tib', '.tiff', '.tlb', '.transition', '.ttc', '.ttf', '.typ',
'.vert', '.wmf', '.xlam', '.xll', '.xls', '.jpg', '.jpeg', '.bmp', '.css', '.tif', '.nib', '.strings', '.tcl', '.wav', '.pcm', '.mp3']
self.knownpopscriptexts = ['.applescript', '.scpt', '.command', '.sh', '.py', '.rb', '.pl', '.lua', '.jsp',
'.jxa', '.php']
if sipdisabled:
self.readonly = None
self.sipdisabled = True
else:
self.readonly = self.sipreadonlydefaults
self.sipdisabled = False
# supported archs
self.SUPPORTED_ARCHITECTURES = ['i386', 'x86_64']
self.LC_REQ_DYLD = 0x80000000
self.LC_LOAD_WEAK_DYLIB = self.LC_REQ_DYLD | 0x18
self.LC_RPATH = (0x1c | self.LC_REQ_DYLD)
self.LC_REEXPORT_DYLIB = 0x1f | self.LC_REQ_DYLD
(
self.LC_SEGMENT, self.LC_SYMTAB, self.LC_SYMSEG, self.LC_THREAD, self.LC_UNIXTHREAD, self.LC_LOADFVMLIB,
self.LC_IDFVMLIB, self.LC_IDENT, self.LC_FVMFILE, self.LC_PREPAGE, self.LC_DYSYMTAB, self.LC_LOAD_DYLIB,
self.LC_ID_DYLIB, self.LC_LOAD_DYLINKER, self.LC_ID_DYLINKER, self.LC_PREBOUND_DYLIB,
self.LC_ROUTINES, self.LC_SUB_FRAMEWORK, self.LC_SUB_UMBRELLA, self.LC_SUB_CLIENT,
self.LC_SUB_LIBRARY, self.LC_TWOLEVEL_HINTS, self.LC_PREBIND_CKSUM
) = range(0x1, 0x18)
self.MH_MAGIC = 0xfeedface
self.MH_CIGAM = 0xcefaedfe
self.MH_MAGIC_64 = 0xfeedfacf
self.MH_CIGAM_64 = 0xcffaedfe
self._CPU_ARCH_ABI64 = 0x01000000
self.CPU_TYPE_NAMES = {
-1: 'ANY',
1: 'VAX',
6: 'MC680x0',
7: 'i386',
self._CPU_ARCH_ABI64 | 7: 'x86_64',
8: 'MIPS',
10: 'MC98000',
11: 'HPPA',
12: 'ARM',
13: 'MC88000',
14: 'SPARC',
15: 'i860',
16: 'Alpha',
18: 'PowerPC',
self._CPU_ARCH_ABI64 | 18: 'PowerPC64',
}
# executable binary
self.MH_EXECUTE = 2
# dylib
self.MH_DYLIB = 6
# bundles
self.MH_BUNDLE = 8
# kext bundle
self.MH_KEXT_BUNDLE = 0xb
self.LC_Header_Sz = 0x8
def isSupportedArchitecture(self, machoHandle):
machoHandle.seek(0)
headersz = 28
header64sz = 32
supported = False
header = ""
try:
magic = struct.unpack('<L', machoHandle.read(4))[0]
machoHandle.seek(0, io.SEEK_SET)
if magic == self.MH_MAGIC or magic == self.MH_CIGAM:
headert = mach_header.from_buffer_copy(machoHandle.read(headersz))
# print("CPUType: %s" % headert.cputype)
if self.CPU_TYPE_NAMES.get(headert.cputype) == 'i386':
supported = True
header = headert
elif magic == self.MH_MAGIC_64 or magic == self.MH_CIGAM_64:
headert = mach_header_64.from_buffer_copy(machoHandle.read(header64sz))
# print("CPUType: %s" % headert.cputype)
if self.CPU_TYPE_NAMES.get(headert.cputype) == 'x86_64':
supported = True
header = headert
else:
header = None
except:
pass
return supported, header
def scriptCheck(self, file, filename, fullpath):
file.seek(0)
if file.read(2) == '#!':
contents = file.read(50)
scripttype = contents.split('\n')[0].split('/')[-1]
file.seek(0)
self.results[fullpath]["script"] = scripttype
self.results[fullpath]["parse"] = "Script"
self.results[fullpath]['filetypeh'] = "Script"
return True
elif '.' in filename and os.path.splitext(filename)[-1] in self.knownpopscriptexts:
self.results[fullpath]["script"] = filename.split('.')[-1]
self.results[fullpath]["parse"] = "Script"
self.results[fullpath]['filetypeh'] = "Script"
return True
else:
return False
def initializeDictionaryItem(self, filename, filepath, parse):
self.results[filepath] = {}
self.results[filepath]["filename"] = filename
self.results[filepath]["parse"] = parse
self.results[filepath]["mode"] = "Passive"
self.writeCheck(filepath)
def writeCheck(self, filepath):
if os.access(filepath, os.W_OK):
self.results[filepath]["writeable"] = True
else:
self.results[filepath]["writeable"] = False
def readWriteCheck(self, rpath):
# Check if current user context has write permissions to the last existing path
lastexistingpath = '/'
for i in range(2, len(rpath.split('/'))):
checkpath = '/'.join(rpath.split('/')[0:i])
if os.path.exists(checkpath):
lastexistingpath = checkpath
if os.access(lastexistingpath, os.W_OK):
contextwriteperm = True
else:
contextwriteperm = False
# Check if rpath is in the read only partition
if rpath.startswith(tuple(self.readonly)):
readonlypartition = True
else:
readonlypartition = False
return readonlypartition, contextwriteperm
def loadedBinaries(self):
if self.verbose:
print("[*] Identifying potential hijackable processes currently running")
for proc in psutil.process_iter(['pid', 'name', 'username', 'exe']):
if proc.info["exe"] is not None and not proc.info["exe"].startswith(tuple(self.readonly)):
if proc.info["name"] not in self.results.keys():
self.initializeDictionaryItem(str(proc.info["name"]).strip(), str(proc.info["exe"]).strip(), True)
if self.verbose:
print("[+] Found: %s" % proc.info["exe"])
if self.verbose:
print("[+] Finished gathering running processes in %s")
def installedBinaries(self, rootDirectory='/'):
if self.verbose:
print("[*] Identifying potential hijackable process in %s" % rootDirectory)
applications = []
if '.app' in rootDirectory:
indices = [i for i, x in enumerate(rootDirectory.split('/')) if ".app" in x]
applications.append('.'.join(rootDirectory.split('/')[indices[-1]].split('.')[:-1]))
for root, dirnames, filenames in os.walk(rootDirectory):
if root.startswith(tuple(self.readonly)):
continue
for filename in filenames:
fullName = os.path.realpath(os.path.join(root, filename))
if not os.path.isfile(fullName):
continue
if '.app' in fullName:
indices = [i for i, x in enumerate(rootDirectory.split('/')) if ".app" in x]
if len(indices) > 0:
appname = '.'.join(rootDirectory.split('/')[indices[-1]].split('.')[:-1])
if appname not in applications:
applications.append(appname)
for filecheck in self.results.keys():
try:
if fullName in self.results[filecheck]["fullpath"]:
continue
except KeyError:
pass
# Check if file is executable and not an uninteresting file
if (os.access(fullName, os.X_OK) and os.path.splitext(fullName)[
-1].lower() not in self.uninterestingexts) or (os.path.splitext(fullName)[-1].lower() not in self.uninterestingexts and os.path.splitext(fullName)[-1].lower() in self.knownpopscriptexts):
# If the files an normally named executable or script add to parsing list and check if current
# user context can write to the file
if os.path.splitext(fullName)[-1] == '.dyblib' or os.path.splitext(fullName)[-1] == '' or \
os.path.splitext(fullName)[-1] in self.knownpopscriptexts or os.path.split(fullName)[-1].startswith(tuple(applications)):
self.initializeDictionaryItem(filename, fullName, True)
if self.verbose:
print("[+] Found executable file: %s" % fullName)
# If the file doesn't meet the above criteria save as a potential interesting file to dict.
else:
self.initializeDictionaryItem(filename, fullName, False)
if '.' in filename:
self.results[fullName]['filetypeh'] = filename.split('.')[-1]
else:
self.results[fullName]['filetypeh'] = "Misc"
if self.verbose:
print("[+] Found interesting file: %s" % fullName)
# Save the file even if it's not executable to the interesting files list
elif os.path.splitext(fullName)[-1].lower() not in self.uninterestingexts:
self.initializeDictionaryItem(filename, fullName, False)
if '.' in filename:
self.results[fullName]['filetypeh'] = filename.split('.')[-1]
else:
self.results[fullName]['filetypeh'] = "Misc"
if self.verbose:
print("[+] Found interesting file: %s" % fullName)
if self.verbose:
print("[+] Finished gathering executable files in %s" % rootDirectory)
def resolvePath(self, binaryPath, unresolvedPath):
resolvedPath = unresolvedPath
unresolvedPath = str(unresolvedPath)
if self.results[binaryPath]['filetype'] == self.MH_EXECUTE:
# resolve '@loader_path'
if unresolvedPath.startswith('@loader_path'):
resolvedPath = os.path.abspath(
os.path.split(binaryPath)[0] + unresolvedPath.replace('@loader_path', ''))
# resolve '@executable_path'
elif unresolvedPath.startswith('@executable_path'):
resolvedPath = os.path.abspath(
os.path.split(binaryPath)[0] + unresolvedPath.replace('@executable_path', ''))
else:
matchindices = [i for i, x in enumerate(binaryPath.split('/')) if x == unresolvedPath.split('/')[-1]]
unmatchindicies = [i for i, x in enumerate(binaryPath.split('/')) if x == 'Contents']
if len(matchindices) > 0:
if unresolvedPath.startswith('@loader_path'):
resolvedPath = os.path.abspath(
'/'.join(binaryPath.split('/')[0:matchindices[-1]]) + "/MacOS" + unresolvedPath.replace(
'@loader_path', ''))
elif unresolvedPath.startswith('@executable_path'):
resolvedPath = os.path.abspath(
'/'.join(binaryPath.split('/')[0:matchindices[-1]]) + "/MacOS" + unresolvedPath.replace(
'@executable_path', ''))
elif len(unmatchindicies) > 0:
if unresolvedPath.startswith('@loader_path'):
resolvedPath = os.path.abspath(
'/'.join(binaryPath.split('/')[0:unmatchindicies[-1]]) + "/Contents/MacOS" + unresolvedPath.replace(
'@loader_path', ''))
elif unresolvedPath.startswith('@executable_path'):
resolvedPath = os.path.abspath(
'/'.join(binaryPath.split('/')[0:unmatchindicies[-1]]) + "/Contents/MacOS" + unresolvedPath.replace(
'@executable_path', ''))
return resolvedPath.rstrip(b'\x00')
def parseExecutables(self):
if self.verbose:
print("[*] Parsing executable files for validity as an executable, script, dylib or bundle")
for binarypath in self.results.keys():
binary = self.results[binarypath]["filename"]
try:
f = open(binarypath, 'rb')
if not f:
if self.verbose:
print("[-] Could not open: %s" % binary)
continue
except:
if self.verbose:
print("[-] Could not open: %s" % binary)
continue
# Check if it is an interesting file and not an executable
if self.results[binarypath]["parse"] is False:
continue
# passed checks as an executable create dictionary placeholder for vulnerabilities
self.results[binarypath]["vulnerable"] = {'DylibHijack': [], 'WeakDylib': [], "BackdoorableScript": []}
isScript = self.scriptCheck(f, binary, binarypath)
if isScript:
if self.verbose:
print("[+] Potential backdoor-able script: %s" % binarypath)
continue
# check if it's a supported (intel) architecture
# ->also returns the supported mach-O header
(isSupported, machoHeader) = self.isSupportedArchitecture(f)
if not isSupported:
if self.verbose:
print("[-] Either not supported architecture or not a binary: %s" % binary)
self.results[binarypath]["parse"] = False
continue
# skip binaries that aren't main executables, dylibs or bundles
if machoHeader.filetype not in [self.MH_EXECUTE, self.MH_DYLIB, self.MH_BUNDLE, self.MH_KEXT_BUNDLE]:
if self.verbose:
print("[-] Not an executable, dylib, bundle, or kext bundle: %s" % binary)
self.results[binarypath]["parse"] = False
continue
self.results[binarypath]["parse"] = "Started Parse"
if self.verbose:
print("[*] Parsing: %s" % binary)
print("\tFull binary path: %s" % binarypath)
# init dictionary for process
self.results[binarypath]["load"] = {'LC_RPATHs': [], 'LC_LOAD_DYLIBs': [], 'LC_LOAD_WEAK_DYLIBs': []}
# save filetype
self.results[binarypath]['filetype'] = machoHeader.filetype
if self.results[binarypath]['filetype'] == self.MH_EXECUTE:
self.results[binarypath]['filetypeh'] = "Executable"
elif self.results[binarypath]['filetype'] == self.MH_DYLIB:
self.results[binarypath]['filetypeh'] = "Dylib"
elif self.results[binarypath]['filetype'] == self.MH_BUNDLE:
self.results[binarypath]['filetypeh'] = "Bundle"
elif self.results[binarypath]['filetype'] == self.MH_KEXT_BUNDLE:
self.results[binarypath]['filetypeh'] = "KextBundle"
else:
self.results[binarypath]['filetypeh'] = "Misc"
# iterate over all load
# ->save LC_RPATHs, LC_LOAD_DYLIBs, and LC_LOAD_WEAK_DYLIBs
if self.CPU_TYPE_NAMES.get(machoHeader.cputype) == 'x86_64':
f.seek(32, io.SEEK_SET)
else:
f.seek(28, io.SEEK_SET)
for cmd in range(machoHeader.ncmds):
# handle LC_RPATH's
# ->resolve and save
# save offset to load commands
try:
lc = load_command.from_buffer_copy(f.read(self.LC_Header_Sz))
except Exception as e:
break # break out of the nested loop and continue with the parent loop
size = lc.cmdsize
if lc.cmd == self.LC_RPATH:
# grab rpath
pathoffset = struct.unpack('<L', f.read(4))[0]
f.seek(pathoffset - (self.LC_Header_Sz + 4), io.SEEK_CUR)
path = f.read(lc.cmdsize - pathoffset)
rPathDirectory = path.rstrip(b'\x00')
if self.verbose:
print("\tOriginal rpath: %s" % rPathDirectory)
# always attempt to resolve '@executable_path' and '@loader_path'
rPathDirectory = self.resolvePath(binarypath, rPathDirectory)
if self.verbose:
print("\tSystem resolved rpath: %s" % rPathDirectory)
self.results[binarypath]["load"]['LC_RPATHs'].append(rPathDirectory)
# handle LC_LOAD_DYLIB
# ->save (as is)
elif lc.cmd == self.LC_LOAD_DYLIB:
# tuple, last member is path to import
pathoffset = struct.unpack('<L', f.read(4))[0]
# skip over version and compatibility
f.seek(pathoffset - (self.LC_Header_Sz + 4), io.SEEK_CUR)
path = f.read(size - pathoffset)
importedDylib = path.rstrip(b'\x00')
if self.verbose:
print("\t%s imports dylib: %s" % (binary, importedDylib))
self.results[binarypath]["load"]['LC_LOAD_DYLIBs'].append(importedDylib)
# handle for LC_LOAD_WEAK_DYLIB
# ->resolve (except for '@rpaths') and save
elif lc.cmd == self.LC_LOAD_WEAK_DYLIB:
# tuple, last member is path to import
pathoffset = struct.unpack('<L', f.read(4))[0]
# skip over version and compatibility
f.seek(pathoffset - (self.LC_Header_Sz + 4), io.SEEK_CUR)
path = f.read(size - pathoffset)
weakDylib = path.rstrip(b'\x00')
# always attempt to resolve '@executable_path' and '@loader_path'
weakDylib = self.resolvePath(binarypath, weakDylib)
if self.verbose:
print("\t%s has a weak dylib: %s" % (binary, weakDylib))
self.results[binarypath]["load"]['LC_LOAD_WEAK_DYLIBs'].append(weakDylib)
else:
f.seek(size - self.LC_Header_Sz, io.SEEK_CUR)
self.results[binarypath]["parse"] = "Complete"
if self.verbose:
print("[+] Completed parsing: %s" % binary)
if self.verbose:
print("[+] Finished parsing files")
def vulnerabilityDictInput(self, vulntype, fullpath, hijackpath, loadorder, certainty, contextwriteperm, readonlypartition, indicator, mode):
ftype = self.results[fullpath]['filetypeh']
binary = self.results[fullpath]['filename']
if contextwriteperm:
context = "Write"
else:
context = "Read"
if readonlypartition and not self.sipdisabled:
context = "ReadOnly"
self.results[fullpath]['vulnerable'][vulntype].append(
{'hijackPath': hijackpath, 'LoadOrder': loadorder, 'Certainty': certainty,
'WriteAccess': contextwriteperm, 'ReadOnlyPartition': readonlypartition, "Mode": mode})
if certainty == 'Definite' and context != 'ReadOnly':
if contextwriteperm:
indicator = indicator * 3
print("[%s] [%s] [%s] [%s] [%s] [%s] %s" % (indicator, ftype, binary, vulntype, certainty, context, hijackpath))
else:
if self.verbose:
if contextwriteperm:
indicator = indicator * 3
print("[%s] [%s] [%s] [%s] [%s] [%s] %s" % (
indicator, ftype, binary, vulntype, certainty, context, hijackpath))
def passiveDylibVulnProcessor(self, binarypath, dylib, vulntype):
mode = "Passive"
# check the first rpath directory (from LC_RPATHs)
# ->is the rpath'd import there!?
for loadorder, rpath in enumerate(self.results[binarypath]['load']['LC_RPATHs']):
hijackpath = rpath + dylib
# if not found means this binary is potentailly vulnerable!
if not os.path.exists(hijackpath):
# Check if current user context has write permissions to the last existing path and if rpath is in the read only partition
readonlypartition, contextwriteperm = self.readWriteCheck(hijackpath)
# Set logic statements for ease of reading
notreadonly = (not readonlypartition)
executablefirstloaded = (self.results[binarypath]['filetype'] == self.MH_EXECUTE and loadorder == 0)
executablenextloaded = (self.results[binarypath]['filetype'] == self.MH_EXECUTE and loadorder < 2)
allfiletypesfirstloaded = (loadorder == 0)
allfiletypesnextloaded = (loadorder < 2)
if (executablefirstloaded and notreadonly) or (executablefirstloaded and self.sipdisabled):
certainty = 'Definite'
indicator = '!'
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
elif (executablenextloaded and notreadonly) or (executablenextloaded and self.sipdisabled):
certainty = 'High'
indicator = '+'
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
elif (allfiletypesnextloaded and notreadonly) or (allfiletypesnextloaded and self.sipdisabled):
certainty = 'Potential'
indicator = '+'
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
elif executablefirstloaded and readonlypartition and not self.sipdisabled:
certainty = 'Definite'
indicator = '-'
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
elif allfiletypesfirstloaded and readonlypartition and not self.sipdisabled:
certainty = 'High'
indicator = '-'
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
elif allfiletypesnextloaded and readonlypartition and not self.sipdisabled:
certainty = 'Potential'
indicator = '-'
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
else:
certainty = 'Low'
indicator = '-'
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
continue
def processBinariesPassive(self):
mode = "Passive"
if self.verbose:
print("[*] Processing results to identify weaknesses")
print("[ID] [FileType] [FileName] [Vulnerability] [Certainty] [Permission] Directory")
# scan all parsed binaries
for binarypath in self.results.keys():
# grab binary entry
binary = self.results[binarypath]["filename"]
# Check if the file was actually parsed
if self.results[binarypath]["parse"] == "Complete":
# STEP 1: check for vulnerable @rpath'd imports
# Note: changed the check for potentials cuz knowledge is power
# check for primary @rpath'd import that doesn't exist
if len(self.results[binarypath]['load']['LC_RPATHs']):
vulntype = "DylibHijack"
# check all @rpath'd imports for the executable
# ->if there is one that isn't found in a primary LC_RPATH, the executable is vulnerable :)
for importedDylib in self.results[binarypath]['load']['LC_LOAD_DYLIBs']:
importedDylib = str(importedDylib)
# skip non-@rpath'd imports
if not importedDylib.startswith('@rpath'):
continue
# chop off '@rpath'
importedDylib = importedDylib.replace('@rpath', '')
# send binary path, dylib, and vulnerablity type and process findings/output
self.passiveDylibVulnProcessor(binarypath, importedDylib, vulntype)
# STEP 2: check for vulnerable weak imports
# can check all binary types...
# check binary
for weakDylib in self.results[binarypath]['load']['LC_LOAD_WEAK_DYLIBs']:
weakDylib = str(weakDylib)
vulntype = "WeakDylib"
# got to resolve weak @rpath'd imports before checking if they exist
if weakDylib.startswith('@rpath'):
# skip @rpath imports if binary doesn't have any LC_RPATHS
if not len(self.results[binarypath]['load']['LC_RPATHs']):
continue
# chop off '@rpath'
weakDylib = weakDylib.replace('@rpath', '')
# send binary path, dylib, and vulnerablity type and process findings/output
self.passiveDylibVulnProcessor(binarypath, weakDylib, vulntype)
# path doesn't need to be resolved
# ->check/save those that don't exist
elif not os.path.exists(weakDylib):
readonlypartition, contextwriteperm = self.readWriteCheck(weakDylib)
if self.results[binarypath]['filetype'] == self.MH_EXECUTE:
certainty = 'High'
indicator = '+'
loadorder = 'unknown'
self.vulnerabilityDictInput(vulntype, binarypath, weakDylib, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
else:
certainty = 'Potential'
indicator = '+'
loadorder = 'unknown'
self.vulnerabilityDictInput(vulntype, binarypath, weakDylib, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
continue
if self.verbose:
print("[+] Completed weakness identification")
def processBinariesActive(self):
def on_timeout(proc, status_dict):
# Kill process on timeout and note as status_dict['timeout']=True
status_dict['timeout'] = True
print("[*] Forced time out")
proc.kill()
vulntype = "DylibHijack"
mode = "Active"
if self.verbose:
print("[*] Actively identifying weaknesses")
print("[ID] [FileType] [FileName] [Vulnerability] [Certainty] [Permission] Directory")
for binarypath in self.results.keys():
if self.results[binarypath]["parse"] == "Complete":
binary = self.results[binarypath]["filename"]
if self.results[binarypath]['filetype'] == self.MH_EXECUTE:
# Set up execution key
self.results[binarypath]["execution"] = {'standardoutput': [], 'erroroutput': []}
# Since this is active output to console instead of asking for verbose
print("[*] Executing %s for 3 seconds" % binary)
# Copy standard shell environment
hijackenv = os.environ.copy()
# Add DYLD_PRINT_RPATHS debugging mode to current Python shell environment
hijackenv["DYLD_PRINT_RPATHS"] = "1"
# Set timeout dictionary to false before execution
status_dict = {'timeout': False}
# Open executable
proc = subprocess.Popen(binarypath, shell=False, env=hijackenv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Set thread timer to 3 seconds
timer = threading.Timer(3, on_timeout, [proc, status_dict])
# Start thread timer to timeout after 3 seconds
timer.start()
# Set process to wait until it is forced to timeout
proc.wait()
# in case we didn't hit timeout
timer.cancel()
print("[*] Killed %s process" % binary)
# Pull stdout and stderr from killed process
result = proc.communicate()
if result[0] != '':
# Add standard out to binaries dictionary
self.results[binarypath]["execution"]['standardoutput'].append(result[0])
if self.verbose:
print("[*] %s had standard output when executed:" % binary)
if len(result[0].split('\n')) > 1:
standardoutput = result[0].split('\n')
for line in standardoutput:
print("\t%s" % line)
else:
print("\t" + result[0])
if result[1] != '':
# Add standard error/debugging messages to binaries dictionary
self.results[binarypath]["execution"]['erroroutput'].append(result[0])
# Split standard error output and look for RPATH failed expanding, then add to vulnerabilities dict
for line in result[1].split('\n'):
if 'RPATH failed expanding' in line:
# Properly split failed load path and turn into an absolute path for reporting
relativepath = line.split('to: ')[-1]
hijackpath = os.path.abspath(relativepath)
if self.verbose:
print("\tOriginal failed rpath: %s" % relativepath)
#print("\tAbsolute path: %s" % hijackpath)
# Check if current user context has write permissions to the last existing path
readonlypartition, contextwriteperm = self.readWriteCheck(hijackpath)
if not readonlypartition or self.sipdisabled:
certainty = 'Definite'
indicator = '+'
loadorder = 0
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
else:
certainty = 'Definite'
indicator = '-'
loadorder = 0
self.vulnerabilityDictInput(vulntype, binarypath, hijackpath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
if self.verbose:
print("[*] Completed active weakness identification")
def ProcessScriptBackdoors(self):
mode = "Passive"
if self.verbose:
print("[*] Parsing backdoorable script list")
vulntype = "BackdoorableScript"
for filepath in self.results.keys():
# Check if current user context has write permissions
readonlypartition, contextwriteperm = self.readWriteCheck(filepath)
contextwriteperm = self.results[filepath]["writeable"]
if self.results[filepath]["parse"] == "Script" and contextwriteperm:
certainty = 'Potential'
indicator = '+'
loadorder = 0
self.vulnerabilityDictInput(vulntype, filepath, filepath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
elif self.results[filepath]["parse"] == "Script" and self.verbose:
certainty = 'Potential'
indicator = '-'
loadorder = 0
self.vulnerabilityDictInput(vulntype, filepath, filepath, loadorder, certainty,
contextwriteperm, readonlypartition, indicator, mode)
if self.verbose:
print("[*] Finished listing scripts")
def ProcessInterestingFiles(self):
if self.verbose:
print("[*] Parsing interesting files list")
for fullpath in self.results.keys():
file = self.results[fullpath]["filename"]
if self.results[fullpath]["parse"] is False and self.verbose:
if '.' in file:
filetype = file.split('.')[-1]
else:
filetype = "Misc"
print("[*] [%s] [%s] [InterestingFile] %s" % (filetype, file, fullpath))
if self.verbose:
print("[*] Finished listing files")
def GetResults(self):
return self.results
class CSVout:
def __init__(self, filename, results, sipdisabled):
if filename.lower().endswith('.csv'):
self.filename = '.'.join(filename.split('.')[:-1])
else:
self.filename = filename
self.vulnfilename = self.filename + '-vulnerabilities.csv'
self.interestingfilename = self.filename + '-interesting-files.csv'
self.results = results
self.sipdisabled = sipdisabled
self.QUOTE = '"'
self.sep = ','
def csvlinewrite(self, row):
self.f.write(self.joinline(row) + '\n')
def closecsv(self):
self.f.close()
self.f = None
def quote(self, value):
if not isinstance(value, str):
value = str(value)
return self.QUOTE + value + self.QUOTE
def joinline(self, row):
return self.sep.join([self.quote(value) for value in row])
def writevulns(self):
self.f = open(self.vulnfilename, 'w')
# Needed to sort vulnerabilities by Certainty and if they occur in a read-only partition; also prioritize scripts for Potential certainty
sortbycertainty = ['Definite', 'High', 'Potential', 'Low']
if self.sipdisabled:
sortbysip = [False]
else:
sortbysip = [False, True]
scriptsort = ["Script", "Complete"]
self.csvlinewrite(['Filename', 'Full path', 'File type', 'Discovery Mode', 'Vulnerability', 'Certainty', 'Read Only Partition (SIP)','Write permission', 'Hijack This Path', 'Dylib Load Order'])
for sipcheck in sortbysip:
for certain in sortbycertainty:
for scriptcheck in scriptsort:
for fullpath in self.results.keys():
file = self.results[fullpath]['filename']
if self.results[fullpath]['parse'] == 'Complete' or self.results[fullpath]['parse'] == 'Script':
filetype = self.results[fullpath]['filetypeh']
if self.results[fullpath]['parse'] == 'Script':
if self.results[fullpath]['parse'] != scriptcheck:
continue
scriptinfo = self.results[fullpath]['vulnerable']['BackdoorableScript'][0]
vulnerability = "Backdoorable Script"
certainty = scriptinfo["Certainty"]
if certain != certainty:
continue
writeperms = self.results[fullpath]['writeable']
sip = scriptinfo["ReadOnlyPartition"]
if not self.sipdisabled and sip != sipcheck:
continue
hijackpath = "See Full path"
loadorder = "Unknown"
mode = scriptinfo["Mode"]
row = [file, fullpath, filetype, mode, vulnerability, certainty, sip, writeperms, hijackpath, loadorder]
self.csvlinewrite(row)
elif self.results[fullpath]['parse'] == 'Complete':
if self.results[fullpath]['parse'] != scriptcheck:
continue
if len(self.results[fullpath]['vulnerable']['DylibHijack']) > 0:
vulnerability = "Dylib Hijack"
for hijackabledylib in self.results[fullpath]['vulnerable']['DylibHijack']:
certainty = hijackabledylib["Certainty"]
if certain != certainty:
continue
writeperms = hijackabledylib["WriteAccess"]
sip = hijackabledylib["ReadOnlyPartition"]
if not self.sipdisabled and sip != sipcheck:
continue
hijackpath = hijackabledylib["hijackPath"]
loadorder = hijackabledylib["LoadOrder"]
mode = hijackabledylib["Mode"]
row = [file, fullpath, filetype, mode, vulnerability, certainty, sip, writeperms, hijackpath, loadorder]
self.csvlinewrite(row)
if len(self.results[fullpath]['vulnerable']['WeakDylib']) > 0:
vulnerability = "Weak Dylib"
for hijackabledylib in self.results[fullpath]['vulnerable']['WeakDylib']:
certainty = hijackabledylib["Certainty"]
if certain != certainty:
continue
writeperms = hijackabledylib["WriteAccess"]
sip = hijackabledylib["ReadOnlyPartition"]
if not self.sipdisabled and sip != sipcheck:
continue
hijackpath = hijackabledylib["hijackPath"]
loadorder = hijackabledylib["LoadOrder"]
mode = hijackabledylib["Mode"]
row = [file, fullpath, filetype, mode, vulnerability, certainty, sip, writeperms, hijackpath, loadorder]
self.csvlinewrite(row)
self.closecsv()
print('[%s] Created %s' % ('*', self.vulnfilename))
def writeinterestingfiles(self):
# Example entry:
# Filename: {
# 'writeable': Bool, # indicates whether or not the current user that ran the tool has write permissions on the file
# 'filetypeh': 'String', # indicates the file type as a string : Executable, Dylib, Bundle, KextBundle, Script, Misc (based on file extension)
# 'parse': 'String', # indicates if the file was an executable and was parsed for weaknesses
# 'fullpath': 'String', # full file path to file
# }
self.f = open(self.interestingfilename, 'w')
self.csvlinewrite(['Filename', 'Full path', 'File type', 'Write permission'])
for fullpath in self.results.keys():
if self.results[fullpath]['parse'] is False:
file = self.results[fullpath]['filename']
if 'filetypeh' in self.results[fullpath].keys():
filetype = self.results[fullpath]['filetypeh']
else:
filetype = ""
writeperms = self.results[fullpath]['writeable']
row = [file, fullpath, filetype, writeperms]
self.csvlinewrite(row)
self.closecsv()
print('[%s] Created %s' % ('*', self.interestingfilename))
# Class that utilizes system standard output and writes to a file.
# -----------------------------------------------
class Logger(object):
def __init__(self, filename="Default.log"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
# For standalone use of dyib-hijacker
class Main():
def __init__(self):
parser = argparse.ArgumentParser(description='Application Hijack Scanner for macOS')
typeofsearch = parser.add_mutually_exclusive_group(required=True)
typeofsearch.add_argument('-r', '--running', action='store_true', help='Check currently running processes')
typeofsearch.add_argument('-i', '--installed', action='store_true', default=False,
help='Check all installed applications')
typeofsearch.add_argument('-p', '--application', default=False,
help='Check a specific application i.e. /Application/Safari.app')
output = parser.add_mutually_exclusive_group(required=False)
output.add_argument('-oS', '--outputstandard', default=False, help='Outputs standard output to a .log file')
output.add_argument('-oC', '--outputcsv', default=False, help='Outputs results to a .csv file')
output.add_argument('-oA', '--outputall', default=False, help='Outputs results to a .csv file and standard log')
parser.add_argument('-s', '--sipdisabled', default=False, action='store_true',
help='Use if SIP is disabled on the system to search typically read-only paths')
aggression = parser.add_mutually_exclusive_group(required=True)
aggression.add_argument('-A', '--active', default=False, action='store_true',
help='Executes main executable binaries with env var export DYLD_PRINT_RPATHS="1"')
aggression.add_argument('-P', '--passive', default=False, action='store_true',
help='Performs classic Dylib Hijack Scan techniques')
aggression.add_argument('-b', '--bothchecks', default=False, action='store_true',
help='Performs both active and passive checks')
parser.add_argument('-v', '--verbose', default=False, action='store_true',
help='Output in verbose mode while script runs')
parser.add_argument('-d', '--debug', default=False, action='store_true', help=argparse.SUPPRESS)
args = parser.parse_args()
self.verbosity = args.verbose
self.sipdisabled = args.sipdisabled
self.running = args.running
self.installed = args.installed
self.application = args.application
self.active = args.active
self.passive = args.passive
if args.bothchecks:
self.active = True
self.passive = True
self.debug = args.debug
self.outputcsv = args.outputcsv
self.outputstandard = args.outputstandard
if args.outputall:
self.outputcsv = args.outputall