5-6-KNN-detect-Webshell.py 747 B

12345678910111213141516171819202122232425262728
  1. import numpy as np
  2. from sklearn.neighbors import KNeighborsClassifier
  3. from sklearn.model_selection import train_test_split, cross_val_score
  4. from sklearn.feature_extraction.text import CountVectorizer
  5. from datasets import Datasets
  6. import matplotlib.pyplot as plt
  7. import pandas as pd
  8. def main():
  9. # 加载ADFA-LD 数据
  10. x1, y1 = Datasets.load_adfa_normal()
  11. x2, y2 = Datasets.load_adfa_attack(r"Web_Shell_\d+/UAD-W*")
  12. x = x1 + x2
  13. y = y1 + y2
  14. # 词袋特征
  15. cv = CountVectorizer()
  16. x = cv.fit_transform(x).toarray()
  17. knn = KNeighborsClassifier(n_neighbors=3)
  18. scores = cross_val_score(knn, x, y, cv=10, scoring='accuracy')
  19. print(scores.mean()) # 0.9663706140350878
  20. if __name__ == "__main__":
  21. main()