-
Notifications
You must be signed in to change notification settings - Fork 4
/
ghidra_analysis.py
952 lines (763 loc) · 33 KB
/
ghidra_analysis.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
# bbm
import time
import sys
# import ast
from ghidra.util.classfinder import ClassSearcher
from ghidra.app.plugin.core.analysis import ConstantPropagationAnalyzer
from ghidra.program.util import SymbolicPropogator
from ghidra.program.model.mem import MemoryAccessException
from ghidra.util.exception import CancelledException
from ghidra.program.model.block import BasicBlockModel
from ghidra.util.task import TaskMonitor
from collections import Counter, defaultdict
import json
import os
import stat
# findcrypt
import functools
import struct
# import crypt const
import const
from ghidra.program.model.symbol import SourceType
from ghidra.program.model.listing import CodeUnit
# findIPC
import re
from ghidra.program.util import DefinedDataIterator
# ----------FIRMWARE UPDATE-RELATED KEYWORDS------------
# checksumWords = ['checksum', 'md5', 'sha256', 'crc', 'hash', 'md5sum', 'sha256sum', 'cksum']
# deviceWords = ['modulename', 'device', 'platform']
# versionWords = ['version', 'fwtool']
# signWords = ['signature', 'rsasign', 'rsa', 'certificate', 'cert'] #'sign',
signWords = ['RSA_verify', 'DSA_verify', 'ECDSA_do_verify', 'RsaSSL_Verify', 'DsaVerify', 'ecc_verify_hash', 'rsa_verify_hash', 'dsa_verify_hash', 'ecc_verify_hash', 'rsa_pkcs1_verify', 'ecdsa_verify', 'rsa_sha512_verify', 'dsa_verify', 'ecdsa_verify', 'HMAC_Update', 'CMAC_Update', 'Poly1305_Update', 'HmacUpdate', 'CmacUpdate', 'Poly1305Update', 'hmac_process', 'omac_process', 'poly1305_process', 'cipher_cmac_update', 'md_hmac_update', 'poly1305_update', 'hmac_sha1_update', 'cmac_aes128_update', 'poly1305_aes_update']
checksumWords = ['SHA256_Update', 'SHA3_absorb', 'RIPEMD160_Update', 'Sha256Update', 'Sha3_512_Update', 'RipeMdUpdate', 'sha256_process', 'sha3_process', 'rmd160_process', 'sha256_update_ret', 'sha512_process', 'ripemd160_update_ret', 'sha256_update', 'sha3_update', 'ripemd160_update', 'MD4_Update', 'MD5_Update', 'SHA1_Update', 'Md4Update', 'Md5Update', 'ShaUpdate', 'md4_process', 'md5_process', 'sha1_process', 'md4_update_ret', 'md5_update_ret', 'sha1_update_ret', 'md4_update', 'md5_update', 'sha1_update', 'crc']
deviceWords = ['get_modelid', 'fw_check', 'check_imagefile', 'check_imageheader', 'get_productid', 'upgradeCgiCheck', 'getProductId', 'is_valid_hwid', 'findStrInFile', 'check_hw_type']
versionWords = ['current_firm', 'firmware_version', 'upgradeCgiCheck', 'getProductVer', 'board_identify', 'check_image_version', 'findStrInFile', ]
rebootWords = ["reboot", "bin/reboot"]
writeWords = ["mtd", "bin/mtd", "flash"]
deliveryWords = ["wget", "bin/wget"]
# findIPC
def find_IPC():
# outfile = open(results_file, "w")
kws = set()
fp_strs = set()
fps = set()
pattern = re.compile(r'((?:[A-Z]:|(?<![:/])[\/]|\~[\/]|(?:\.{1,2}[\/])+)[\w+\s_\-\(\)\/]*(?:\.\w+)*)')
for string in DefinedDataIterator.definedStrings(currentProgram):
references = getReferencesTo(string.getAddress())
if references:
keyword = str(string.toString()).replace('ds "', '').replace('"','')
kws.add(string) #keyword
# outfile.write(keyword+"\n")
filepaths = pattern.findall(keyword)
filepaths = [path.strip().strip("'").strip('"') for path in filepaths] #.strip('\\n').strip('\\')
fp_strs = fp_strs | set(filepaths)
if filepaths:
fps.add(string)
# get the reference address of filepaths
fp_addrs = {}
for fp in fps:
fp_str = str(fp.toString()).replace('ds "', '').replace('"','')
references = getReferencesTo(fp.getAddress())
fp_addrs[fp_str] = []
for r in references:
r_addr = r.getFromAddress()
fp_addrs[fp_str].append(str(r_addr))
return kws, fp_strs, fp_addrs
# findcrypt
# ghidra api
# def find(find_bytes, min_addr=None):
# min_addr = min_addr or currentProgram.getMinAddress()
# return currentProgram.getMemory().findBytes(min_addr, find_bytes, None, True, monitor)
def create_label(addr, label_name, source=SourceType.USER_DEFINED):
sym_table = currentProgram.getSymbolTable()
sym_table.createLabel(addr, label_name, source)
def get_instructions_from(addr=None):
return currentProgram.getListing().getInstructions(addr, True)
def get_all_instructions():
return currentProgram.getListing().getInstructions(True)
def get_instruction_at(addr):
return getInstructionAt(addr)
def get_memory_address_ranges():
return currentProgram.getMemory().getAddressRanges()
def has_scalar_operand(inst, idx=1):
return inst.getScalar(idx) is not None
def set_eol_comment(addr, text):
code_unit = currentProgram.getListing().getCodeUnitAt(addr)
code_unit.setComment(CodeUnit.EOL_COMMENT, text)
def get_function_containing(addr):
return getFunctionContaining(addr)
def get_instructions_in_func(func):
inst = get_instruction_at(func.getEntryPoint())
while inst and getFunctionContaining(inst.getAddress()) == func:
yield inst
inst = inst.getNext()
# partial funcs
pack_longlong = functools.partial(struct.pack, '<Q')
pack_long = functools.partial(struct.pack, '<L')
# global value
# generate scalar on operand and its address pairs
SCALAR_ADDR_PAIRS = {inst.getScalar(1).getValue(): inst.getAddress() for inst in filter(has_scalar_operand, get_all_instructions())}
class NonSparseConst:
BYTE = 'B'
LONG = 'L'
LONGLONG = 'Q'
def __init__(self, const):
self._const = const
self.algorithm = const['algorithm']
self.name = const['name']
self.size = const['size']
self.array = const['array']
self._byte_array = None
def handle_byte(self):
return self.array
def handle_long(self):
return ''.join(map(pack_long, self.array))
def handle_longlong(self):
return ''.join(map(pack_longlong, self.array))
def to_bytes(self):
handler = {
self.BYTE: self.handle_byte,
self.LONG: self.handle_long,
self.LONGLONG: self.handle_longlong
# if there'll be another types, add handler here
}.get(self.size)
if handler is None:
raise ValueError('{} is not supported'.format(self.size))
return bytes(bytearray(handler()))
@property
def byte_array(self):
if self._byte_array is None:
self._byte_array = self.to_bytes()
return self._byte_array
class SparseConst:
def __init__(self, const):
self._const = const
self.algorithm = const['algorithm']
self.name = const['name']
self.array = const['array']
class OperandConst:
def __init__(self, const):
self._const = const
self.algorithm = const['algorithm']
self.name = const['name']
self.value = const['value']
def find_crypt_non_sparse_consts(names, algs, addrs):
# print('[*] processing non-sparse consts')
for nsc in map(NonSparseConst, const.non_sparse_consts):
found = find(nsc.byte_array)
if found:
names.append(nsc.name)
algs.append(nsc.algorithm)
addrs.append(found)
# print(' [+] found {name} for {alg} at {addr}'.format(name=nsc.name, alg=nsc.algorithm, addr=found))
create_label(found, nsc.name)
return names, algs, addrs
def find_crypt_sparse_consts(names, algs, addrs):
# print('[*] processing sparse consts')
for sc in map(SparseConst, const.sparse_consts):
# get address of first const matched one in operands
found_addr = SCALAR_ADDR_PAIRS.get(sc.array[0])
if found_addr:
# check the rest of consts, maybe it should be in the same function
# it is noted that it will be failed if the constants are not used in function (like shellcode).
maybe_crypto_func = get_function_containing(found_addr)
insts = get_instructions_in_func(maybe_crypto_func)
# get all scalars in same function
insts_with_scalars = filter(has_scalar_operand, insts)
scalars = [inst.getScalar(1).getValue() for inst in insts_with_scalars]
# check all values in consts array are contained in scalars in same function
if all([c in scalars for c in sc.array]):
# if all consts are contained
# add comment at the first found const's address
names.append(sc.name)
algs.append(sc.algorithm)
addrs.append(found_addr)
# print(' [+] found {name} for {alg} at {addr}'.format(name=sc.name, alg=sc.algorithm, addr=found_addr))
create_label(found_addr, sc.name)
return names, algs, addrs
def find_crypt_operand_consts(names, algs, addrs):
# print('[*] processing operand consts')
for oc in map(OperandConst, const.operand_consts):
found_addr = SCALAR_ADDR_PAIRS.get(oc.value)
if found_addr:
names.append(oc.name)
algs.append(oc.algorithm)
addrs.append(found_addr)
# print(' [+] found {name} for {alg} at {addr}'.format(name=oc.name, alg=oc.algorithm, addr=found_addr))
set_eol_comment(found_addr, oc.name)
return names, algs, addrs
def find_check_strs(kws, keywords):
# print('[*] processing check strings')
strs = []
# syms = []
# kstrs = []
# Get symbols
sm = currentProgram.getSymbolTable()
symbols = sm.getDefinedSymbols()
# Check path for calls to crypto funcs, etc
crypto_syms = []
# First identify crypto symbols so we don't repeat
while symbols.hasNext():
s = symbols.next()
for cs in keywords:
if cs in s.getName():
crypto_syms.append(s)
symbols = sm.getDefinedSymbols()
# Now check if refs to crypto symbols in the program
for s in crypto_syms:
s_refs = s.getReferences()
for r in s_refs:
r_addr = r.getFromAddress()
if r_addr >= currentProgram.minAddress and r_addr <= currentProgram.maxAddress:
for cs in keywords:
if cs in s.getName():
# print(' [+] found {name} for {alg} at {addr}'.format(name=s.getName(), alg=cs, addr=r_addr))
strs.append(s.getName()+":"+str(r_addr))
# syms.append(s)
for kw in kws:
keyword = str(kw.toString()).replace('ds "', '').replace('"','')
references = getReferencesTo(kw.getAddress())
for r in references:
r_addr = r.getFromAddress()
for cs in keywords:
if cs in keyword and keyword not in strs:
# print(' [+] found {name} for {alg} at {addr}'.format(name=keyword, alg=cs, addr=r_addr))
strs.append(keyword+":"+str(r_addr))
# kstrs.append(kw)
return strs #, syms, kstrs
# function for find keyword address
def find_keyword_addrs(kws, keywords):
keyword_addrs = {}
for keyword in keywords:
keyword_addrs[keyword] = {}
# Get symbols
sm = currentProgram.getSymbolTable()
symbols = sm.getDefinedSymbols()
keyword_syms = []
# identify whether keywords are in symbols
while symbols.hasNext():
s = symbols.next()
for cs in keywords:
if cs in s.getName():
keyword_syms.append(s)
symbols = sm.getDefinedSymbols()
# Now check if refs to crypto symbols in the program
for s in keyword_syms:
s_refs = s.getReferences()
for r in s_refs:
r_addr = r.getFromAddress()
if r_addr >= currentProgram.minAddress and r_addr <= currentProgram.maxAddress:
for cs in keywords:
if cs in s.getName():
keyword_addrs[cs][str(s.getName())] = str(r_addr)
for kw in kws:
keyword = str(kw.toString()).replace('ds "', '').replace('"','')
references = getReferencesTo(kw.getAddress())
for r in references:
r_addr = r.getFromAddress()
for cs in keywords:
if cs in keyword and keyword not in keyword_addrs.keys():
keyword_addrs[cs][str(keyword)] = str(r_addr)
# kstrs.append(kw)
return keyword_addrs
def find_crypt():
names = list()
algs = list()
addrs = list()
# strs = list()
names, algs, addrs = find_crypt_non_sparse_consts(names, algs, addrs)
names, algs, addrs = find_crypt_sparse_consts(names, algs, addrs)
names, algs, addrs = find_crypt_operand_consts(names, algs, addrs)
# strs = find_crypt_strs(strs)
return names, algs, addrs#, strs
def find_check(kws):
checksums = list()
devices = list()
versions = list()
signs = list()
reboots = list()
writes = list()
deliveries = list()
checksums = find_check_strs(kws, checksumWords)
devices = find_check_strs(kws, deviceWords)
versions = find_check_strs(kws, versionWords)
signs = find_check_strs(kws, signWords)
reboots = find_check_strs(kws, rebootWords)
writes = find_check_strs(kws, writeWords)
deliveries = find_check_strs(kws, deliveryWords)
return checksums, devices, versions, signs, reboots, writes, deliveries
# Verbosity and scope
DEBUG = False
VERBOSE = False
VVERBOSE = False # False
# Sinks
cf_sinks = ['system', '___system', 'bstar_system', 'popen',
'doSystemCmd', 'doShell', 'twsystem', 'CsteSystem', 'cgi_deal_popen',
'ExeCmd', 'ExecShell', 'exec_shell_popen', 'exec_shell_popen_str',
'strcpy', 'sprintf', 'memcpy', 'strcat', 'reboot']
exec_sinks = ['system', '___system', 'bstar_system', 'popen',
'doSystemCmd', 'doShell', 'twsystem', 'CsteSystem', 'cgi_deal_popen',
'ExeCmd', 'ExecShell', 'exec_shell_popen', 'exec_shell_popen_str',
'strcpy', 'sprintf', 'memcpy', 'strcat', 'reboot']
# bof_sinks = ['strcpy', 'sprintf', 'memcpy', 'strcat']
# Results file and utilities
global f
f = None
syms = {}
analyzer = None
def a2h(address):
return '0x' + str(address)
def getAnalyzer():
global analyzer
for a in ClassSearcher.getInstances(ConstantPropagationAnalyzer):
if a.canAnalyze(currentProgram):
analyzer = a
break
else:
assert 0
def getCallingArgs(addr, pos):
if not 0 <= pos <= 3:
return
arch = str(currentProgram.language.processor)
if arch == 'ARM':
reg = currentProgram.getRegister('r%d' % pos)
elif arch == 'MIPS':
if getInstructionAt(addr):
nextInst = getInstructionAt(addr).next
if len(nextInst.pcode): # not NOP
addr = addr.add(8)
reg = currentProgram.getRegister('a%d' % pos)
elif arch == 'x86' and str(currentProgram.language.getProgramCounter()) == 'RIP':
if pos == 3:
return
reg = currentProgram.getRegister(['RDI', 'RSI', 'RDX'][pos])
else:
return
return getRegister(addr, reg)
def getRegister(addr, reg):
if analyzer is None:
getAnalyzer()
func = getFunctionContaining(addr)
if func is None:
return
if func in syms:
symEval = syms[func]
else:
symEval = SymbolicPropogator(currentProgram)
symEval.setParamRefCheck(True)
symEval.setReturnRefCheck(True)
symEval.setStoredRefCheck(True)
analyzer.flowConstants(currentProgram, func.entryPoint, func.body, symEval, monitor)
syms[func] = symEval
return symEval.getRegisterValue(addr, reg)
def getStr(addr):
ad = addr
ret = ''
try:
while not ret.endswith('\0'):
ret += chr(getByte(ad) % 256)
ad = ad.add(1)
except MemoryAccessException:
return
return ret[:-1]
def getStrArg(addr, argpos=0):
rv = getCallingArgs(addr, argpos)
if rv is None:
return
return getStr(toAddr(rv.value))
# Customized function for reboot check
def checkReboot(addr, argpos=0):
arg = getStrArg(addr, argpos)
if arg is not None:
if "reboot" in arg:
return True
return False
def checkConstantStr(addr, argpos=0):
# empty string is not considered as constant, for it may be uninitialized global variable
return bool(getStrArg(addr, argpos))
def checkSafeFormat(addr, offset=0):
data = getStrArg(addr, offset)
if data is None:
return False
fmtIndex = offset
for i in range(len(data) - 1):
if data[i] == '%' and data[i + 1] != '%':
fmtIndex += 1
if data[i + 1] == 's':
if fmtIndex > 3:
return False
if not checkConstantStr(addr, fmtIndex):
return False
return True
def getCallee(inst):
callee = None
if len(inst.pcode):
if inst.pcode[-1].mnemonic == 'CALL':
callee = getFunctionAt(inst.getOpObjects(0)[0])
elif inst.pcode[-1].mnemonic == 'CALLIND':
regval = getRegister(inst.address, inst.getOpObjects(0)[0])
if regval is not None:
callee = getFunctionAt(toAddr(regval.value))
return callee
referenced = set()
def findSinkPath(target_addr, vuln, target=None):
# Control-flow search has additional checks, different entry point
def cf_search(start_func):
bbm = BasicBlockModel(currentProgram)
all_blocks = bbm.getCodeBlocks(TaskMonitor.DUMMY)
pending = []
completed = []
cur_visited_block_path = []
taint_path_count = 0
total_path_count = 0
if VVERBOSE:
funcs = currentProgram.getFunctionManager().getFunctions(True)
# print_funcs(funcs)
# Get symbols
sm = currentProgram.getSymbolTable()
symbols = sm.getDefinedSymbols()
# Find main first
block = all_blocks.next()
while all_blocks.hasNext() and block.getName() != 'main':
block = all_blocks.next()
# Get first function, then get that block
if block.getName() != 'main':
first_func_addr = getFirstFunction().getBody().getMinAddress()
block = bbm.getCodeBlocksContaining(first_func_addr, TaskMonitor.DUMMY)[0]
if DEBUG:
print("\t!!! Could not find main !!!\n\t! Starting from first function !\n")
# DFS analysis
flow = [[block.getName(), block.getMinAddress(), block.getFlowType()]]
while block:
# Keep track of completed blocks
completed.append(block)
if VERBOSE:
print("\t===BLOCK===")
print("\tLabel: {}".format(block.getName()))
print("\tMin Address: {}".format(block.getMinAddress()))
print("\tMax address: {}".format(block.getMaxAddress()))
print("\tFlow Type: {}\n".format(block.getFlowType()))
if VVERBOSE:
print("\t===DIS===")
print_disassembly(block)
# inst = getInstructionAt(block.getMinAddress())
# if inst:
# callee = getCallee(inst)
# print(callee)
dests = block.getDestinations(TaskMonitor.DUMMY)
# Check if block is a sink, if so we found a path from source to sink, stop
if block.getName() in cf_sinks: # and checkReboot(block.getMinAddress())) or block.getName()=="reboot"
if DEBUG:
print("\t===VULN===")
print('\t' + block.getName())
# Check symbol table for symbol references in this block
while symbols.hasNext():
s = symbols.next()
s_refs = s.getReferences()
for r in s_refs:
r_addr =r.getFromAddress()
if r_addr >= block.getMinAddress() and r_addr <= block.getMaxAddress():
if DEBUG:
print('\t' + str(s) + " referenced in vulnerable block")
print('\tRef Address: ' + str(r_addr))
symbols = sm.getDefinedSymbols()
total_path_count += 1
else:
# This part does the DFS by getting next unvisited child block, explore next
# If no new destination blocks, we will backtrack
while dests.hasNext():
d = dests.next()
d_block = d.getDestinationBlock()
if d_block.getName() in cf_sinks or d_block not in completed:
pending.append(d_block)
# Update current path
flow.append([d_block.getName(), d_block.getMinAddress(), d_block.getFlowType()])
# If a sink we stop searching down the tree, so don't add this block to current path
if d_block.getName() not in cf_sinks:
cur_visited_block_path.append(block)
break
if pending:
# Found child, continue down
block = pending.pop()
else:
# No unvisited child, so check if we can backtrack up tree
if cur_visited_block_path:
block = cur_visited_block_path.pop()
flow.pop()
else:
# Back to root, done
block = None
if VVERBOSE:
with open("symbol_table.txt", 'w') as st:
print >>st, 'SYMBOL TABLE'
while symbols.hasNext():
s = symbols.next()
print >>st, "Symbol: ", str(s)
return taint_path_count, total_path_count
# Keyword search starts at kw ref, simply looks for path
def kw_search(start_func, vuln):
bbm = BasicBlockModel(currentProgram)
block = bbm.getCodeBlocksContaining(start_func, TaskMonitor.DUMMY)[0]
pending = []
completed = []
cur_visited_block_path = []
if vuln == 'taint_analysis':
sinks = exec_sinks
# DFS analysis
flow = [[block.getName(), block.getMinAddress(), block.getFlowType()]]
while block:
# Keep track of completed blocks
completed.append(block)
if VERBOSE:
print("\t===BLOCK===")
print("\tLabel: {}".format(block.getName()))
print("\tMin Address: {}".format(block.getMinAddress()))
print("\tMax address: {}".format(block.getMaxAddress()))
print("\tFlow Type: {}\n".format(block.getFlowType()))
if VVERBOSE:
print("\t===DIS===")
print_disassembly(block)
print('')
dests = block.getDestinations(TaskMonitor.DUMMY)
# Check if block is a sink, if so we found a path from source to sink, stop
if block.getName() in cf_sinks: # and checkReboot(block.getMinAddress())) or block.getName()=="reboot"
if DEBUG:
print("\t===VULN===")
print('\t' + block.getName())
# if f is not None:
# if target:
# print >>f, 'Target: ', target, ' @ ', start_func
# else:
# print >>f, 'IPC @ ', start_func
# print_path_to_file(flow)
return flow[-1][0]
else:
# This part does the DFS by getting next unvisited child block, explore next
# If no new destination blocks, we will backtrack
while dests.hasNext():
d = dests.next()
d_block = d.getDestinationBlock()
if d_block.getName() in sinks or d_block not in completed:
pending.append(d_block)
# Update current path
flow.append([d_block.getName(), d_block.getMinAddress(), d_block.getFlowType()])
# print([d_block.getName(), d_block.getMinAddress(), d_block.getFlowType()])
# If a sink we stop searching down the tree, so don't add this block to current path
if d_block.getName() not in sinks:
cur_visited_block_path.append(block)
break
if pending:
# Found child, continue down
block = pending.pop()
else:
# No unvisited child, so check if we can backtrack up tree
if cur_visited_block_path:
block = cur_visited_block_path.pop()
flow.pop()
else:
# Back to root, done
block = None
return None
def print_path_to_file(path):
if f is not None:
print >>f, '[Path to sink: \n\t(>> block name : block addr : flow type)'
for i in range(len(path)):
b_name, b_addr, flow_type = path[i]
print >>f, '\t>>', b_name, '\t: ', a2h(b_addr), '\t: ', flow_type
print >>f, ']\n'
def print_disassembly(block):
listing = currentProgram.getListing()
insns = listing.getInstructions(block, True)
while insns.hasNext():
ins = insns.next()
print("\t{} {}".format(ins.getAddressString(False, True), ins))
def print_funcs(func_it):
while func_it.hasNext():
f1 = func_it.next()
print("Function Name",f1.getName())
print("Function Body" , f1.getBody())
print("Function Entry" , f1.getEntryPoint())
print("Functions Calls",f1.getCalledFunctions(TaskMonitor.DUMMY))
print("Function is Called From",f1.getCallingFunctions(TaskMonitor.DUMMY))
print('')
# Determine which search to run
if vuln == 'control_flow':
start_func = getFunctionContaining(target_addr)
return cf_search(start_func)
elif vuln == 'taint_analysis':
# Find entry points for each kw ref
# If no target then searching IPC, target_addr is already correct
if target:
cur_addr = find(target_addr, target)
else:
cur_addr = target_addr
target_found = False
target_list = []
vuln_path_found = 0 #False
if cur_addr:
target_found = True
searched_addrs = []
# Find each occurrence of target, check if vuln path, stop if there is
if target:
# print("target: " + target)
# print("cur_addr: " + a2h(cur_addr))
while target_found and cur_addr < currentProgram.maxAddress and cur_addr not in searched_addrs: #and not vuln_path_found
searched_addrs.append(cur_addr)
refs = getReferencesTo(cur_addr)
# Run search on each ref/entry point
for r in refs:
# print("ref: " + a2h(r.getFromAddress()))
end_func = kw_search(r.getFromAddress(), vuln)
if end_func:
vuln_path_found = vuln_path_found + 1#True
target_list.append(str(getDataContaining(cur_addr).toString()).replace('ds "', '').replace('"','') \
+ ": " + a2h(r.getFromAddress())+": " + end_func)
# break
cur_addr = cur_addr.add(1)
cur_addr = find(cur_addr, target)
if not cur_addr:
break
# print("cur_addr: " + a2h(cur_addr))
else:
# IPC, just search the one address
searched_addrs.append(cur_addr)
end_func = kw_search(r.getFromAddress(), vuln)
if end_func:
vuln_path_found = vuln_path_found + 1#True
target_list.append(str(getDataContaining(cur_addr).toString()).replace('ds "', '').replace('"','') \
+ ": " + a2h(r.getFromAddress())+": " + end_func)
if DEBUG:
if target:
print("\nFound Target: " + str(target) + "? " + str(target_found))
else:
print("\nIPC searched @ " + str(cur_addr))
print("Path Found? " + str(vuln_path_found))
print('')
return vuln_path_found, target_list
def check_files(rootpath, fwresults, fps):
traverse_path = os.path.join(fwresults, "traverse.json")
with open(traverse_path, "r") as f:
files = json.load(f)
htmls = files['html']
shs = files['sh']
elfs = files['elf']
html_list = []
sh_list = []
elf_list = []
module_list = []
filtered_fps = []
# for filepath in fps:
for filepath in fps:
original_fp = filepath
try:
idx = [path.split("/")[-1] for path in htmls].index(filepath.split("/")[-1])
if filepath[0] == "/":
filepath = filepath[1:]
filepath = os.path.join(rootpath, filepath)
if filepath not in html_list:
html_list.append(htmls[idx])
filtered_fps.append(original_fp)
continue
except ValueError:
pass
try:
idx = [path.split("/")[-1] for path in shs].index(filepath.split("/")[-1])
if filepath[0] == "/":
filepath = filepath[1:]
filepath = os.path.join(rootpath, filepath)
if filepath not in sh_list:
sh_list.append(shs[idx])
filtered_fps.append(original_fp)
continue
except ValueError:
pass
try:
idx = [path.split("/")[-1] for path in elfs].index(filepath.split("/")[-1])
if filepath[0] == "/":
filepath = filepath[1:]
filepath = os.path.join(rootpath, filepath)
if (".so" in filepath or ".ko" in filepath) and filepath not in module_list:
module_list.append(elfs[idx])
filtered_fps.append(original_fp)
elif filepath not in elf_list:
elf_list.append(elfs[idx])
filtered_fps.append(original_fp)
continue
except ValueError:
pass
return list(set(html_list)), list(set(sh_list)), list(set(elf_list)), list(set(module_list)), list(set(filtered_fps))
def run_analysis():
args = getScriptArgs()
with open(args[0], 'r') as f:
results = json.load(f)
path = currentProgram.getExecutablePath()
if path in list(results.keys()):
return
f = open(args[0], 'w')
rootpath = args[1]
fwresults = args[2]
# keywords to search
keywords = args[3].strip('[]').split(',')
results[path] = {}
# find IPC
kws, fps, fp_addrs = find_IPC()
# find crypts
# names, algs, addrs = find_crypt()
checksums, devices, versions, signs, reboots, writes, deliveries = find_check(kws)
# identify the addresses of reboots
reboot_addrs = find_keyword_addrs(kws, rebootWords)
# identify the addresses of IPC keywords
keyword_addrs = find_keyword_addrs(kws, keywords)
t = time.time()
reboot_total = 0
mtd_total = 0
# delivery_total = 0
reboots_cmdi = []
mtds = []
# deliveries = []
if reboots == list() and writes == list():
taint_path_count, total_path_count = findSinkPath(currentProgram.minAddress, 'control_flow')
elif reboots != list():
# Check each keyword for reboot
for i, param in enumerate(reboots):
reboot_num, reboot_list = findSinkPath(currentProgram.minAddress, 'taint_analysis', param)
reboot_total = reboot_total + reboot_num
reboots_cmdi = reboots_cmdi + reboot_list
elif writes != list():
# Check each keyword for mtd
for i, param in enumerate(writes):
mtd_num, mtd_list = findSinkPath(currentProgram.minAddress, 'taint_analysis', param)
mtd_total = mtd_total + mtd_num
mtds = mtds + mtd_list
kws_list = []
for kw in kws:
keyword = str(kw.toString()).replace('ds "', '').replace('"','')
kws_list.append(keyword)
results[path]['para'] = kws_list
htmls, shs, elfs, modules, filtered_fps = check_files(rootpath, fwresults, fps)
filtered_fp_addrs = {key: value for key, value in fp_addrs.items() if key in filtered_fps}
results[path]['call'] = {}
results[path]['call']['html'] = htmls
results[path]['call']['sh'] = shs
results[path]['call']['elf'] = elfs
results[path]['module'] = modules
# results[path]['crypt'] = list(set(algs))
results[path]['reboot'] = reboots
results[path]['write'] = writes
results[path]['delivery'] = deliveries
results[path]['checksum'] = checksums
results[path]['device'] = devices
results[path]['version'] = versions
results[path]['signature'] = signs
# include the addresses of IPC keywords
results[path]['ipc'] = keyword_addrs
results[path]['invocation'] = filtered_fp_addrs
if f is not None:
# print(results)
json.dump(results, f)
f.close()
t = time.time() - t
# print('Time Elapsed:' + str(t))
return
if __name__ == '__main__':
run_analysis()