1. 程式人生 > >學習筆記(七):樸素貝葉斯在Web安全中的六個應用

學習筆記(七):樸素貝葉斯在Web安全中的六個應用

一、檢測Web異常操作

       1.資料蒐集:一樣

       2.特徵化

            使用詞集模型,統計全部操作命令,去重後形成字典或詞彙表:

with open(filename) as f:
    for line in f:
        line=line.strip('\n')
        dist.append(line)
fdist = FreqDist(dist).keys()

            以此詞彙表作為向量空間,將每個命令序列轉換成對應的向量:

def get_user_cmd_feature_new(user_cmd_list,dist):
    user_cmd_feature=[]
    for cmd_list in user_cmd_list:
        v = [0]*len(dist)
        for i in range(0, len(dist)):
            if dist[i] in cmd_list:
                v[i] +=1
        user_cmd_feature.append(v)
    return user_cmd_feature

3.訓練模型

clf = GaussianNB().fit(x_train,y_train)

4.效果驗證

 

二、檢測WebShell(一)

    將網際網路上搜集到的Webshell作為黑樣本,當前最新的wordpress原碼作為白樣本。將一個PHP檔案作為字串處理,以基於單詞的2-gram切割,遍歷全部檔案形成基於2-gram的詞彙表,再將每個PHP向量化。

   

def load_file(file_path):
    t=""
    with open(file_path) as f:
        for line in f:
            line = line.strip('\n')
            t+=line
    return t
def load_file(path):
    files_list =[]
    for r,d,files in os.walk(path):
        for file in files:
            if file.endwith('.php'):
                file_path = path+file
                print "Load %s" %file_path
                t = load_file(file_path)
                files_list.append(t)
    return files_list
webshell_bigram_vectorizer = CountVectorizer(ngram_range=(2,2),decode_error="ignor",token_pattern=r'\b\w+\b')
webshell_files_list = load_files("...")
x1 = webshell_bigram_vectorizer.fit_transform(webshell_files_list).toarray()
vocabulary = webshell_bigram_vectorizer.vocabulary_

2.特徵化

wp_bigram_vectorizer = CountVectorizer(ngram_range=(2,2),decode_error="ignore",token_pattern=r'\b\w+\b',min_df=1,vocabulary = vocabulary)
wp_files_list = load_files("...")
x2 = wp.bigram_vectorzier.fit_transform(wp_files_list).toarray()
y2 = [0]*len(x2)
x=np.concatenate((x1,x2))
y=np.concatenate((y1,y2))

3訓練樣本、4效果驗證原理同上

三、檢測WebShell(二)

       webshell本質上是一個遠端管理工具,一系列管理功能其實是一系列函式呼叫,我們嘗試針對函式呼叫建立特徵。

     1.資料蒐集

        針對黑樣本集合,以1-gram演算法生成全域性的詞彙表。

  

webshell_bigram_vectorizer=CountVectorizer(ngram_range=(1,1),decode_error="ignore",token_pattern=r'\b\w+\b\(|\'\w+\")
webshell_files_list = load_files("...")
x1=webshell_bigram_vectorizer.fit_transform(webshell_files_list).toarray()
vocabulary = webshell_bigram_vectorizer.vocabulary_

   2.特徵化

     

wp_bigram_vectorizer = CountVectorizer(ngram_range=(2,2),decode_error="ignore",token_pattern=r'\b\w+\b\(|\'\w+\",min_df=1,vocabulary = vocabulary)
wp_files_list = load_files("...")
x2 = wp.bigram_vectorzier.fit_transform(wp_files_list).toarray()

3和4同上

 

四、檢測DGA域名

   1.資料蒐集: 載入alexa前1000的域名作為白樣本,標記為0,分別載入cryptolocker和post-tovar-goz家族的DGA域名,分別標記為2和3

x1_domain_list = load_alexa("...")
x2_domain_list = load_dga("...")
x3_domain_list = load_dga("...")
x_domain_list = np.concatenate(x1_domain_list,x2_domain_list,x3_domain_list)
y1=[0]*len(x1_domain_list)
y2=[1]*len(x2_domain_list)
y3=[2]*len(x3_domain_list)
y = np.concatenate((y1,y2,y3))

    2.特徵化

cv=CountVectorizer(ngram_range=(2,2),decode_error="ignore",token_pattern=r"\w",min_df=1)
x=cv.fit_transform(x_domain_list).toarray()

      3和4原理同上

 

五、檢測對Apache的DDoS攻擊

       KDD 99資料集中與DDoS相關的特徵主要為:

           ·網路連線基本特徵:包括duration, src_bytes, dst_bytes, land, wrong_fragment,urgent

           ·基於時間的網路流量統計特徵:包括count, srv_count, serror_rate, srv_serror_rate, rerror_rate, srv_rerror_rate, same_srv_rate, deff_srv_rate, rv_diff_host_rate

           ·基於主機的網路流量統計:包括dst_host_count, dst_houst_srv_count, dst_host_same_srv_rate, dst_host_diff_srv_rate, dst_host_same_src_port_rate, dst_host_srv_diff_host_rate, dst_host_serror_rate, dst_host_srv_serror_rate, dst_host_rerror_rate, dst_host_srv_rerror_rate

      1.資料搜尋:載入KDD 99資料集中的資料,以及篩選出apache2和normal,以及滿足http協議的資料,同(檢測ROOTKIT)

      2.特徵化:挑選與DDoS有關的特徵,同(檢測ROOTKIT)

      3和4原理同上

六、識別驗證碼

       1.資料蒐集:MNIST是一個入門級計算機視覺資料集,包含各種手寫數字圖片及其對應標籤

def load_data():
    with gzip.open('..') as fp:
        training_data, valid_data, test_data = pickle.load(fp)
    return training_data, valid_data, test_data

       2.特徵化:MNIST已經將24*24的圖片特徵成長度為784的一維向量。

       3.訓練模型

training_data, valid_data, test_data = load_data()
x1,y1=training_data
x2,y2=test_data
clf=GaussianNB()
clf.fix(x1,y1)

        4.效果驗證原理同上