-
Notifications
You must be signed in to change notification settings - Fork 2
/
c_RandomForest.py
1338 lines (1043 loc) · 56.3 KB
/
c_RandomForest.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.model_selection import train_test_split, cross_val_score
import datetime
import sys
import time
import os
from sklearn.tree import export_graphviz
from subprocess import call
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import graphviz
import pickle
import copy
#******************************************************************************
#* This program will fetch 3 json file.
#* First one is malware_feature.json which is the training data of ML model
#* Second one is behavior file that include a dict that malware family mapped to coresponding malicious behaviors.
#* Third is the family_number file that is a dict that family number mapped to family name
#*
#* This program will conbin abovementioned three json files into a training data of Random Forest ML.
#*
#*
#* *******************************************************************************
#* open family json file that is a dict {family number : malware family name....}
#* print what malware families in this json file
#* return a family dict for later using
def get_family_dict_from(file):
f_file = open(file)
f_dict = json.load(f_file)
f_file.close()
f_keys = f_dict.keys()
for th in range(len(f_keys)):
f_keys[th] = int(f_keys[th])
f_keys.sort()
print('Below are family data--------------------\n\n')
for num in f_keys:
print([num, f_dict[str(num)]])
print('\n')
return f_dict
#* open malware behavior labels json file
#* print all malware behaviors labels
#* and creat a dict {label-number: number} for later using
def get_label_dict(file):
new_dict = { }
la_file = open(file)
la_dict=json.load(la_file)
label_dict = la_dict['all'].keys()
label_list = list(label_dict)
label_list.sort()
print('Below are behavior labels --------------------\n\n')
for num in range(len(label_list)):
new_dict[str(num)] = label_list[num]
print( [num,label_list[num] ] )
print('\n')
return new_dict
#* create a list that specify what need to be removed
def set_up_removed_label_list(num, num2):
num2_len = num2.split(',') #* get a string like '0,1,2,3' and convert to [0,1,2,3]
if len(num2_len) == 1:
num2 = int(num2)
removed_list = [ ]
for i in range(num):
each_la = ''
for j in range(num):
if i == j:
pass
else:
if len(each_la) ==0:
each_la = each_la +str(j)
else:
each_la = each_la +','+str(j)
removed_list.append(each_la)
if num2 == 77:
return removed_list
elif num2 == 99:
return ['99']
else:
removed_num = num2
new_removed_list = [ removed_list[removed_num] ]
return new_removed_list
else:
for th in range(len(num2_len)):
num2_len[th] = int(num2_len[th])
removed_list=''
for i in range(num):
if i not in num2_len:
if len(removed_list)==0:
removed_list = removed_list + str(i)
else:
removed_list = removed_list +','+str(i)
return [ removed_list ]
#* create part of file name from removed behavior labels ex. if removed label list is [0,1,2,3], the part of file name will be label-0-1-2-3
def create_file_name_from_removed_bahavior(string, dic):
if string =='99':
return 'all-label'
label_len = len(dic)
default_string = ''
for i in range(label_len):
if len(default_string)==0:
default_string = default_string+str(i)
else:
default_string = default_string +','+str(i)
default_string_content = default_string.split(',')
string_content = (string).split(',')
#print(string_content)
for letter in string_content:
if letter in default_string_content:
default_string_content.remove(letter)
labels_name ='label'
for str_num in default_string_content:
if len(labels_name)==0:
labels_name = labels_name+str_num
else:
labels_name = labels_name +'-'+str_num
return labels_name
def file_to_each_list(family_file):
file = open(family_file)
data = json.load(file)
th_smaple = data.keys()
for i in range(len(th_smaple)):
th_smaple[i] = int(th_smaple[i])
th_smaple = sorted(th_smaple)
num = int(raw_input('How many family you want to fetch from size %d family files for each running\n'%(len(th_smaple))))
sample = []
for i in range(len(th_smaple)/num):
inside_data = ''
for j in range(num):
if len(inside_data)==0:
inside_data = inside_data + '%s'%(th_smaple.pop(0))
else:
inside_data = inside_data + ',%s'%(th_smaple.pop(0))
sample.append(inside_data)
return sample
#* create part of file name from removed malware families ex. if removed family list is [0,1,2,3], the part of file name will be family-0-1-2-3
def create_file_name_from_removed_families(string_content): #*'1,2,3'
if string_content=='99':
return 'No-removed-family'
num = string_content.split(',')
all_name = 'family'
for letter in num:
if len(all_name)==0:
all_name = all_name +letter
else:
all_name = all_name +'-%s'%(letter)
return all_name
#* creat all log file and record some basic information like file name, saving location an where are json files from.
def basic_question (time_stamp):
log, folder_loc = creat_log(save_log_location_g, file_name_g, time_stamp)
print('*****************File name = %s \n'%(file_name_g))
print('*****************Log is saved in %s \n'%(save_log_location_g))
print('*****************Get feature file from %s \n'%(feature_file_g))
print('*****************Get behavior file from %s \n'%(behavors_file_g))
print('*****************Get family file from %s \n'%(family_file_g))
write_log(log, "\nLog is saved in %s \n"%(save_log_location_g))
write_log(log, "\nGet feature file from %s \n"%(feature_file_g))
write_log(log, "\nGet behavior file from %s\n"%(behavors_file_g))
write_log(log, "\nGet family file from %s\n"%(family_file_g))
write_log(log, "\nThis family file include below families >>\n")
for num in range(len(f_dict)):
write_log(log,"%d, %s\n" %(num, f_dict[str(num)]))
return log, folder_loc
def creat_log(location, file_name, time_stamp):
if not location.endswith('/'):
location=location+'/'
folder_loc = location+file_name+'_'+time_stamp+'_result'
if not os.path.exists(folder_loc):
os.mkdir(folder_loc)
log = open (folder_loc+'/'+file_name+'_'+time_stamp+'.log', 'w')
log.write("\n")
log.write("result of %s\n"%(file_name))
folder_loc = folder_loc + '/'
return log, folder_loc
#* logged malware family names that would be removed from training data
#* if the number is 99, then, no malware family have been removed
#* And return a list [ ] that inlcude what family would be chosen in order to later using
def question_removed_family(log, csv_data):
removed_family_local = removed_family_g.split(',') #* get a string '9,1,10' and changed to list with string number ['9', '1', '10']
flag = 0
for number in removed_family_local:
removed_family_local[flag] = str(number)
flag = flag +1
removed_family_local.sort()
if removed_family_local[-1] != '99':
removed = []
for num in removed_family_local:
removed.append(f_dict[num])
print('Below families has been removed_family >>\n %s \n'%(removed))
write_log(log,'\nBelow families has been removed_family >>\n %s \n'%(removed_family_local))
write_log(log,'\nBelow families has been removed_family >>\n %s \n'%(removed))
#!----------------------------------------------------
for j in f_dict:
up_data_3 = { f_dict[j] : 'v' } #! 'v' can changed to j (int)
csv_data['Family_data']['Training' ].update(up_data_3)
for k in removed:
up_data_3_sub = { k : csv_data['Family_data']['Training'].pop(k) }
csv_data['Family_data']['Removed' ].update(up_data_3_sub)
#!----------------------------------------------------
return removed_family_local, csv_data
else:
print('This ML model will trained by all family\n')
write_log(log,'\n This ML model will trained by all family \n')
#!----------------------------------------------------
for j in f_dict:
up_data_3 = { f_dict[j] : 'v' } #! 'v' can changed to j (int)
csv_data['Family_data']['Training' ].update(up_data_3)
up_data_3_sub = { }
csv_data['Family_data']['Removed' ].update(up_data_3_sub)
#!----------------------------------------------------
return[ ] , csv_data
#* logged malware family names that would be put into testing data
#* if the number is 99, then, all malware families would be in testing data
#* And return a list [ ] that inlcude what family would be chosen in order to later using
def question_tested_family(log, csv_data):
tested_family_local = tested_family_g.split(',') #* get a string '9,1,10' and chagnge to list with string number ['9', '1', '10']
flag = 0
for number in tested_family_local:
tested_family_local[flag] = str(number)
flag = flag +1
tested_family_local.sort()
if tested_family_local[-1] != '99':
tested = []
for num in tested_family_local:
tested.append(f_dict[num])
print('Below families would be put in test data >>\n %s \n'%(tested))
write_log(log,'\nBelow families would be put in test data >>\n %s \n'%(tested_family_local))
write_log(log,'\nBelow families would be put in test data >>\n %s \n'%(tested))
#!----------------------------------------------------
for k in tested:
up_data_3_sub = { k : 'v' }
csv_data['Family_data']['Testing' ].update(up_data_3_sub)
#!----------------------------------------------------
return tested_family_local, csv_data
else:
print('No family was selected to test\nThus, system will do self_testing')
write_log(log,'No family was selected to test\nThus, system will do self_testing')
#!----------------------------------------------------
for j in f_dict:
up_data_3_sub = { f_dict[j] : 'v' } #! 'v' can changed to j (int)
csv_data['Family_data']['Testing' ].update(up_data_3_sub)
#!----------------------------------------------------
return[ ], csv_data
#* decide whether there is malware family label in y_training and y_target
#* Normally, flage =1 is the default setting that there is no family label in y because we want to test zero day family.
def question_include_family(log):
if include_family_g != 1:
write_log(log, "\nThis target without family labels\n")
print('This target without family labels\n')
else :
write_log(log, "\nThis target with family labels\n")
print('This target with family labels\n')
def show_dict_col(file):
dic = open(file)
data = json.load(dic)
label_dict = data['all'].keys()
label_list = list(label_dict)
label_list.sort()
return label_list
#* logged malware behavior labels that would be removed from training data (y_training or y_testing)
#* if the number is 99, then, no malware labels have been removed
#* And return a list [ ] that inlcude what label would be chosen in order to later using
def question_remove_label(log, csv_data):
all_be = show_dict_col(behavors_file_g) #* get a dict that includes all malicious label
remove = [ ]
removed_label = removed_label_g.split(',') #* get a string '9,1,10' and chagnge to list with string number ['9', '1', '10']
flag = 0
for number in removed_label:
removed_label[flag] = int(number)
flag = flag +1
removed_label.sort()
if removed_label[-1] != 99:
for num in removed_label:
remove.append(all_be[num])
print('removed_belable =>>\n%s\n'%(removed_label))
print('remove =>>\n%s\n'%(remove))
write_log(log, "\nThis model without below labels >> \n %s \n"%(removed_label))
write_log(log, "\nThis model without below labels >> \n %s \n"%(remove))
#!----------------------------------------------------
up_data_1= { 'Labels_data':{ } }
csv_data.update(up_data_1)
up_data_2 = {'Used' : {},'Removed' : {} }
for j in range(len(all_be)):
up_data_3 = { all_be[j] : 'v' } #! 'v' can changed to j (int)
up_data_2['Used' ].update(up_data_3)
for k in remove:
up_data_3_sub = { k : up_data_2['Used'].pop(k) }
up_data_2['Removed' ].update(up_data_3_sub)
csv_data['Labels_data'].update( up_data_2 )
#!----------------------------------------------------
return remove, csv_data
else:
write_log(log, "\nThis model with all behavior labels\n")
print('This model with all behavior labels\n')
#!----------------------------------------------------
up_data_1= { 'Labels_data':{ } }
csv_data.update(up_data_1)
up_data_2 = {'Used' : {},'Removed' : {} }
for j in range(len(all_be)):
up_data_3 = { all_be[j] : 'v' } #! 'v' can changed to j (int)
up_data_2['Used' ].update(up_data_3)
up_data_3_sub = { }
up_data_2['Removed' ].update(up_data_3_sub)
csv_data['Labels_data'].update( up_data_2 )
#!----------------------------------------------------
return[ ], csv_data
#* This is the setting for times, testsize and n_samplese
def parameter_setting(log):
times = 10 #!int(raw_input("How many times want to repeat\n (input a number ex:10)\n") or 10)
write_log(log, "\nThis modle will run %d times\n"%(times))
testsize = 0.3 #!float(raw_input("Please input a rate to split ML data\n(ex : 0.3)\n") or 0.3)
write_log(log,'\nSpliing X data into training and testin by szie %.3f\n'%(testsize))
n_samples_g = 50 #!int(raw_input("Please input a number which is how many sample you want\n(ex : 10)\n") or 10)
write_log(log,'\nGet %d samples from X_test\n'%(n_samples_g))
return times, testsize, n_samples_g
#* Read behavior labels json file which is used to be y_target
def read_behavior_label_n_list_from_json():
print("reading behavior labels.....%f" %(time.time()-start_time))
behavors_json = open (behavors_file_g)
behavors_data = json.load(behavors_json)
family_json = open (family_file_g)
family_data = json.load(family_json)
new_behav = {}
for item in behavors_data:
if item != 'all':
for ke in family_data.keys():
if item == family_data[ke]:
new_behav[ke] = behavors_data[item]
print("reading behavior labels.....Done %f" %(time.time()-start_time))
return new_behav
#* fetch application names and its coresponding family labels from malware feature to form a y_target data
#* Then, concatenate the behavior labels with y_target data and remove selected behavior labels .
def assign_Y_data(log, data, behavior, removed_belable):
print("Assigning Y data.... %f" %(time.time()-start_time))
y = data['Malware family']
y = y.to_dict()
y = merge_family_behavior_dict_fomat(y, behavior)
y = pd.DataFrame.from_dict(y, orient='index')
y = y.fillna(0)
y = y.astype('int')
y = y [sorted(y.columns)]
y = give_first_position_to('Malware family', y)
if len(removed_belable) !=0:
for label in removed_belable:
if label in y.columns:
y=y.drop(label, axis = 1)
write_log(log,'\nThis below Y has been remove some labels\n')
write_log(log,'%s\n'%(y))
print("Assigning Y data.... Done %f" %(time.time()-start_time))
return y
else:
print('Don\'t need to remove any behavior labels\n')
write_log(log,'This below Y has all labels\n')
write_log(log,'%s\n'%(y))
print("Assigning Y data.... Done %f" %(time.time()-start_time))
return y
def merge_family_behavior_dict_fomat(y, behavior):
key_list = y.keys()
for key in key_list:
number = y[key]
y[key]={'Malware family':number}
y[key].update(behavior[ str(number) ])
return y
#* fetch application names and its coresponding malware features from malware feature to form a X training data
def assign_X_data(log, data):
print("Assigning X data.... %f" %(time.time()-start_time))
X = data
X = X.fillna(0)
X = X.astype('int')
X = X [sorted(X.columns)]
X = give_first_position_to('Malware family', X)
#print(X)
write_log(log,"\nThere are %d features in whole X\n"%len(X.columns))
write_log(log,"\nThere are %d samples in whole X\n"%len(X.index))
write_log(log,"\n%s\n\n"%X)
write_log(log,"\nTop 4 col values >> \n\n%s\n\n"%X.iloc[:,0:4])
print("Assigning X data.... Done %f" %(time.time()-start_time))
return X
def give_first_position_to(name, df):
col_list = df.columns.tolist()
new =[ ]
for col in col_list:
if name == col:
new.append( col )
col_list.remove(col)
new = new+col_list
return df[new]
#* remove selected malware families from X, y data and split them in order to form X_training and y_training
#* use selected malware malware families from X, y data to form X_testing and y_testing
def Spliting_data(log, X, y, removed_family_list, tested_family_list, testsize):
print("Spliting data .... %f" %(time.time()-start_time))
if len(removed_family_list) != 0: #! ==0 >>> no values
X_train = remove_columns_from(X, removed_family_list, 'Malware family') #! for unknow family
y_train = remove_columns_from(y, removed_family_list, 'Malware family') #! for unknow family
else:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = testsize, random_state = 1)
if len(tested_family_list) != 0: #! ==0 >>> no values
X_test = selected_columns_from(X, tested_family_list, 'Malware family')
y_test = selected_columns_from(y, tested_family_list, 'Malware family')
print('after sorted ')
X_test = X_test.sort_values(by=['Malware family'])
y_test = y_test.sort_values(by=['Malware family'])
family_type = {}
for item in y_test['Malware family'].values:
if item not in family_type:
family_type[item] = 1
else:
family_type[item] = family_type[item] +1
X_train = X_train.drop(['Malware family'], axis=1)
X_test = X_test.drop(['Malware family'], axis=1)
if include_family_g == 0:
y_train = y_train.drop(['Malware family'], axis=1)
y_test = y_test.drop(['Malware family'], axis=1)
write_log(log,"\n--------------------------------below are X train------------------------------------\n\n%s\n\n%s\n\n"%(X_train, X_train.iloc[:,0:4] ) )
#print('--------------------------------below are X train------------------------------------')
#print(X_train)
write_log(log,"\n--------------------------------below are y train------------------------------------\n\n%s\n\n"%(y_train) )
#print('--------------------------------below are y train------------------------------------')
#print(y_train)
write_log(log,"\n--------------------------------below are X test------------------------------------\n\n%s\n\n"%X_test )
#print('--------------------------------below are X test------------------------------------')
#print(X_test)
write_log(log,"\n--------------------------------below are y test------------------------------------\n\n%s\n\n"%y_test )
#print('--------------------------------below are y test------------------------------------')
#print(y_test)
print("Spliting data .... Done %f" %(time.time()-start_time))
return X_train, y_train, X_test, y_test, family_type
def remove_columns_from(df, li, name):
for family_number in li:
selected_data = list( df[ ( df[name] == int(family_number) ).values ].index )
df = df.drop(selected_data, axis=0)
return df
def selected_columns_from(df, li, name):
flag = (df[name] == int(li[0]) ).values
for family_number in li[1:]:
flag = flag + (df[name] == int(family_number)).values
return df[flag]
def show_y_target_lables(y_test):
label = []
for ind in y_test.columns:
label.append(ind)
return label
#* this part is used to predict answer via trained random froest
def data_outcoming(log,malware_tree, X_test, y_test, n_samples) :
print("Data outcoming... %f" %(time.time()-start_time))
test = X_test.sample(n_samples)
write_log(log,"\nSample of X_test >>\n%s\n\n%s\n\n"%(test, test.iloc[:,0:4]))
write_log(log,"\nSample of y_test >>\n%s\n" %(y_test.loc[test.index,:]))
pre = malware_tree.predict(test)
pre = pre.astype('int')
if pre.shape != (y_test.loc[test.index,:].values).shape:
pre = pre[:,np.newaxis]
print("\n------below are predicted answer----------------\n")
write_log(log,"\n------below are predicted answer----------------\n" )
write_log(log,"%s" %(pre))
write_log(log,"\n------below are answer----------------\n" )
write_log(log,"%s" %(y_test.loc[test.index,:].values))
print("Data outcoming... Done %f" %(time.time()-start_time))
return test, pre
#* by compare with y_test and predcited answer to calculate the precision, recall, f1 and accuracy for each run and overall model
def calculate_accuracy (log, y_test, pre, test, family_type, csv_data ):
print("Calculating_accuracy... %f" %(time.time()-start_time))
labels = show_y_target_lables(y_test)
presice_all = 0.0
presice_family = 0.0
presice_sample = 0.0
TP = 0.0
FP = 0.0
FN = 0.0
hamming_loss =[]
precision_rate= []
recall_rate=[]
beha_accuracy =[]
num_of_beha = len(y_test.loc[test.index,:].values[0])
num_of_apps = len(y_test.loc[test.index,:].values)
for i in range(num_of_beha):
beha_accuracy.append(0.0)
hamming_loss.append(0.0)
precision_rate.append([0.0, 0.0, 0.0])
recall_rate.append([0.0, 0.0, 0.0])
all_ans = [ [[0.0,0.0,0.0,0.0 ] for beha_num in range(num_of_beha) ] for family_num in range(len(family_type))]
all_ans_summay = [ [[0.0, 0.0,0.0 ] for beha_num in range(num_of_beha) ] for family_num in range(len(family_type))]
num_step_chage = []
for f in sorted(family_type.keys()):
num_step_chage.append( family_type[f] )
for th_num in range(len(num_step_chage)):
if th_num != 0:
num_step_chage[th_num] = num_step_chage[th_num] + num_step_chage[th_num-1]
th_step = 0
write_log(log, "\n----below are wrong answer-------------\n")
for row in range(num_of_apps):
flag = 0
for col in range(num_of_beha):
if pre[row][col] == y_test.loc[test.index,:].values[row][col]:
presice_all = presice_all +1.0
beha_accuracy[col] = beha_accuracy[col] + 1.0
if pre[row][col] =='1' or pre[row][col] ==1: #! Ture Positive
TP = TP +1.0
precision_rate[col][0] = precision_rate[col][0] + 1.0
recall_rate[col][0] = recall_rate[col][0] + 1.0
if row < num_step_chage[th_step]:
all_ans[th_step][col][0] = all_ans[th_step][col][0] +1.0
elif row == num_step_chage[th_step]:
th_step = th_step +1
if th_step < len(num_step_chage):
all_ans[th_step][col][0] = all_ans[th_step][col][0] +1.0
else: #! True negative
if row < num_step_chage[th_step]:
all_ans[th_step][col][1] = all_ans[th_step][col][1] +1.0
elif row == num_step_chage[th_step]:
th_step = th_step +1
if th_step < len(num_step_chage):
all_ans[th_step][col][1] = all_ans[th_step][col][1] +1.0
else:
if pre[row][col]=='1' or pre[row][col]==1: #! False Positive
precision_rate [col][1] = precision_rate [col][1] + 1.0
FP = FP + 1.0
if row < num_step_chage[th_step]:
all_ans[th_step][col][2] = all_ans[th_step][col][2] +1.0
elif row == num_step_chage[th_step]:
th_step = th_step +1
if th_step < len(num_step_chage):
all_ans[th_step][col][2] = all_ans[th_step][col][2] +1.0
else:
recall_rate[col][1] = recall_rate[col][1] + 1.0 #! False Negative
FN = FN + 1.0
if row < num_step_chage[th_step]:
all_ans[th_step][col][3] = all_ans[th_step][col][3] +1.0
elif row == num_step_chage[th_step]:
th_step = th_step +1
if th_step < len(num_step_chage):
all_ans[th_step][col][3] = all_ans[th_step][col][3] +1.0
hamming_loss[col]=hamming_loss[col]+1.0
if flag == 0: #! know how many sample is wrong
presice_sample = presice_sample+1.0
flag = flag+1.0
write_log(log, " ( %d , %d ) \n"%(row, col))
if y_test.loc[test.index,:].columns[col] == 'Malware family':
write_log(log, " ( %s , %s ) "%( y_test.loc[test.index,:].index[row], y_test.loc[test.index,:].columns[col]) )
right_family = f_dict[ str(y_test.loc[test.index,:].values[row][col]) ]
wrong_family = f_dict[ str(pre[row][col]) ]
write_log(log, " ( Right one is %s , Wrong one is %s ) "%(right_family, wrong_family ) )
write_log(log, " ( %s , %s ) \n"%( y_test.loc[test.index,:].values[row][col], pre[row][col]) )
else:
write_log(log, " ( %s , %s ) "%( y_test.loc[test.index,:].index[row], y_test.loc[test.index,:].columns[col]) )
write_log(log, " ( Right one is %s , Wrong one is %s ) \n"%( y_test.loc[test.index,:].values[row][col], pre[row][col]) )
if pre[row][0] == y_test.loc[test.index,:].values[row][0]:
presice_family = presice_family+1.0
presice_all_base = num_of_apps * num_of_beha
presice_family_base = num_of_apps
presice_sample_base = num_of_apps
presice_all_answer = (presice_all / presice_all_base)
presice_family_answer = (presice_family / presice_family_base)
presice_sample_answer = ((presice_sample_base-presice_sample) / presice_sample_base)
if (TP+FP)>0:
overall_precision = TP / (TP+FP)
else:
overall_precision=0
if (TP+FN) > 0:
overall_recall = TP / (TP+FN)
else:
overall_recall = 0
if (overall_precision+overall_recall) > 0:
overall_f1 = 2*overall_precision*overall_recall /(overall_precision + overall_recall)
else:
overall_f1= 0
overall_hamming_loss = (FN+FP)/(num_of_beha*num_of_apps)
#!----------------------------------------------------
up_data_3_n_TP = { 'overall_TP' : TP }
csv_data['All_parameter']['For_all'].update(up_data_3_n_TP)
up_data_3_n_FP = { 'overall_FP' : FP }
csv_data['All_parameter']['For_all'].update(up_data_3_n_FP)
up_data_3_n_FN = { 'overall_FN' : FN }
csv_data['All_parameter']['For_all'].update(up_data_3_n_FN)
#!---------------------------------------------------------------
print("all label accuracy : %.3f "%presice_all_answer)
write_log(log, "\n Totoal labels are %.3f and corrected labels are %.3f\n"%(presice_all_base, presice_all))
write_log(log, " All labels accuracy : %.3f \n" %(presice_all_answer))
print("family accuracy : %.3f "%presice_family_answer)
write_log(log, " \nTotoal family labels are %.3f and corrected labels are %.3f\n"%(presice_family_base, presice_family))
write_log(log, " All labels accuracy : %.3f \n" %(presice_family_answer))
print("Sample accuracy : %.3f "%presice_sample_answer)
write_log(log, " \nTotoal sample are %.3f and wrong are %.3f\n"%(presice_sample_base, presice_sample))
write_log(log, " All Sample accuracy : %.3f \n" %(presice_sample_answer))
print("overll_precision : %.3f "%overall_precision)
write_log(log, " \nTotoal TP are %d and FP are %d\n"%(TP, FP))
write_log(log, " overall_precision : %.3f \n" %(overall_precision))
print("overll_recall : %.3f "%overall_recall)
write_log(log, " \nTotoal TP are %d and FN are %d\n"%(TP, FN))
write_log(log, " overall_recall : %.3f \n" %(overall_recall))
print("over_f1 : %.3f "%overall_f1)
write_log(log, " overall_f1 : %.3f \n" %(overall_f1))
print("over_hamming : %.3f "%overall_hamming_loss)
write_log(log, " overall_hamming : %.3f \n" %(overall_hamming_loss))
each_accuracy = list_divided(beha_accuracy, num_of_apps)
hamming_loss = list_divided(hamming_loss, num_of_apps)
print("Each accuracy : %s "%each_accuracy)
write_log(log, " \nEach accuracy : %s \n"%each_accuracy)
print("Each hamming_loss : %s "%hamming_loss)
write_log(log, " \nEach hamming_loss : %s \n"%hamming_loss)
for th in range(len(precision_rate)):
if (precision_rate[th][0]+precision_rate[th][1]) !=0:
precision_rate[th][2] = precision_rate[th][0] / (precision_rate[th][0]+precision_rate[th][1])
else:
precision_rate[th][2] = 0
if (recall_rate[th][0] +recall_rate[th][1] ) != 0:
recall_rate[th][2] = recall_rate[th][0] / (recall_rate[th][0] +recall_rate[th][1] )
else:
recall_rate[th][2] = 0
f1_list =[]
for th in range(len(precision_rate)):
precision_rate[th] = precision_rate[th][2]
recall_rate[th] = recall_rate[th][2]
if ( precision_rate[th] + recall_rate[th] ) ==0:
f1_list.append(0)
else:
f1_list .append( recall_rate[th] * precision_rate[th] *2 /(recall_rate[th]+ precision_rate[th]) )
print("Each presicion : %s "%(precision_rate))
write_log(log, " \nEach presicion : %s \n"%precision_rate)
print("Each recall : %s "%(recall_rate))
write_log(log, " \nEach recall : %s \n"%recall_rate)
print("Each F1 : %s"%(f1_list))
write_log(log, " \nEach F1 : %s\n"%f1_list)
new_all_ans = process_all_ans(all_ans, all_ans_summay, family_type, labels)
print("Calculating_accuracy... Done %f" %(time.time()-start_time))
return [presice_all_answer, presice_family_answer, presice_sample_answer, each_accuracy, hamming_loss, precision_rate, recall_rate, f1_list, new_all_ans, overall_precision, overall_recall, overall_f1, overall_hamming_loss]
def get_data_from_list (th_new_all_ans_list, num):
new_return_list = []
for be_list in th_new_all_ans_list:
new_return_list.append(be_list[num])
return new_return_list
def process_all_ans(all_ans, all_ans_summay ,family_type, labels):
for f in range(len(all_ans)):
for be in range(len(all_ans[f])):
if all_ans[f][be][0] !=0:
P = all_ans[f][be][0] / ( all_ans[f][be][0]+ all_ans[f][be][2] )
R = all_ans[f][be][0] / (all_ans[f][be][0]+ all_ans[f][be][3])
F1 = 2*P*R/(P+R)
all_ans_summay[f][be][0] = P
all_ans_summay[f][be][1] = R
all_ans_summay[f][be][2] = F1
else:
P = 0
R = 0
F1 = 0
all_ans_summay[f][be][0] = P
all_ans_summay[f][be][1] = R
all_ans_summay[f][be][2] = F1
return all_ans_summay
def list_divided(li, num):
for th in range(len(li)):
li[th] = li[th]/num
return li
def sum_two_list(l1, l2):
if len(l1)==0:
for th in range(len(l2)):
l1.append(l2[th])
else:
if len(l2) ==len(l1):
for th in range(len(l2)):
l1[th] = l1[th] + l2[th]
return l1
def write_log(log, message):
log.write(message)
def close_log(log):
log.close()
#* get top n feature importance from random forest model and save into a sorted list
#* the list will like [[importance01, coresponding feature01], [importance02, coresponding feature02], [importance03, coresponding feature03]...]
# * [ [0.1, 12th feature], [0.08, 99th features], [0.05 32th features].....]
def top_30_feature_importances(imp_f, n):
ma_list = list(imp_f)
rank = [ [0,0 ] for i in range(len(ma_list))]
for th in range(len(ma_list)):
rank[th][0] = ma_list[th]
rank[th][1] = th
rank.sort(reverse=True)
return rank[0:n]
#* get top n feature importance list and convert "th features" to features string
# * Then, save into the dict
def top_30_feature_importances_withfeature(rank, X_train, csv_data):
new_rank = copy.deepcopy(rank)
for th in new_rank:
th[1] = X_train.columns[th[1]]
up_data_3_fea_imp = { th[1] :th[0] }
csv_data['Feature_importance'][ 'Feature_importance'].update(up_data_3_fea_imp)
return new_rank, csv_data
def analyisi_setting_from_name(file_path):
file_name = file_path.split('/')[-1]
file_setting = file_name.split('_')
re_f = file_setting[0]
la_in_y = file_setting[1]
min_sam = file_setting[2]
max_f = file_setting[3]
est_num = file_setting[4]
max_de = file_setting[5]
min_sam_split = file_setting[6]
return (re_f, la_in_y, min_sam, max_f, est_num, max_de, min_sam_split)
#* if there is a ML model with same setting, this program will reuse it
#* the settings include removed_family, removed_behavior_label, estimators, min_sample_leaf, max_depth, min_sample_split and max_features
def if_there_is_same_tree_can_be_used(log, path):
path = path.replace(" ","")
if not path.endswith('/'):
path= path +'/'
sav_list=[]
for dirpath,dirname,filename in os.walk(path):
for f in filename:
if f.endswith(".sav"):
file_path = dirpath+'/'+f
sav_list.append(file_path)
tree_vs_setting ={}
for tree in sav_list:
tree_vs_setting[analyisi_setting_from_name(tree)] = tree
current_setting = analyisi_setting_from_name(file_name_g)
if current_setting in tree_vs_setting:
print('-------------------This model will using old tree--------------------------')
write_log(log, '-------------------This model will using old tree-------------\n\nfrom >>> %s\n\n'%(tree_vs_setting[current_setting]))
return tree_vs_setting[current_setting]
else:
print('-------------------This model will using neEEEEEEEEEEEEEEEEEEEEEEEEEw tree--------------------------')
write_log(log, '-------------------This model will using neEEEEEEEEEEEEEEEEEEEEEEEEEw tree-------------\n\n' )
return False
def run(data):
time_stamp = datetime.datetime.now().strftime('%m_%d_%H_%M_%S')
csv_data = { 'All_parameter':{ 'For_all' : {} }, 'Each_accuracy':{ }, 'Each_hamming':{ }, 'Each_precision':{ }, 'Each_recall':{ }, 'Each_f1':{ } , 'Sum_accuracy':{ } , 'Feature_importance':{ 'Feature_importance':{}} , 'Family_data':{ 'Training' : {},'Testing' : {}, 'Removed':{} } }
#* this csv_file record all relevent information
log, folder_loc = basic_question (time_stamp)
removed_family_list, csv_data = question_removed_family(log, csv_data) #! get a list like ['1', '5', '10' ]
tested_family_list, csv_data = question_tested_family(log, csv_data)
question_include_family(log)
removed_belable, csv_data = question_remove_label(log, csv_data)
times, testsize, n_samples_g = parameter_setting(log)
behavior = read_behavior_label_n_list_from_json() #* return a behavior dict
y = assign_Y_data(log, data, behavior, removed_belable)
X = assign_X_data(log,data)
X_train, y_train, X_test, y_test, family_type = Spliting_data(log, X, y, removed_family_list, tested_family_list, testsize)
labels = show_y_target_lables(y_test)
print("Learning.... %f" %(time.time()-start_time))
if tree_mode_g == '2':
if if_there_is_same_tree_can_be_used(log, save_log_location_g) : #* if there is a ML model with same setting, this program will reuse it
mode_file_path = if_there_is_same_tree_can_be_used(log, save_log_location_g)
malware_tree = pickle.load(open(mode_file_path, 'rb'))
up_data_3_tree = { 'Tree' : 'Old' }
csv_data['All_parameter']['For_all'].update(up_data_3_tree)
up_data_3_tree_type = { 'Tree type' : 'Random' }
csv_data['All_parameter']['For_all'].update(up_data_3_tree_type)
else:
if len(y_test.columns)<2:
malware_tree = RandomForestClassifier(n_estimators= esti_g, max_depth= max_de_g, n_jobs=-1, min_samples_split=min_sam_split_g, max_features= max_fea_g ,min_samples_leaf= min_sam_leaf_g, random_state=1, oob_score=True, warm_start=False).fit(X_train, y_train.values.ravel())
else:
malware_tree = RandomForestClassifier(n_estimators= esti_g, max_depth= max_de_g, n_jobs=-1, min_samples_split=min_sam_split_g, max_features= max_fea_g ,min_samples_leaf= min_sam_leaf_g, random_state=1, oob_score=True, warm_start=False).fit(X_train, y_train)
mode = malware_tree.estimators_[10]
dot_data = tree.export_graphviz(mode, out_file=None, feature_names=X_train.columns, rounded= True )
graph = graphviz.Source(dot_data) #* export the graph of one tree from random forest. this just for references
if not folder_loc.endswith('/'):
pic_name = folder_loc + '/'+file_name_g
else:
pic_name = folder_loc +file_name_g
graph.render(pic_name)
tree_file_name =folder_loc+file_name_g+'_tree.sav'
pickle.dump(malware_tree, open(tree_file_name, 'wb'))
up_data_3_tree = { 'Tree' : 'New' }
csv_data['All_parameter']['For_all'].update(up_data_3_tree)
up_data_3_tree_type = { 'Tree type' : 'Random' }
csv_data['All_parameter']['For_all'].update(up_data_3_tree_type)
else: #* here is the model for extra tree. If wanna to use this, just revise " tree_mode_g" not equal to 2.
if if_there_is_same_tree_can_be_used(log, save_log_location_g) :
mode_file_path = if_there_is_same_tree_can_be_used(log, save_log_location_g)
malware_tree = pickle.load(open(mode_file_path, 'rb'))
up_data_3_tree = { 'Tree' : 'Old' }
csv_data['All_parameter']['For_all'].update(up_data_3_tree)
up_data_3_tree_type = { 'Tree type' : 'Extra' }
csv_data['All_parameter']['For_all'].update(up_data_3_tree_type)
else:
if len(y_test.columns)<2:
malware_tree = ExtraTreesClassifier(n_estimators= esti_g, max_depth= max_de_g, n_jobs=-1, min_samples_split=min_sam_split_g, max_features= max_fea_g ,min_samples_leaf= min_sam_leaf_g, random_state=1, warm_start=False).fit(X_train, y_train.values.ravel())
else:
malware_tree = ExtraTreesClassifier(n_estimators= esti_g, max_depth= max_de_g, n_jobs=-1, min_samples_split=min_sam_split_g, max_features= max_fea_g ,min_samples_leaf= min_sam_leaf_g, random_state=1, warm_start=False).fit(X_train, y_train)
mode = malware_tree.estimators_[10]
dot_data = tree.export_graphviz(mode, out_file=None, feature_names=X_train.columns, rounded= True )
graph = graphviz.Source(dot_data)
if not folder_loc.endswith('/'):
pic_name = folder_loc + '/'+file_name_g
else:
pic_name = folder_loc +file_name_g
graph.render(pic_name)
tree_file_name =folder_loc+file_name_g+'_tree.sav'
pickle.dump(malware_tree, open(tree_file_name, 'wb'))
up_data_3_tree = { 'Tree' : 'New' }
csv_data['All_parameter']['For_all'].update(up_data_3_tree)
up_data_3_tree_type = { 'Tree type' : 'Extra' }
csv_data['All_parameter']['For_all'].update(up_data_3_tree_type)