-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_indeces_flirt.py
executable file
·287 lines (233 loc) · 9.96 KB
/
generate_indeces_flirt.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
'''
This python code was written to create another version of the database that contains names of the functions
See generate_indeces.py
'''
#!/usr/bin/python3
import subprocess, os, fnmatch, codecs, random, shutil, multiprocessing, glob
import sys, bisect, numpy, traceback, re
from math import ceil
from absl import app
from absl import flags
from subprocess import Popen, PIPE, STDOUT
from operator import itemgetter
from collections import defaultdict
import itertools
flags.DEFINE_string('work_directory',
"./db/",
"The directory into whre the database will be created")
# Clobber existing data directory or not.
flags.DEFINE_boolean('clobber', False, "Clobber output directory or not.")
# Directory for executable files to train on.
flags.DEFINE_string('executable_directory', './',
"The directory where the ELF and PE executables to train on can be found " +\
"in their relevant subdirectories ELF/**/* and PE/**/*")
# Max number of pairs for the created dataset.
flags.DEFINE_integer('max_func_pairs', 1000000, "Max number of function pairs for " +
"the created databases")
flags.DEFINE_integer('max_file_pairs', 5000, "Max number of file pairs for " +
"the created databases")
#=============================================================================
FLAGS = flags.FLAGS
# file_name = NAME-ARCH-COMP-VERS-FLAG.PLAT.txt
NAME = 0
ARCH = 1
COMP = 2
VERS = 3
FLAG = 4
PLAT = 5
def FindSymbolFiles():
return [filename[:-4] for filename in os.listdir(FLAGS.work_directory)
if filename[-4:] == ".txt"]
def FindModifiedSymbolFiles(modified):
return [filename[:-4] for filename in os.listdir(FLAGS.work_directory + modified)
if filename[-4:] == ".txt"]
def FindDiffX(symbol_files, X, dontcare):
symbol_files_pair_list = []
# file_name = NAME-ARCH-COMP-VERS-FLAG.PLAT.txt
for file_pair in itertools.combinations(symbol_files , r=2):
fileone = file_pair[0].split('-')
filetwo = file_pair[1].split('-')
fileone.append(file_pair[0].split('.')[-1])
filetwo.append(file_pair[1].split('.')[-1])
append = 1
for i in range(6):
if i in dontcare:
continue
if i in X:
if fileone[i] == filetwo[i]:
append = 0
else:
if fileone[i] != filetwo[i]:
append = 0
if append:
symbol_files_pair_list.append(file_pair)
random.shuffle(symbol_files_pair_list)
symbol_files_pair_list = symbol_files_pair_list[:FLAGS.max_file_pairs]
return symbol_files_pair_list
def FindandWriteFunctionPair(symbol_files_pair_list, db_name):
files_and_address_list = []
try:
for symbol_file_pair in symbol_files_pair_list:
symbol_dict = defaultdict(list)
with open(FLAGS.work_directory + symbol_file_pair[0] + ".txt" , "r") as fileone:
for line in fileone:
symbol_dict[line.split()[2]].append((line.split()[0], line.split()[1]))
with open(FLAGS.work_directory + symbol_file_pair[1] + ".txt" , "r") as filetwo:
for line in filetwo:
if line.split()[2] in symbol_dict:
symbol_dict[line.split()[2]].append((line.split()[0], line.split()[1]))
files_and_address_list += list(symbol_dict.items())
random.shuffle(files_and_address_list)
size = WriteFunctionPair(files_and_address_list, FLAGS.work_directory +
"databases_flirt/%s.txt" % db_name)
print("Done writing %d to the DB \"%s.txt\"" % (size, db_name) )
except:
print("Error while creating DB \"%s.txt\"" % db_name)
def CreatDB():
if not os.path.exists(FLAGS.work_directory + "/databases"):
os.mkdir(FLAGS.work_directory + "databases")
print("Creating DB from binaries compiled with different compiler flags..." , end=" ")
FindandWriteFunctionPair(FindDiffX(FindSymbolFiles(), list([FLAG]), list([])), "different_flags")
print("Creating DB from binaries compiled with different versions of compilers...", end = " ")
FindandWriteFunctionPair(FindDiffX(FindSymbolFiles(), list([VERS]), list([])), "different_compiler_versions")
print("Creating DB from binaries compiled with different compilers...", end = " ")
FindandWriteFunctionPair(FindDiffX(FindSymbolFiles(), list([COMP]), list([VERS])), "different_compilers")
print("Creating DB from binaries compiled for different architectures...", end = " ")
FindandWriteFunctionPair(FindDiffX(FindSymbolFiles(), list([ARCH]), list([])), "different_archs")
print("Creating DB from binaries compiled for different platforms...", end = " ")
FindandWriteFunctionPair(FindDiffX(FindSymbolFiles(), list([PLAT]), list([COMP,VERS,FLAG])), "different_platforms")
print("Creating DB from random binaries...", end = " ")
FindandWriteFunctionPair(FindDiffX(FindSymbolFiles(), list([]), list([ARCH,COMP,VERS,FLAG,PLAT])), "random")
print("Creating DB of Modified binaries...")
CreateModifiedDB()
def CreateModifiedDB():
try:
error = 0
for i in range(0,4):
small_modified = []
medium_modified = []
large_modified = []
for symbol_file in FindModifiedSymbolFiles("modified" + str(i)):
symbol_dict_small = defaultdict(list)
symbol_dict_medium = defaultdict(list)
symbol_dict_large = defaultdict(list)
with open(FLAGS.work_directory + "modified" + str(i) + "/" + symbol_file + ".txt" , "r") as fileone:
for line in fileone:
if line.split()[2] in ModifiedFunctions(0,i):
symbol_dict_small[line.split()[2]].append((line.split()[0],
line.split()[1]))
elif line.split()[2] in ModifiedFunctions(1,i):
symbol_dict_medium[line.split()[2]].append((line.split()[0],
line.split()[1]))
elif line.split()[2] in ModifiedFunctions(2,i):
symbol_dict_large[line.split()[2]].append((line.split()[0],
line.split()[1]))
with open(FLAGS.work_directory + symbol_file + ".txt" , "r") as filetwo:
for line in filetwo:
if line.split()[2] in symbol_dict_small:
symbol_dict_small[line.split()[2]].append((line.split()[0],
line.split()[1]))
elif line.split()[2] in symbol_dict_medium:
symbol_dict_medium[line.split()[2]].append((line.split()[0],
line.split()[1]))
elif line.split()[2] in symbol_dict_large:
symbol_dict_large[line.split()[2]].append((line.split()[0],
line.split()[1]))
small_modified += list(symbol_dict_small.items())
medium_modified += list(symbol_dict_medium.items())
large_modified += list(symbol_dict_large.items())
random.shuffle(small_modified)
random.shuffle(medium_modified)
random.shuffle(large_modified)
error = -1
size = WriteFunctionPair(small_modified, FLAGS.work_directory +
"databases_flirt/small_size_modified" + str(i) + ".txt")
print("Done writing %d to the DB \"small_size_modified" % size + str(i) + ".txt\"" )
error = -2
size = WriteFunctionPair(medium_modified, FLAGS.work_directory +
"databases_flirt/medium_size_modified" + str(i) + ".txt")
print("Done writing %d to the DB \"medium_size_modified" % size + str(i) + ".txt\"" )
error = -3
size = WriteFunctionPair(large_modified, FLAGS.work_directory +
"databases_flirt/large_size_modified" + str(i) + ".txt")
print("Done writing %d to the DB \"large_size_modified" % size + str(i) + ".txt\"")
except IOError:
if error == -1:
print("Error while creating \"small_size_modified" + str(i) + ".txt\" database")
elif error == -2:
print("Error while creating \"medium_size_modified" + str(i) + ".txt\" database")
elif error == -3:
print("Error while creating \"large_size_modified" + str(i) + ".txt\" database")
else:
print("Error while creating modified databases (error = %d , sf = %s)" % (error, symbol_file))
def WriteFunctionPair( pairs, output ):
"""
Take a set of pairs ((file_locA, addressA), (file_locB, addressB)) and write them
into a file as:
file_locA:addressA file_locB:addressB
"""
result = open(output,"wt")
count = 0
for entry in pairs:
pair = entry[1]
if (len(pair) == 2 and count < FLAGS.max_func_pairs and pair[0][0] != pair[1][0]):
count +=1
result.write("%s:%s %s:%s %s\n" % (pair[0][0], pair[0][1], pair[1][0],
pair[1][1], entry[0]))
result.close()
return count
# Modification types:
# -------------------
# 0 : change a const
# 1 : add a check
# 2 : remove one line
# 3 : negate if condition
# Small functions
# ---------------
# attack_get_opt_str
# rand_next 3
# add_attack 3
# resolv_domain_to_hostname
# checksum_generic
# Medium Functions
# ---------------
# attack_parse
# ensure_single_instance
# resolv_lookup
# attack_start
# util_atoi
# Large Functions
# --------------
# attack_tcp_stomp
# killer_init
# main
# attack_app_http
# attack_udp_dns
def ModifiedFunctions(size, modified):
if size == 0:
if modified == 3:
return ["YXR0YWNrX2dldF9vcHRfc3Ry", "cmVzb2x2X2RvbWFpbl90b19ob3N0bmFtZQ==", "Y2hlY2tzdW1fZ2VuZXJpYw==" ]
else:
return ["YXR0YWNrX2dldF9vcHRfc3Ry","cmFuZF9uZXh0", "YWRkX2F0dGFjaw==", "cmVzb2x2X2RvbWFpbl90b19ob3N0bmFtZQ==", "Y2hlY2tzdW1fZ2VuZXJpYw==" ]
elif size == 1:
return ["YXR0YWNrX3BhcnNl" , "ZW5zdXJlX3NpbmdsZV9pbnN0YW5jZQ==", "cmVzb2x2X2xvb2t1cA==", "YXR0YWNrX3N0YXJ0" ,"dXRpbF9hdG9p"]
elif size == 2:
return ["YXR0YWNrX3RjcF9zdG9tcA==", "a2lsbGVyX2luaXQ=", "bWFpbg==", "YXR0YWNrX2FwcF9odHRw", "YXR0YWNrX3VkcF9kbnM="]
else :
return []
def main(argv):
del argv # unused.
# Refuse to run on Python less than 3.5 (unpredictable!).
if sys.version_info[0] < 3 or sys.version_info[1] < 5:
print("This script requires Python version 3.5 or higher.")
sys.exit(1)
if FLAGS.clobber:
shutil.rmtree(FLAGS.work_directory)
os.mkdir(FLAGS.work_directory)
if FLAGS.work_directory[-1] != '/':
FLAGS.work_directory = FLAGS.work_directory + '/'
# Build Databases for each test...
print("Grouping extracted symbols for DBs...")
CreatDB()
if __name__ == '__main__':
app.run(main)