forked from JonathanSalwan/Tigress_protection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve-vm.py
executable file
·779 lines (593 loc) · 22.5 KB
/
solve-vm.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
#!/usr/bin/env python
## -*- coding: utf-8 -*-
##
## Jonathan Salwan
##
import ctypes
import os
import random
import string
import struct
import sys
import time
import lief
from triton import *
from scripts.templates import *
from arybo.tools.triton_ import tritonexprs2arybo, tritonast2arybo
from arybo.lib.exprs_asm import to_llvm_function
from arybo.lib.mba_exprs import ExprCond
# Used for nested vm
sys.setrecursionlimit(100000)
# Script options
DEBUG = True
METRICS = True
OPAQUE = False
# The debug function
def debug(s):
if DEBUG: print(s)
# VMs input
VM_INPUT = b'1234'
# Multiple-paths
condition = list()
paths = list()
# Memory mapping
BASE_PLT = 0x10000000
BASE_ARGV = 0x20000000
BASE_ALLOC = 0x30000000
BASE_STACK = 0x9fffffff
# Signal handlers used by raise() and signal()
sigHandlers = dict()
# File descriptors used by fopen() and fprintf()
fdHandlers = dict()
# Allocation information used by malloc()
mallocCurrentAllocation = 0
mallocMaxAllocation = 2048
mallocBase = BASE_ALLOC
mallocChunkSize = 0x00010000
# Total of instructions executed
totalInstructions = 0
totalUniqueInstructions = {}
# Total of functions simulated
totalFunctions = 0
# Time of execution
startTime = None
endTime = None
def getMemoryString(ctx, addr):
s = str()
index = 0
while ctx.getConcreteMemoryValue(addr+index):
c = chr(ctx.getConcreteMemoryValue(addr+index))
if c not in string.printable: c = ""
s += c
index += 1
return s
def getFormatString(ctx, addr):
return getMemoryString(ctx, addr) \
.replace("%s", "{}").replace("%d", "{:d}").replace("%#02x", "{:#02x}") \
.replace("%#x", "{:#x}").replace("%x", "{:x}").replace("%02X", "{:02x}") \
.replace("%c", "{:c}").replace("%02x", "{:02x}").replace("%ld", "{:d}") \
.replace("%*s", "").replace("%lX", "{:x}").replace("%08x", "{:08x}") \
.replace("%u", "{:d}").replace("%lu", "{:d}") \
# Simulate the rand() function
def randHandler(ctx):
debug('[+] rand hooked')
# Return value
return random.randrange(0xffffffff)
# Simulate the malloc() function
def mallocHandler(ctx):
global mallocCurrentAllocation
global mallocMaxAllocation
global mallocBase
global mallocChunkSize
debug('[+] malloc hooked')
# Get arguments
size = ctx.getConcreteRegisterValue(ctx.registers.rdi)
if size > mallocChunkSize:
debug('[+] malloc failed: size too big')
sys.exit(-1)
if mallocCurrentAllocation >= mallocMaxAllocation:
debug('[+] malloc failed: too many allocations done')
sys.exit(-1)
area = mallocBase + (mallocCurrentAllocation * mallocChunkSize)
mallocCurrentAllocation += 1
# Return value
return area
# Simulate the calloc() function
def callocHandler(ctx):
global mallocCurrentAllocation
global mallocMaxAllocation
global mallocBase
global mallocChunkSize
debug('[+] malloc hooked')
# Get arguments
nmemb = ctx.getConcreteRegisterValue(ctx.registers.rdi)
size = ctx.getConcreteRegisterValue(ctx.registers.rsi)
# Total size
size = nmemb * size
if size > mallocChunkSize:
debug('[+] malloc failed: size too big')
sys.exit(-1)
if mallocCurrentAllocation >= mallocMaxAllocation:
debug('[+] malloc failed: too many allocations done')
sys.exit(-1)
area = mallocBase + (mallocCurrentAllocation * mallocChunkSize)
mallocCurrentAllocation += 1
# Return value
return area
# Simulate the memcpy() function
def memcpyHandler(ctx):
debug('[+] memcpy hooked')
# Get arguments
arg1 = ctx.getConcreteRegisterValue(ctx.registers.rdi)
arg2 = ctx.getConcreteRegisterValue(ctx.registers.rsi)
arg3 = ctx.getConcreteRegisterValue(ctx.registers.rdx)
mems = ctx.getSymbolicMemory()
for index in range(arg3):
ctx.concretizeMemory(arg1 + index)
ctx.setConcreteMemoryValue(arg1 + index, ctx.getConcreteMemoryValue(arg2 + index))
try:
ctx.assignSymbolicExpressionToMemory(mems[arg2 + index], MemoryAccess(arg1 + index, CPUSIZE.BYTE))
except:
pass
return arg1
# Simulate the memset() function
def memsetHandler(ctx):
debug('[+] memset hooked')
dst = ctx.getConcreteRegisterValue(ctx.registers.rdi)
src = ctx.getConcreteRegisterValue(ctx.registers.rsi)
size = ctx.getConcreteRegisterValue(ctx.registers.rdx)
for index in range(size):
dmem = MemoryAccess(dst + index, CPUSIZE.BYTE)
cell = ctx.getAstContext().extract(7, 0, ctx.getRegisterAst(ctx.registers.rsi))
expr = ctx.newSymbolicExpression(cell, "memset byte")
ctx.setConcreteMemoryValue(dmem, cell.evaluate())
ctx.assignSymbolicExpressionToMemory(expr, dmem)
return dst
# Simulate the signal() function
def signalHandler(ctx):
debug('[+] signal hooked')
# Get arguments
signal = ctx.getConcreteRegisterValue(ctx.registers.rdi)
handler = ctx.getConcreteRegisterValue(ctx.registers.rsi)
global sigHandlers
sigHandlers.update({signal: handler})
# Return value (void)
return ctx.getConcreteRegisterValue(ctx.registers.rax)
# Simulate the raise() function
def raiseHandler(ctx):
debug('[+] raise hooked')
# Get arguments
signal = ctx.getConcreteRegisterValue(ctx.registers.rdi)
handler = sigHandlers[signal]
ctx.processing(Instruction(b"\x6A\x00")) # push 0
emulate(ctx, handler)
# Return value
return 0
# Simulate the strlen() function
def strlenHandler(ctx):
debug('[+] strlen hooked')
# Get arguments
arg1 = getMemoryString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rdi))
# Return value
return len(arg1)
# Simulate the strtoul() function
def strtoulHandler(ctx):
debug('[+] strtoul hooked')
# Get arguments
nptr = getMemoryString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rdi))
endptr = ctx.getConcreteRegisterValue(ctx.registers.rsi)
base = ctx.getConcreteRegisterValue(ctx.registers.rdx)
# Return value
return int(nptr, base)
# Simulate the printf() function
def printfHandler(ctx):
debug('[+] printf hooked')
# Get arguments
arg1 = getFormatString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rdi))
arg2 = ctx.getConcreteRegisterValue(ctx.registers.rsi)
arg3 = ctx.getConcreteRegisterValue(ctx.registers.rdx)
arg4 = ctx.getConcreteRegisterValue(ctx.registers.rcx)
arg5 = ctx.getConcreteRegisterValue(ctx.registers.r8)
arg6 = ctx.getConcreteRegisterValue(ctx.registers.r9)
nbArgs = arg1.count("{")
args = [arg2, arg3, arg4, arg5, arg6][:nbArgs]
s = arg1.format(*args)
if DEBUG:
sys.stdout.write(s)
# Return value
return len(s)
# Simulate the putchar() function
def putcharHandler(ctx):
debug('[+] putchar hooked')
# Get arguments
arg1 = ctx.getConcreteRegisterValue(ctx.registers.rdi)
sys.stdout.write(chr(arg1) + '\n')
# Return value
return 2
# Simulate the puts() function
def putsHandler(ctx):
debug('[+] puts hooked')
# Get arguments
arg1 = getMemoryString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rdi))
sys.stdout.write(arg1 + '\n')
# Return value
return len(arg1) + 1
# Simulate the printf() function
def fprintfHandler(ctx):
global fdHandlers
debug('[+] fprintf hooked')
# Get arguments
arg1 = ctx.getConcreteRegisterValue(ctx.registers.rdi)
arg2 = getFormatString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rsi))
arg3 = ctx.getConcreteRegisterValue(ctx.registers.rdx)
arg4 = ctx.getConcreteRegisterValue(ctx.registers.rcx)
arg5 = ctx.getConcreteRegisterValue(ctx.registers.r8)
arg6 = ctx.getConcreteRegisterValue(ctx.registers.r9)
nbArgs = arg2.count("{")
args = [arg3, arg4, arg5, arg6][:nbArgs]
s = arg2.format(*args)
fdHandlers[arg1].write(s)
# Return value
return len(s)
# Simulate the free() function (skip this behavior)
def freeHandler(ctx):
debug('[+] free hooked')
return None
# Simulate the fopen() function
def fopenHandler(ctx):
global fdHandlers
debug('[+] fopen hooked')
# Get arguments
arg1 = getFormatString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rdi))
arg2 = getFormatString(ctx, ctx.getConcreteRegisterValue(ctx.registers.rsi))
fd = open(arg1, arg2)
idf = len(fdHandlers) + 3 # 3 because 0, 1, 3 are already reserved.
fdHandlers.update({idf : fd})
# Return value
return idf
def libcMainHandler(ctx):
debug('[+] __libc_start_main hooked')
# Get arguments
main = ctx.getConcreteRegisterValue(ctx.registers.rdi)
# Push the return value to jump into the main() function
ctx.concretizeRegister(ctx.registers.rsp)
ctx.setConcreteRegisterValue(ctx.registers.rsp, ctx.getConcreteRegisterValue(ctx.registers.rsp)-CPUSIZE.QWORD)
ret2main = MemoryAccess(ctx.getConcreteRegisterValue(ctx.registers.rsp), CPUSIZE.QWORD)
ctx.concretizeMemory(ret2main)
ctx.setConcreteMemoryValue(ret2main, main)
# Setup argc / argv
ctx.concretizeRegister(ctx.registers.rdi)
ctx.concretizeRegister(ctx.registers.rsi)
argvs = [
bytes(sys.argv[1].encode('utf-8')), # argv[0]
bytes(VM_INPUT), # argv[1]
]
# Define argc / argv
base = BASE_ARGV
addrs = list()
index = 0
for argv in argvs:
addrs.append(base)
ctx.setConcreteMemoryAreaValue(base, argv+b'\x00')
base += len(argv)+1
debug('[+] argv[%d] = %s' %(index, argv))
index += 1
argc = len(argvs)
argv = base
for addr in addrs:
ctx.setConcreteMemoryValue(MemoryAccess(base, CPUSIZE.QWORD), addr)
base += CPUSIZE.QWORD
ctx.setConcreteRegisterValue(ctx.registers.rdi, argc)
ctx.setConcreteRegisterValue(ctx.registers.rsi, argv)
return 0
def errnoHandler(ctx):
debug('[+] __errno_location hooked')
errno = 0xdeadbeaf
ctx.setConcreteMemoryValue(MemoryAccess(errno, CPUSIZE.QWORD), 0)
return errno
customRelocation = [
('__libc_start_main', libcMainHandler, BASE_PLT + 0),
('__errno_location', errnoHandler, BASE_PLT + 1),
('calloc', callocHandler, BASE_PLT + 2),
('fopen', fopenHandler, BASE_PLT + 3),
('fprintf', fprintfHandler, BASE_PLT + 4),
('free', freeHandler, BASE_PLT + 5),
('malloc', mallocHandler, BASE_PLT + 6),
('memcpy', memcpyHandler, BASE_PLT + 7),
('memset', memsetHandler, BASE_PLT + 8),
('printf', printfHandler, BASE_PLT + 9),
('putchar', putcharHandler, BASE_PLT + 10),
('puts', putsHandler, BASE_PLT + 11),
('raise', raiseHandler, BASE_PLT + 12),
('rand', randHandler, BASE_PLT + 13),
('signal', signalHandler, BASE_PLT + 14),
('strlen', strlenHandler, BASE_PLT + 15),
('strtoul', strtoulHandler, BASE_PLT + 16),
('strtoull', strtoulHandler, BASE_PLT + 17),
]
def hookingHandler(ctx):
global condition
global paths
global totalFunctions
pc = ctx.getConcreteRegisterValue(ctx.registers.rip)
for rel in customRelocation:
if rel[2] == pc:
# Emulate the routine and the return value
ret_value = rel[1](ctx)
if ret_value is not None:
ctx.concretizeRegister(ctx.registers.rax)
ctx.setConcreteRegisterValue(ctx.registers.rax, ret_value)
# Used for metric
totalFunctions += 1
# tigress user input
if rel[0] == 'strtoul':
debug('[+] Symbolizing the strtoul return')
var1 = ctx.symbolizeRegister(ctx.registers.rax)
var0 = ctx.getSymbolicVariable(0)
ctx.setConcreteVariableValue(var0, ctx.getConcreteVariableValue(var1))
rax = ctx.getSymbolicRegister(ctx.registers.rax)
ast = ctx.getAstContext()
rax.setAst(ast.variable(var0))
# tigress user end-point
if rel[0] == 'printf':
debug('[+] Slicing end-point user expression')
if ctx.getSymbolicRegister(ctx.registers.rsi):
exprs = ctx.sliceExpressions(ctx.getSymbolicRegister(ctx.registers.rsi))
paths.append(exprs)
#else:
# ast = ctx.getAstContext()
# n = ctx.newSymbolicExpression(ast.bv(ctx.getConcreteRegisterValue(ctx.registers.rsi), 64))
# exprs = {n.getId() : n}
# paths.append(exprs)
else:
debug('[+] -------------------------------------------------------------- ')
debug('[+] /!\ /!\ /!\ /!\ /!\ /!\ Symbolic lost! /!\ /!\ /!\ /!\ /!\ /!\ ')
debug('[+] -------------------------------------------------------------- ')
sys.exit(-1)
# Get the return address
ret_addr = ctx.getConcreteMemoryValue(MemoryAccess(ctx.getConcreteRegisterValue(ctx.registers.rsp), CPUSIZE.QWORD))
# Hijack RIP to skip the call
ctx.concretizeRegister(ctx.registers.rip)
ctx.setConcreteRegisterValue(ctx.registers.rip, ret_addr)
# Restore RSP (simulate the ret)
ctx.concretizeRegister(ctx.registers.rsp)
ctx.setConcreteRegisterValue(ctx.registers.rsp, ctx.getConcreteRegisterValue(ctx.registers.rsp)+CPUSIZE.QWORD)
return
# Emulate the binary.
def emulate(ctx, pc):
global condition
global totalInstructions
global totalUniqueInstructions
count = 0
while pc:
# Fetch opcodes
opcodes = ctx.getConcreteMemoryAreaValue(pc, 16)
# Create the Triton instruction
instruction = Instruction()
instruction.setOpcode(opcodes)
instruction.setAddress(pc)
# Process
if ctx.processing(instruction) == False:
debug('[-] Instruction not supported: %s' %(str(instruction)))
break
#print(instruction)
count += 1
if pc in totalUniqueInstructions:
totalUniqueInstructions[pc] += 1
else:
totalUniqueInstructions[pc] = 1
if instruction.getType() == OPCODE.X86.HLT:
break
if ctx.isRegisterSymbolized(ctx.registers.rip) and len(condition) == 0:
exprs = ctx.sliceExpressions(ctx.getSymbolicRegister(ctx.registers.rip))
condition.append((instruction.isConditionTaken(), exprs))
# Simulate routines
hookingHandler(ctx)
# Next
pc = ctx.getConcreteRegisterValue(ctx.registers.rip)
debug('[+] Instruction executed: %d' %(count))
debug('[+] Unique instruction executed: %d' %(len(totalUniqueInstructions)))
debug('[+] PC len: %d' %(len(condition)))
# Used for metric
totalInstructions += count
return
def loadBinary(ctx, binary):
# Map the binary into the memory
phdrs = binary.segments
for phdr in phdrs:
size = phdr.physical_size
vaddr = phdr.virtual_address
debug('[+] Loading 0x%06x - 0x%06x' %(vaddr, vaddr+size))
ctx.setConcreteMemoryAreaValue(vaddr, phdr.content)
return
def makeRelocation(ctx, binary):
# Perform our own relocations
try:
for rel in binary.pltgot_relocations:
symbolName = rel.symbol.name
symbolRelo = rel.address
for crel in customRelocation:
if symbolName == crel[0]:
debug('[+] Hooking %s' %(symbolName))
ctx.setConcreteMemoryValue(MemoryAccess(symbolRelo, CPUSIZE.QWORD), crel[2])
except:
pass
# Perform our own relocations
try:
for rel in binary.dynamic_relocations:
symbolName = rel.symbol.name
symbolRelo = rel.address
for crel in customRelocation:
if symbolName == crel[0]:
debug('[+] Hooking %s' %(symbolName))
ctx.setConcreteMemoryValue(MemoryAccess(symbolRelo, CPUSIZE.QWORD), crel[2])
except:
pass
return
def recompile(M):
name = 'llvm_expressions/%s.ll' %(sys.argv[1].split('/')[-1])
nameO2 = 'llvm_expressions/%s.O2.ll' %(sys.argv[1].split('/')[-1])
fd = open(name, 'w')
M = str(M).replace('__arybo', 'SECRET')
M = str(M).replace('unknown-unknown-unknown', 'x86_64-pc-linux-gnu')
fd.write(M)
fd.close()
os.system("clang -O2 -S -emit-llvm -o - %s > %s" %(name, nameO2))
debug('[+] LLVM module wrote in %s' %(name))
debug('[+] Recompiling deobfuscated binary...')
dst = 'deobfuscated_binaries/%s' %(sys.argv[1].split('/')[-1] + '.deobfuscated')
os.system("clang %s -O2 deobfuscated_binaries/run.c -o %s" %(name, dst))
debug('[+] Deobfuscated binary recompiled: %s' %(dst))
return
def run(ctx, binary):
# Concretize previous context
ctx.concretizeAllMemory()
ctx.concretizeAllRegister()
# Define a fake stack
ctx.setConcreteRegisterValue(ctx.registers.rbp, BASE_STACK)
ctx.setConcreteRegisterValue(ctx.registers.rsp, BASE_STACK)
# Let's emulate the binary from the entry point
debug('[+] Starting emulation.')
emulate(ctx, binary.entrypoint)
debug('[+] Emulation done.')
return
def metrics():
global METRICS
if METRICS:
print('--------------------------------------------------------------------')
print('->', sys.argv[1].split('/')[-1])
print(' Instructions executed:', totalInstructions)
print(' Unique Instructions executed:', len(totalUniqueInstructions))
print(' Functions simulated:', totalFunctions)
print(' Time of analysis:', endTime - startTime, "seconds")
return
def generateSymbolicExpressions(pathNumber):
global paths
exprs = paths[pathNumber]
ssa = str()
last = 0
for k, v in sorted(exprs.items()):
ssa += str(v) + '\n'
last = k
name = 'symbolic_expressions/%s.py' %(sys.argv[1].split('/')[-1])
debug('[+] Generating %s' %(name))
fd = open(name, 'w')
fd.write(TEMPLATE_GENERATE_HASH_SSA % (ssa, last))
fd.close()
return
def generateLLVMExpressions(ctx, pathNumber):
global paths
exprs = paths[pathNumber]
debug('[+] Converting symbolic expressions to an LLVM module...')
e = tritonexprs2arybo(exprs)
var = tritonast2arybo(ctx.getAstContext().variable(ctx.getSymbolicVariable(0)))
M = to_llvm_function(e,[var.v])
return M
def main():
global VM_INPUT
global condition
global paths
# Get a Triton context
ctx = TritonContext()
# Set the architecture
ctx.setArchitecture(ARCH.X86_64)
# Set optimization
ctx.setMode(MODE.ALIGNED_MEMORY, True)
ctx.setMode(MODE.ONLY_ON_SYMBOLIZED, True)
# AST representation as Python syntax
ctx.setAstRepresentationMode(AST_REPRESENTATION.PYTHON)
if len(sys.argv) != 2:
debug('[-] Syntax: %s <target vm>' %(sys.argv[0]))
return -1
# Parse the binary
binary = lief.parse(sys.argv[1])
# Load the binary
loadBinary(ctx, binary)
# Perform our own relocations
makeRelocation(ctx, binary)
# Init and emulate
run(ctx, binary)
# we got 100% of code coverage (there is only one path).
if len(condition) == 0 or OPAQUE == True:
# Generate symbolic epxressions of the first path
generateSymbolicExpressions(0)
# Generate llvm of the first path
M = generateLLVMExpressions(ctx, 0)
# Recompile the LLVM-IL
recompile(M)
else:
ssa_pc = str()
exprs_pc = condition[0][1]
last_pc = None
for k, v in sorted(exprs_pc.items()):
ssa_pc += str(v) + '\n'
last_pc = v
ssa_b1 = str()
exprs_b1 = paths[0]
last_b1 = 0
for k, v in sorted(exprs_b1.items()):
ssa_b1 += ' ' + str(v) + '\n'
last_b1 = k
ssa_b1 += ' endb = ref_%d\n' %(last_b1)
debug('[+] Asking for a new input...')
pcAst = ctx.getPathPredicate()
ast = ctx.getAstContext()
model = ctx.getModel(ast.lnot(pcAst))
if model:
VM_INPUT = str(model[0].getValue())
else:
debug('[+] No model found! Opaque predicate?')
# Generate symbolic epxressions of the first path
generateSymbolicExpressions(0)
# Generate llvm of the first path
M = generateLLVMExpressions(ctx, 0)
# Recompile the LLVM-IL
recompile(M)
return 0
# Re-simulate an execution to take another path
run(ctx, binary)
ssa_b2 = str()
exprs_b2 = paths[1]
last_b2 = 0
for k, v in sorted(exprs_b2.items()):
ssa_b2 += ' ' + str(v) + '\n'
last_b2 = k
ssa_b2 += ' endb = ref_%d\n' %(last_b2)
name = 'symbolic_expressions/%s.py' %(sys.argv[1].split('/')[-1])
debug('[+] Generating %s' %(name))
fd = open(name, 'w')
if condition[0][0]:
fd.write(TEMPLATE_GENERATE_HASH_SSA_PC1 % (ssa_pc, '%s' %(str(last_pc.getAst().getChildren()[0])), ssa_b1, ssa_b2))
else:
fd.write(TEMPLATE_GENERATE_HASH_SSA_PC1 % (ssa_pc, '%s' %(str(last_pc.getAst().getChildren()[0])), ssa_b2, ssa_b1))
fd.close()
debug('[+] Converting symbolic expressions to an LLVM module...')
last_pc_expr = None
last_pc_id = 0
exprs_pc = condition[0][1]
for k, v in sorted(exprs_pc.items()):
last_pc_expr = v
last_pc_id = k
del condition[0][1][last_pc_id]
ast = ctx.getAstContext()
nc = ast.ite(last_pc_expr.getAst().getChildren()[0], ast.bvtrue(), ast.bvfalse())
expr = ctx.newSymbolicExpression(nc)
condition[0][1][expr.getId()] = expr
c = tritonexprs2arybo(condition[0][1])
e1 = tritonexprs2arybo(paths[0])
e2 = tritonexprs2arybo(paths[1])
ast = ctx.getAstContext()
var = tritonast2arybo(ast.variable(ctx.getSymbolicVariable(0)))
if condition[0][0]:
M = to_llvm_function(ExprCond(c, e1, e2), [var.v])
else:
M = to_llvm_function(ExprCond(c, e2, e1), [var.v])
# Recompile the LLVM-IL
recompile(M)
return 0
if __name__ == '__main__':
startTime = time.clock()
retValue = main()
endTime = time.clock()
metrics()
sys.exit(retValue)