1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# -*- coding: utf-8 -*-
from keras.models import load_model
from keras.preprocessing.sequence import pad_sequences
import numpy as np
import sys
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
max_len = 100
lstm_model = 'model/lstm_packetpre.h5' # lstm参数更多,准确率更高
mlp_model = 'model/mlp_packetin_model.h5' # mlp参数少一点,速度更快
class BenignPacketData(object):
"""
目的:用单个包(不是流)对10种良性应用进行分类,只能针对这10种
注意:调用的时候如果与预期不符,则多半是编码问题。检测self.data看看与输入是否一致
对应关系如下
0 Facetime
1 BitTorrent
2 WorldOfWarcraft
3 FTP
4 MySQL
5 Skype
6 SMB
7 Gmail
8 Weibo
9 Outlook
"""
def __init__(self, msg, modelpath=mlp_model):
self.data = self.getdata(msg)
self.model = load_model(modelpath)
self.lable_list = ['Facetime', 'BitTorrent', 'WorldOfWarcraft', 'FTP', 'MySQL',
'Skype', 'SMB', 'Gmail', 'Weibo', 'Outlook']
def getdata(self, msg):
# s = str(msg.split(",")[6].encode('utf-8')).split("=")[1].strip("'").strip('"')
msg = str(msg.encode('raw_unicode_escape')).strip('b\'')
s = msg.split(",")[6].split("=")[1].strip("'").strip('"')
data = [int(hex(x), 16) for x in bytes(s, encoding="utf-8")]
return data
def predict(self):
x_data = np.array(self.data)
x_data = x_data.flatten()[None] # 变成这样[[]]
x_train = pad_sequences(x_data, maxlen=max_len)
# x_train = pad_sequences(x_data, maxlen=max_len, padding='post', truncating='post')
predict = self.model.predict(x_train)
# predict_classes = self.model.predict_classes(x_train)
# print('classes: ', predict_classes)
predict = np.argmax(predict)
return predict
def predict_classes(self):
num = self.predict()
return self.lable_list[int(num)]
if __name__ == '__main__':
# Outlook: 9
line1 = "version=0x4,msg_type=0xa,msg_len=0x5ed,xid=0x0,OFPPacketIn(buffer_id=4294967295,cookie=0,data='\x02\x1a\xc5\x01\x00\x00\x02\x1a\xc5\x02\x00\x00\x08\x00E\x00\x05\xb1\xc3\x00@\x00 \x06Lw\x01\x025*\x01\x01\x0e\xa3\x01\xbb\x85\xe5ur\xfb\xce \xea71\x80\x18T\xd8\xd4\xd6\x00\x00\x01\x01\x08\n\x1e\xb5QU\r=\x07\x8a\x17\x03\x00\x05x^\xaa^a\xdb\x89v\xa9p\x80\x01\x82\x04j\xe9{4\x18w\xfaf\xfd\xe6\xe2sh;F\x07\xa0a`\xf4\x14\xab!\xa2Vg\xd8\xb0_9$vMD4\xd96\xea\x1d\x08hf\xc9\xe6\x90\xaenwE\xb6Km/\xad\x17\xee]\xaf\xb8\x19\x1fla\x10\x99*a\xc4!#\x14}\xedV\xd3\xf8\xffy\xe5\xddG\'\x05\xb6\x85\xf2\x86X\xd8.\x96\x97\x10O\xd8\xda\xbcPJW\x19k\x08\xad\xeco\xd5\xbb|X\xb6\x90\xab,\xc3\\\x0c[\xd2\x15\x83\xf2-\xe2(8\x1fq\xf6Ovj\'>\xedV( \x93\x93\x80t\x1c{\xcd\xecj\x98\x8e\x863\x17.@65=\xa9\x100\x91\x06\xfc\xea\xab\xbb\x12L\\q d\x1d\xd9\xb1\xd5\xcb\xc1\xae\xab\xe1p\xb6\xb2\xaf\xf9\xd4}$\x16\xc8n}\xccs\xfe\x10\x00\x7f\xba\xd4\xea\xd4\xa6\x83m@7\xc0+\xe9\xc5\x8bb<5\x9f\x92\xce\xae\x1b2j^P\x8e\xa6\x9a\x14O\xf3\xbc\x12p\xf1""\xd5u\xa9/_\x13\xc2\xf1\xb3\xa3R\x8d\xbd(Q}d@\xa6+\xd7\x85\x91H\xb2l\x0e\x1a\xb5(\xefj?6\xf1\xf4\x81n:\x94\xedn0\x94+\x03L\x82\x06\xb9\x8d\xaa\x08&\x03m\xa4!G\xc5\x80\x1bQv\x03n+\xd1\x98)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\x9d\xcd\xa0,}\\\x1f\x8f\xafG\xda<J\xcf\'\xach\xbe=\xfd\xd8\x1b\x99Q\xd2\xb9\x96h\xc1\xf6I\xbf\x97\xd0\x95\xd8\xf4\x8b\xd8LJ\x87r-*{HH!\x1bj\xcd0\x876)_g\x16&\xc3rIn`\xd2\x9am\xbf\x83D\xc5\x99\xbf#W;\xa09\x0c\xdc\xb5\x0b\x9dOMw\x99Q\xb3\x8fI:\x92\x08\xab\xe9uf\xa3""t\xe0>&E\x7f\xfb\xf8\x937\x93\xceX\xd1\x14\xd5\x0f\xb4\xd0\xf3N\xf1/\xcc\x95\x18\x1e\xcdP\xb4\x91<\xa3x\x81\xe7d\xfd\x80\xcf\xeb\xd4\x1a0\xeb\xf5[\xee\xe4J\xac*5\xc9\xffI\x9dO\x12\xa3\xd3\\\x18\x8chcc\x11fz\xb4\xc7sM=c\x97\r\x0e\x84\x81\xd1\x8a\xe74&\x91\x17_\xbe\t\xd5\xf2\x06O\x05\x9a\xec\x08T\xf0\x1d\x88=\xffh\xcb\xcd\x93\xd5""\xbcF\xc1\x15\xc4N\x8aT\xe0\xd4\xb6:\x08z\xcf\x1d\xeb\x97\x92\x1d\t\xea\x970\xf9)\xdb\x83^\xfb\xe5\xdfE\xcf\x88\xe8K\xc7\x1f0f+\xc9\xdaKf\xdbr\xc8R\x87\xf5\xc9\x86p\xc1\xea\x1bD\xee\xd3\xcdg\x8c\x06c\x00\xf1\xe5~/U\x9f\x1d\xa5\x0e\xd4n\x9a\x0e\x99\x0c\xce\x8b\xee\x82\x94\xf1\x96\xe6\xfe\x02g)\xe2X\x90H;\xdc\xd0',match=OFPMatch(oxm_fields={'in_port': 1}),reason=0,table_id=0,total_len=1475)"
thispacket1 = BenignPacketData(line1)
print('测试1:', thispacket1.predict(), thispacket1.predict_classes())
# WorldOfWarcraft: 2
line1 = "version=0x4,msg_type=0xa,msg_len=0x74,xid=0x0,OFPPacketIn(buffer_id=4294967295,cookie=0,data='\x02\x1a\xc5\x02\x00\x00\x02\x1a\xc5\x01\x00\x00\x08\x00E\x00\x008\xda\x19@\x00 \x06\xc8\xc5\x01\x01$\xae\x01\x02\x910\x96\xcd\x0e\x8c\xe5\xfb\xa3\x94\x00\x00\x00\x00\x90\x02\x16\xa0?\x12\x00\x00\x02\x04\x05\xb4\x01\x01\x08\n\xf5m-$\x00\x00\x00\x00\x89\xc8 \xd5',match=OFPMatch(oxm_fields={'in_port': 1}),reason=0,table_id=0,total_len=74)"
thispacket2 = BenignPacketData(line1)
print('测试2:', thispacket2.predict(), thispacket2.predict_classes())
# Skype: 5
line1 = "version=0x4,msg_type=0xa,msg_len=0x2ca,xid=0x0,OFPPacketIn(buffer_id=4294967295,cookie=0,data='\x02\x1a\xc5\x01\x00\x00\x02\x1a\xc5\x02\x00\x00\x08\x00E\x00\x02\x8e\xc6\x87@\x00 \x06Ja\x01\x02l\xea\x01\x01\xd8\x94\x01\xbbq\xa6f\xc6H|\xe6\xe5\xecZ\x80\x18I\x88G\xc0\x00\x00\x01\x01\x08\n!\xa0\x9b\xf7\x02\xf8\xd5\xbe\x16\x03\x00\x007\x02\x00\x003\x03\x00W\x94I\x0e_\xa4\x15I_X\x15S\xdfS\x15L_\xa4\x15k\xdf\x84\x15p_\xa4\x15y_\xa4\x15\x98\x00\xc0\x14\x00\x00\x0b\xff\x01\x00\x01\x00\x00\x0b\x00\x02\x01\x00\x16\x03\x00\x01\x90\x0b\x00\x01\x8c\x00\x01\x89\x00\x01\x860\x82\x01\x820\x82\x01,\x02\x01\x010\r\x06\t*\x86H\x86\xf7\r\x01\x01\x05\x05\x000H1\x0b0\t\x06\x03U\x04\x06\x13\x02US1\x0e0\x0c\x06\x03U\x04\x08\x13\x05Texas1\x110\x0f\x06\x03U\x04\x07\x13\x08ServerCA1\x160\x14\x06\x03U\x04\x03\x14\rserver_ca.int0\x1e\x17\r100204221041Z\x17\r370622221041Z0P1\x0b0\t\x06\x03U\x04\x06\x13\x02US1\x0e0\x0c\x06\x03U\x04\x08\x13\x05Texas1\x100\x0e\x06\x03U\x04\x07\x13\x07Servers1\x1f0\x1d\x06\x03U\x04\x03\x14\x16serverA_512.server.int0\\0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00\x03K\x000H\x02A\x00\xacY;\x9e\x8c\x1fp\xccw\x99\xb3\x07\t\x86<\x85#\x8a \x8c?\x1b\xa21\xddn\xec\x15%u\x8f\'H/\xfc\x83y""\x97\xcf\x1f\xbf\xa0Y\x88\xb05\xf7\x95""\n{\xd7|}\x15t\xb0\xaf\x9e? \xad)\x02\x03\x01\x00\x010\r\x06\t*\x86H\x86\xf7\r\x01\x01\x05\x05\x00\x03A\x00\x97\xdf\xb0\xa3\xea\x06\x9a\xd4\x19\xe1\x84tT,\xa8le\xc2W:\n\x15%\xb87~\xb3\xa2\x83\x8d\x00\x8a \xccq]\xb1\x0cU\x16\x84\xae\x02\xa0\x8e\xe1.%\xcah\x8eP\xb1(H\xd8P[\x95\xbe(\xbf\xf6\xf5\x16\x03\x00\x00{\x0c\x00\x00w\x03\x00\x131\x04_\x84\x90\x80\xd5B\xcee\x89\xf2\xc1\x90\x0f\xf1L""\xa6\xcdh\xf1\xb1\xd9\xfe\xa4\xd3\xb5:%\xea\xcb@\xd9NR\x05\x1d@\x8f\xfaS\x1c\xda\x95]\x82\x0b\xba/\x00@v\xd1Z\x15\x8d\x1d\r!\xe9C\xb3\xc2\xaeR\xddG\x8c\xe2\x9d\x89\x81V\xc0\x9b\xa4\x1d\x1a\x94\xd4\x9f\x8f2H\x96\xb1oO^{\x03\xfc(:\xc3\x10\xd2\xa7\x1b\x89P:\xd6\xcd\xb1\x7f.\x8f\xf0\x9a\x97hH\xde\\\x16\x03\x00\x00\x04\x0e\x00\x00\x00\xe2\x957\x9b',match=OFPMatch(oxm_fields={'in_port': 1}),reason=0,table_id=0,total_len=672)"
thispacket3 = BenignPacketData(line1)
print('测试3:', thispacket3.predict(), thispacket3.predict_classes())
|