Skip to content

Commit 2f1cfb3

Browse files
author
gustavo
committed
autopep8, starting refactoring
1 parent 8c947b7 commit 2f1cfb3

30 files changed

+2740
-2449
lines changed

fextractor

Lines changed: 99 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,19 @@ import csv
2525
import sys
2626
import random
2727

28-
from vdiscover.Detection import GetArgs, GetFiles, GetCmd
28+
from vdiscover.Detection import GetArgs, GetFiles, GetCmd
2929

3030
# static feature extraction
3131

3232
from vdiscover.RandomWalk import RandomWalkElf
3333

3434
# dynamic feature extraction
3535

36-
from vdiscover.Process import Process
37-
from vdiscover.Mutation import NullMutator, RandomByteMutator, RandomExpanderMutator, RandomInputMutator
38-
from vdiscover.Printer import TypePrinter
39-
from vdiscover.Misc import readmodfile
40-
from vdiscover.Input import prepare_inputs
41-
42-
36+
from vdiscover.Process import Process
37+
from vdiscover.Mutation import NullMutator, RandomByteMutator, RandomExpanderMutator, RandomInputMutator
38+
from vdiscover.Printer import TypePrinter
39+
from vdiscover.Misc import readmodfile
40+
from vdiscover.Input import prepare_inputs
4341

4442

4543
if __name__ == "__main__":
@@ -48,16 +46,19 @@ if __name__ == "__main__":
4846
random.seed()
4947

5048
# To help argparse to detect the number of columns correctly
51-
#os.environ['COLUMNS'] = str(os.popen('stty size', 'r').read().split()[1]) #str(shutil.get_terminal_size().columns)
49+
# os.environ['COLUMNS'] = str(os.popen('stty size',
50+
# 'r').read().split()[1]) #str(shutil.get_terminal_size().columns)
5251

5352
if open("/proc/sys/kernel/randomize_va_space").read().strip() != "0":
5453
print("Address space layout randomization (ASLR) is enabled, disable it before continue to use the cache")
5554
print("Hint: # echo 0 > /proc/sys/kernel/randomize_va_space")
5655
sys.exit(-1)
5756

5857
# Arguments
59-
parser = argparse.ArgumentParser(description='Feature extraction of VDiscover')
60-
parser.add_argument("testcase", help="Testcase to analyze", type=str, default=None)
58+
parser = argparse.ArgumentParser(
59+
description='Feature extraction of VDiscover')
60+
parser.add_argument(
61+
"testcase", help="Testcase to analyze", type=str, default=None)
6162

6263
parser.add_argument("--static",
6364
help="Extract only static features from an executable",
@@ -67,54 +68,80 @@ if __name__ == "__main__":
6768
help="Extract only dynamic features from a testcase",
6869
action="store_true", default=False)
6970

70-
parser.add_argument("--mclass", type=str,
71-
help="Include class column, to use later in training mode",
72-
action="store", default=None)
71+
parser.add_argument(
72+
"--mclass",
73+
type=str,
74+
help="Include class column, to use later in training mode",
75+
action="store",
76+
default=None)
7377

7478
parser.add_argument("--out-file",
7579
help="File to output the extracted features",
7680
type=str, default="/dev/stdout")
7781

78-
parser.add_argument("--max-subtraces-collected", type=int,
79-
help="Maximum number of subtraces collected (static features only)", default=100)
80-
81-
parser.add_argument("--max-subtraces-explored", type=int,
82-
help="Maximum number of subtraces explored (static features only)", default=10000)
83-
84-
parser.add_argument("--min-subtrace-size", type=int,
85-
help="Minumum number of events in each subtrace collected (static features only)", default=3)
86-
87-
parser.add_argument("--show-stdout",
88-
help="Don't use /dev/null as stdout/stderr (dynamic features only)",
89-
action="store_true", default=False)
90-
91-
parser.add_argument("--inc-mods",
92-
help="Only extract features from the libraries matching the strings inside this file (dynamic features only)",
93-
type=str, default=None)
94-
95-
parser.add_argument("--ign-mods",
96-
help="Ignore extracted features from the libraries matching the string inside this file (dynamic features only)",
97-
type=str, default=None)
98-
99-
parser.add_argument("--timeout", dest="timeout", type=int,
100-
help="Timeout in seconds (dynamic features only)", default=3)
101-
102-
parser.add_argument("--max-mutations", type=int,
103-
help="Maximum number of mutations to the original testcase (dynamic features only)", default=0)
82+
parser.add_argument(
83+
"--max-subtraces-collected",
84+
type=int,
85+
help="Maximum number of subtraces collected (static features only)",
86+
default=100)
87+
88+
parser.add_argument(
89+
"--max-subtraces-explored",
90+
type=int,
91+
help="Maximum number of subtraces explored (static features only)",
92+
default=10000)
93+
94+
parser.add_argument(
95+
"--min-subtrace-size",
96+
type=int,
97+
help="Minumum number of events in each subtrace collected (static features only)",
98+
default=3)
99+
100+
parser.add_argument(
101+
"--show-stdout",
102+
help="Don't use /dev/null as stdout/stderr (dynamic features only)",
103+
action="store_true",
104+
default=False)
105+
106+
parser.add_argument(
107+
"--inc-mods",
108+
help="Only extract features from the libraries matching the strings inside this file (dynamic features only)",
109+
type=str,
110+
default=None)
111+
112+
parser.add_argument(
113+
"--ign-mods",
114+
help="Ignore extracted features from the libraries matching the string inside this file (dynamic features only)",
115+
type=str,
116+
default=None)
117+
118+
parser.add_argument(
119+
"--timeout",
120+
dest="timeout",
121+
type=int,
122+
help="Timeout in seconds (dynamic features only)",
123+
default=3)
124+
125+
parser.add_argument(
126+
"--max-mutations",
127+
type=int,
128+
help="Maximum number of mutations to the original testcase (dynamic features only)",
129+
default=0)
104130

105131
options = parser.parse_args()
106132
testcase = options.testcase
107133

108134
static_only = options.static
109135
dynamic_only = options.dynamic
110136

111-
if (not static_only and not dynamic_only) or (static_only and dynamic_only):
112-
print "The feature extraction requires to select either static of dynamic features exclusively"
113-
exit(-1)
137+
if (not static_only and not dynamic_only) or (
138+
static_only and dynamic_only):
139+
print "The feature extraction requires to select either static of dynamic features exclusively"
140+
exit(-1)
114141

115142
max_subtraces_collected = options.max_subtraces_collected
116-
max_subtraces_explored = options.max_subtraces_explored
117-
min_subtrace_size = options.min_subtrace_size
143+
max_subtraces_explored = options.max_subtraces_explored
144+
min_subtrace_size = options.min_subtrace_size
118145

119146
incmodfile = options.inc_mods
120147
ignmodfile = options.ign_mods
@@ -135,39 +162,42 @@ if __name__ == "__main__":
135162

136163
if static_only:
137164

138-
RandomWalkElf(program, csvfile, mclass, max_subtraces_collected, max_subtraces_explored, min_subtrace_size)
165+
RandomWalkElf(program, csvfile, mclass, max_subtraces_collected,
166+
max_subtraces_explored, min_subtrace_size)
139167

140168
elif dynamic_only:
141169

142-
os.chdir("inputs")
170+
os.chdir("inputs")
143171

144-
envs = dict()
145-
args = GetArgs()
146-
files = GetFiles()
172+
envs = dict()
173+
args = GetArgs()
174+
files = GetFiles()
147175

148-
original_inputs = RandomInputMutator(args + files, NullMutator)
149-
#expanded_input_generator = RandomInputMutator(args + files, RandomExpanderMutator)
150-
mutated_input_generator = RandomInputMutator(args + files, RandomByteMutator)
151-
if included_mods == []:
152-
included_mods = [program]
176+
original_inputs = RandomInputMutator(args + files, NullMutator)
177+
#expanded_input_generator = RandomInputMutator(args + files, RandomExpanderMutator)
178+
mutated_input_generator = RandomInputMutator(
179+
args + files, RandomByteMutator)
180+
if included_mods == []:
181+
included_mods = [program]
153182

154-
app = Process(program, envs, timeout, included_mods, ignored_mods, no_stdout = not show_stdout )
155-
prt = TypePrinter(csvfile, testcase, mclass)
183+
app = Process(program, envs, timeout, included_mods,
184+
ignored_mods, no_stdout=not show_stdout)
185+
prt = TypePrinter(csvfile, testcase, mclass)
156186

157-
# unchanged input
158-
null_mutt, original_input = original_inputs.next()
159-
original_events = app.getData(prepare_inputs(original_input))
187+
# unchanged input
188+
null_mutt, original_input = original_inputs.next()
189+
original_events = app.getData(prepare_inputs(original_input))
160190

161-
if original_events is None:
162-
print "Execution of",program,"failed!"
163-
exit(-1)
191+
if original_events is None:
192+
print "Execution of", program, "failed!"
193+
exit(-1)
164194

165-
prt.print_events(program,original_events)
195+
prt.print_events(program, original_events)
166196

167-
for (i, (d, mutated)) in enumerate(mutated_input_generator):
197+
for (i, (d, mutated)) in enumerate(mutated_input_generator):
168198

169-
if i >= max_mut:
170-
break
199+
if i >= max_mut:
200+
break
171201

172-
events = app.getData(prepare_inputs(mutated))
173-
prt.print_events(program,events)
202+
events = app.getData(prepare_inputs(mutated))
203+
prt.print_events(program, events)

setup.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
url='http://vdiscover.org/',
1313
author='G.Grieco',
1414
author_email='[email protected]',
15-
scripts=['fextractor', 'vpredictor', 'tcreator', 'tseeder', 'vd'],
15+
scripts=[
16+
'fextractor',
17+
'vpredictor',
18+
'tcreator',
19+
'tseeder',
20+
'vd'],
1621
install_requires=[
1722
"python-ptrace",
18-
"scikit-learn"
19-
],
23+
"scikit-learn"],
2024
)
21-

tcreator

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,73 @@ import sys
2525
import csv
2626

2727
from vdiscover.Detection import WriteTestcase
28-
concatenate = lambda *lists: reduce((lambda a,b: a.extend(b) or a),lists,[])
28+
from functools import reduce
29+
concatenate = lambda *lists: reduce((lambda a, b: a.extend(b) or a), lists, [])
2930

3031
if __name__ == "__main__":
3132

3233
# Arguments
33-
parser = argparse.ArgumentParser(description='A small utility to create new test cases using a name and a command line')
34-
parser.add_argument("--name", help="The name of the ", type=str, default=None)
35-
parser.add_argument("--cmd", help="Command-line to execute", type=str, default=None)
36-
parser.add_argument("--batch", help="A csv with the command lines", type=str, default=None)
37-
38-
parser.add_argument("--copy", help="Force the copy of the files in command lines instead of symbolic linking", action='store_true', default=False)
39-
40-
parser.add_argument("outdir", help="Output directory to write testcases", type=str, default=None)
34+
parser = argparse.ArgumentParser(
35+
description='A small utility to create new test cases using a name and a command line')
36+
parser.add_argument("--name", help="The name of the ",
37+
type=str, default=None)
38+
parser.add_argument(
39+
"--cmd", help="Command-line to execute", type=str, default=None)
40+
parser.add_argument(
41+
"--batch", help="A csv with the command lines", type=str, default=None)
42+
43+
parser.add_argument(
44+
"--copy",
45+
help="Force the copy of the files in command lines instead of symbolic linking",
46+
action='store_true',
47+
default=False)
48+
49+
parser.add_argument(
50+
"outdir",
51+
help="Output directory to write testcases",
52+
type=str,
53+
default=None)
4154

4255
options = parser.parse_args()
4356
name = options.name
4457
cmd = options.cmd
4558
in_file = options.batch
4659
copy = options.copy
47-
out_dir= options.outdir
60+
out_dir = options.outdir
4861

4962
if (name is not None and cmd is not None) ^ (in_file is not None):
50-
pass
63+
pass
5164
else:
52-
#or (name not is None and cmd is not None) and in_file is None:
53-
print "Either name and command should be used or an input file"
54-
exit(-1)
65+
# or (name not is None and cmd is not None) and in_file is None:
66+
print "Either name and command should be used or an input file"
67+
exit(-1)
5568

5669
try:
57-
os.makedirs(out_dir)
70+
os.makedirs(out_dir)
5871
except:
59-
pass
72+
pass
6073

6174
if in_file is not None:
62-
infile = open(in_file,"r")
63-
csvreader = csv.reader(infile, delimiter='\t')
64-
os.chdir(out_dir)
75+
infile = open(in_file, "r")
76+
csvreader = csv.reader(infile, delimiter='\t')
77+
os.chdir(out_dir)
6578

66-
for i,row in enumerate(csvreader):
67-
args = filter(lambda x: x is not '', row[0].split(" "))
68-
name = args[0].replace("/","_")+":"+str(i)
69-
WriteTestcase(name,args[0],args[1:], copy)
79+
for i, row in enumerate(csvreader):
80+
args = filter(lambda x: x is not '', row[0].split(" "))
81+
name = args[0].replace("/", "_") + ":" + str(i)
82+
WriteTestcase(name, args[0], args[1:], copy)
7083

7184
else:
7285

73-
os.chdir(out_dir)
74-
args = cmd.split("'")
75-
args = map(lambda x: x.split(" "), args)
76-
pargs = []
77-
78-
for arg in args:
79-
if arg <> '':
80-
pargs = pargs + arg
81-
#args = concatenate(args)
82-
print "Procesing '" + " ".join(pargs) + "'"
83-
#args = filter(lambda x: x is not '', cmd.split(" "))
84-
WriteTestcase(name,pargs[0],pargs[1:], copy)
85-
86+
os.chdir(out_dir)
87+
args = cmd.split("'")
88+
args = map(lambda x: x.split(" "), args)
89+
pargs = []
90+
91+
for arg in args:
92+
if arg != '':
93+
pargs = pargs + arg
94+
#args = concatenate(args)
95+
print "Procesing '" + " ".join(pargs) + "'"
96+
#args = filter(lambda x: x is not '', cmd.split(" "))
97+
WriteTestcase(name, pargs[0], pargs[1:], copy)

0 commit comments

Comments
 (0)