-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.py
348 lines (304 loc) · 12 KB
/
reader.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import numpy as np
import datetime
import glob
import random
class Reader:
def __init__(self, fp_logfile, debug_mode):
self.debug_mode = debug_mode
file_count = 0
x_train = []
y_train = []
x_test = []
y_test = []
rootDir = './nProbe/dumpedited'
lineBuff = ""
lineBuff_lenprv = -1
c = 0
for dirName, subdirList, fileList in os.walk(rootDir):
for fname in fileList:
file_count += 1
file_data = open(dirName + '/' + fname, 'r')
file_data.readline() #read away the first line (title)
classType = "LEGIT" #0
if dirName.endswith("PINGATT") or fname.endswith("PINGATT"):
classType = "PINGATT" #1
elif dirName.endswith("SYNATT") or fname.endswith("SYNATT"):
classType = "SYNATT" #2
elif dirName.endswith("UDPATT") or fname.endswith("UDPATT"):
classType = "UDPATT" #3
elif dirName.endswith("PORTSC") or fname.endswith("PORTSC"):
classType = "PORTSC" #4
while True:
c = (c + 1) % 4 # Write every 4th packet to the test array, all the others to the train array
line = file_data.readline()
if not line:
break
if (c != 0):
lineBuff = [np.uint16(s) for s in line.replace('.','|').split('|')]
if lineBuff_lenprv != -1 and len(lineBuff) != lineBuff_lenprv:
print("error with input data")
exit(0)
else:
lineBuff_lenprv = len(lineBuff)
x_train.append(lineBuff)
if classType=="LEGIT":
y_train.append(0)
elif classType=="PINGATT":
y_train.append(1)
elif classType=="SYNATT":
y_train.append(2)
elif classType=="UDPATT":
y_train.append(3)
elif classType=="PORTSC":
y_train.append(4)
else:
lineBuff = [np.uint16(s) for s in line.replace('.','|').split('|')]
if lineBuff_lenprv != -1 and len(lineBuff) != lineBuff_lenprv:
print("error with input data")
exit(0)
else:
lineBuff_lenprv = len(lineBuff)
x_test.append(lineBuff)
if classType=="LEGIT":
y_test.append(0)
elif classType=="PINGATT":
y_test.append(1)
elif classType=="SYNATT":
y_test.append(2)
elif classType=="UDPATT":
y_test.append(3)
elif classType=="PORTSC":
y_test.append(4)
file_data.close()
self.x_train = np.array(x_train)
self.y_train = np.array(y_train)
self.x_test = np.array(x_test)
self.y_test = np.array(y_test)
self.x_trainNorm = self.normalize(self.x_train)
self.x_testNorm = self.normalize(self.x_test)
assert (self.x_train.shape[1] == self.x_test.shape[1]), "THERE WAS AN ERROR IN THE CODE READING THE DATA"
assert (self.x_train.shape[0] == self.y_train.shape[0]), "THERE WAS AN ERROR IN THE CODE READING THE DATA"
assert (self.x_test.shape[0] == self.y_test.shape[0]), "THERE WAS AN ERROR IN THE CODE READING THE DATA"
if self.debug_mode:
st = str(datetime.datetime.now()) + ': Finished formating the input arrays.\nOpened ' + str(file_count) + ' files.\nSizes are: x_train=' + str(self.x_train.shape) +' y_train=' + str(self.y_train.shape) + ' x_test=' + str(self.x_test.shape) + ' y_test=' + str(self.y_test.shape)
print(st)
fp_logfile.write(st + '\n')
print('DATA PREVIEW:\n')
self.previewData()
@staticmethod
def write_it(x, y, fp, label):
line = fp.readline()
#assert(not not line, "ERROR")
lineBuff = [np.uint16(s) for s in line.replace('.','|').split('|')]
x.append(lineBuff)
y.append(label)
@staticmethod
def getDataset(DataSetType):
# DataSetType: 1: Legitimate + Ping Flood
# 2: Legitimate + SYN FLOOD
# 3: Legitimate + UDP FLOOD
# 4: Legitimate + PORTSCAN
# 5: Legitimate + PORTSCAN + SYN FLOOD
x_train = []
y_train = []
x_test = []
y_test = []
LegitFile = './nProbe/dumpedited/edited'
PingAttFile = './nProbe/dumpedited/editedPINGATT'
SYNATTFile = './nProbe/dumpedited/editedSYNATT'
UDPATTFile = './nProbe/dumpedited/editedUDPATT'
PortScanFile = './nProbe/dumpedited/editedPORTSC'
if DataSetType == 0:
fp_leg = open(LegitFile, 'r')
fp_leg.readline() #read away the first line (title)
fp_attPING = open(PingAttFile, 'r')
fp_attPING.readline() #read away the first line (title)
fp_attSYN = open(SYNATTFile, 'r')
fp_attSYN.readline()
fp_attUDP = open(UDPATTFile, 'r')
fp_attUDP.readline()
fp_attPORTSC = open(PortScanFile, 'r')
fp_attPORTSC.readline()
#Train Dataset: 10 legitimate and 10 attack samples, loop 3,000 times.
for i in range(3000):
for j in range(random.choice([10,20,50,100])):
Reader.write_it(x_train, y_train, fp_leg, 0)
for j in range(random.choice([2,5,10,20])):
Reader.write_it(x_train, y_train, fp_attPING, 1)
for j in range(random.choice([2,5,10,20])):
Reader.write_it(x_train, y_train, fp_attSYN, 2)
for j in range(random.choice([2,5,10,20])):
Reader.write_it(x_train, y_train, fp_attUDP, 3)
for j in range(random.choice([2,5,10,20])):
Reader.write_it(x_train, y_train, fp_attPORTSC, 4)
#Test Dataset: 10 legitimate and 10 attack samples, loop 900 times.
for i in range(900):
for j in range(random.choice([10,20,50,100])):
Reader.write_it(x_test, y_test, fp_leg, 0)
for j in range(random.choice([2,5,10,20])):
Reader.write_it(x_test, y_test, fp_attPING, 1)
for j in range(random.choice([2,5,10,20])):
Reader.write_it(x_test, y_test, fp_attSYN, 2)
for j in range(random.choice([2,5,10,20])):
Reader.write_it(x_test, y_test, fp_attUDP, 3)
for j in range(random.choice([2,5,10,20])):
Reader.write_it(x_test, y_test, fp_attPORTSC, 4)
elif DataSetType == 1 or DataSetType == 2 or DataSetType == 3:
fp_leg = open(LegitFile, 'r')
fp_leg.readline() #read away the first line (title)
fp_att = open(SYNATTFile, 'r')
if DataSetType == 3:
fp_att.close()
fp_att = open(UDPATTFile, 'r')
elif DataSetType == 1:
fp_att.close()
fp_att = open(PingAttFile, 'r')
fp_att.readline() #read away the first line (title)
#Train Dataset: 10 legitimate and 10 attack samples, loop 30,000 times.
#TOT 300,000 leg, 300,000 att.
for i in range(3000): ############ CORRECTION: TOTAL 30,000 legitimate, 30,000 attack
for j in range(10):
Reader.write_it(x_train, y_train, fp_leg, 0)
for j in range(10):
Reader.write_it(x_train, y_train, fp_att, 1)
#Test Dataset: 10 legitimate and 10 attack samples, loop 9000 times.
#TOT 90,000 leg, 90,000 att
for i in range(9000):
for j in range(10):
Reader.write_it(x_test, y_test, fp_leg, 0)
for j in range(10):
Reader.write_it(x_test, y_test, fp_att, 1)
elif DataSetType == 4:
fp_leg = open(LegitFile, 'r')
fp_leg.readline() #read away the first line (title)
fp_att = open(PortScanFile, 'r')
fp_att.readline() #read away the first line (title)
#Train Dataset: 50 legitimate and 10 attack samples, loop 6000 times.
#TOT 300,000 leg, 60,000 att.
for i in range(6000):
for j in range(50):
Reader.write_it(x_train, y_train, fp_leg, 0)
for j in range(10):
Reader.write_it(x_train, y_train, fp_att, 1)
#Test Dataset: First 30000 leg. Then 1 legitimate and 10 attack samples, loop 2800 times. Then 30000 leg.
#TOT 62,800 leg, 28,000 att
for i in range(30000):
Reader.write_it(x_test, y_test, fp_leg, 0)
for i in range(2800):
for j in range(1):
Reader.write_it(x_test, y_test, fp_leg, 0)
for j in range(10):
Reader.write_it(x_test, y_test, fp_att, 1)
for i in range(30000):
Reader.write_it(x_test, y_test, fp_leg, 0)
elif DataSetType == 5:
fp_leg = open(LegitFile, 'r')
fp_leg.readline() #read away the first line (title)
fp_attPORTSC = open(PortScanFile, 'r')
fp_attPORTSC.readline() #read away the first line (title)
fp_attSYN = open(SYNATTFile, 'r')
fp_attSYN.readline() #read away the first line (title)
#Train Dataset: 50 legitimate and 10 SYN attack and 10 PORSTC attack samples, loop 6000 times.
#TOT 300,000 leg, 60,000 SYN att, 60,000 PORTSC att.
for i in range(6000):
for j in range(50):
Reader.write_it(x_train, y_train, fp_leg, 0)
for j in range(10):
Reader.write_it(x_train, y_train, fp_attSYN, 1)
for j in range(10):
Reader.write_it(x_train, y_train, fp_attPORTSC, 2)
#Test Dataset: First 30000 leg. Then 1 legitimate and 10 SYN attack and 10 PORTSC attack samples, loop 2800 times.
#Then 30,000 SYN attack, then 30,000 leg.
#TOT 62,800 leg, 58,000 SYN att, 28,000 PORTSC att
for i in range(30000):
Reader.write_it(x_test, y_test, fp_leg, 0)
for i in range(2800):
for j in range(1):
Reader.write_it(x_test, y_test, fp_leg, 0)
for j in range(10):
Reader.write_it(x_test, y_test, fp_attSYN, 1)
for j in range(10):
Reader.write_it(x_test, y_test, fp_attPORTSC, 2)
for j in range(30000):
Reader.write_it(x_test, y_test, fp_attSYN, 1)
for i in range(30000):
Reader.write_it(x_test, y_test, fp_leg, 0)
elif DataSetType == 6: #Παραλλαγή του 2
fp_leg = open(LegitFile, 'r')
fp_leg.readline() #read away the first line (title)
fp_att = open(SYNATTFile, 'r')
fp_att.readline() #read away the first line (title)
#Train Dataset:
for i in range(10000):
for j in range(random.choice([1,5,10,20])):
Reader.write_it(x_train, y_train, fp_leg, 0)
for j in range(random.choice([1,5,10,20])):
Reader.write_it(x_train, y_train, fp_att, 1)
#Test Dataset:
for i in range(9000):
for j in range(random.choice([1,5,10,20])):
Reader.write_it(x_test, y_test, fp_leg, 0)
for j in range(random.choice([1,5,10,20])):
Reader.write_it(x_test, y_test, fp_att, 1)
elif DataSetType == 100: #Final Test με input από το newcapture1, μόνο Legitimate κίνηση, μόνο test αρχεία
fp_leg = open('./final_test/dumpedited/editedLEGIT', 'r')
fp_leg.readline() #read away the first line (title)
#Test Dataset:
for i in range(62000):
Reader.write_it(x_test, y_test, fp_leg, 0)
Reader.write_it(x_train, y_train, fp_leg, 0)
x_train = np.array(x_train)
y_train = np.array(y_train)
x_test = np.array(x_test)
y_test = np.array(y_test)
x_trainNorm = Reader.Normalize(x_train)
x_testNorm = Reader.Normalize(x_test)
assert (x_train.shape[1] == x_test.shape[1]), "THERE WAS AN ERROR IN THE CODE READING THE DATA"
assert (x_train.shape[0] == y_train.shape[0]), "THERE WAS AN ERROR IN THE CODE READING THE DATA"
assert (x_test.shape[0] == y_test.shape[0]), "THERE WAS AN ERROR IN THE CODE READING THE DATA"
st = 'Finished formating the input arrays. Sizes are: x_train=' + str(x_train.shape) +' y_train=' + str(y_train.shape) + ' x_test=' + str(x_test.shape) + ' y_test=' + str(y_test.shape)
print(st)
return (x_trainNorm, y_train),(x_testNorm, y_test)
def previewData(self):
#st = ' y_train:' + str(self.y_train) + ' y_test:' + str(self.y_test)
#print(st)
print('x_train\n')
for i in range(5):
print('Sample ' + str(i) + ': ')
list = [self.x_train[i,j] for j in range(self.x_train.shape[1])]
print(list)
list = [self.x_trainNorm[i,j] for j in range(self.x_trainNorm.shape[1])]
print(list)
print('y_train: ' + str(self.y_train[i]))
def getData(self):
return (self.x_train, self.y_train),(self.x_test, self.y_test)
def getDataNormalized(self):
return (self.x_trainNorm, self.y_train),(self.x_testNorm, self.y_test)
def normalize(self, dataArray):
minima = np.array(np.amin(dataArray, axis = 0))
maxima = np.array(np.amax(dataArray, axis = 0))
rang = maxima - minima
rang[rang==0] = 1
return (dataArray - minima) / rang
@staticmethod
def Normalize(dataArray):
minima = np.array(np.amin(dataArray, axis = 0))
maxima = np.array(np.amax(dataArray, axis = 0))
rang = maxima - minima
rang[rang==0] = 1
return (dataArray - minima) / rang
@staticmethod
def getInputShape():
length = 0
rootDir='./nProbe/dumpedited'
for dirName, subdirList, fileList in os.walk(rootDir):
for fname in fileList:
file_data = open(dirName + '/' + fname, 'r')
line = file_data.readline()
if not line: break
line = file_data.readline()
if not line: break
length = len(line.replace('.','|').split('|'))
break
return length