-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.py
More file actions
294 lines (225 loc) · 7.95 KB
/
build.py
File metadata and controls
294 lines (225 loc) · 7.95 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import numpy as np
import pprint, re, random, pickle, json, argparse
from datetime import datetime
class Build:
def __init__(self, files):
self.data = []
self.graph = {}
self.nodes = {}
self.node_map = {}
self.id_count = {}
self.od_count = {}
self.idw_count = {}
self.odw_count = {}
self.lcc_count = {}
self.f = {}
self.fvecs = []
self.content = self.read(files)
self.non_bot_tuples, self.bot_tuples = self.mod(self.content)
'''
File read helper function
'''
def read(self, files):
combined_content = []
for file in files:
with open('./datasets/' + str(file), 'r') as of:
content = of.readlines()
for line in content[1:]:
combined_content.append(line)
of.close()
print("Read file - " + str(file))
print("Read Dataset...")
return combined_content
'''
Return tuples containing non-bot flows and bot-flows separately
'''
def mod(self, content):
non_bot_tuples = []
bot_tuples = []
non_bot_flow_tuples = 100000
for line in content:
flow = line.split(',')[14]
# NON BOTNET FLOWS
if len(re.findall("Botnet", str(flow))) == 0:
if non_bot_flow_tuples > 0:
non_bot_flow_tuples -= 1
non_bot_tuples.append(line)
else:
bot_tuples.append(line)
return (non_bot_tuples, bot_tuples)
def build_train_set(self, nb, b):
train_set = nb + b
random.shuffle(train_set)
print("Built training dataset...")
return train_set
def build_test_set(self, nb, b, p):
print("Total tuples to choose test set from: " + str(len(nb + b)) + " = " + str(len(nb)) + " + " + str(len(b)))
tnb = int((p*len(nb))/100)
random.shuffle(nb)
random.shuffle(b)
test_set = nb[:tnb] + b
print("Total non-bot tuples: " + str(tnb) + " -> " + str((tnb*100)/float(tnb+len(b))) + " %")
print("Total bot tuples: " + str(len(b)) + " -> " + str((len(b)*100)/float(tnb+len(b))) + " %")
random.shuffle(test_set)
print("Total size of test set: " + str(tnb + len(b)))
print("Built testing dataset...")
return test_set
'''
Flow ingestion
'''
def extract(self, x):
if len(x.split(',')) == 15:
sip = x.split(',')[3]
dip = x.split(',')[6]
self.nodes[sip] = 1
byteSize = int(x.split(',')[12])/int(x.split(',')[11])
srcpkts = int(x.split(',')[13])/byteSize
dstpkts = int(x.split(',')[11]) - srcpkts
flow = x.split(',')[14]
if len(re.findall("Botnet", str(flow))) > 0:
self.node_map[sip] = 1
else:
self.node_map[sip] = 0
# SIP -> DIP
if sip not in self.graph:
self.graph[sip] = {}
if dip not in self.graph[sip]:
self.graph[sip][dip] = (srcpkts, (srcpkts, dstpkts))
else:
srcpktsPrev = self.graph[sip][dip][0]
self.graph[sip][dip] = (srcpkts + srcpktsPrev, (srcpkts, dstpkts))
if dip in self.graph:
if sip in self.graph[dip]:
srcpktsX = self.graph[sip][dip][0]
dstpktsY = self.graph[dip][sip][1][1]
self.graph[sip][dip] = (srcpktsX + dstpktsY, (srcpkts, dstpkts))
# DIP -> SIP
if dip not in self.graph:
self.graph[dip] = {}
if sip not in self.graph[dip]:
self.graph[dip][sip] = (dstpkts, (srcpkts, dstpkts))
else:
dstpktsPrev = self.graph[dip][sip][0]
self.graph[dip][sip] = (dstpkts + dstpktsPrev, (srcpkts, dstpkts))
if sip in self.graph:
if dip in self.graph[sip]:
dstpktsX = self.graph[dip][sip][0]
srcpktsY = self.graph[sip][dip][1][1]
self.graph[dip][sip] = (srcpktsY + dstpktsX, (srcpkts, dstpkts))
return (sip, dip)
'''
F-Norm Implementation using D = 1
'''
def normalize(self, fn, node):
N = 0
sumF = np.array([0, 0, 0, 0]).astype('float64')
for n in self.nodes:
if n != node:
if node in self.graph:
if n in self.graph[node]:
N += 1
sumF += np.array(self.f[n][0]).astype('float64')
if N > 0:
u = sumF/float(N)
for idx, ui in enumerate(u):
if ui == 0.0:
ui += 0.01
fn[idx] = fn[idx]/float(ui)
return fn
'''
Graph Transform and extracted feature storage
'''
def preprocess(self):
for line in self.data:
self.extract(line)
node_dict = {}
for n in self.node_map:
if self.node_map[n] not in node_dict:
node_dict[self.node_map[n]] = 1
else:
node_dict[self.node_map[n]] += 1
print(node_dict)
print("Graph built!")
# FOR EACH NODE CALCULATE THE FEATURE TUPLE [ F0, F1, F2, F3, F4 ]
# bots = 0
# for it in self.node_map:
# if self.node_map[it] == 1:
# bots += 1
print(len(self.nodes))
# print(bots)
for node in self.nodes:
self.f[node] = [ [ self.ID(node), self.OD(node), self.IDW(node), self.ODW(node) ], self.node_map[node] ]
print("Feature Extraction done!")
with open('./saved/f.json', 'w') as feat:
json.dump(self.f, feat, indent=4)
# NORMALIZE
for node in self.nodes:
# self.f[node][0] = self.normalize(self.f[node][0], node)
self.fvecs.append(self.f[node][0])
# if self.node_map[node] == 1:
# print(self.f[node][0])
with open('./saved/fvecs.json', 'w') as fv:
json.dump(self.fvecs, fv, indent=4)
with open('./saved/f.json', 'w') as feat:
json.dump(self.f, feat, indent=4)
print("Normalizing Done!")
'''
Feature Extraction
'''
# IN-DEGREE (ID)
def ID(self, node):
c = 0
for n in self.nodes:
if n != node:
if n in self.graph:
if node in self.graph[n]:
c += 1
return c
# OUT-DEGREE (OD)
def OD(self, node):
c = 0
for n in self.nodes:
if n != node:
if node in self.graph:
if n in self.graph[node]:
c += 1
return c
# IN-DEGREE WEIGHT (IDW)
def IDW(self, node):
c = 0
for n in self.nodes:
if n != node:
if n in self.graph:
if node in self.graph[n]:
c += self.graph[n][node][0]
return c
# OUT-DEGREE WIGHT (ODW)
def ODW(self, node):
c = 0
for n in self.nodes:
if n != node:
if node in self.graph:
if n in self.graph[node]:
c += self.graph[node][n][0]
return c
# LOCAL CLUSTERING COEFFICIENT (LCC)
def LCC(self, node):
N = 0
NgNodes = []
for n in self.nodes:
if n != node:
if node in self.graph:
if n in self.graph[node]:
N += 1
NgNodes.append(n)
F = 0
for j in NgNodes:
for k in NgNodes:
if j != k:
if j in self.graph:
if k in self.graph[j]:
F += 1
if N > 1:
return F/float(N*(N-1))
else:
return 0.0