-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatefeatures.py
398 lines (338 loc) · 13.7 KB
/
generatefeatures.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from locale import currency
from os import read
from numpy.core.fromnumeric import cumprod
from pkg_resources import cleanup_resources
import pyodbc
from pickle import dump
from pandas import read_sql_query
from pandas import DataFrame
global genetated_dataset
global srv_src_data
# Initiliser les colonnes du dataset
df_columns = ['srcip','dstip', 'dur', 'proto', 'service', 'state', 'spkts', 'dpkts',
'sbytes', 'dbytes', 'sttl', 'dttl', 'sload', 'dload',
'sloss', 'dloss', 'sintpkt', 'dintpkt', 'sjit', 'djit', 'swin',
'stcpb', 'dtcpb', 'dwin', 'tcprtt', 'synack', 'ackdat', 'smeansz',
'dmeansz', 'trans_depth', 'res_bdy_len', 'ct_srv_src', 'ct_state_ttl',
'ct_dst_ltm', 'ct_src_dport_ltm', 'ct_dst_sport_ltm', 'ct_dst_src_ltm',
'is_ftp_login', 'ct_ftp_cmd', 'ct_flw_http_mthd', 'ct_src_ltm', 'ct_srv_dst', 'is_sm_ips_ports']
#Pour recuperer les donnees
query = """
SELECT srcip, dstip, sport, dsport, proto,
[state],sbytes, dbytes,sttl, dttl,
sloss, dloss, sload, dload, spkts,
dpkts, swin, dwin, stcpb, dtcpb,
smeansz, dmeansz, sjit, djit, stime,
ltime, sintpkt, dintpkt, tcprtt, synack,
ackdat,trans_depth, res_bdy_len, dur, method,
[user], [password], command
from dbo.flows
"""
def add_basic_features(cnxn):
#query for adding basic features to the dataset
# dur, proto, service, state, spkts, dpkts,
# sbytes, dbytes, sttl, dttl, sload, dload,
# sloss, dloss, sintpkt, dintpkt, sjit, djit, swin,
# stcpb, dtcpb, dwin, tcprtt, synack, ackdat, smeansz,
# dmeansz, trans_depth, res_bdy_len
query = """
SELECT *
from dbo.flows
"""
# Read sql query with pandas
SQL_Query = read_sql_query(query, cnxn)
#Generate the dataframe with pandas while reading the sql query
generated_dataset = DataFrame(SQL_Query, columns=df_columns)
generated_dataset.fillna(-1, inplace=True)
return generated_dataset
def set_is_sm_ips_ports(data_records):
is_sm_ips_ports = 0
sm_port_data = list()
for data_record in data_records:
srcip = data_record.srcip
dstip = data_record.dstip
sport = data_record.sport
dport = data_record.dsport
if(srcip == dstip and sport == dport):
is_sm_ips_ports = 1
else:
is_sm_ips_ports = 0
sm_port_data.append(is_sm_ips_ports)
generated_dataset["is_sm_ips_ports"] = sm_port_data
def set_ct_state_ttl(data_records):
ct_state_ttl = 0
ttl_data = list()
for data_record in data_records:
#Recuperer les attributs
sttl = data_record.sttl
dttl = data_record.dttl
state = data_record.state
if(sttl in [62,63,254,255] and dttl in [252, 253] and state == "FIN"):
ct_state_ttl = 1
elif(sttl in [0,62,254] and dttl == 0 and state == "INT"):
ct_state_ttl = 2
elif(sttl in [62,254] and dttl in [60,252,253] and state == "CON"):
ct_state_ttl = 3
elif(sttl == 254 and dttl == 252 and state == "ACC"):
ct_state_ttl = 4
elif(sttl == 254 and dttl == 252 and state == "CLO"):
ct_state_ttl = 5
elif(sttl == 254 and dttl == 0 and state == "REQ"):
ct_state_ttl = 6
else:
ct_state_ttl = 0
ttl_data.append(ct_state_ttl)
generated_dataset["ct_state_ttl"] = ttl_data
def set_ct_flw_http_mthd(cnxn):
http_data = list()
ct_flw_http_mthd = 0
http_query = """select method from dbo.flows"""
cursor = cnxn.cursor()
cursor.execute(http_query)
curr_method = cursor.fetchone()
#fetchone retourne un tuple donc currmethod[0]
while curr_method:
next_method = cursor.fetchone()
if ((curr_method == next_method) and curr_method[0] is not None):
ct_flw_http_mthd +=1
else:
ct_flw_http_mthd = 0
curr_method = next_method
http_data.append(ct_flw_http_mthd)
generated_dataset["ct_flw_http_mthd"] = http_data
cursor.close()
def set_is_ftp_login(data_records):
is_ftp_login = 0
ftp_login_data = list()
for data_record in data_records:
service = data_record[13]
user = data_record.user
password = data_record.password
if (service == "ftp" and user != "" and password != ""):
is_ftp_login = 1
else:
is_ftp_login = 0
ftp_login_data.append(is_ftp_login)
generated_dataset["is_ftp_login"] = ftp_login_data
def set_ct_ftp_cmd(cnxn):
ct_ftp_cmd = 0
ftp_cmd_data = list()
query = """ select command from flows """
cursor = cnxn.cursor()
cursor.execute(query)
curr_ftp_cmd = cursor.fetchone()
while curr_ftp_cmd:
next_ftp_cmd = cursor.fetchone()
if((curr_ftp_cmd == next_ftp_cmd) and (curr_ftp_cmd != "" and next_ftp_cmd != "")):
ct_ftp_cmd = 1
else:
ct_ftp_cmd = 0
curr_ftp_cmd = next_ftp_cmd
ftp_cmd_data.append(ct_ftp_cmd)
generated_dataset["ct_ftp_cmd"] = ftp_cmd_data
cursor.close()
def set_ct_srv_src(cnxn):
ct_srv_src = 0
srv_src_data = list()
query = """ select dur,srcip, service from flows"""
cursor = cnxn.cursor()
cursor.execute(query)
# data_records = cursor.fetchall()
record_chunk100 = list()
record_chunk100 = cursor.fetchmany(100)
while record_chunk100:
# fetch 100 records from all the records
for index, data_record in enumerate(record_chunk100):
#compare current srv_src with the next srv_src
if index < len(record_chunk100) - 1:
if (data_record == record_chunk100[index + 1]):
ct_srv_src += 1
elif (data_record != record_chunk100[index + 1]):
ct_srv_src = 1
else:
ct_srv_src = 0
srv_src_data.append(ct_srv_src)
srv_src_data.append(srv_src_data[-1])
record_chunk100 = cursor.fetchmany(100)
generated_dataset["ct_srv_src"] = srv_src_data
cursor.close()
def set_ct_srv_dst(cnxn):
ct_srv_dst = 0
srv_dst_data = list()
query = """ select dstip, service from flows"""
cursor = cnxn.cursor()
cursor.execute(query)
record_chunk100 = list()
record_chunk100 = cursor.fetchmany(100)
while record_chunk100:
# fetch 100 records from all the records
for index, data_record in enumerate(record_chunk100):
#compare current srv_src with the next srv_src
if index < len(record_chunk100) - 1:
if (data_record == record_chunk100[index + 1]):
ct_srv_dst += 1
elif (data_record != record_chunk100[index + 1]):
ct_srv_dst = 1
else:
ct_srv_dst = 0
srv_dst_data.append(ct_srv_dst)
srv_dst_data.append(srv_dst_data[-1])
record_chunk100 = cursor.fetchmany(100)
generated_dataset["ct_srv_dst"] = srv_dst_data
cursor.close()
def set_ct_dst_ltm(cnxn):
ct_dst_ltm = 0
dst_ltm_data = list()
query = """ select dstip, DATEPART(MILLISECOND,ltime) from flows"""
cursor = cnxn.cursor()
cursor.execute(query)
record_chunk100 = list()
record_chunk100 = cursor.fetchmany(100)
while record_chunk100:
# fetch 100 records from all the records
for index, data_record in enumerate(record_chunk100):
#compare current srv_src with the next srv_src
if index < len(record_chunk100) - 1:
if (data_record == record_chunk100[index + 1]):
ct_dst_ltm += 1
elif (data_record != record_chunk100[index + 1]):
ct_dst_ltm = 1
else:
ct_dst_ltm = 0
dst_ltm_data.append(ct_dst_ltm)
dst_ltm_data.append(dst_ltm_data[-1])
record_chunk100 = cursor.fetchmany(100)
generated_dataset["ct_dst_ltm"] = dst_ltm_data
cursor.close()
print(len(dst_ltm_data))
generated_dataset["ct_dst_ltm"] = dst_ltm_data
def set_ct_src_ltm(cnxn):
ct_src_ltm = 0
src_ltm_data = list()
query = """ select srcip, DATEPART(MILLISECOND,ltime) from flows"""
cursor = cnxn.cursor()
cursor.execute(query)
record_chunk100 = list()
record_chunk100 = cursor.fetchmany(100)
while record_chunk100:
# fetch 100 records from all the records
for index, data_record in enumerate(record_chunk100):
#compare current srv_src with the next srv_src
if index < len(record_chunk100) - 1:
if (data_record == record_chunk100[index + 1]):
ct_src_ltm += 1
elif (data_record != record_chunk100[index + 1]):
ct_src_ltm = 1
else:
ct_src_ltm = 0
src_ltm_data.append(ct_src_ltm)
src_ltm_data.append(src_ltm_data[-1])
record_chunk100 = cursor.fetchmany(100)
generated_dataset["ct_src_ltm"] = src_ltm_data
cursor.close()
def set_ct_src_dport_ltm(cnxn):
ct_src_dport_ltm = 0
src_dport_ltm_data = list()
query = """ select srcip ,dsport,DATEPART(MILLISECOND,ltime) from flows"""
cursor = cnxn.cursor()
cursor.execute(query)
record_chunk100 = list()
record_chunk100 = cursor.fetchmany(100)
while record_chunk100:
# fetch 100 records from all the records
for index, data_record in enumerate(record_chunk100):
#compare current srv_src with the next srv_src
if index < len(record_chunk100) - 1:
if (data_record == record_chunk100[index + 1]):
ct_src_dport_ltm += 1
elif (data_record != record_chunk100[index + 1]):
ct_src_dport_ltm = 1
else:
ct_src_dport_ltm = 0
src_dport_ltm_data.append(ct_src_dport_ltm)
src_dport_ltm_data.append(src_dport_ltm_data[-1])
record_chunk100 = cursor.fetchmany(100)
generated_dataset["ct_src_dport_ltm"] = src_dport_ltm_data
cursor.close()
def set_ct_dst_sport_ltm(cnxn):
ct_dst_sport_ltm = 0
dst_sport_ltm_data = list()
# Pour recuperer l'@ ip et le port et les millisecondes de ltime
query = """ select dstip ,sport,DATEPART(MILLISECOND,ltime) from flows"""
cursor = cnxn.cursor()
cursor.execute(query)
record_chunk100 = list()
record_chunk100 = cursor.fetchmany(100)
while record_chunk100:
# fetch 100 records from all the records
for index, data_record in enumerate(record_chunk100):
#compare current srv_src with the next srv_src
if index < len(record_chunk100) - 1:
if (data_record == record_chunk100[index + 1]):
ct_dst_sport_ltm += 1
elif (data_record != record_chunk100[index + 1]):
ct_dst_sport_ltm = 1
else:
ct_dst_sport_ltm = 0
dst_sport_ltm_data.append(ct_dst_sport_ltm)
dst_sport_ltm_data.append(dst_sport_ltm_data[-1])
record_chunk100 = cursor.fetchmany(100)
#Ajouter la colonne dans le dataset
generated_dataset["ct_dst_sport_ltm"] = dst_sport_ltm_data
cursor.close()
def set_ct_dst_src_ltm(cnxn):
ct_dst_src_ltm = 0
dst_src_ltm_data = list()
# Pour recuperer l'@ ip et le port et les millisecondes de ltime
query = """ select srcip,dstip,DATEPART(MILLISECOND,ltime) from flows"""
cursor = cnxn.cursor()
cursor.execute(query)
record_chunk100 = list()
record_chunk100 = cursor.fetchmany(100)
while record_chunk100:
# fetch 100 records from all the records
for index, data_record in enumerate(record_chunk100):
#compare current srv_src with the next srv_src
if index < len(record_chunk100) - 1:
if (data_record == record_chunk100[index + 1]):
ct_dst_src_ltm += 1
elif (data_record != record_chunk100[index + 1]):
ct_dst_src_ltm = 1
else:
ct_dst_src_ltm = 0
dst_src_ltm_data.append(ct_dst_src_ltm)
dst_src_ltm_data.append(dst_src_ltm_data[-1])
record_chunk100 = cursor.fetchmany(100)
#Ajouter la colonne dans le dataset
generated_dataset["ct_dst_src_ltm"] = dst_src_ltm_data
cursor.close()
if __name__ == "__main__":
# #Database information
server = 'localhost'
database = 'networkdata'
username = 'SA'
password = '@123admin'
#Initialize the dataframe
generated_dataset = DataFrame(columns=df_columns)
# Connect to the database
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
# recuperer les donnees de la BDD
data_records = cursor.execute(query).fetchall()
generated_dataset = add_basic_features(cnxn)
set_is_sm_ips_ports(data_records)
set_ct_state_ttl(data_records)
set_ct_flw_http_mthd(cnxn)
set_is_ftp_login(data_records)
set_ct_ftp_cmd(cnxn)
set_ct_srv_src(cnxn)
set_ct_srv_dst(cnxn)
set_ct_dst_ltm(cnxn)
set_ct_src_ltm(cnxn)
set_ct_dst_sport_ltm(cnxn)
set_ct_src_dport_ltm(cnxn)
set_ct_dst_src_ltm(cnxn)
#Sauvgarder le dataset avec pickle
dump(generated_dataset, open("dataset3","wb"))
cnxn.close()
#cursor.close()