-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_preprocess.py
More file actions
370 lines (263 loc) · 13.6 KB
/
data_preprocess.py
File metadata and controls
370 lines (263 loc) · 13.6 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
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
import os
import argparse
import math
import re
import pandas
import pandas as pd
from typing import List, Dict, Optional
from urllib import parse
def isnan(value):
try:
return math.isnan(value)
except:
return False
def preprocess_get_method(data: pandas.core.series.Series) -> pandas.core.series.Series:
"""
Preprocess a single row (Get Method) of IDS data sheet to divide each feature
Args:
data (pandas.core.series.Series) : single row of IDS data sheet
Retrun:
data (pandas.core.series.Series) : Divided features listed below.
'PAYLOAD', 'APP_PROTO', 'SRC_PORT', 'DST_PORT', 'IMPACT', 'RISK', 'JUDGEMENT',
'Method', 'Method-URL', 'HTTP', 'Host', 'User-Agent', 'Accept', 'Accept-Encoding',
'Accept-Language','Accept-Charset', 'Content-Type', 'Content-Length',
'Connection', 'Cookie', 'Upgrade-Insecure-Requests', 'Pragma',
'Cache-Control', and 'Body'
"""
payload = data['PAYLOAD']
if not ('GET' in payload):
raise ValueError('Invalid Payload ! (GET Method is not Found)')
data['Method'] = 'GET'
data['Method-URL'] = parse.unquote(payload.split('GET')[1].split(' HTTP/')[0])
if 'HTTP/' in payload:
data['HTTP'] = payload.split('HTTP/')[1].split('..')[0]
if 'Host: ' in payload:
data['Host'] = payload.split('Host: ')[1].split('..')[0]
if 'User-Agent: ' in payload:
data['User-Agent'] = payload.split('User-Agent: ')[1].split('..')[0]
if 'Accept: ' in payload:
data['Accept'] = payload.split('Accept: ')[1].split('..')[0]
if 'Accept-Encoding: ' in payload:
data['Accept-Encoding'] = payload.split('Accept-Encoding: ')[1].split('..')[0]
if 'Accept-Language: ' in payload:
data['Accept-Language'] = payload.split('Accept-Language: ')[1].split('..')[0]
if 'Accept-Charset: ' in payload:
data['Accept-Charset'] = payload.split('Accept-Charset: ')[1].split('..')[0]
if 'Content-Type: ' in payload:
data['Content-Type'] = payload.split('Content-Type: ')[1].split('..')[0]
if 'Content-Length: ' in payload:
data['Content-Length'] = payload.split('Content-Length: ')[1].split('..')[0]
if 'Connection: ' in payload:
data['Connection'] = payload.split('Connection: ')[1].split('..')[0]
if 'Cookie: ' in payload:
data['Cookie'] = payload.split('Cookie: ')[1].split('..')[0]
if 'Upgrade-Insecure-Requests: ' in payload:
data['Upgrade-Insecure-Requests'] = payload.split('Upgrade-Insecure-Requests: ')[1].split('..')[0]
if 'Pragma: ' in payload:
data['Pragma'] = payload.split('Pragma: ')[1].split('..')[0]
if 'Cache-Control: ' in payload:
data['Cache-Control'] = payload.split('Cache-Control: ')[1].split('..')[0]
data['Body'] = ''
return data
def preprocess_post_method(data: pandas.core.series.Series) -> pandas.core.series.Series:
"""
Preprocess a single row (Post Method) of IDS data sheet to divide each feature
Args:
data (pandas.core.series.Series) : single row of IDS data sheet
Retrun:
data (pandas.core.series.Series) : Divided features listed below.
'PAYLOAD', 'APP_PROTO', 'SRC_PORT', 'DST_PORT', 'IMPACT', 'RISK', 'JUDGEMENT',
'Method', 'Method-URL', 'HTTP', 'Host', 'User-Agent', 'Accept', 'Accept-Encoding',
'Accept-Language','Accept-Charset', 'Content-Type', 'Content-Length',
'Connection', 'Cookie', 'Upgrade-Insecure-Requests', 'Pragma',
'Cache-Control', and 'Body'
"""
payload = data['PAYLOAD']
if not ('POST' in payload):
raise ValueError('Invalid Payload ! (POST Method is not Found)')
data['Method'] = 'POST'
data['Method-URL'] = parse.unquote(payload.split('POST')[1].split(' HTTP/')[0])
if 'HTTP/' in payload:
data['HTTP'] = payload.split('HTTP/')[1].split('..')[0]
if 'Host: ' in payload:
data['Host'] = payload.split('Host: ')[1].split('..')[0]
if 'User-Agent: ' in payload:
data['User-Agent'] = payload.split('User-Agent: ')[1].split('..')[0]
if 'Accept: ' in payload:
data['Accept'] = payload.split('Accept: ')[1].split('..')[0]
if 'Accept-Encoding: ' in payload:
data['Accept-Encoding'] = payload.split('Accept-Encoding: ')[1].split('..')[0]
if 'Accept-Language: ' in payload:
data['Accept-Language'] = payload.split('Accept-Language: ')[1].split('..')[0]
if 'Accept-Charset: ' in payload:
data['Accept-Charset'] = payload.split('Accept-Charset: ')[1].split('..')[0]
if 'Content-Type: ' in payload:
data['Content-Type'] = payload.split('Content-Type: ')[1].split('..')[0]
if 'Content-Length: ' in payload:
data['Content-Length'] = payload.split('Content-Length: ')[1].split('..')[0]
if 'Connection: ' in payload:
data['Connection'] = payload.split('Connection: ')[1].split('..')[0]
if 'Cookie: ' in payload:
data['Cookie'] = payload.split('Cookie: ')[1].split('..')[0]
if 'Upgrade-Insecure-Requests: ' in payload:
data['Upgrade-Insecure-Requests'] = payload.split('Upgrade-Insecure-Requests: ')[1].split('..')[0]
if 'Pragma: ' in payload:
data['Pragma'] = payload.split('Pragma: ')[1].split('..')[0]
if 'Cache-Control: ' in payload:
data['Cache-Control'] = payload.split('Cache-Control: ')[1].split('..')[0]
data['Body'] = re.sub('[.]{2,}', '..', payload).split('..')[-1]
return data
def preprocess(args: argparse.ArgumentParser)-> None:
'''
Preprocess dataset with 2 steps
Step1. Decompose and extract components from full data
Step2. Combined all of components and convert to text and label
Args:
args.data_dir (str) : directory of csv dataset berfore the preprocessing
args.data_save_dir (str) : directory of csv dataset after the preprocessing
args.ignored_keywords (str) : keyword list to ignored data preprocessing
'''
pd.set_option('mode.chained_assignment', None) # Turn of the warning sign
# Step1. Decompose and extract components from full data
sheet = pd.read_excel(args.data_dir)[['PAYLOAD',
'APP_PROTO',
'SRC_PORT',
'DST_PORT',
'IMPACT',
'RISK',
'JUDGEMENT']]
sheet_analysis = {
'Post': [],
'Get': [],
'Ignored-App-Proto': [],
'Ignored-Keyword': [],
'Ignored-No-Method': [],
'Ignored-Error': []
}
ignored_keywords = [ik.strip() for ik in args.ignored_keywords.split(',')]
for i in range(len(sheet)):
try:
row = sheet.loc[i].copy()
# Protocol Filtering (Exclude if protocol is not http or https)
if not any(s in row['APP_PROTO'].lower() for s in ['http', 'https']):
sheet_analysis['Ignored-App-Proto'] += [row]
# Keyword Filtering (Exclude if payload has specific keywords)
elif any(s in row['PAYLOAD'] for s in ignored_keywords):
sheet_analysis['Ignored-Keyword'] += [row]
elif 'GET ' in row['PAYLOAD']:
row = preprocess_get_method(row)
sheet_analysis['Get'] += [row]
elif 'POST ' in row['PAYLOAD']:
row = preprocess_post_method(row)
sheet_analysis['Post'] += [row]
# No Method (Exclude if payload has no method such as 'POST' or 'GET')
else:
sheet_analysis['Ignored-No-Method'] += [row]
except:
# Error logging
sheet_analysis['Ignored-Error'] += [row]
pass
for key in sheet_analysis.keys():
if len(sheet_analysis[key]) == 0: continue
sheet_analysis[key] = pd.concat(sheet_analysis[key], axis=1).T
dataset_prep1 = pd.concat([sheet_analysis['Post'], sheet_analysis['Get']], axis=0)
dataset_prep1_dir = os.path.join(args.data_save_dir, 'dataset_prep1.csv')
dataset_prep1.to_csv(dataset_prep1_dir, sep=',', index = False, escapechar='\\')
print(f'<< Filename : {args.data_dir.split("/")[-1].split(".")[0]} >>\n')
print(f'ㅁ Number of Total data : {len(sheet)}\n')
print(f'ㅁ Number of GET data : {len(sheet_analysis["Get"])}\n')
print(f'ㅁ Number of POST data : {len(sheet_analysis["Post"])}\n')
print(f'ㅁ Number of INGORED data : {len(sheet_analysis["Ignored-App-Proto"])+len(sheet_analysis["Ignored-Keyword"])+len(sheet_analysis["Ignored-No-Method"])+len(sheet_analysis["Ignored-Error"])}')
print(f' ㅇ App-Proto : {len(sheet_analysis["Ignored-App-Proto"])}')
print(f' ㅇ Keyword : {len(sheet_analysis["Ignored-Keyword"])}')
print(f' ㅇ No-Method: {len(sheet_analysis["Ignored-No-Method"])}')
print(f' ㅇ Error: {len(sheet_analysis["Ignored-Error"])}\n\n')
# Step2. Combined all of components and convert to text and label
sheet = pd.read_csv(dataset_prep1_dir)[['APP_PROTO',
'SRC_PORT',
'DST_PORT',
'IMPACT',
'RISK',
'JUDGEMENT',
'Method',
'Method-URL',
'HTTP',
'Host',
'User-Agent',
'Accept',
'Accept-Encoding',
'Accept-Language',
'Accept-Charset',
'Content-Type',
'Content-Length',
'Connection',
'Cookie',
'Upgrade-Insecure-Requests',
'Pragma',
'Cache-Control',
'Body']]
sheet['text'] = ''
sheet['label'] = ''
for i in range(len(sheet)):
text = ''
row = sheet.loc[i].copy()
if not isnan(row['APP_PROTO']):
text += f'APP_PROTO: {row["APP_PROTO"]}\n'
if not isnan(row['SRC_PORT']):
text += f'SRC_PORT: {row["SRC_PORT"]}\n'
if not isnan(row['DST_PORT']):
text += f'DST_PORT: {row["DST_PORT"]}\n'
if not isnan(row['IMPACT']):
text += f'IMPACT: {row["IMPACT"]}\n'
if not isnan(row['RISK']):
text += f'RISK: {row["RISK"]}\n'
if not isnan(row['Method']):
text += f'Method: {row["Method"]}\n'
if not isnan(row['Method-URL']):
text += f'Method-URL: {row["Method-URL"]}\n'
if not isnan(row['HTTP']):
text += f'HTTP: {row["HTTP"]}\n'
if not isnan(row['Host']):
text += f'Host: {row["Host"]}\n'
if not isnan(row['User-Agent']):
text += f'User-Agent: {row["User-Agent"]}\n'
if not isnan(row['Accept']):
text += f'Accept: {row["Accept"]}\n'
if not isnan(row['Accept-Encoding']):
text += f'Accept-Encoding: {row["Accept-Encoding"]}\n'
if not isnan(row['Accept-Language']):
text += f'Accept-Language: {row["Accept-Language"]}\n'
if not isnan(row['Accept-Charset']):
text += f'Accept-Charset: {row["Accept-Charset"]}\n'
if not isnan(row['Content-Type']):
text += f'Content-Type: {row["Content-Type"]}\n'
if not isnan(row['Content-Length']):
text += f'Content-Length: {row["Content-Length"]}\n'
if not isnan(row['Connection']):
text += f'Connection: {row["Connection"]}\n'
if not isnan(row['Cookie']):
text += f'Cookie: {row["Cookie"]}\n'
if not isnan(row['Upgrade-Insecure-Requests']):
text += f'Upgrade-Insecure-Requests: {row["Upgrade-Insecure-Requests"]}\n'
if not isnan(row['Pragma']):
text += f'Pragma: {row["Pragma"]}\n'
if not isnan(row['Cache-Control']):
text += f'Cache-Control: {row["Cache-Control"]}\n'
if not isnan(row['Body']):
text += f'Body: {row["Body"]}\n'
label = int(int(row['JUDGEMENT'])>0)
sheet['text'][i] = text
sheet['label'][i] = label
dataset_prep2 = sheet[['text', 'label']]
dataset_prep2_dir = os.path.join(args.data_save_dir, 'dataset_prep2.csv')
dataset_prep2.to_csv(dataset_prep2_dir, sep=',', index = False, escapechar='\\')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", "-d", type=str, default="./dataset/dataset_org.xlsx",
help="Original dataset directory")
parser.add_argument("--data_save_dir", "-s", type=str, default="./dataset",
help="Preprocessed dataset save directory")
parser.add_argument("--ignored_keywords", "-i", type=str, default="Qualys, NCSC, BlueCoat",
help="If single data include the keyword, it is excluded while preprocessing")
args = parser.parse_args()
preprocess(args)