Repository navigation
Expand file tree
/
Copy pathprepro.py
More file actions
113 lines (89 loc) · 2.78 KB
/
Copy pathprepro.py
File metadata and controls
113 lines (89 loc) · 2.78 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
import os
import time
from scapy.all import *
#from pcapng.scanner import FileScanner
import numpy as np
import os
import multiprocessing as mp
import pickle as pk
from utils import *
with open('objs/fileName2Application.pickle', 'rb') as f:
dict_name2label = pk.load(f)
with open('objs/fileName2Characterization.pickle', 'rb') as f:
dict_name2class = pk.load(f)
def pkts2X(pkts):
X = []
#lens = []
for p in pkts:
#===================================
# step 1 : remove Ether Header
#===================================
r = raw(p)[14:]
r = np.frombuffer(r, dtype = np.uint8)
#p.show()
#===================================
# step 2 : pad 0 to UDP Header
# it seems that we need to do nothing this step
# I found some length of raw data is larger than 1500
# remove them.
#===================================
if (TCP in p or UDP in p):
"""
if UDP in p:
# todo : padding 0 to
print ('UDP', r[:20])
print(p[IP].src, p[IP].dst)
else :
print('TCP', r[:20])
print(p[IP].src, p[IP].dst)
"""
if (len(r) > 1500):
pass
else:
X.append(r)
#lens.append(len(r))
else:
pass
return X#, lens
def get_data_by_file(filename):
pkts = rdpcap(filename)
X = pkts2X(pkts)
# save X to npy and delete the original pcap (it's too large).
return X
def task(filename):
global dict_name2label
global counter
head, tail = os.path.split(filename)
cond1 = os.path.isfile(os.path.join('data', tail+'.pickle'))
cond2 = os.path.isfile(os.path.join('data', tail+'_class.pickle'))
if (cond1 and cond2):
with lock:
counter.value += 1
print('[{}] {}'.format(counter, filename))
return '#ALREADY#'
X = get_data_by_file(filename)
if (not cond1):
y = [dict_name2label[tail]] * len(X)
with open(os.path.join('data', tail+'.pickle'), 'wb') as f:
pk.dump((X, y), f)
if (not cond2):
y2 = [dict_name2class[tail]] * len(X)
with open(os.path.join('data', tail+'_class.pickle'), 'wb') as f:
pk.dump(y2, f)
with lock:
counter.value += 1
print('[{}] {}'.format(counter, filename))
return 'Done'
#=========================================
# mp init
#=========================================
lock = mp.Lock()
counter = mp.Value('i', 0)
cpus = mp.cpu_count()//2
pool = mp.Pool(processes=cpus)
todo_list = gen_todo_list('../pcaps')
#todo_list = todo_list[:3]
total_number = len(todo_list)
done_list = []
res = pool.map(task, todo_list)
print(len(res))