forked from dhabensky/PyBounds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskExecutor.py
More file actions
290 lines (225 loc) · 9.29 KB
/
Copy pathTaskExecutor.py
File metadata and controls
290 lines (225 loc) · 9.29 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
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
#!/usr/bin/python3.5
__author__ = 'avdoshintheannihilator'
import struct
from os.path import dirname, realpath
from subprocess import *
from External.TaskParser import TaskParser
from External.ArgumentSequence import *
from External.TimeoutOutputParser import Parser
from External.RunCmdTimeout import RunCmdTimeout
from External.Escape import *
from Shared.ExecutionResult import ExecutionResult
from Shared.Files import *
realpath = str(dirname(realpath(__file__)))
#files = Files('')
class TaskExecutor:
def __init__(self):
self.time_limit = 1.5 # sec
self.memory_limit = 1000000 # Kb
self.task_temp_file = None
self.task_parser = None
# smart mode
self.str_param = None
self.ip_coincidence = None
self.args_max = None
self.end_bounds_check = False
self.arch = None
def check_ip(self, ip, curent_args):
adr = str(ip)
# print(adr)
position = adr.find('x')
adr = adr[(position + 1):]
# print("target = " + target)
adr = int(adr, 16)
try:
str_adr = ((struct.pack(">I", adr)).decode())[::-1]
# print (str_adr)
except:
return False
if self.arch == "x86":
if (len(str_adr) == 4) and (self.str_param.find(str_adr) != -1):
if self.ip_coincidence is None:
self.ip_coincidence = str_adr
curent_args = curent_args.replace('\t', ' ')
print(curent_args)
return False
if self.ip_coincidence != str_adr:
print(self.args_max)
return True
else:
curent_args = curent_args.replace('\t', ' ')
self.args_max = curent_args
return False
elif self.ip_coincidence is not None:
print(self.args_max)
return True
elif self.arch == "x64":
if ((len(str_adr) >= 4) or (len(str_adr) <= 6)) and (self.str_param.find(str_adr) != -1):
if self.ip_coincidence is None:
self.ip_coincidence = str_adr
curent_args = curent_args.replace('\t', ' ')
print(curent_args)
return False
if self.ip_coincidence != str_adr:
print(self.args_max)
return True
else:
curent_args = curent_args.replace('\t', ' ')
self.args_max = curent_args
return False
elif self.ip_coincidence is not None:
print(self.args_max)
return True
def run(self, verbose=False, silent=False, smart=False):
task_result = []
task_list = self.task_parser.build_task_list(silent)
for task in task_list:
cases = []
task_result.append(cases)
oldresult = None
progname = ""
try:
for current_args in task:
if current_args[0].startswith("."):
current_args[0] = os.path.abspath(current_args[0])
progname = os.path.split(current_args[0])[1]
current_args = "\t".join(map(shell_escape, current_args))
# print(current_args)
if verbose:
print(current_args)
result = self.exec_and_get_result(progname, current_args)
if smart:
if result.ip != 0:
if self.check_ip(result.ip, current_args):
self.end_bounds_check = True
break
if oldresult is None or not result.equal(oldresult):
result.args_start_bound = current_args
result.args_start_list = task.get_args_str()
result.args_end_bound = current_args
result.args_end_list = result.args_start_list
cases.append(result)
else:
cases[len(cases) - 1].args_end_bound = current_args
cases[len(cases) - 1].args_end_list = task.get_args_str()
oldresult = result
except Exception as ex:
if not silent:
print("Error during task " + progname)
print(str(type(ex)) + " " + str(ex))
print("Task not completed")
if not self.end_bounds_check and smart:
print(self.args_max)
dump(task_result, files.output)
def res_from_dump(self):
try:
result = load(files.result_dump)
result.log.append("OUT. deserialized from file")
return result
except:
return ExecutionResult()
def res_from_stderr(self):
try:
parser = Parser(self.time_limit, self.memory_limit)
result = parser.parse_file(files.stderr)
return result
except:
return ExecutionResult()
def exec_and_get_result(self, progname, args):
rm_file(files.result_dump)
try:
timeout = self.exec_gdb_timeout(progname, args)
except Exception as ex:
print(str(ex.__class__.__name__) + " in exec_gdb(): " + str(ex))
raise
dumpres = self.res_from_dump()
stderrres = self.res_from_stderr()
if stderrres.state == 'memexceed':
return stderrres
if dumpres.state == 'crash':
if timeout:
res = ExecutionResult()
res.on_timeout(self.time_limit)
return res
if stderrres.state == 'signal':
return stderrres
return dumpres
if dumpres.state == 'exit' or dumpres.state == 'gdberror':
return dumpres
if timeout or stderrres.state == 'timeout':
res = ExecutionResult()
res.on_timeout(self.time_limit)
return res
if stderrres.state == 'signal':
return stderrres
return stderrres
def exec_gdb_timeout(self, progname, args):
try:
# + "task=r\"" + args.replace('"', '\\"') + "\";"\
echoarg = "python "\
+ "path=\"" + str(realpath) + "\";"\
+ "dir_name=\"" + self.task_parser.dir_name + "\";"\
+ "taskfile=\"" + self.task_parser.task_file + "\";"\
+ "exec(open(\"" + str(realpath) + "/GdbInternal/Executor.py\").read())"
# print echoarg
# input()
# print()
task_to_gdb = open((self.task_parser.dir_name + "/task_to_gdb"), 'w')
task_to_gdb.write(args)
task_to_gdb.close()
pipe = " | "
gdbstart = "perl -w " + str(realpath) + "/timeout" + " -t " + str(self.time_limit) + " -m " + str(self.memory_limit)\
+ " gdb --silent 2>" + shell_escape(files.stderr) + " 1>/dev/null"
cmd = "echo " + shell_escape(echoarg) + pipe + gdbstart
# print("cmd :", cmd)
# print()
return RunCmdTimeout(cmd, progname, self.time_limit).run()
except Exception as ex:
print("exec_gdb_timeout " + str(ex))
return False
def main(dir_name, task_file, time, memory, verbose, silent, smart, arch):
global files
# print(realpath)
files = Files(dir_name, task_file)
# if not os.path.exists(os.path.expanduser("~/.PyBounds")):
# os.mkdir(os.path.expanduser("~/.PyBounds"))
try:
files = Files(dir_name, task_file)
if not os.path.exists(files.regclasses):
try:
open(files.regclasses, 'w').close()
except:
pass
parser = TaskParser()
executor = TaskExecutor()
# executor.compression = CompressionParameters()
# executor.compression.main(files.task)
parser.dir_name = dir_name
parser.set_task_file(files.task)
parser.load_registered_classes(files.regclasses)
# executor.compression.return_file_back(files.task)
executor.task_parser = parser
executor.task_temp_file = files.task_temp
executor.time_limit = time
executor.memory_limit = memory
if smart:
file_param = open(task_file, 'r')
executor.str_param = str(file_param.read())
file_param.close()
position = executor.str_param.find("LongArgFromFile(\'")
executor.str_param = executor.str_param[position + 17:]
position = executor.str_param.find("\'")
executor.str_param = executor.str_param[:position]
file_param = open(executor.str_param, 'r')
executor.str_param = str(file_param.read())
file_param.close()
executor.arch = arch
executor.run(verbose, silent, smart)
rm_file(files.result_dump)
rm_file(files.task_temp)
#os.system("cat " + shell_escape(files.stderr))
rm_file(files.stderr)
except Exception as ex:
print("TaskExecutor.main error" + str(ex))
if not smart:
print("PyBounds: tasks completed")