-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpreprocess.py
More file actions
377 lines (309 loc) · 10.7 KB
/
preprocess.py
File metadata and controls
377 lines (309 loc) · 10.7 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
371
372
373
374
375
376
377
import pickle
import random
from typing import Any, List, Union
import numpy as np
import pandas as pd
from scapy.all import sniff
from scapy.compat import raw
from scapy.layers.dns import DNS
from scapy.layers.inet import IP, TCP, UDP
from scapy.layers.l2 import Ether
from scapy.packet import Padding, Packet
from scipy import sparse
from sklearn.model_selection import train_test_split
import glob
# Traffic type labels
PREFIX_TO_TRAFFIC_ID = {
'chat': 0,
'email': 1,
'file_transfer': 2,
'streaming': 3,
'torrent': 4,
'voip': 5,
'vpn_chat': 6,
'vpn_email': 7,
'vpn_file_transfer': 8,
'vpn_streaming': 9,
'vpn_torrent': 10,
'vpn_voip': 11
}
# Application labels
PREFIX_TO_APP_ID = {
'aim_chat': 0,
'email': 1,
'facebook': 2,
'ftps': 3,
'icq': 4,
'gmail': 5,
'hangouts': 6,
'netflix': 7,
'scp': 8,
'sftp': 9,
'skype': 10,
'spotify': 11,
'torrent': 12,
'tor': 13,
'vimeo': 14,
'voipbuster': 15,
'youtube': 16
}
# Auxiliary task labels
AUX_ID = {
'all_chat': 0,
'all_email': 1,
'all_file_transfer': 2,
'all_streaming': 3,
'all_torrent': 4,
'all_voip': 5
}
def reduce_tcp(
packet: Packet,
n_bytes: int = 20
) -> Packet:
""" Reduce the size of TCP header to 20 bytes.
Args:
packet: Scapy packet.
n_bytes: Number of bytes to reserve.
Returns:
IP packet.
"""
if TCP in packet:
# Calculate the TCP header length
tcp_header_length = packet[TCP].dataofs * 32 / 8
# Check if the TCP header length is greater than 20 bytes
if tcp_header_length > n_bytes:
# Reduce the TCP header length to 20 bytes
packet[TCP].dataofs = 5 # 5 * 4 = 20 bytes
del packet[TCP].options # Remove any TCP options beyond the 20 bytes
# Recalculate the TCP checksum
del packet[TCP].chksum
del packet[IP].chksum
packet = packet.__class__(bytes(packet)) # Recreate the packet to recalculate checksums
# Display the modified packet
# print("Modified Packet:")
# print(packet.show())
return packet
def pad_udp(packet: Packet):
""" Pad the UDP header to 20 bytes with zero.
Args:
packet: Scapy packet.
Returns:
IP packet.
"""
if UDP in packet:
# Get layers after udp
layer_after = packet[UDP].payload.copy()
# Build a padding layer
pad = Padding()
pad.load = "\x00" * 12
# Concat the origin payload with padding layer
layer_before = packet.copy()
layer_before[UDP].remove_payload()
packet = layer_before / pad / layer_after
return packet
def packet_to_sparse_array(
packet: Packet,
max_length: int = 1500
) -> sparse.csr_matrix:
""" Normalize the byte string and convert to sparse matrix
Args:
packet: Scapy packet.
max_length: Max packet length
Returns:
Sparse matrix.
"""
arr = np.frombuffer(raw(packet), dtype=np.uint8)[0:max_length] / 255
if len(arr) < max_length:
pad_width = max_length - len(arr)
arr = np.pad(arr, pad_width=(0, pad_width), constant_values=0)
arr = sparse.csr_matrix(arr, dtype=np.float32)
return arr
def filter_packet(pkt: Packet):
""" Filter packet approach following MTC author.
Args:
pkt: Scapy packet.
Returns:
Scapy packet if pass all filtering rules. Or `None`.
"""
# eliminate Ethernet header with the physical layer information
if Ether in pkt:
# print('Ethernet header in packet')
pkt = pkt[Ether].payload
else:
# print('Ethernet header not in packet')
pass
# IP header was changed to 0.0.0.0
if IP in pkt:
# print('IP header in packet')
pkt[IP].src = "0.0.0.0"
pkt[IP].dst = "0.0.0.0"
# print(pkt[IP].src, pkt[IP].dst, 'after modification')
else:
# print('IP header not in packet')
return None
if TCP in pkt:
# print('TCP header in packet')
# print(f'Len of TCP packet: {len(pkt[TCP])}, payload: {len(pkt[TCP].payload)}')
pkt = reduce_tcp(pkt)
# print(f'Len of TCP packet: {len(pkt[TCP])}, payload: {len(pkt[TCP].payload)} after reducing')
elif UDP in pkt:
# print('UDP header in packet')
# print(f'Len of UDP packet: {len(pkt[UDP])}, payload: {len(pkt[UDP].payload)}')
pkt = pad_udp(pkt)
# print(f'Len of UDP packet: {len(pkt[UDP])}, payload: {len(pkt[UDP].payload)} after padding')
else:
return None
# Pre-define TCP flags
FIN = 0x01
SYN = 0x02
RST = 0x04
PSH = 0x08
ACK = 0x10
URG = 0x20
ECE = 0x40
CWR = 0x80
# Parsing transport layer protocols using Scapy
# Checking if it is an IP packet
if IP in pkt:
# Obtaining data from the IP layer
ip_packet = pkt[IP]
# If it is a TCP protocol
if TCP in ip_packet:
# Obtaining data from the TCP layer
tcp_packet = ip_packet[TCP]
# Checking for ACK, SYN, FIN flags
if tcp_packet.flags & 0x16 in [ACK, SYN, FIN]:
# print('TCP has ACK, SYN, and FIN packets')
# print(pkt)
# Returning None (or an empty packet b'')
return None
# If it is a UDP protocol
elif UDP in ip_packet:
# Obtaining data from the UDP layer
udp_packet = ip_packet[UDP]
# Checking for DNS protocol (assuming the value is 53)
if udp_packet.dport == 53 or udp_packet.sport == 53 or DNS in pkt:
# print('UDP has DNS packets')
# print(pkt)
# Returning None (or an empty packet b'')
return None
else:
# Not a TCP or UDP packet
return None
# Valid packet
return pkt
else:
# Not an IP packet
return None
def split_data(
data: List[sparse.csr_matrix],
train_ratio: float = 0.64,
val_ratio: float = 0.16,
test_ratio: float = 0.20,
random_state: int = 42
) -> Union[Any, Any, Any]:
""" Split data to training, validation and testing data.
Args:
data: List of features.
train_ratio: The ratio of training data.
val_ratio: The ratio of validation data.
test_ratio: The ratio of testing data.
random_state: Random seed.
Returns:
None.
"""
# Check if the ratios add up to 1
assert train_ratio + val_ratio + test_ratio == 1, "Ratios should add up to 1"
# Splitting into temporary train and test sets
train_val, test = train_test_split(data, test_size=test_ratio, random_state=random_state)
# Calculate the ratios for train and validation sets based on the remaining data after test split
remaining_ratio = 1 - test_ratio
# train_ratio_adjusted = train_ratio / remaining_ratio
val_ratio_adjusted = val_ratio / remaining_ratio
# Split the remaining data into train and validation sets
train, val = train_test_split(train_val, test_size=val_ratio_adjusted, random_state=random_state)
return train, val, test
def preprocess_data(
annotation_csv: str = 'data/annotation.csv',
pcap_dir: str = 'pcaps',
limited_count: int = None
):
""" Perform data preprocessing
Args:
annotation_csv: Annotation file with CSV format which map the *.pcap* file names and labels of the three tasks.
pcap_dir: The folder stored *.pcap* files.
limited_count: Limited amount of record to fetch from *.pcap* files.
Returns:
"""
# Load annotation file
annotation_df = pd.read_csv(annotation_csv)
# Group applications to filter data for each class
g = annotation_df.groupby('application_class')
for group in g.groups:
print(f'Filter `{group}` application class data')
df = g.get_group(group)
data_rows = []
# Parse each *.pcap* file from each application class
for pcap_f_name, traffic, app, aux in df.to_records(False).tolist():
print(f'Load file: {pcap_f_name}')
pkt_arrays = []
# Callback function for sniffing
def method_filter(pkt):
# Eliminate Ethernet header with the physical layer information
pkt = filter_packet(pkt)
# A valid packet would be returned
if pkt is not None:
# Convert to sparse matrix
ary = packet_to_sparse_array(pkt)
pkt_arrays.append(ary)
# Limit the number of data for testing
# Hint: `sniff` is a better way to parse *.pcap* files for saving memory, comparing to `rdpcap`
if limited_count:
sniff(offline=f'{pcap_dir}/{pcap_f_name}', prn=method_filter, store=0, count=10)
else:
sniff(offline=f'{pcap_dir}/{pcap_f_name}', prn=method_filter, store=0)
# Concat feature and labels
for array in pkt_arrays:
row = {
"app_label": PREFIX_TO_APP_ID.get(app),
"traffic_label": PREFIX_TO_TRAFFIC_ID.get(traffic),
"aux_label": AUX_ID.get(aux),
"feature": array
}
data_rows.append(row)
# Release memory
del pkt_arrays
print(f'Save `{group}` application class data with {len(data_rows)} rows')
# Save a preprocessed data to pickle file
with open(f'data/data_rows_{group}.pkl', 'wb') as f:
pickle.dump(data_rows, f)
# Release memory
del data_rows
def collect_data(n: int = 50000):
"""Collect data from each application class with down-sampling"""
sampled_data_rows = []
# Load per class data from pickle files
data_rows_files = glob.glob('data_rows_*')
for file in data_rows_files:
with open(file, 'rb') as f:
data_rows = pickle.load(f)
# Shuffle data
for _ in range(10):
random.shuffle(data_rows)
data_rows = data_rows[:n]
sampled_data_rows += data_rows
# Shuffle data
for _ in range(10):
random.shuffle(sampled_data_rows)
# Split data to training, validation and testing data
train_data, val_data, test_data = split_data(sampled_data_rows)
# Store preprocessed adn splited data to pickle files
with open('data/train_data_rows.pkl', 'wb') as f:
pickle.dump(train_data, f)
with open('data/val_data_rows.pkl', 'wb') as f:
pickle.dump(val_data, f)
with open('data/test_data_rows.pkl', 'wb') as f:
pickle.dump(test_data, f)
if __name__ == "__main__":
preprocess_data()
collect_data()