-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpyconfuzzer.py
154 lines (139 loc) · 5.37 KB
/
pyconfuzzer.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
#!/usr/bin/python
import subprocess
import z3
from threading import Thread
import parser
import master
import viewer
# TODO: Hook into Server
class FuzzedProgram:
program = None
tainted = []
paths = {}
value = ''
completed = {}
queued = []
problems = []
prioritized = []
def __init__(self, program, taintedInputs, draw=True):
self.program = program.split(' ')
self.tainted = taintedInputs
self.iters = 0
self.draw = draw
if self.draw:
viewer.startGraph()
def getTainted(self):
return '|'.join([':'.join(t) for t in self.tainted])
def setInput(self, val):
self.value = val
def send(self, pathID, value, priority=100):
master.assignTask({'path': pathID,
'program': self.program,
'args': ['-tainted-input', self.getTainted()],
'inputs': {self.tainted[0][1]: value.encode('hex')}}, priority)
self.queued.append(pathID)
#f = open(self.tainted[0][1], 'w')
#f.write(self.value)
#f.close()
#pinCmd = ['pin', '-t', 'confuzzer.so', '-tainted-input', self.getTainted(), '--']
#subprocess.call(pinCmd + self.program)
#self.data = open('execution.dat').read().split('\n')
def parse(self, data):
branches = []
constraints = []
vrs = {}
for l in data:
if not l.startswith('br_') and not l.startswith(' '):
continue
idnt, eqtn = l.split(':', 1)
idnt = idnt.strip()
if idnt.startswith('br_'):
asm = eqtn.split(' (', 1)[0].strip()
state = eqtn.split('(', 1)[1].split(')', 1)[0].strip()
var = eqtn.rsplit(' - ', 1)[1].strip()
formula = parser.asmInstruction(asm, var)
taken = parser.asmBranch(asm, state)
branches.append((idnt, formula, taken))
else:
asm, eqtn = eqtn.split(' ::: ', 1)
if not idnt in vrs:
vrs[idnt] = z3.BitVec(idnt, 32)
ai = parser.asmInstruction(asm.strip(), eqtn.strip())
if ai:
constraints.append((idnt, ai))
return (branches, constraints, vrs)
def process(self):
self.send('', '')
while len(self.queued):
self.iters += 1
print 'Left:', len(self.queued)
(pathID, data) = master.getResult()
self.queued.remove(pathID)
if pathID in self.completed:
continue
if data[-1].strip() == 'SEGFAULT':
(bS, cS, vrs) = self.parse(data[:-1])
self.paths[pathID] = bS + [['SEGFAULT', pathID, '']]
self.problems.append(pathID)
continue
(bS, cS, vrs) = self.parse(data)
self.paths[pathID] = bS
if self.draw:
viewer.drawGraph(self.paths)
for bi in range(len(bS)):
solver = z3.Solver()
for (v,cf) in cS:
parser.asmZ3(solver, vrs, cf)
keep = bS[0:bi]
negate = bS[bi]
for (b, bf, bt) in keep:
parser.asmZ3(solver, vrs, bf, bt)
parser.asmZ3(solver, vrs, negate[1], not negate[2])
for k,v in vrs.iteritems():
if k.startswith('$E'):
solver.add(v >= 0)
solver.add(v < 256)
if solver.check().r == 1:
valM = {}
m = solver.model()
maxRead = 0
for v in m:
if str(v).startswith('$E'):
valM[str(v)[2:]] = m[v]
maxRead = max(int(str(v)[2:]), maxRead)
value = ''
for i in range(maxRead+1):
if str(i) in valM:
value += chr(valM[str(i)].as_long())
else:
break
priority = 0
for i in range(len(keep)):
(b, _, bt) = keep[i]
if (i, b, bt) in self.prioritized:
priority += 1
(b, _, bt) = negate
if (len(keep), b, not bt) in self.prioritized:
priority += 1
if value not in self.queued and value not in self.completed and value != pathID:
print self.iters, value
self.send(value, value, 100-priority)
self.completed[pathID] = data
def userInput(fp):
while True:
i = raw_input('Prioritize Branch ((TRUE|FALSE)_ID): ')
(val, i, name) = i.split('_',2)
fp.prioritized.append((int(i), name, val.lower() == 'true'))
def run(program, taintedInput):
print "Testing %s with tainted input %s" % (program, taintedInput)
fp = FuzzedProgram(program, taintedInput, True)
t = Thread(target=userInput, args=(fp,))
#t.start()
fp.setInput('')
fp.process()
#print fp.paths.keys()
if fp.problems:
print "Crashing Inputs:"
for i in fp.problems:
print "\t%s" % i
viewer.drawGraph(fp.paths, True)