-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
feature_extraction.py
280 lines (237 loc) · 8.59 KB
/
feature_extraction.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
import logging
import math
from collections import Counter
from typing import Union
import click
import pandas as pd
import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import lit, col
from pyspark.sql.pandas.functions import pandas_udf
from utils import read_csv, patch_time_windows, init_local_spark
"""
Feature Extraction Class
"""
class FeatureExtractor:
def __init__(self, spark: SparkSession, df: pyspark.sql.dataframe, window_seconds: int = 3 * 60):
self.spark = spark
# patch time window
self.df = patch_time_windows(df=df, window_seconds=window_seconds)
# extract packet rate
self.df = (
self.df
.withColumn('packet_rate', col('packet') / col('duration'))
)
# extract packet rate
self.df = (
self.df
.withColumn('packet_rate', col('packet') / col('duration'))
)
# extract byte rate
self.df = (
self.df
.withColumn('byte_rate', col('num_of_bytes') / col('duration'))
)
# udf functions of extraction methods
self.extract_num_flow_udf = pandas_udf(self.extract_num_flow, 'double')
self.mean_udf = pandas_udf(self.mean, 'double')
self.std_udf = pandas_udf(self.std, 'double')
self.entropy_udf = pandas_udf(self.entropy, 'double')
self.port_proportion_udf = pandas_udf(self.port_proportion, 'array<double>')
self.build_label_udf = pandas_udf(self.build_label, 'string')
@staticmethod
def extract_num_flow(grouped_data: pd.Series) -> float:
"""
Extract number of flow
:param grouped_data: grouped data
:type grouped_data: pd.Series
:return: num_flow
:rtype: float
"""
return float(len(grouped_data))
@staticmethod
def mean(grouped_data: pd.Series) -> float:
"""
Extract mean of a given pandas Series
:param grouped_data: grouped data
:type grouped_data: pd.Series
:return: mean value
:rtype: float
"""
return grouped_data.mean()
@staticmethod
def std(grouped_data: pd.Series) -> float:
"""
Extract standard deviation of a given pandas Series
:param grouped_data: grouped data
:type grouped_data: pd.Series
:return: standard deviation value
:rtype: float
"""
return grouped_data.std()
@staticmethod
def entropy(grouped_data: pd.Series) -> float:
"""
Extract shannon entropy of a given pandas Series
:param grouped_data: grouped data
:type grouped_data: pd.Series
:return: entropy
:rtype: float
"""
ent = 0.0
if len(grouped_data) <= 1:
return ent
counter = Counter(grouped_data)
probs = [c / len(grouped_data) for c in counter.values()]
for p in probs:
if p > 0.0:
ent -= p * math.log2(p)
return ent
@staticmethod
def port_proportion(grouped_data: pd.Series) -> list:
"""
Extract port proportion of a given pandas Series
:param grouped_data: grouped data
:type grouped_data: pd.Series
:return: standard deviation value
:rtype: list of float
"""
common_port = {
20: 0, # FTP
21: 1, # FTP
22: 2, # SSH
23: 3, # Telnet
25: 4, # SMTP
50: 5, # IPSec
51: 6, # IPSec
53: 7, # DNS
67: 8, # DHCP
68: 9, # DHCP
69: 10, # TFTP
80: 11, # HTTP
110: 12, # POP3
119: 13, # NNTP
123: 14, # NTP
135: 15, # RPC
136: 16, # NetBios
137: 17, # NetBios
138: 18, # NetBios
139: 19, # NetBios
143: 20, # IMAP
161: 21, # SNMP
162: 22, # SNMP
389: 23, # LDAP
443: 24, # HTTPS
3389: 25, # RDP
}
proportion = [0.0] * (len(common_port) + 1)
for port in grouped_data:
idx = common_port.get(port)
if idx is None:
idx = -1
proportion[idx] += 1
proportion = [x / sum(proportion) for x in proportion]
return proportion
@staticmethod
def build_label(grouped_data: pd.Series) -> Union[str, None]:
"""
Build label from a given pandas Series
:param grouped_data: grouped data
:type grouped_data: pd.Series
:return: label
:rtype: str, None
"""
counter = Counter(grouped_data)
total = sum(counter.values())
half_total = 0.5 * total
for label, count in counter.items():
if count > half_total:
return label
return None
def extract_features(self) -> pyspark.sql.dataframe:
df = (
self.df
# group by src_ip and time_window as in paper
.groupby('time_window', 'src_ip')
# start extracting feature
.agg(
self.extract_num_flow_udf(lit(1)).alias('num_flow'),
self.mean_udf('duration').alias('mean_duration'),
self.mean_udf('packet').alias('mean_packet'),
self.mean_udf('num_of_bytes').alias('mean_num_of_bytes'),
self.mean_udf('packet_rate').alias('mean_packet_rate'),
self.mean_udf('byte_rate').alias('mean_byte_rate'),
self.std_udf('duration').alias('std_duration'),
self.std_udf('packet').alias('std_packet'),
self.std_udf('num_of_bytes').alias('std_num_of_bytes'),
self.std_udf('packet_rate').alias('std_packet_rate'),
self.std_udf('byte_rate').alias('std_byte_rate'),
self.entropy_udf('protocol').alias('entropy_protocol'),
self.entropy_udf('dst_ip').alias('entropy_dst_ip'),
self.entropy_udf('src_port').alias('entropy_src_port'),
self.entropy_udf('dst_port').alias('entropy_dst_port'),
self.entropy_udf('flags').alias('entropy_flags'),
self.port_proportion_udf('src_port').alias('proportion_src_port'),
self.port_proportion_udf('dst_port').alias('proportion_dst_port'),
self.build_label_udf('label').alias('label'),
)
# filter out num_flow < 10
.filter((col('num_flow') >= 10))
# sort by time window and source ip
.orderBy('time_window', 'src_ip')
# drop num_flow
.drop('num_flow')
# fill na
.na.fill(0.0)
)
return df
@click.command()
@click.option('--train', help='path to the directory containing train csv files', required=True)
@click.option('--test', help='path to the directory containing test csv files', required=True)
@click.option('--target_train', help='path to the directory to persist train features files', required=True)
@click.option('--target_test', help='path to the directory to persist test features files', required=True)
def main(train: str, test: str, target_train: str, target_test: str):
# initialise logger
logger = logging.getLogger(__file__)
logger.addHandler(logging.StreamHandler())
logger.setLevel('INFO')
logger.info('Initialising local spark')
spark = init_local_spark()
# processing train
logger.info('Processing train csv files')
logger.info('Read csv')
# read csv
df_train = read_csv(spark=spark, path=train)
logger.info('Extracting features...')
# extraction
df_train_extractor = FeatureExtractor(spark=spark, df=df_train)
df_train_feature = df_train_extractor.extract_features()
logger.info('Persisting...')
# persist
(
df_train_feature
.write
.mode('overwrite')
.parquet(target_train)
)
logger.info('Train csv extraction done')
# processing test
logger.info('Processing test csv files')
logger.info('Read csv')
# read csv
df_test = read_csv(spark=spark, path=test)
logger.info('Extracting features...')
# extraction
df_test_extractor = FeatureExtractor(spark=spark, df=df_test)
df_test_feature = df_test_extractor.extract_features()
logger.info('Persisting...')
# persist
(
df_test_feature
.write
.mode('overwrite')
.parquet(target_test)
)
logger.info('Test csv extraction done')
if __name__ == '__main__':
main()