-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-anti-spec.py
More file actions
executable file
·241 lines (204 loc) · 7.56 KB
/
find-anti-spec.py
File metadata and controls
executable file
·241 lines (204 loc) · 7.56 KB
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
# compute anti-spec of CGC sample programs with Angr
import numpy
import random
import os
import sys
import string
import inspect, re
import angr
import angr.analyses
def varname(p):
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
m = re.search(r'\bvarname\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line)
if m:
return m.group(1)
# load the known sources into a dictionary in the format of
# {function-name:[return-type, [parameter1-type, parameter2-type,...]], ...}
def loadKnownSources(fn):
fh = file(fn,'r')
if fh==None:
raise IOError("error occurred when opening file: " + fn)
contents = fh.readlines()
fh.close()
sourcelist = {}
n=0
#rx = re.compile("(?<=[\s:~])(\w+)\s*\(([\w\s,<>\[\].=&':/*]*?)\)\s*(const)?\s*(?={)")
rx = re.compile("\s*(unsigned|signed|\s*)?(int|ssize_t|size_t|void|long|short|float|double)\s*(\w+)\s*\(([\w+\s,<>\[\].=&':/*]*?)\)\s*(const)?;")
for line in contents:
line=line.lstrip().rstrip()
m = rx.match(line)
if m==None:
print >> sys.stderr, "not a C function declaration: %s" % (line)
continue
#print "return type:%s, function: %s, parameters: %s" % (str(m.group(1))+' '+str(m.group(2)), m.group(3), m.group(4))
rettype=(str(m.group(1))+' '+str(m.group(2))).lstrip().rstrip()
funcname=m.group(3)
allparams = m.group(4)
paratypelist=[]
pnts = string.split(allparams,',')
for pnt in pnts:
items=string.split(pnt)
if len(items)<2:
continue
paratypelist.append( ''.join(items[:len(items)-1]) )
if funcname not in sourcelist.keys():
sourcelist[funcname] = list()
#v = sourcelist[funcname]
#v.append( rettype )
#v.append( paratypelist )
sourcelist[funcname] = [rettype, paratypelist]
n=n+1
print >> sys.stdout, "%d known sources are loaded" % (n)
#print sourcelist
return sourcelist
def getFuncAddress(cfg, funcName, plt=None ):
found = [
addr for addr,func in cfg.kb.functions.iteritems()
if funcName == func.name and (plt is None or func.is_plt == plt)
]
if len( found ) > 0:
print "Found "+funcName+"'s address at "+hex(found[0])+"!"
return found[0]
else:
#raise Exception("No address found for function : "+funcName)
return None
def locateCFGNode(graph, naddr):
for node in graph.nodes():
if repr(node.addr) == repr(naddr):
return node
return None
def locateDDGNode(ddg, cfgnode):
ret =[]
for node in ddg.data_graph.nodes():
if repr(node.location.block_addr) == repr(cfgnode.addr):
ret.append (node)
return ret
def peekcfg(cfg):
for fk in cfg.kb.functions.keys():
print "%s: %s" % (fk, cfg.kb.functions[fk])
print ap.loader.describe_addr(fk)
def antispec_per_source(ap,caller, csaddr,cfg,cdg,ddg):
cfgnode = locateCFGNode (cfg.graph, csaddr)
'''
ddgnode = locateNode (ddg.graph, srcaddr)
cdgnode = locateNode (cdg.graph, srcaddr)
assert ddgnode!=None and cdgnode!=None
cfgnodes = cfg.get_all_nodes( csaddr )
print "cfg nodes associated with the callsite"
for cn in cfgnodes:
print cn
print "DDG nodes associated with the callsite"
ddgnodes = locateDDGNode(ddg, cfgnode)
for dn in ddgnodes:
print dn
'''
print "definitions at the call site"
dview = angr.analyses.ddg.DDGView(cfg, ddg)
defs = []
'''
defs = set()
for i in range(0, cfgnode.size):
item = dview[cfgnode.addr+i]
defs.add(a for a in item.definitions)
'''
#print cfgnode.function_address
sltargets = []
#i = 0
for ia in cfgnode.instruction_addrs:
item = dview [ia]
print ia
print item
print item.definitions
defs.append( item.definitions )
#sltargets.append ( (cfgnode, i) )
#i=i+1
print defs
# take all statements in the callsite block as slicing criteria
for i, stmt in enumerate( ap.factory.block(cfgnode.addr).vex.statements ):
sltargets.append ( (cfgnode, i) )
bs = ap.analyses.BackwardSlice(cfg, cdg=cdg, ddg=ddg, targets=sltargets)
#bs = ap.analyses.BackwardSlice(cfg, cdg=cdg, ddg=ddg, targets=[(cfgnode, -1)])
print "backward slice of call site"
#print bs
print bs.chosen_statements
variables=[]
for baddr,sids in bs.chosen_statements.iteritems():
for sid in sids:
defs.append( dview[baddr+sid].definitions )
for node in ddg.data_graph.nodes():
if (node.location.block_addr) == hex(baddr): #and node.location.stmt_idx == sid:
variables.append ( node )
print defs
print variables
''' forward analysis to check against boundary checking
'''
#print cfg.kb.functions.function(name="_terminate")
#ddg.pp()
#print ddg.get_all_nodes(cfgnode.addr, -1)
#for node in ddg.graph.nodes():
for node in ddg.data_graph.nodes():
#print type(node)
print node
#print node.location
#print node.location.block_addr, node.location.stmt_idx, node.location.sim_procedure
for edge in ddg.data_graph.edges():
print edge
print "done OKAY: %s" % (cfgnode)
print "done OKAY: %x" % (cfgnode.addr)
print "done OKAY: %x" % (ap.factory.block(cfgnode.addr).addr)
#print "callsite instructions: %s" % (cfgnode.irsb)
#print "callsite instructions: %s" % (ap.factory.block(cfgnode.addr).capstone.insns)
print "callsite instructions"
for stmt in (ap.factory.block(cfgnode.addr).capstone.insns):
print stmt
sddg = ap.analyses.DDG (cfg, start=csaddr)
sddg.pp()
print caller
print sddg.function_dependency_graph(caller)
def getCallers(cfg,faddr):
ret = []
for fk in cfg.kb.functions.keys():
func = cfg.kb.functions[fk]
for cs in func.get_call_sites():
callee = func.get_call_target(cs)
#print "cs target: %s %s" % (callee, cfg.kb.functions[callee])
if callee == faddr:
ret.append([func,cs])
return ret
def do_analysis(fbin, sources):
ap = angr.Project(fbin, load_options={'auto_load_libs': False})
cfg = ap.analyses.CFGAccurate(keep_state=True, context_sensitivity_level=2)
cfg.normalize()
#peekcfg(cfg)
cdg = ap.analyses.CDG (cfg)
ddg = ap.analyses.DDG (cfg)
'''
ddg.pp()
vfg = ap.analyses.VFG(cfg)
for node in vfg.graph.nodes():
print node
'''
#ddg = ap.analyses.VSA_DDG ( vfg )
for fname in sources.keys():
faddr = getFuncAddress(cfg, fname)
if faddr == None:
continue
print "user input source: %s %s %s..." % (sources[fname][0], fname, sources[fname][1])
print "callers of this user input sources:"
allcallercs = getCallers(cfg,faddr)
for callercs in allcallercs:
caller = callercs[0]
csaddr = callercs[1]
print caller.name
print "callsite in caller: %s" % (csaddr)
print "strings in this caller: %s" % (caller.string_references())
antispec_per_source(ap,caller,csaddr,cfg,cdg,ddg)
if __name__=="__main__":
if len(sys.argv)<2:
print >> sys.stderr, "missing the program to analyze..."
sys.exit(1)
sources = loadKnownSources('/home/hcai/Environments/known-sources.txt')
print >> sys.stdout, "now analyzing %s with angr facilities..." % (sys.argv[1])
do_analysis(sys.argv[1], sources)
sys.exit(0)
# hcai: set ts=4 tw=120 sts=4 sw=4