龙空技术网

朴素贝叶斯检测WebShell

ailx10 27

前言:

眼前我们对“webshell识别”大概比较注意,大家都想要知道一些“webshell识别”的相关内容。那么小编也在网上汇集了一些关于“webshell识别””的相关内容,希望朋友们能喜欢,朋友们快快来了解一下吧!

物以内聚,人以群分?先前咱们玩KNN的时候,通过系统调用号来检测WebShell,对正常用户的系统调用和黑客的系统调用进行分类。

对于PHP网站,为了获取WebShell,黑客一般都要上传木马,本质上PHP文件,今天采用朴素贝叶斯对PHP文件进行分类,找出网站中的木马。

简单介绍一下PHP后门程序:

<?phpif(isset($_REQUEST['cmd'])){        $cmd = ($_REQUEST['cmd']);        system($cmd);        die;}?>

玩法:

上传了后门程序之后,就可以操作目标网站里的任意文件了。

1.数据搜集和数据清洗

黑样本,共计57个文件,将每个PHP文件的内容转化成一行字符串,以2-gram算法生成全局词汇表,共计13283个词汇。 2-gram算法是将2个单词用空格连接起来。

xiaoma/├── 1148d726e3bdec6db65db30c08a75f80.php ...├── 2.php├── 40ab2eb6afa30df156b4c408a7a3317b.php ...├── accept_language.php├── b38459c79905892681e3a479a2fdba57.php├── b940f0e06b6cd558be898e003d4741f3.php├── backupsql.php ...├── errors.php├── f1290186a5d0b1ceab27f4e77c0c5d68.php├── f24c72e6474a4542b1ce105978e9c84c.php├── f322d368c773189a9ae2f11e46b067df.php├── f694b2486701e8c79aa8a8d1daf49231.php├── f88d296ba2cf8b67097da5996b505e09.php├── long_hm.php├── PHANTASMA.php├── simple-backdoor.php├── TeSt.php├── wget2.php├── wget.php└── yjh.php0 directories, 57 files

白样本,100个文件,是wordpress网站的部分源代码。

2.特征化

使用黑样本生成词汇表vocabuliary,将白样本特征化,设置CountVectorizer函数的vocabulary,这样才能以黑样本生成的词汇来进行向量化。

    webshell_bigram_vectorizer = CountVectorizer(ngram_range=(2, 2), decode_error="ignore",                                        token_pattern = r'\b\w+\b',min_df=1)    webshell_files_list=load_files("../data/PHP-WEBSHELL/xiaoma/")    x1=webshell_bigram_vectorizer.fit_transform(webshell_files_list).toarray()    y1=[1]*len(x1)    vocabulary=webshell_bigram_vectorizer.vocabulary_    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("../data/wordpress/")    x2=wp_bigram_vectorizer.fit_transform(wp_files_list).toarray()    y2=[0]*len(x2)        x=np.concatenate((x1,x2))    y=np.concatenate((y1, y2))
3.训练样本

创建NB实例:

clf = GaussianNB()
4.效果验证

使用3折交叉验证

    score =  cross_validation.cross_val_score(clf, x, y, n_jobs=-1,cv=3)    print score    print np.mean(score)

k折交叉验证:在样本不充足的情况下,为了充分利用数据集对算法效果进行测试,将数据集分k份,一份作为测试集,k-1份作为训练集。训练k次,测试k次。

贝叶斯训练PHP词汇表,识别WebShell的准确率79%

[0.73584906 0.82692308 0.80769231]0.7901548137397194
5.完整代码
import osfrom sklearn.feature_extraction.text import CountVectorizerimport sysimport numpy as npfrom sklearn import cross_validationfrom sklearn.naive_bayes import GaussianNBdef load_file(file_path):    t=""    with open(file_path) as f:        for line in f:            line=line.strip('\n')            t+=line    return tdef load_files(path):    files_list=[]    for r, d, files in os.walk(path):        for file in files:            if file.endswith('.php'):                file_path=path+file                #print "Load %s" % file_path                t=load_file(file_path)                files_list.append(t)    return  files_listif __name__ == '__main__':    #bigram_vectorizer = CountVectorizer(ngram_range=(2, 2),token_pattern = r'\b\w+\b', min_df = 1)    webshell_bigram_vectorizer = CountVectorizer(ngram_range=(2, 2), decode_error="ignore",                                        token_pattern = r'\b\w+\b',min_df=1)    webshell_files_list=load_files("../data/PHP-WEBSHELL/xiaoma/")    x1=webshell_bigram_vectorizer.fit_transform(webshell_files_list).toarray()    y1=[1]*len(x1)    vocabulary=webshell_bigram_vectorizer.vocabulary_    #print vocabulary    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("../data/wordpress/")    x2=wp_bigram_vectorizer.fit_transform(wp_files_list).toarray()    y2=[0]*len(x2)         x=np.concatenate((x1,x2))    y=np.concatenate((y1, y2))        clf = GaussianNB()    score =  cross_validation.cross_val_score(clf, x, y, n_jobs=-1,cv=3)    print score    print np.mean(score)

标签: #webshell识别