-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
executable file
·144 lines (116 loc) · 4.58 KB
/
play.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
# compute anti-spec of CGC sample programs with Angr
import numpy
import random
import os
import sys
import string
import inspect, re
import pyvex
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 )
n=n+1
print >> sys.stdout, "%d known sources are loaded" % (n)
#print sourcelist
return sourcelist
def do_analysis(fbin):
import angr
import angr.analyses
#ap = angr.Project(fbin, load_options={'auto_load_libs': False})
#ap = angr.Project(fbin, load_options={'auto_load_libs': True}, main_opts={'backend':'cgc'})
#ap = angr.Project(fbin, load_options={'auto_load_libs': True}, main_opts={'backend':'elf'})
ap = angr.Project(fbin, load_options={'auto_load_libs': True})
cfg = ap.analyses.CFGAccurate(keep_state=True)
for node in cfg.graph.nodes():
print `node`
print repr(node.addr)
print node.to_codenode()
print node.input_state
print node.final_states
print node.simprocedure_name
print node.instruction_addrs
print node.block_id
print node.irsb
for stmt in ap.factory.block(node.addr).vex.statements:
stmt.pp()
print ap.loader.describe_addr(node.addr)
print "===="
so = ap.loader.find_object_containing(node.addr)
if so is not None and node.addr in so.symbols_by_addr:
name = so.symbols_by_addr[node.addr].name
print name
cdg = ap.analyses.CDG (cfg)
ddg = ap.analyses.DDG (cfg)
for fk in cfg.kb.functions.keys():
func = cfg.kb.functions[fk]
print "%s: %s" % (fk, func)
print ap.loader.describe_addr(fk)
print "call sites in %s " % (func.name)
for cs in func.get_call_sites():
print "cs addr: %s" % (cs)
callee = func.get_call_target(cs)
print "cs target: %s %s" % (callee, cfg.kb.functions[callee])
print ap.entry
print ap.loader.main_object
print ap.loader.main_object.get_symbol("main")
#for addr, symbol in ap.analyses.Identifier().run():
# print hex(addr), symbol
s = ap.factory.entry_state()
print "Entry state: %s" % (s)
print "Entry state log actions: %s\n %s\n %s\n %s" % (s.log.actions, s.libc, s.cgc, s.fs)
'''
sm = ap.factory.simulation_manager(save_unconstrained=True)
#symbolically execute the binary until an unconstrained path is reached
while len(sm.unconstrained)==0:
sm.step()
unconstrained_state = sm.unconstrained[0]
crashing_input = unconstrained_state.posix.dumps(0)
print "buffer overflow found!"
print repr(crashing_input)
'''
print ap.loader.all_objects
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])
sys.exit(0)
# hcai: set ts=4 tw=120 sts=4 sw=4