forked from ymirsky/Kitsune-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetStat.py
133 lines (114 loc) · 6.41 KB
/
netStat.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import numpy as np
## Prep AfterImage cython package
import os
import subprocess
import pyximport
pyximport.install()
import AfterImage as af
#import AfterImage_NDSS as af
#
# MIT License
#
# Copyright (c) 2018 Yisroel mirsky
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
class netStat:
#Datastructure for efficent network stat queries
# HostLimit: no more that this many Host identifiers will be tracked
# HostSimplexLimit: no more that this many outgoing channels from each host will be tracked (purged periodically)
# Lambdas: a list of 'window sizes' (decay factors) to track for each stream. nan resolved to default [5,3,1,.1,.01]
def __init__(self, Lambdas = np.nan, HostLimit=255,HostSimplexLimit=1000):
#Lambdas
if np.isnan(Lambdas):
self.Lambdas = [5,3,1,.1,.01]
else:
self.Lambdas = Lambdas
#HT Limits
self.HostLimit = HostLimit
self.SessionLimit = HostSimplexLimit*self.HostLimit*self.HostLimit #*2 since each dual creates 2 entries in memory
self.MAC_HostLimit = self.HostLimit*10
#HTs
self.HT_jit = af.incStatDB(limit=self.HostLimit*self.HostLimit)#H-H Jitter Stats
self.HT_MI = af.incStatDB(limit=self.MAC_HostLimit)#MAC-IP relationships
self.HT_H = af.incStatDB(limit=self.HostLimit) #Source Host BW Stats
self.HT_Hp = af.incStatDB(limit=self.SessionLimit)#Conversation (Host Port) BW Stats
def findDirection(self,IPtype,srcIP,dstIP,eth_src,eth_dst): #cpp: this is all given to you in the direction string of the instance (NO NEED FOR THIS FUNCTION)
if IPtype==0: #is IPv4
lstP = srcIP.rfind('.')
src_subnet = srcIP[0:lstP:]
lstP = dstIP.rfind('.')
dst_subnet = dstIP[0:lstP:]
elif IPtype==1: #is IPv6
src_subnet = srcIP[0:round(len(srcIP)/2):]
dst_subnet = dstIP[0:round(len(dstIP)/2):]
else: #no Network layer, use MACs
src_subnet = eth_src
dst_subnet = eth_dst
return src_subnet, dst_subnet
def updateGetStats(self, IPtype, srcMAC, dstMAC, srcIP, srcProtocol, dstIP, dstProtocol, datagramSize, timestamp, counter):
# the following prefixing was added to solve a problem
# with a flow that has the same source and destination MAC
# but it creates a problem with the flows in the reverse direction
# srcMAC='sm'+srcMAC
# dstMAC='dm'+dstMAC
# srcIP='si'+srcIP
# dstIP='di'+dstIP
# print(counter)
# if (counter==46):
# print(srcIP,srcMAC,dstIP,dstMAC)
# Host BW: Stats on the srcIP's general Sender Statistics
Hstat = np.zeros((3*len(self.Lambdas,)))
for i in range(len(self.Lambdas)):
Hstat[(i*3):((i+1)*3)] = self.HT_H.update_get_1D_Stats(srcIP, timestamp, datagramSize, self.Lambdas[i])
#MAC.IP: Stats on src MAC-IP relationships
MIstat = np.zeros((3*len(self.Lambdas,)))
for i in range(len(self.Lambdas)):
MIstat[(i*3):((i+1)*3)] = self.HT_MI.update_get_1D_Stats(srcMAC+'_'+srcIP, timestamp, datagramSize, self.Lambdas[i])
# Host-Host BW: Stats on the dual traffic behavior between srcIP and dstIP
HHstat = np.zeros((7*len(self.Lambdas,)))
for i in range(len(self.Lambdas)):
HHstat[(i*7):((i+1)*7)] = self.HT_H.update_get_1D2D_Stats(srcIP, dstIP,timestamp,datagramSize,self.Lambdas[i],counter=counter)
# Host-Host Jitter:
HHstat_jit = np.zeros((3*len(self.Lambdas,)))
for i in range(len(self.Lambdas)):
HHstat_jit[(i*3):((i+1)*3)] = self.HT_jit.update_get_1D_Stats(srcIP+'_'+dstIP, timestamp, 0, self.Lambdas[i],isTypeDiff=True)
# Host-Host BW: Stats on the dual traffic behavior between srcIP and dstIP
HpHpstat = np.zeros((7*len(self.Lambdas,)))
if srcProtocol == 'arp':
for i in range(len(self.Lambdas)):
HpHpstat[(i*7):((i+1)*7)] = self.HT_Hp.update_get_1D2D_Stats(srcMAC, dstMAC, timestamp, datagramSize, self.Lambdas[i],counter=counter)
else: # some other protocol (e.g. TCP/UDP)
for i in range(len(self.Lambdas)):
if srcIP == srcIP + srcProtocol :
print ('collision', srcIP, srcProtocol)
HpHpstat[(i*7):((i+1)*7)] = self.HT_Hp.update_get_1D2D_Stats(srcIP +'_'+ srcProtocol, dstIP +'_'+ dstProtocol, timestamp, datagramSize, self.Lambdas[i],counter=counter)
return np.concatenate((Hstat, MIstat, HHstat, HHstat_jit, HpHpstat)) # concatenation of stats into one stat vector
def getNetStatHeaders(self):
MIstat_headers = []
Hstat_headers = []
HHstat_headers = []
HHjitstat_headers = []
HpHpstat_headers = []
for i in range(len(self.Lambdas)):
Hstat_headers += ["H_dir_"+h for h in self.HT_H.getHeaders_1D(Lambda=self.Lambdas[i],ID=None)]
MIstat_headers += ["MI_dir_"+h for h in self.HT_MI.getHeaders_1D(Lambda=self.Lambdas[i],ID=None)]
HHstat_headers += ["HH_"+h for h in self.HT_H.getHeaders_1D2D(Lambda=self.Lambdas[i],IDs=None,ver=2)]
HHjitstat_headers += ["HH_jit_"+h for h in self.HT_jit.getHeaders_1D(Lambda=self.Lambdas[i],ID=None)]
HpHpstat_headers += ["HpHp_" + h for h in self.HT_Hp.getHeaders_1D2D(Lambda=self.Lambdas[i], IDs=None, ver=2)]
return MIstat_headers + Hstat_headers + HHstat_headers + HHjitstat_headers + HpHpstat_headers