-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_all.py
executable file
·349 lines (308 loc) · 12.3 KB
/
run_all.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#! /usr/bin/python
# coding:utf-8
try:
import cmd2 as cmd
except ImportError:
import cmd
from tools.core import *
from tools.script_tools import *
cases = []
select_cases = []
select_bench = []
select_vul = []
select_attack = []
attack_mode = True
run_single = False
report_buf = []
root_path = os.path.abspath('.')
class ui(cmd.Cmd):
prompt = "CSTE>"
intro = '''简要说明:通过以下命令, 运行指定的漏洞程序
show 显示可配置信息
set 配置运行参数
run 运行选择的漏洞程序
aslr status 获取ASLR的状态
aslr on/off/conservative 修改ASLR状态
help [cmd] 查看相关命令信息
q 退出'''
def do_reload(self, line):
'''Reload the test cases.'''
global cases, root_path
root_path = os.path.abspath('.')
if 'src' in os.listdir('.'):
path = root_path + '/src'
else:
print "No test cases found please run the script in the CSTE root path." # TODO auto correct path.
return True
cases = list_cases(path)
def do_show(self, line):
'''
Options(default value):
bench(all), vul(all), attack(all), mode(attack)
Format:
show show all options
show vul/v show all vulnerability types
show attacks/a show all attack types
show bench/b show all benchmarks
mode: attack/normal
show all show all test cases for single run
Use "set key value" to set these options.
Use "run" to run all test cases.
Use "show selected/s" to confirm the options.
'''
global cases, select_cases, select_vul, select_attack, select_bench, attack_mode, run_single
i = 1
types = set()
# if all
if len(line.split()) == 0:
self.do_help("show")
# if 1 arg
elif len(line.split()) == 1:
# if selected
if line.startswith('s'):
print "Vulnerabilities: ", select_vul if select_vul else 'all'
print "Attack types: ", select_attack if select_attack else 'all'
print "Bench mark: ", select_bench if select_bench else 'all'
print "Mode:", 'Attack' if attack_mode else 'Normal'
print "Single mode:", "Yes" if run_single else "No"
return
# if vul
if line.startswith('v'):
for case in cases:
types.update(case.define_data['vul_type'])
# if attack/all
if line.startswith('a'):
if line.split()[-1] == 'all':
for case in cases:
print i, ':', case.path.replace(os.path.abspath(root_path + '/src') + '/', '')
i += 1
else:
for case in cases:
types.update([i["type"] for i in case.define_data["attack_class"]])
# if bench
if line.startswith('b'):
for case in cases:
types.update([case.define_data['bench']])
for t in types:
print t
# if 2 arg
elif len(line.split()) == 2:
arg_name = line.split()[1]
# if vul
if line.startswith('v'):
for case in cases:
if arg_name in case.define_data["vul_type"]:
print i, ':', case.path.replace(os.path.abspath(root_path + '/src') + '/', '')
i += 1
# if attack
if line.startswith('a'):
for case in cases:
if arg_name in [att['type'] for att in case.define_data["attack_class"]]:
print i, ':', case.path.replace(os.path.abspath(root_path + '/src') + '/', '')
i += 1
# if bench
if line.startswith('b'):
for case in cases:
if arg_name == case.define_data["bench"]:
print i, ':', case.path.replace(os.path.abspath(root_path + '/src') + '/', '')
i += 1
def do_guide(self, line):
print self.intro
def _count(self):
global cases, select_cases, select_bench, select_vul, select_attack, attack_mode
def in_bench(c):
if not select_bench:
return True
else:
if c.define_data['bench'] in select_bench:
return True
else:
return False
def in_vul(c):
if not select_vul:
return True
else:
for v in c.define_data['vul_type']:
if v in select_vul: return True
return False
def in_attack(c):
if not select_attack:
return True
else:
for a in [a['type'] for a in c.define_data["attack_class"]]:
if a in select_attack: return True
return False
def in_mode(c):
if attack_mode:
return True if len(c.define_data["attack_class"]) else False
else:
return True if len(c.define_data["normal_class"]) else False
select_cases = []
for case in cases:
if in_bench(case) and in_vul(case) and in_attack(case) and in_mode(case):
select_cases.append(case)
def do_set(self, line):
'''
Select all/by bench/vul type/attack type/attack/normal
Format:
(default) select all
set bench/b [bench_name] select all test cases in the bench
set vul/v [vul_type] select all test cases in the vulnerability type
set attack/a [attack_type] select all test cases with the attack_type(and select attack mode)
set mode/m attack/a/normal/n select attack/normal mode
set single/s [number] select by number (in "show all") (will override/override by [bench,vul,attack]
Quick set:
set default default
set all all test cases, same as default
set normal all test cases, but normal input
set attack all test cases, same as default
'''
global cases, select_cases, select_bench, select_cases, select_attack, select_vul, attack_mode, run_single
if not line:
self.do_help('set')
return
# not quick set
if len(line.split())>1:
# if number
if line.startswith('s'):
indexes = [int(i) - 1 for i in line.split()[1:]]
select_cases = [cases[i] for i in indexes]
run_single = True
# if bench
elif line.startswith('b'):
arg = line.split()[1:] if len(line.split()) > 1 else None
if not arg: print "Format error."; return
select_bench = arg
self._count()
if run_single:
print "This operation will override the [single] selection."
run_single = False
# if vul
elif line.startswith('v'):
arg = line.split()[1:] if len(line.split()) > 1 else None
if not arg: print "Format error."; return
select_vul = arg
self._count()
if run_single:
print "This operation will override the [single] selection."
run_single = False
# if attack
elif line.startswith('a'):
arg = line.split()[1:] if len(line.split()) > 1 else None
if not arg: print "Format error."; return
select_attack = arg
self._count()
if run_single:
print "This operation will override the [single] selection."
run_single = False
# if mode
elif line.startswith('m'):
arg = line.split()[1] if len(line.split()) > 1 else None
if arg.startswith('a'):
attack_mode = True
elif arg.startswith('n'):
attack_mode = False
else:
print "Wrong mode."
if not run_single: self._count()
# quick set: default, all, normal, attack
else:
if line.strip()=='default':
select_cases = []
select_bench = []
select_vul = []
select_attack = []
attack_mode = True
elif line.strip()=='all':
select_cases = []
select_bench = []
select_vul = []
select_attack = []
attack_mode = True
elif line.strip()=='normal':
select_cases = []
select_bench = []
select_vul = []
select_attack = []
attack_mode = False
elif line.strip()=='attack':
select_cases = []
select_bench = []
select_vul = []
select_attack = []
attack_mode = True
self._count()
print "Now selected %d cases" % len(select_cases)
def do_run(self, line):
'''
Run
Format:
run run the test cases and show the report
'''
global report_buf, select_cases, cases
if not select_cases:
select_cases = cases
print "No test cases selected, run all."
else:
for c in select_cases:
print colorize("Running: ",'cyan'), c.define_data['name']
report_buf += c.run_all(attack_type=select_attack, attack_mode=attack_mode, check=True)
for i in report_buf: print i
report_buf = []
def do_check(self, line):
'''Check all selected.'''
if select_cases:
for case in select_cases:
ans = case.check()
report_buf.append(ans)
print ans
else:
print "No case selected, check all."
for case in cases:
ans = case.check()
report_buf.append(ans)
print ans
def do_add(self, line):
'''Add cases after select'''
print '''Not implemented yet.'''
def do_remove(self, line):
'''Remove cases after select'''
print '''Not implemented yet.'''
def do_report(self, line): # design how to report
''''''
print '''Not implemented yet.'''
def do_aslr(self, line):
'''Check status/Turn on/Turn off ASLR of system.
Format: aslr status/check/on/off/conservative'''
if line in ['status', 'frame_check', 'on', 'off', 'conservative']:
if line[1] in ['h', 't']:
state = aslr_status()
if state == 2:
print "ASLR: ON\n"
elif state == 0:
print "ASLR: OFF\n"
elif state == 1:
print "ASLR: Conservative ON\n"
else:
print "Invalid Value."
elif line[1] == 'n':
aslr_on()
elif line[1] == 'f':
aslr_off()
elif line[1] == 'o':
aslr_conservative()
else:
print colorize('[Error]: ', 'red'), 'Wrong Format.'
self.do_help('aslr')
def complete_aslr(self, text, line, begidx, endidx):
return [i for i in ['status', 'check', 'on', 'off', 'conservative'] if i.startswith(text)]
def do_q(self, line):
'''Quit.'''
return True
CSTEui = ui()
# delete unused command (make command list clear)
for attr in ['do_list', 'do_r', 'do_cmdenvironment', 'do_history', 'do_hi', 'do_save',
'do_pause', 'do_ed', 'do_edit', 'do_EOF', 'do_eof', 'do_li', 'do_l', 'do_quit']:
if hasattr(cmd.Cmd, attr): delattr(cmd.Cmd, attr)
CSTEui.do_reload('')
CSTEui.cmdloop()