-
Notifications
You must be signed in to change notification settings - Fork 11
/
nn.py
536 lines (466 loc) · 18.6 KB
/
nn.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import pickle
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import sys
import glob
import math
import time
from functools import partial
import keras
import random
import socket
import subprocess
import numpy as np
import tensorflow as tf
import keras.backend as K
from collections import Counter
set_random_seed = tf.compat.v1.set_random_seed
from keras.models import Sequential
from keras.layers import Input, Dense, Dropout, Activation
from keras.callbacks import ModelCheckpoint
from keras.models import Model
from keras.utils import to_categorical
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
import ipdb
import statistics
HOST = '127.0.0.1'
PORT = 12012
MAX_FILE_SIZE = 5000
MAX_BITMAP_SIZE = 1562
round_cnt = 0
# Choose a seed for random initilzation
#seed = int(time.time())
seed = 12
np.random.seed(seed)
random.seed(seed)
set_random_seed(seed)
seed_list = glob.glob('./seeds/*')
new_seeds = glob.glob('./seeds/id_*')
SPLIT_RATIO = len(seed_list)
# get binary argv
argvv = sys.argv[1:].copy()
seed_list.sort()
new_edges = []
beta = []
alpha = []
ec_num = 0
soft_num = 0
ctx_num = 0
from keras.backend.tensorflow_backend import set_session
from keras.backend.tensorflow_backend import clear_session
from keras.backend.tensorflow_backend import get_session
import gc
# process training data from afl raw data
def process_data():
global MAX_BITMAP_SIZE
global MAX_FILE_SIZE
global SPLIT_RATIO
global seed_list
global new_seeds
global new_edges
global ec_num
global ctx_num
global soft_num
# process vari seeds
vari_seeds = glob.glob('./vari_seeds/id_*')
for f in vari_seeds:
if './seeds/'+f.split('/')[-1] in seed_list:
continue
try:
subprocess.call(['./afl-tmin','-i',f,'-o', './seeds/'+f.split('/')[-1], '-k', str(MAX_FILE_SIZE)]+argvv,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except subprocess.CalledProcessError:
print("afl-tmin " + f + " error")
# get training samples
init_list = glob.glob('./seeds/id:*')
init_list.sort()
SPLIT_RATIO = len(seed_list)
rand_index = np.arange(SPLIT_RATIO)
#np.random.shuffle(seed_list)
new_seeds = glob.glob('./seeds/id_*')
new_seeds.sort(key=lambda x: int(x.split('_')[3]))
seed_list = init_list + new_seeds
call = subprocess.check_output
# get MAX_FILE_SIZE
cwd = os.getcwd()
max_file_name = call(['ls', '-S', cwd + '/seeds/']).decode('utf8').split('\n')[0].rstrip('\n')
MAX_FILE_SIZE = os.path.getsize(cwd + '/seeds/' + max_file_name)
# create directories to save label, spliced seeds, variant length seeds, crashes and mutated seeds.
if os.path.isdir("./bitmaps_ec/") == False:
os.makedirs('./bitmaps_ec')
if os.path.isdir("./bitmaps_ctx/") == False:
os.makedirs('./bitmaps_ctx')
if os.path.isdir("./bitmaps_soft/") == False:
os.makedirs('./bitmaps_soft')
if os.path.isdir("./vari_seeds/") == False:
os.makedirs('./vari_seeds')
if os.path.isdir("./crashes/") == False:
os.makedirs('./crashes')
# ec process raw
# obtain raw bitmaps
raw_bitmap = {}
tmp_cnt = []
out = ''
seed = np.zeros((len(seed_list),MAX_FILE_SIZE))
bitmap_list = glob.glob('./bitmaps_ec/*')
argvv[0] = sys.argv[1] + '_ec'
for i,f in enumerate(seed_list):
# read a input into a matrix
tmp = open(f,'rb').read()
ln = len(tmp)
if ln < MAX_FILE_SIZE:
tmp = tmp + (MAX_FILE_SIZE - ln) * b'\x00'
seed[i] = np.array([j for j in list(tmp)]).astype('float32')/255
# obtain bitmap
tmp_list = []
file_name = './bitmaps_ec/'+f.split('/')[-1]+'.npy'
if file_name in bitmap_list:
tmp_list = np.load(file_name)
tmp_cnt = tmp_cnt + tmp_list.tolist()
else:
try:
out = call(['./afl-showmap', '-q', '-e', '-o', '/dev/stdout', '-m', '512', '-t', '500'] + argvv + [f])
except subprocess.CalledProcessError:
print("find a crash " + f)
try:
out = call(['./afl-showmap', '-q', '-e', '-o', '/dev/stdout', '-m', '512', '-t', '5000'] + argvv + [f])
except subprocess.CalledProcessError:
print("crash again, skip")
for line in out.splitlines():
edge = int(line.split(b':')[0])
tmp_cnt.append(edge)
tmp_list.append(edge)
#save afl-showmap results
np.save(file_name, tmp_list)
raw_bitmap[f] = tmp_list
# process bitmaps for each input
counter = Counter(tmp_cnt).most_common()
ec_label = [int(f[0]) for f in counter]
ec_label_dict = dict(zip(ec_label, range(len(ec_label))))
bitmap = np.zeros((len(seed_list), len(ec_label)))
for idx, i in enumerate(seed_list):
tmp = raw_bitmap[i]
for j in tmp:
bitmap[idx][ec_label_dict[j]] = 1
ec_bitmap = bitmap
# ctx process raw
# obtain raw bitmaps
raw_bitmap = {}
tmp_cnt = []
out = ''
bitmap_list = glob.glob('./bitmaps_ctx/*')
argvv[0] = sys.argv[1] + '_ctx'
for i,f in enumerate(seed_list):
# obtain bitmap
tmp_list = []
file_name = './bitmaps_ctx/'+f.split('/')[-1]+'.npy'
if file_name in bitmap_list:
tmp_list = np.load(file_name)
tmp_cnt = tmp_cnt + tmp_list.tolist()
else:
try:
# append "-o tmp_file" to strip's arguments to avoid tampering tested binary.
out = call(['./afl-showmap', '-q', '-e', '-o', '/dev/stdout', '-m', '512', '-t', '500'] + argvv + [f])
except subprocess.CalledProcessError:
print("find a crash " + f)
try:
out = call(['./afl-showmap', '-q', '-e', '-o', '/dev/stdout', '-m', '512', '-t', '5000'] + argvv + [f])
except subprocess.CalledProcessError:
print("crash again, skip")
for line in out.splitlines():
edge = int(line.split(b':')[0])
tmp_cnt.append(edge)
tmp_list.append(edge)
#save afl-showmap results
np.save(file_name, tmp_list)
raw_bitmap[f] = tmp_list
# process bitmaps for each input
counter = Counter(tmp_cnt).most_common()
ctx_label = ec_label + list(set([int(f[0]) for f in counter]) - set(ec_label))
ctx_label_dict = dict(zip(ctx_label, range(len(ctx_label))))
bitmap = np.zeros((len(seed_list), len(ctx_label)))
#bitmap[:,:ec_bitmap.shape[1]] = ec_bitmap
for idx, i in enumerate(seed_list):
tmp = raw_bitmap[i]
for j in tmp:
bitmap[idx][ctx_label_dict[j]] = 1
ctx_bitmap = bitmap
# process soft label
bitmap_list = glob.glob('./bitmaps_soft/*')
argvv[0] = sys.argv[1] + '_soft'
soften_label = {}
for i,f in enumerate(seed_list):
tmp_list = []
file_name = './bitmaps_soft/'+f.split('/')[-1]+'.npy'
if file_name in bitmap_list:
half_label = np.load(file_name)
else:
try:
out = call(['./afl-showmap', '-q', '-e', '-o', '/dev/stdout', '-m', '512', '-t', '500'] + argvv + [f])
except subprocess.CalledProcessError:
print("find a crash " + f)
try:
out = call(['./afl-showmap', '-q', '-e', '-o', '/dev/stdout', '-m', '512', '-t', '5000'] + argvv + [f])
except subprocess.CalledProcessError:
print("crash again, skip")
for line in out.splitlines():
edge = int(line.split(b':')[0])
tmp_list.append(edge)
half_label =[ele for ele in tmp_list if ele not in raw_bitmap[f]]
#save afl-showmap results
np.save(file_name, half_label)
for e in half_label:
if e not in soften_label:
soften_label[e] = [i]
else:
soften_label[e].append(i)
#patch soft label to bitmap
for edge_id,seed_id_list in soften_label.items():
if edge_id not in ctx_label_dict:
continue
iidx = ctx_label_dict[edge_id]
for seed_id in seed_id_list:
# in case there is edge conflic and overwrite 1 with 0.25.
if bitmap[seed_id][iidx] == 0:
bitmap[seed_id][iidx] = 0.25
soft_bitmap = bitmap
# delete all 1 label
all_1_idx = np.where(np.sum(soft_bitmap==1, axis=0) == soft_bitmap.shape[0])[0]
soft_bitmap = np.delete(soft_bitmap, all_1_idx, 1)
# label dimension reduction
fit_bitmap, indices = np.unique(soft_bitmap,axis=1, return_inverse=True)
reconstruct_idx = list(set(indices.tolist()[:ec_bitmap.shape[1]]))
ec_num = len(reconstruct_idx)
reconstruct_idx = reconstruct_idx + list(set(indices.tolist()[ec_bitmap.shape[1]:ctx_bitmap.shape[1]]) - set(reconstruct_idx))
ctx_num = len(reconstruct_idx) - ec_num
soft_num = 0
print(fit_bitmap[:, np.asarray(reconstruct_idx)].shape, ec_num, ctx_num, soft_num)
fit_bitmap = fit_bitmap[:, np.asarray(reconstruct_idx)]
print("#####data dimension############# " + str(fit_bitmap.shape))
MAX_BITMAP_SIZE = fit_bitmap.shape[1]
# select new edges
if round_cnt >= 1:
old_fitmap = np.load("prior_bitmap.npy")
fit_bitmap_partial = fit_bitmap[:old_fitmap.shape[0]]
new_edges=np.where(np.sum(fit_bitmap_partial,axis=0)==0)[0].tolist()
print("####new_edge num################# : "+ str(len(new_edges)))
else:
new_edges = []
np.save("prior_bitmap", fit_bitmap)
# normalize seed
mean_var = np.zeros((2,MAX_FILE_SIZE))
for i in range(seed.shape[1]):
mean_var[0,i] = np.mean(seed[:,i])
mean_var[1,i] = np.std(seed[:,i])
seed[:,i] = (seed[:,i]-mean_var[0,i])/mean_var[1,i] if mean_var[1,i]!=0 else seed[:,i]-mean_var[0,i]
return seed,fit_bitmap
# learning rate decay
def step_decay(epoch):
initial_lrate = 0.001
drop = 0.7
epochs_drop = 10.0
lrate = initial_lrate * math.pow(drop, math.floor((1 + epoch) / epochs_drop))
return lrate
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
self.lr = []
def on_epoch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
self.lr.append(step_decay(len(self.losses)))
print(step_decay(len(self.losses)))
# compute jaccard accuracy for multiple label
def accur_1(y_true, y_pred):
y_true = tf.round(y_true)
pred = tf.round(y_pred)
summ = tf.constant(MAX_BITMAP_SIZE, dtype=tf.float32)
wrong_num = tf.subtract(summ, tf.reduce_sum(tf.cast(tf.equal(y_true, pred), tf.float32), axis=-1))
right_1_num = tf.reduce_sum(tf.cast(tf.logical_and(tf.cast(y_true, tf.bool), tf.cast(pred, tf.bool)), tf.float32), axis=-1)
ret = K.mean(tf.divide(right_1_num, tf.add(right_1_num, wrong_num)))
return ret
def weighted_cross_entropy(y_true, y_pred):
"""
Penalizes miss predictions.
Parameters
----------
beta: float
Notes
-----
WCE = - (beta * p * log(p_hat) + (1 - p) * log(1 - p_hat))
- Setting beta < 1 will penalize false positives more than false negatives
- Setting beta > 1 will penalize false negatives more than false positives
"""
def convert_to_logits(y_pred):
# see https://github.com/tensorflow/tensorflow/blob/r1.10/tensorflow/python/keras/backend.py#L3525
y_pred = tf.clip_by_value(
y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())
return tf.log(y_pred / (1 - y_pred))
y_pred = convert_to_logits(y_pred)
loss = tf.nn.weighted_cross_entropy_with_logits(logits=y_pred, targets=y_true, pos_weight=beta)
loss = alpha * loss
return tf.reduce_mean(loss)
def gen_adv4(f, fl, model, layer_list, idxx, splice, seed):
adv_list = []
#loss = layer_list[-2][1].output[:, f]
loss = layer_list[4][1].output[:,np.random.randint(512)]
#loss = layer_list[3][1].output
grads = K.gradients(loss, model.input)[0]
iterate = K.function([model.input], [loss, grads])
ll = len(fl)
for index in range(ll):
x = seed[fl[index]].reshape((1,seed.shape[1]))
loss_value, grads_value = iterate([x])
idx = np.flip(np.argsort(np.absolute(grads_value), axis=1)[:, -MAX_FILE_SIZE:].reshape((MAX_FILE_SIZE,)), 0)
val = np.sign(grads_value[0][idx])
adv_list.append((idx, val, seed_list[fl[index]]))
return adv_list
# grenerate gradient information to guide furture muatation
def gen_mutate3(model, edge_num, sign, seed, label, weighted):
tmp_list = []
# select seeds
rand_seed1 = []
rand_seed2 = []
# use rare edge selction or not
#if (int(round_cnt/3)%2) == 1:
if (int(round_cnt/2)%2) == 3:
interested_indice = np.random.choice(range(label.shape[1]), edge_num, replace=True).tolist()
rand_seed1 = np.random.choice(range(seed.shape[0]), edge_num, replace=True).tolist()
weighted = False
else:
# select rare edges
if(edge_num > len(new_edges)):
interested_indice = new_edges
if (round_cnt%2) == 1:
rare_label = label[:,ec_num:ctx_num]
rare_edge_list = [ele+ec_num for ele in np.argsort(np.sum(rare_label, axis=0)).tolist()]
rare_label = label[:, :ec_num]
rare_label = np.where(rare_label==0.25, 0, rare_label)
rare_edge_list = rare_edge_list + np.argsort(np.sum(rare_label, axis=0)).tolist()
else:
rare_label = label[:,:ec_num]
rare_label = np.where(rare_label==0.25, 0, rare_label)
rare_edge_list = np.argsort(np.sum(rare_label, axis=0)).tolist()
for rare_edge in rare_edge_list:
if rare_edge in new_edges:
continue
else:
interested_indice.append(rare_edge)
if len(interested_indice) == edge_num:
break
else:
interested_indice = new_edges[:edge_num]
# select inputs
for edge in interested_indice:
one_idx = np.where(label[:,edge]==1)[0]
tmp_rand = np.random.choice(one_idx,1, replace=False)[0]
rand_seed1.append(tmp_rand)
print("### rare edge selection: " + str(weighted))
fn = gen_adv4
layer_list = [(layer.name, layer) for layer in model.layers]
with open('gradient_info_p', 'w') as f:
t0 = time.time()
for idxx in range(len(interested_indice[:])):
# kears's would stall after multiple gradient compuation. Release memory and reload model to fix it.
if (idxx % 100 == 0):
clear_session()
model = build_model(label, True)
model.load_weights('model.h5')
layer_list = [(layer.name, layer) for layer in model.layers]
print("number of feature " + str(idxx) + " " + str(time.time()-t0))
index = int(interested_indice[idxx])
fl = [rand_seed1[idxx]]
adv_list = fn(index, fl, model, layer_list, idxx, 0, seed)
tmp_list.append(adv_list)
for ele in adv_list:
ele0 = [str(el) for el in ele[0]]
ele1 = [str(int(el)) for el in ele[1]]
ele2 = ele[2]
f.write(",".join(ele0) + '|' + ",".join(ele1) + '|' + ele2 + "\n")
def build_model(data, weighted_loss):
global beta
global alpha
batch_size = 32
num_classes = MAX_BITMAP_SIZE
epochs = 50
model = Sequential()
model.add(Dense(2048, input_dim=MAX_FILE_SIZE))
model.add(Activation('relu'))
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(num_classes))
model.add(Activation('sigmoid'))
opt = keras.optimizers.adam(lr=0.0001)
pos_weight = (np.sum(data==0, axis=0)+ np.sum(data==0.25, axis=0)/4) / (np.sum(data==1, axis=0)+0.75*np.sum(data==0.25, axis=0))
# pos_weight = ((data.shape[0] - np.sum(data,axis=0))/np.sum(data, axis=0))
beta = pos_weight
alpha = (1 + 1/beta)/2
# loss = partial(weighted_cross_entropy, beta=pos_weight)
model.summary()
if weighted_loss:
print("build weighted_loss model")
model.compile(loss=weighted_cross_entropy, optimizer=opt, metrics=[accur_1])
else:
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=[accur_1])
return model
def train(model, seed, label):
loss_history = LossHistory()
lrate = keras.callbacks.LearningRateScheduler(step_decay)
save_best = keras.callbacks.ModelCheckpoint("model.h5", monitor='loss', verbose=0, save_best_only=True, save_weights_only=True, mode='min', period=1)
callbacks_list = [loss_history, lrate, save_best]
if seed.shape[0] > 5000:
model.fit(seed,label,
batch_size=int(seed.shape[0]/50),
epochs=300,
verbose=1, callbacks=callbacks_list)
else:
model.fit(seed,label,
steps_per_epoch=50,
epochs=100,
verbose=1, callbacks=callbacks_list)
def gen_grad(data):
global round_cnt
t0 = time.time()
seed, label = process_data()
model = build_model(label,True)
weighted = True
train(model, seed, label)
gen_mutate3(model,750, True, seed, label, weighted)
round_cnt = round_cnt + 1
print(time.time() - t0)
def setup_server():
with open("mut_cnt", 'w') as f:
f.write(str(0))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(5)
conn, addr = sock.accept()
print('@@@@@@@@@@@@@connected by mtfuzz execution moduel' + str(addr))
gen_grad('train')
print("@@@@@@@@@@@@@@gen_data done")
conn.sendall(b"start")
data = conn.recv(1024)
print("@@@@@@@@@@@start gen_data")
gen_grad(data)
print("@@@@@@@@@@@@gen_data done")
conn.sendall(b"close?")
conn.recv(1024)
conn.close()
print("@@@@@@@@@@@@@@@close connection")
while True:
try:
conn, addr = sock.accept()
print('@@@@@@@@@@@@@connected by mtfuzz execution moduel' + str(addr))
conn.sendall(b"start")
data = conn.recv(1024)
print("@@@@@@@@@@@start gen_data")
gen_grad(data)
print("@@@@@@@@@@@@@@gen_data done")
conn.sendall(b"close?")
conn.recv(1024)
finally:
conn.close()
print("@@@@@@@@@@@@@@@close connection")
setup_server()