-
Notifications
You must be signed in to change notification settings - Fork 43
/
fextractor
executable file
·203 lines (152 loc) · 6.26 KB
/
fextractor
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
#!/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 vdiscover.Detection import GetArgs, GetFiles, GetCmd
# static feature extraction
from vdiscover.RandomWalk import RandomWalkElf
# dynamic feature extraction
from vdiscover.Process import Process
from vdiscover.Mutation import NullMutator, RandomByteMutator, RandomExpanderMutator, RandomInputMutator
from vdiscover.Printer import TypePrinter
from vdiscover.Misc import readmodfile
from vdiscover.Input import prepare_inputs
if __name__ == "__main__":
# Random seed initialziation
random.seed()
# 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)
if open("/proc/sys/kernel/randomize_va_space").read().strip() != "0":
print("Address space layout randomization (ASLR) is enabled, disable it before continue to use the cache")
print("Hint: # echo 0 > /proc/sys/kernel/randomize_va_space")
sys.exit(-1)
# Arguments
parser = argparse.ArgumentParser(
description='Feature extraction of VDiscover')
parser.add_argument(
"testcase", help="Testcase to analyze", type=str, default=None)
parser.add_argument("--static",
help="Extract only static features from an executable",
action="store_true", default=False)
parser.add_argument("--dynamic",
help="Extract only dynamic features from a testcase",
action="store_true", default=False)
parser.add_argument(
"--mclass",
type=str,
help="Include class column, to use later in training mode",
action="store",
default=None)
parser.add_argument("--out-file",
help="File to output the extracted features",
type=str, default="/dev/stdout")
parser.add_argument(
"--max-subtraces-collected",
type=int,
help="Maximum number of subtraces collected (static features only)",
default=100)
parser.add_argument(
"--max-subtraces-explored",
type=int,
help="Maximum number of subtraces explored (static features only)",
default=10000)
parser.add_argument(
"--min-subtrace-size",
type=int,
help="Minumum number of events in each subtrace collected (static features only)",
default=3)
parser.add_argument(
"--show-stdout",
help="Don't use /dev/null as stdout/stderr (dynamic features only)",
action="store_true",
default=False)
parser.add_argument(
"--inc-mods",
help="Only extract features from the libraries matching the strings inside this file (dynamic features only)",
type=str,
default=None)
parser.add_argument(
"--ign-mods",
help="Ignore extracted features from the libraries matching the string inside this file (dynamic features only)",
type=str,
default=None)
parser.add_argument(
"--timeout",
dest="timeout",
type=int,
help="Timeout in seconds (dynamic features only)",
default=3)
parser.add_argument(
"--max-mutations",
type=int,
help="Maximum number of mutations to the original testcase (dynamic features only)",
default=0)
options = parser.parse_args()
testcase = options.testcase
static_only = options.static
dynamic_only = options.dynamic
if (not static_only and not dynamic_only) or (
static_only and dynamic_only):
print "The feature extraction requires to select either static of dynamic features exclusively"
exit(-1)
max_subtraces_collected = options.max_subtraces_collected
max_subtraces_explored = options.max_subtraces_explored
min_subtrace_size = options.min_subtrace_size
incmodfile = options.inc_mods
ignmodfile = options.ign_mods
# modules to include or ignore
included_mods = readmodfile(incmodfile)
ignored_mods = readmodfile(ignmodfile)
show_stdout = options.show_stdout
max_mut = options.max_mutations
timeout = options.timeout
mclass = options.mclass
csvfile = options.out_file
os.chdir(testcase)
program = GetCmd(None)
if static_only:
RandomWalkElf(program, csvfile, mclass, max_subtraces_collected,
max_subtraces_explored, min_subtrace_size)
elif dynamic_only:
os.chdir("inputs")
envs = dict()
args = GetArgs()
files = GetFiles()
original_inputs = RandomInputMutator(args + files, NullMutator)
#expanded_input_generator = RandomInputMutator(args + files, RandomExpanderMutator)
mutated_input_generator = RandomInputMutator(
args + files, RandomByteMutator)
if included_mods == []:
included_mods = [program]
app = Process(program, envs, timeout, included_mods,
ignored_mods, no_stdout=not show_stdout)
prt = TypePrinter(csvfile, testcase, mclass)
# unchanged input
null_mutt, original_input = original_inputs.next()
original_events = app.getData(prepare_inputs(original_input))
if original_events is None:
print "Execution of", program, "failed!"
exit(-1)
prt.print_events(program, original_events)
for (i, (d, mutated)) in enumerate(mutated_input_generator):
if i >= max_mut:
break
events = app.getData(prepare_inputs(mutated))
prt.print_events(program, events)