-
Notifications
You must be signed in to change notification settings - Fork 9
/
nndpi_preprocessing.py
392 lines (316 loc) · 12.7 KB
/
nndpi_preprocessing.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
import argparse
import fnmatch
import os
from multiprocessing import Pool
import dpkt
import numpy as np
import pandas as pd
from tqdm import tqdm
LINKTYPE_ETHERNET = 1
LINKTYPE_RAW = 101
def get_tags(file):
"""
Used to get the 4 tags from filenames
Parameters:
file (str): a string of the actual filename
Returns:
(tuple):
tag1 (str): Category of packets.
tag2 (str): Application/Protocol used by packets, same as tag1 if no application is defined.
tag3 (str): Category and Application used by packets. same as tag2 if no categories defined per the application.
tag4 (str): VPN or Non-VPN + tag1
"""
file = os.path.basename(file)
if "vpn" in file:
vpn = "vpn"
file_ = file.replace("vpn_", "")
else:
vpn = "non_vpn"
file_ = file
if fnmatch.fnmatch(file_, "AIMchat*.pca*") or fnmatch.fnmatch(
file_, "aim_chat*.pca*"
):
tag1, tag2, tag3 = ("chat", "aim", "aim_chat")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "email*.pca*"):
tag1, tag2, tag3 = ("email", "email", "email")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "facebook_audio*.pca*"):
tag1, tag2, tag3 = ("audio", "facebook", "facebook_audio")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "facebookchat*.pca*") or fnmatch.fnmatch(
file_, "facebook_chat*.pca*"
):
tag1, tag2, tag3 = ("chat", "facebook", "facebook_chat")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "facebook_video*.pca*"):
tag1, tag2, tag3 = ("video", "facebook", "facebook_video")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "ftps*.pca*"):
tag1, tag2, tag3 = ("file_transfer", "ftps", "ftps")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "gmailchat*.pca*"):
tag1, tag2, tag3 = ("chat", "gmail", "gmail_chat")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "hangouts_audio*.pca*"):
tag1, tag2, tag3 = ("audio", "hangouts", "hangouts_audio")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "hangouts_chat*.pca*") or fnmatch.fnmatch(
file_, "hangout_chat*.pca*"
):
tag1, tag2, tag3 = ("chat", "hangouts", "hangouts_chat")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "hangouts_video*.pca*"):
tag1, tag2, tag3 = ("video", "hangouts", "hangouts_video")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "ICQchat*.pca*") or fnmatch.fnmatch(
file_, "icq_chat*.pca*"
):
tag1, tag2, tag3 = ("chat", "icq", "icq_chat")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "netflix*.pca*"):
tag1, tag2, tag3 = ("video", "netflix", "netflix")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "scp*.pca*"):
tag1, tag2, tag3 = ("file_transfer", "scp", "scp")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "sftp*.pca*"):
tag1, tag2, tag3 = ("file_transfer", "sftp", "sftp")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "skype_audio*.pca*"):
tag1, tag2, tag3 = ("audio", "skype", "skype_audio")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "skype_chat*.pca*"):
tag1, tag2, tag3 = ("chat", "skype", "skype_chat")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "skype_file*.pca*"):
tag1, tag2, tag3 = ("file_transfer", "skype", "skype_file")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "skype_video*.pca*"):
tag1, tag2, tag3 = ("video", "skype", "skype_video")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "spotify*.pca*"):
tag1, tag2, tag3 = ("audio", "spotify", "spotify")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "torFacebook.pca*"):
tag1, tag2, tag3 = ("tor", "facebook", "tor_facebook")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "torGoogle.pca*"):
tag1, tag2, tag3 = ("tor", "google", "tor_google")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "torTwitter.pca*"):
tag1, tag2, tag3 = ("tor", "twitter", "tor_twitter")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "torVimeo*.pca*"):
tag1, tag2, tag3 = ("video", "vimeo", "tor_vimeo")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "torYoutube*.pca*"):
tag1, tag2, tag3 = ("video", "youtube", "tor_youtube")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "Torrent*.pca*") or fnmatch.fnmatch(
file_, "bittorrent*.pca*"
):
tag1, tag2, tag3 = ("P2P", "torrent", "torrent")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "vimeo*.pca*"):
tag1, tag2, tag3 = ("video", "vimeo", "vimeo")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "voipbuster*.pca*"):
tag1, tag2, tag3 = ("audio", "voipbuster", "voipbuster")
return (tag1, tag2, tag3, vpn + "_" + tag1)
elif fnmatch.fnmatch(file_, "youtube*.pca*"):
tag1, tag2, tag3 = ("video", "youtube", "youtube")
return (tag1, tag2, tag3, vpn + "_" + tag1)
else:
raise ValueError(
file,
"file with unproper labelling, please rename the file to fit one of the working tags",
)
def preprocess_pkts(filename, max_len=1500, normalized=False, return_df=False):
"""
Used to preprocess pcap and pcapng files from the dataset and saves it in feather format.
Parameters:
filename (str): Complete path to filename
max_len (int): Maximum Number of bytes in every packet.
Bigger packets will be trimmed, while
smaller ones will be padded.
default = 1500
normalized (bool): Whether to normalize packets or not.
Should be set to False in order to use
the embedding layer without tokenization.
default = False
Returns:
allpkts (pd.DataFrame): Dataframe containing processed packets.
"""
pcp = open(filename, mode="rb")
if filename.endswith(".pcapng"):
pkt_cat = dpkt.pcapng.Reader(pcp)
elif filename.endswith(".pcap"):
pkt_cat = dpkt.pcap.Reader(pcp)
else:
raise TypeError("Unsupported file type")
raw = pkt_cat.readpkts()
not_tcp_udp = []
pkts = []
protocol = []
pkt_ix = []
for i, pkt in enumerate(raw):
# Check if Ethernet-Packet or RAW-IP Packet
if pkt_cat.datalink() == LINKTYPE_ETHERNET:
eth = dpkt.ethernet.Ethernet(pkt[1])
if eth.type == dpkt.ethernet.ETH_TYPE_IP:
ip = eth.data
else:
continue
elif pkt_cat.datalink() == LINKTYPE_RAW:
ip = dpkt.ip.IP(pkt[1])
else:
continue
if ip.p not in [dpkt.ip.IP_PROTO_TCP, dpkt.ip.IP_PROTO_UDP]: # not tcp or udp
not_tcp_udp.append(i)
continue
# mask ips
ip.src = b"\x00\x00\x00\x00"
ip.dst = b"\x00\x00\x00\x00"
if ip.p == dpkt.ip.IP_PROTO_UDP: # udp
udp = ip.data
if type(udp) != dpkt.udp.UDP:
continue
try:
dpkt.dns.DNS(udp.data)
continue
except:
# pad to 20 byte
if udp.ulen - len(udp.data) == 8:
padded_udp_header = bytes(udp)[:8] + b"\x00" * 12
padded_udp_pkt = padded_udp_header + udp.data
ip.data = padded_udp_pkt
protocol.append("udp")
elif ip.p == dpkt.ip.IP_PROTO_TCP: # TCP
tcp = ip.data
if type(tcp) != dpkt.tcp.TCP:
continue
# Filter packets of 3-way handshake
fin_flag = (tcp.flags & dpkt.tcp.TH_FIN) != 0
syn_flag = (tcp.flags & dpkt.tcp.TH_SYN) != 0
ack_flag = (tcp.flags & dpkt.tcp.TH_ACK) != 0
if (syn_flag or ack_flag or fin_flag) and not tcp.data:
continue
protocol.append("tcp")
pkt_ix.append(i + 1)
# add bytes to make the packet reach 1500 bytes
ip_as_bytes = bytes(ip)
packet_len = len(ip_as_bytes)
remaining = max_len - packet_len
if remaining > 0:
ip_as_bytes = ip_as_bytes + b"\x00" * remaining
else:
ip_as_bytes = ip_as_bytes[:max_len]
if normalized:
afterdiv = np.array(bytearray(ip_as_bytes)) / 255
pkts.append(afterdiv)
else: # this means non normalized with max=255
pkts.append(np.array(bytearray(ip_as_bytes)))
if pkts:
allpkts = pd.DataFrame(np.vstack(pkts))
allpkts["protocol"] = protocol
allpkts["filename"] = filename.split("/")[-1]
allpkts["ix"] = pkt_ix
tags = get_tags(filename)
allpkts["tag_1"] = tags[0]
allpkts["tag_2"] = tags[1]
allpkts["tag_3"] = tags[2]
allpkts["tag_4"] = tags[3]
save_dir = "./ProcessedPackets/" + filename.split("/")[-1] + ".feather"
allpkts.columns = [str(col) for col in allpkts.columns]
allpkts.to_feather(save_dir)
if return_df:
return allpkts
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--n_jobs",
default=1,
type=int,
help="""Number of workers,
-1 to use all available cores""",
)
parser.add_argument(
"--pcap_dir", default="./CompletePCAPs/", type=str, help="Raw PCAP files dir"
)
parser.add_argument(
"--processed_pcap_dir",
default="./ProcessedPackets/",
type=str,
help="Processed PCAP files dir",
)
parser.add_argument(
"--max_len",
default=1500,
type=np.uint32,
help="Preprocessed packet length in bytes",
)
parser.add_argument(
"--one_df",
default=True,
type=bool,
help="Save processed packets in one .feather DataFrame",
)
args = parser.parse_args()
pcap_dir = args.pcap_dir
processed_pcap_dir = args.processed_pcap_dir
pkt_files = [
os.path.join(pcap_dir, file)
for file in os.listdir(pcap_dir)
if (file.endswith(".pcapng") or file.endswith(".pcap"))
]
processed_pkt_files = [
os.path.join(processed_pcap_dir, file)
for file in os.listdir(processed_pcap_dir)
if (file.endswith(".feather"))
]
to_process_pkts = [
pkt
for pkt in pkt_files
if pkt.split("/")[-1]
not in [
processed_pkt.split("/")[-1].rstrip(".feather")
for processed_pkt in processed_pkt_files
]
]
if args.n_jobs == 1:
use_threads = False
pbar = tqdm(to_process_pkts)
for filename in pbar:
pbar.set_description("Processing {}".format(filename))
allinfile = preprocess_pkts(
filename, max_len=args.max_len, normalized=False
)
# use multiple workers
# be careful--expensive RAM usage ~48GB
elif args.n_jobs == -1:
num_cores = os.cpu_count()
use_threads = True
pool = Pool(num_cores)
df = pd.concat(pool.map(preprocess_pkts, to_process_pkts))
elif args.n_jobs > 1:
num_cores = args.n_jobs
use_threads = True
pool = Pool(num_cores)
pool.map(preprocess_pkts, to_process_pkts)
else:
raise ValueError("n_jobs can not be negative")
# be careful--expensive RAM usage ~48GB
if args.one_df:
dfs = []
print("Combining all processed packets into a single DataFrame..")
processed_pkt_files = [
os.path.join(processed_pcap_dir, file)
for file in os.listdir(processed_pcap_dir)
if (file.endswith(".feather"))
]
if processed_pkt_files:
for file in tqdm(processed_pkt_files):
dfs.append(pd.read_feather(file, use_threads=use_threads))
dfs = pd.concat(dfs).reset_index(drop=True)
dfs.to_feather("./CombinedPackets/allpkts.feather")