-
Notifications
You must be signed in to change notification settings - Fork 43
/
vdp
executable file
·134 lines (103 loc) · 4.06 KB
/
vdp
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
#!/usr/bin/python2
"""
This file is part of VDISCOVER.
VDISCOVER is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VDISCOVER is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VDISCOVER. If not, see <http://www.gnu.org/licenses/>.
Copyright 2014 by G.Grieco
"""
import os
import argparse
import csv
import sys
import random
from subprocess import Popen, PIPE, STDOUT
from vdiscover.Detection import GetArgs, GetFiles, GetCmd
from vdiscover.Mutation import NullMutator, RandomByteMutator, RandomExpanderMutator, RandomInputMutator, opened_files
from vdiscover.Input import prepare_inputs
if __name__ == "__main__":
# To help argparse to detect the number of columns correctly
# os.environ['COLUMNS'] = str(os.popen('stty size',
# 'r').read().split()[1]) #str(shutil.get_terminal_size().columns)
# Arguments
parser = argparse.ArgumentParser(
description='Vulnerability Detection Procedure')
parser.add_argument(
"testcase", help="Testcase to analyze", type=str, default=None)
parser.add_argument("cmd", help="Testcase to analyze",
type=str, default=None)
parser.add_argument(
"--io-mode",
help="Input-Output mode",
action="store_true",
default=False)
parser.add_argument(
"--seed-range",
type=str,
help="A seed range to feed the cmd (io mode only)",
default="0:0")
parser.add_argument("--show-stdout",
help="Don't use /dev/null as stdout/stderr",
action="store_true", default=False)
parser.add_argument("--show-cmd",
help="xxx",
action="store_true", default=False)
parser.add_argument("--timeout", dest="timeout", type=int,
help="Timeout in seconds (io mode only)", default=3)
options = parser.parse_args()
start_seed, stop_seed = tuple(options.seed_range.split(":"))
testcase = options.testcase
vdp_cmd = options.cmd
show_stdout = options.show_stdout
io_mode = options.io_mode
timeout = options.timeout
show_cmd = options.show_cmd
os.chdir(testcase)
program = GetCmd(None)
os.chdir("inputs")
envs = dict()
args = GetArgs()
files = GetFiles()
original_inputs = RandomInputMutator(args + files, NullMutator)
_, original_input = original_inputs.next()
prepared_inputs = prepare_inputs(original_input)
if show_cmd:
print vdp_cmd, program, " ".join(prepared_inputs)
exit(0)
if io_mode:
DEVNULL = open(os.devnull, 'wb')
in_filename = files[0].GetName()
out_filename = files[0].GetFilename()
vdp_cmd = vdp_cmd.replace("<input>", in_filename)
vdp_cmd = vdp_cmd.replace("<output>", out_filename)
for seed in xrange(int(start_seed), int(stop_seed)):
cmd = vdp_cmd.replace("<seed>", str(seed))
p = Popen(cmd.split(" "), stdin=PIPE,
stdout=DEVNULL, stderr=DEVNULL, env=dict())
p.communicate()
p = Popen(["timeout",
"-k",
"1",
str(timeout),
program] + prepared_inputs,
stdin=PIPE,
stdout=DEVNULL,
stderr=DEVNULL,
env=dict())
p.communicate()
if p.returncode < 0:
print >> sys.stderr, testcase, p.returncode, seed
exit(1)
exit(0)
else:
p = Popen(vdp_cmd.split(" ") + [program] +
prepared_inputs, stdin=PIPE, env=dict())
p.communicate()
exit(p.returncode)