/
code_coverage.py
237 lines (188 loc) · 8.14 KB
/
code_coverage.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##
## Triton tool to perform code coverage
## Romain Thomas - 2015-09-26
##
## Description:
## ------------
##
## This tool aims to reach all basic blocks in a program using dynamic symbolic
## resolution and the snapshot engine. The algorithm is based on Microsoft SAGE's
## fuzzer.
##
##
## Output:
## -------
##
## $ ./triton ./tools/code_coverage.py ./samples/code_coverage/test_atoi a
## [+] Take Snapshot
## [+] In main
## [+] In main() we set :
## [0x7ffc92bdc54a] = 61 a
## [0x7ffc92bdc54b] = 61 a
## [0x7ffc92bdc54c] = 61 a
## [+] Exit point
## {140722770396490: 0}
## {140722770396490: 32}
## {140722770396490: 57}
## [+] Restore snapshot
## [+] In main
## [+] In main() we set :
## [0x7ffc92bdc54a] = 39 9
## [0x7ffc92bdc54b] = 61 a
## [0x7ffc92bdc54c] = 61 a
## [+] Exit point
## {140722770396490: 57, 140722770396491: 0}
## {140722770396490: 57, 140722770396491: 8}
## {140722770396490: 56, 140722770396491: 56}
## [+] Restore snapshot
## [+] In main
## [+] In main() we set :
## [0x7ffc92bdc54a] = 38 8
## [0x7ffc92bdc54b] = 38 8
## [0x7ffc92bdc54c] = 61 a
## [+] Exit point
## {140722770396490: 56, 140722770396491: 56, 140722770396492: 0}
## {140722770396490: 57, 140722770396491: 57, 140722770396492: 8}
## {140722770396490: 57, 140722770396491: 57, 140722770396492: 56}
## {140722770396490: 51, 140722770396491: 51, 140722770396492: 63}
## [+] Restore snapshot
## [+] In main
## [+] In main() we set :
## [0x7ffc92bdc54a] = 33 3
## [0x7ffc92bdc54b] = 33 3
## [0x7ffc92bdc54c] = 3f ?
## ok
## [+] Exit point
## [+] Done !
## $
##
import smt2lib
from triton import *
from collections import OrderedDict
from copy import deepcopy
class Input(object):
def __init__(self, data):
self.__data = data
self.__bound = 0
self.__dataAddr = dict()
@property
def data(self):
return self.__data
@property
def bound(self):
return self.__bound
@property
def dataAddr(self):
return self.__dataAddr
def setBound(self, bound):
self.__bound = bound
def addDataAddress(self, address, value):
self.__dataAddr[address] = value
class TritonExecution(object):
program = None
input = None
worklist = None
inputTested = None
entryPoint = 0
exitPoint = 0
whitelist = None
myPC = None
@staticmethod
def cbefore(instruction):
if instruction.getAddress() == TritonExecution.entryPoint + 2:
TritonExecution.myPC = [] # Reset the path constraint
TritonExecution.input = TritonExecution.worklist.pop() # Take the first input
TritonExecution.inputTested.append(TritonExecution.input) # Add this input to the tested input
return
if instruction.getAddress() == TritonExecution.entryPoint and not isSnapshotEnabled():
print "[+] Take Snapshot"
takeSnapshot()
return
if instruction.isBranch() and instruction.getRoutineName() in TritonExecution.whitelist:
addr1 = instruction.getNextAddress() # next address next from the current one
addr2 = instruction.getOperands()[0].getImm().getValue() # Address in the instruction condition (branch taken)
ripId = getRegSymbolicID(IDREF.REG.RIP) # Get the reference of the RIP symbolic register
# [PC id, address taken, address not taken]
if instruction.isBranchTaken():
TritonExecution.myPC.append([ripId, addr2, addr1])
else:
TritonExecution.myPC.append([ripId, addr1, addr2])
return
if instruction.getAddress() == TritonExecution.exitPoint:
print "[+] Exit point"
# SAGE algorithm
# http://research.microsoft.com/en-us/um/people/pg/public_psfiles/ndss2008.pdf
for j in range(TritonExecution.input.bound, len(TritonExecution.myPC)):
expr = []
for i in range(0,j):
ripId = TritonExecution.myPC[i][0]
symExp = getFullExpression(getSymExpr(ripId).getAst())
addr = TritonExecution.myPC[i][1]
expr.append(smt2lib.smtAssert(smt2lib.equal(symExp, smt2lib.bv(addr, 64))))
ripId = TritonExecution.myPC[j][0]
symExp = getFullExpression(getSymExpr(ripId).getAst())
addr = TritonExecution.myPC[j][2]
expr.append(smt2lib.smtAssert(smt2lib.equal(symExp, smt2lib.bv(addr, 64))))
expr = smt2lib.compound(expr)
model = getModel(expr)
if len(model) > 0:
newInput = deepcopy(TritonExecution.input)
newInput.setBound(j + 1)
for k,v in model.items():
symVar = getSymVar(k)
newInput.addDataAddress(symVar.getKindValue(), v)
print newInput.dataAddr
isPresent = False
for inp in TritonExecution.worklist:
if inp.dataAddr == newInput.dataAddr:
isPresent = True
break
if not isPresent:
TritonExecution.worklist.append(newInput)
# If there is input to test in the worklist, we restore the snapshot
if len(TritonExecution.worklist) > 0 and isSnapshotEnabled():
print "[+] Restore snapshot"
restoreSnapshot()
return
return
@staticmethod
def fini():
print '[+] Done !'
return
@staticmethod
def mainAnalysis(threadId):
print "[+] In main"
rdi = getRegValue(IDREF.REG.RDI) # argc
rsi = getRegValue(IDREF.REG.RSI) # argv
argv0_addr = getMemValue(rsi, IDREF.CPUSIZE.QWORD_BIT) # argv[0] pointer
argv1_addr = getMemValue(rsi + 8, IDREF.CPUSIZE.QWORD_BIT) # argv[1] pointer
print "[+] In main() we set :"
od = OrderedDict(sorted(TritonExecution.input.dataAddr.items()))
for k,v in od.iteritems():
print "\t[0x%x] = %x %c" % (k, v, v)
setMemValue(k, 8, v)
convertMemToSymVar(k, IDREF.CPUSIZE.BYTE_BIT, "addr_%d" % k)
for idx, byte in enumerate(TritonExecution.input.data):
if argv1_addr + idx not in TritonExecution.input.dataAddr: # Not overwrite the previous setting
print "\t[0x%x] = %x %c" % (argv1_addr + idx, ord(byte), ord(byte))
setMemValue(argv1_addr + idx, IDREF.CPUSIZE.BYTE_BIT, ord(byte))
convertMemToSymVar(argv1_addr + idx, IDREF.CPUSIZE.BYTE_BIT, "addr_%d" % idx)
@staticmethod
def run(inputSeed, entryPoint, exitPoint, whitelist = []):
TritonExecution.exitPoint = exitPoint
TritonExecution.entryPoint = entryPoint
TritonExecution.worklist = [Input(inputSeed)]
TritonExecution.inputTested = []
TritonExecution.whitelist = whitelist
startAnalysisFromAddr(entryPoint)
addCallback(TritonExecution.mainAnalysis, IDREF.CALLBACK.ROUTINE_ENTRY, "main") # Called when we are in main's beginning
addCallback(TritonExecution.cbefore, IDREF.CALLBACK.BEFORE)
addCallback(TritonExecution.fini, IDREF.CALLBACK.FINI)
runProgram()
if __name__=='__main__':
#TritonExecution.run("aaa", 0x4004a0, 0x40065D, ["main", "myatoi"]) # ./triton ./tools/code_coverage.py ./samples/code_coverage/test_atoi a
#TritonExecution.run("bad !", 0x400480, 0x40061B, ["main", "check"]) # ./triton ./tools/code_coverage.py ./samples/crackmes/crackme_xor abc
TritonExecution.run("aaaaaaaa", 0x400460, 0x400666, ["main", "check"]) # ./triton ./tools/code_coverage.py ./samples/crackmes/crackme_regex_fsm a
#TritonExecution.run("aaaaaaaa", 0x400460, 0x402ECA, ["main", "checkinput"]) # ./triton ./tools/code_coverage.py ./samples/crackmes/crackme_regex_fsm_obfuscated a