1 Star 0 Fork 2

徐金羽/Mining flow detection

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tshark.py 3.64 KB
一键复制 编辑 原始数据 按行查看 历史
徐金羽 提交于 2023-05-11 03:59 . 增加在线检测脚本
import os
import time
import pandas as pd
import numpy as np
import joblib
import threading
import re
from scapy.all import *
features=['Bwd PSH Flags','Bwd IAT Mean','PSH Flag Cnt','Bwd IAT Max','Flow IAT Min','Bwd Pkt Len Max',
'Fwd IAT Mean','Bwd IAT Tot','Bwd IAT Std','Bwd Header Len','Subflow Bwd Byts','TotLen Bwd Pkts',
'Pkt Len Var','Flow IAT Std','Pkt Len Std','Pkt Len Min','Pkt Len Mean','Fwd IAT Max','Pkt Size Avg',
'Active Min','Bwd IAT Min','Init Bwd Win Byts','Active Std','Fwd Act Data Pkts','Idle Mean','Idle Max']
match_str='\"method\" ?: ?\"(mining\.authorize|mining\.get_transactions' \
'|mining\.subscribe|mining\.submit|getblocktemplate|submitblock' \
'|authorize|get_transactions|subscribe|submit|mining\.notify|mining\.set_difficulty' \
'|notify|set_difficulty)\"'
model_cart = joblib.load('cart.dat')
model_id3 = joblib.load('id3.dat')
def os_cmd(i):
cmd_tshark='sudo tshark -i eno2np1 -a duration:600 -f "net 10.201.0.0/16" -F pcap -w number'+str(i)+'.pcap'
print(cmd_tshark)
os.system(cmd_tshark)
cmd_cic='cd /home/test1/CICFlowMeter-4.0/bin && ./cfm '+'data/number'+str(i)+'.pcap'+' data/csv/'
print(cmd_cic)
os.system(cmd_cic)
testdf='/home/test1/CICFlowMeter-4.0/bin/data/csv/number'+str(i)+'.pcap'+'_Flow.csv'
print(testdf)
df=pd.read_csv(testdf)
nan_list = df.isnull().sum().tolist() # 把每一列的空值个数加起来
print(nan_list)
print(sum(nan_list))
# 无穷值排查
inf_list = np.isinf(df.drop(['Flow ID', 'Src IP', 'Dst IP', 'Timestamp','Label'], axis=1)).sum().tolist() # 把每一列的无穷值个数加起来
print(inf_list)
print(sum(inf_list))
# 删除空行,无穷值
df = df.replace([np.inf, -np.inf], np.nan).dropna(axis=0)
inf_list = np.isinf(df.drop(['Flow ID', 'Src IP', 'Dst IP', 'Timestamp','Label'], axis=1)).sum().tolist()
print(sum(inf_list))
X_test = df[features]
Y_cart = model_cart.predict(X_test)
Y_id3 = model_id3.predict(X_test)
Y = []
for a in range(0, X_test.shape[0]):
if Y_id3[a] == 0 or Y_cart[a] == 0:
Y.append(0)
else:
Y.append(Y_cart[a])
flowID=[]
rule=''
for a in range(0,X_test.shape[0]):
if Y[a] == 1:
flowID.append(df['Flow ID'][a])
mark=df.iloc[a:a+1,:]
mark.to_csv('result.csv',mode='a')
# filename='selection'+str(i)+'.txt'
# f=open(filename,'w')
# f.write(str(flowID))
# f.close()
# print(filename)
for a in flowID:
mark=a.split('-')
rule=rule+'(ip.addr=='+mark[0]+' && ip.addr=='+mark[1]+' && tcp.port=='+mark[2]+' && tcp.port=='+mark[3]+') || '
if flowID:
rule = rule[:-4]
cmd_select = 'sudo tshark -r number' + str(i) + '.pcap' + ' -Y' + ' "' + rule + '"' + ' -w' + ' selection' + str(i) + '.pcap'
print(cmd_select)
os.system(cmd_select)
pkts_name='selection'+str(i)+'.pcap'
new_name='detection'+str(i)+'.pcap'
pkts=rdpcap(pkts_name)
for pkt in pkts.res:
pkt_load = pkt.getlayer('Raw')
if re.search(match_str, str(pkt_load)):
rename_cmd='mv '+pkts_name+' '+new_name
os.system(rename_cmd)
break
cmd_delpcap = 'sudo rm number' + str(i) + '.pcap'
print(cmd_delpcap)
os.system(cmd_delpcap)
cmd_delcsv = 'sudo rm csv/number' + str(i) + '.pcap' + '_Flow.csv'
print(cmd_delcsv)
os.system(cmd_delcsv)
i=1
while True:
th=threading.Thread(target=os_cmd,args=(i,))
th.start()
i=i+1
time.sleep(180)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xu-jinyu-cifer/mining-flow-detection.git
git@gitee.com:xu-jinyu-cifer/mining-flow-detection.git
xu-jinyu-cifer
mining-flow-detection
Mining flow detection
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385