《Web安全之深度学习实战》笔记:第九章 Linux后门检测

本章主要以ADFA-LD数据集为例介绍Linux系统的后门检测,使用特征提取方法为2-Gram和TF-IDF,介绍的分类算法包括朴素贝叶斯NB、XGBoost和深度学习之多层感知机MLP。

一、数据集

其实这个数据集在《web安全之机器学习入门》中多个单元中均有提到,ADFA数据集是澳大利亚国防学院对外发布的一套主机级入侵检测系统的数据集合,被广泛应用于入侵检测类产品的测试。该数据集包括ADFA-LD和ADFA-WD,分别代表Linux系统的数据集和Windows系统的数据集。

        以ADFA-LD为例,它完整记录了一段时间内的操作系统的系统调用。内核提供用户空间程序与内核空间进行交互的一套标准接口,这些接口让用户态程序能受限访问硬件设备,比如申请系
统资源,操作设备读写,创建新进程等。用户空间发生请求,内核空间负责执行,这些接口便是用户空间和内核空间共同识别的桥梁,这里提到两个字“受限”,是由于为了保证内核稳定性,而不能让用户空间程序随意更改系统,必须是内核对外开放的且满足权限的程序才能调用相应接口。在用户空间和内核空间之间,有一个叫做“系统调用”的中间层,是连接用户态和内核态的桥梁。这样即提高了内核的安全性,也便于移植——只需实现同一套接口即可。Linux系统,用户空间通过向内核空间发出系统调用产生软中断,从而让程序陷入内核态,执行相应的操作。对于每个系统调用都会有一个对应的系统调用号,比很多操作系统要少很多。

数据集处理代码如下

def load_all_files():
    import glob
    x=[]
    y=[]
    #加载攻击样本
    files=glob.glob("../data/ADFA-LD/Attack_Data_Master/*/*")
    for file in files:
        with open(file) as f:
            lines=f.readlines()
        x.append(" ".join(lines))
        y.append(1)
    print ("Load black data %d" % len(x))
    #加载正常样本
    files=glob.glob("../data/ADFA-LD/Training_Data_Master/*")
    for file in files:
        with open(file) as f:
            lines=f.readlines()
        x.append(" ".join(lines))
        y.append(0)
    print ("Load full data %d" % len(x))

    return x,y

调用如下所示

x,y=load_all_files()

 二、特征提取

def get_feature_wordbag():
    max_features=1000
    x,y=load_all_files()
    vectorizer = CountVectorizer(
                                 ngram_range=(3, 3),
                                 token_pattern=r'\b\d+\b',
                                 decode_error='ignore',
                                 strip_accents='ascii',
                                 max_features=max_features,
                                 stop_words='english',
                                 max_df=1.0,
                                 min_df=1 )
    print (vectorizer)
    x = vectorizer.fit_transform(x)

    transformer = TfidfTransformer(smooth_idf=False)
    x=transformer.fit_transform(x)

    x = x.toarray()

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4)
    return x_train, x_test, y_train, y_test

三、模型构建

1、NB

逻辑处理示意图如下所示

 代码如下所示

def do_nb(x_train, x_test, y_train, y_test):
    gnb = GaussianNB()
    gnb.fit(x_train,y_train)
    y_pred=gnb.predict(x_test)
    print(classification_report(y_test, y_pred))
    print (metrics.confusion_matrix(y_test, y_pred))

2、XGBoost

逻辑处理示意图如下所示

源码如下所示 

def do_xgboost(x_train, x_test, y_train, y_test):
    xgb_model = xgb.XGBClassifier().fit(x_train, y_train)
    y_pred = xgb_model.predict(x_test)
    print(classification_report(y_test, y_pred))
    print (metrics.confusion_matrix(y_test, y_pred))

3、MLP

逻辑处理示意图如下所示

源码如下所示

def do_mlp(x_train, x_test, y_train, y_test):
    # Building deep neural network
    clf = MLPClassifier(solver='lbfgs',
                        alpha=1e-5,
                        hidden_layer_sizes = (5, 2),
                        random_state = 1)
    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_test)
    print(classification_report(y_test, y_pred))
    print (metrics.confusion_matrix(y_test, y_pred))

四、测试结果

这里要注意以下,作者的源码中打印的信息有些内容写的是2-gram,这是因为作者写的代码没有审查好,如下所示

if __name__ == "__main__":
    print ("Hello Linux Rootkit")
    print ("3-Gram&tf-idf and nb")
    x_train, x_test, y_train, y_test=get_feature_wordbag()
    do_nb(x_train, x_test, y_train, y_test)
    print ("2-Gram&tf-idf and xgboost")
    do_xgboost(x_train, x_test, y_train, y_test)
    print ("2-Gram&tf-idf and mlp")
    do_mlp(x_train, x_test, y_train, y_test)

实际上这要与前面get_feature_wordbag()函数中配置的N-gram一致,比如本小节我这里的示例代码是3*3,故而正常应该这样写,才不会让读者有误解

if __name__ == "__main__":
    print ("Hello Linux Rootkit")
    print ("3-Gram&tf-idf and nb")
    x_train, x_test, y_train, y_test=get_feature_wordbag()
    do_nb(x_train, x_test, y_train, y_test)
    print ("3-Gram&tf-idf and xgboost")
    do_xgboost(x_train, x_test, y_train, y_test)
    print ("3-Gram&tf-idf and mlp")
    do_mlp(x_train, x_test, y_train, y_test)

运行结果如下

3-Gram&tf-idf and nb
Load black data 746
Load full data 1579
CountVectorizer(analyzer='word', binary=False, decode_error='ignore',
                dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',
                lowercase=True, max_df=1.0, max_features=1000, min_df=1,
                ngram_range=(3, 3), preprocessor=None, stop_words='english',
                strip_accents='ascii', token_pattern='\\b\\d+\\b',
                tokenizer=None, vocabulary=None)
              precision    recall  f1-score   support

           0       0.86      0.97      0.92       332
           1       0.97      0.83      0.89       300

    accuracy                           0.91       632
   macro avg       0.91      0.90      0.90       632
weighted avg       0.91      0.91      0.90       632

[[323   9]
 [ 51 249]]
3-Gram&tf-idf and xgboost

              precision    recall  f1-score   support

           0       0.93      0.93      0.93       332
           1       0.93      0.93      0.93       300

    accuracy                           0.93       632
   macro avg       0.93      0.93      0.93       632
weighted avg       0.93      0.93      0.93       632

[[310  22]
 [ 22 278]]
3-Gram&tf-idf and mlp

              precision    recall  f1-score   support

           0       0.89      0.95      0.92       332
           1       0.94      0.87      0.90       300

    accuracy                           0.91       632
   macro avg       0.91      0.91      0.91       632
weighted avg       0.91      0.91      0.91       632

[[314  18]
 [ 40 260]]

Process finished with exit code 0

如上即为运行结果,并没有作者的运行结果一模一样,但是相差不算大。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mooyuan天天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值