#-*-Coding:utf-8-*-import reimport matplotlib.pyplot as Pltimport osfrom sklearn.feature_extraction.text import Countv Ectorizerfrom sklearn Import cross_validationimport osimport numpy as Npfrom sklearn.neural_network import Mlpclassifierdef Load_one_flle (filename): x=[] with open (filename) as F:line=f.readline () Line=line.strip (' \ n ') return Linedef load_adfa_training_files (RootDir): x=[] y=[] list = Os.listdir (RootDir) for I in range (0, Len (list)): Path = OS.PA Th.join (RootDir, list[i]) if Os.path.isfile (path): X.append (Load_one_flle (path)) print ("Load file (%s)"% path) y.append (0) return x,ydef dirlist (path, allfile): filelist = os.listdir (path) for filename in Filelist:filepath = Os.path.join (PA th, filename) if Os.path.isdir (filepath): Dirlist (filepath, Allfile) else:allfile.append (filepath) return allfiledef Load_adfa_java_files (RootDir): x=[] y=[] allfile=dirlist (rootdir,[]) for file in Allfile:if Re.match (r "ADFA-LD/Attack_ Data_master/java_meterpreter_\d+/uad-java-meterpreter* ", file): Print (" Load file (%s) "% file) x.append (Load_one_flle (file)) Y.append (1) return x,yif __name__ = ' __main__ ': x1 , Y1=load_adfa_training_files ("adfa-ld/training_data_master/") x2,y2=load_adfa_java_files ("ADFA-LD/Attack_Data_ master/") x=x1+x2 y=y1+y2 #print x vectorizer = Countvectorizer (min_df=1) x=vectorizer.fit_transform (x) X=x.toarray () MLP = Mlpclassifier (hidden_layer_sizes= (150,50), max_iter=10, alpha=1e-4, solver= ' SGD ', verbose=10, tol=1e-4, random_ State=1, learning_rate_init=.1) Score=cross_validation.cross_val_score (MLP, x, Y, N_jobs=-1, cv=10) print (Np.mean ( Score))
Detecting Java Code Overflow attacks using neural network algorithms