-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocess.py
More file actions
523 lines (510 loc) · 20.9 KB
/
preprocess.py
File metadata and controls
523 lines (510 loc) · 20.9 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import json
from datetime import datetime
from datetime import timezone
import pytz
import timestampExtractor
import yaml
from abbrvs import get_short
from attacktimes import get_phase
import glob
def get_ts_label(label):
# Events in the AIT-LDSv2 sometimes have multiple labels; this function only returns a single label that we consider
# the most descriptive one
if len(label) == 1:
return label[0]
keywords = [
"wpscan",
"dirb",
"dnsteal",
"dns_scan",
"service_scan",
"attacker_change_user",
"escalated_sudo_command",
"webshell_cmd",
"crack_passwords",
]
for keyword in keywords:
if keyword in label:
return keyword
print("Unknown label: " + label)
def get_labels(scenario, aitlds_path, path):
# Return labels for log events
for glob_path in glob.glob(aitlds_path + "/" + scenario + "/labels" + path):
with (
open(glob_path) as label_file,
open(glob_path.replace("/labels/", "/gather/")) as log_file,
):
lines_labels = {}
for line in label_file:
j = json.loads(line)
lines_labels[j["line"]] = j["labels"]
final_labels = {}
line_count = 0
for line in log_file:
line = line.strip("\n\r ")
line_count += 1
if line_count in lines_labels:
if (
line in final_labels
and final_labels[line] != lines_labels[line_count]
):
print(
"Duplicated line with labels "
+ str(final_labels[line])
+ " and "
+ str(lines_labels[line_count])
+ ": "
+ line
)
final_labels[line] = lines_labels[line_count]
return final_labels
def netflow_label_mapping(label):
if label == "-":
return label
elif label in [
"browsing/update",
"benign_share",
"proxy",
"SSH",
"mail",
"update/command on unassigned port",
"DNS",
"monitoring",
"HTTP(S) intra",
"HTTP",
"HTTPS",
"HTTP(S) DMZ",
"broken flow - benign",
]:
# Normal behavior
return None
elif label == "data exfiltration":
return "dnsteal"
elif label == "online_cracking":
return "crack_passwords"
elif label == "service_scan":
return label
else:
print("Unknown label: " + label)
def get_netflows(scenario, aitnds_path):
# Return labels for netflows
# Requires that AIT-NDS files (tcp_complete.csv, tcp_nocomplete.csv, and udp_complete.csv) are available
with (
open(aitnds_path + "/" + scenario + "_netflows/tcp_complete.csv") as tcp_comp,
open(aitnds_path + "/" + scenario + "_netflows/tcp_nocomplete.csv") as tcp_no,
open(aitnds_path + "/" + scenario + "_netflows/udp_complete.csv") as udp,
):
header = True
cols = None
final_labels = {}
for line in udp:
line = line.strip("\n\r ")
if header is True:
header = False
cols = line.split(",")
continue
parts = line.split(",")
proto = "UDP"
cip = parts[cols.index("#c_ip:1")]
cport = parts[cols.index("c_port:2")]
srcip = parts[cols.index("s_ip:10")]
srcport = parts[cols.index("s_port:11")]
time = float(parts[cols.index("c_first_abs:3")])
while time > 10000000000:
time /= 10
label = parts[cols.index("label")]
if label == "data exfiltration":
if (proto, cip, cport, srcip, srcport) not in final_labels:
final_labels[(proto, cip, cport, srcip, srcport)] = [(time, label)]
else:
final_labels[(proto, cip, cport, srcip, srcport)].append(
(time, label)
)
header = True
cols = None
for line in tcp_comp:
line = line.strip("\n\r ")
if header is True:
header = False
cols = line.split(",")
continue
parts = line.split(",")
proto = "TCP"
cip = parts[cols.index("#15#c_ip:1")]
cport = parts[cols.index("c_port:2")]
srcip = parts[cols.index("s_ip:15")]
srcport = parts[cols.index("s_port:16")]
time = float(parts[cols.index("first:29")])
while time > 10000000000:
time /= 10
label = parts[cols.index("label")]
if (
label.startswith("check_")
or label.startswith("read_")
or label.startswith("list_")
):
label = "command"
if label not in [
"browsing/update",
"benign_share",
"proxy",
"SSH",
"mail",
"update/command on unassigned port",
"DNS",
"monitoring",
"HTTP(S) intra",
"HTTP",
"HTTPS",
"HTTP(S) DMZ",
]:
if (proto, cip, cport, srcip, srcport) not in final_labels:
final_labels[(proto, cip, cport, srcip, srcport)] = [(time, label)]
else:
final_labels[(proto, cip, cport, srcip, srcport)].append(
(time, label)
)
header = True
cols = None
for line in tcp_no:
line = line.strip("\n\r ")
if header is True:
header = False
cols = line.split(",")
continue
parts = line.split(",")
proto = "TCP"
cip = parts[cols.index("#15#c_ip:1")]
cport = parts[cols.index("c_port:2")]
srcip = parts[cols.index("s_ip:15")]
srcport = parts[cols.index("s_port:16")]
time = float(parts[cols.index("first:29")])
while time > 10000000000:
time /= 10
label = parts[cols.index("label")]
if label not in ["broken flow - benign"]:
if (proto, cip, cport, srcip, srcport) not in final_labels:
final_labels[(proto, cip, cport, srcip, srcport)] = [(time, label)]
else:
final_labels[(proto, cip, cport, srcip, srcport)].append(
(time, label)
)
return final_labels
scenarios = [
"russellmitchell",
"fox",
"harrison",
"santos",
"shaw",
"wardbeck",
"wheeler",
"wilson",
]
aitlds_path = "./aitldsv2"
aitnds_path = "./aitnds"
do_event_labeling = True
# If set to True, the log data set from the AIT-LDSv2 for the respective scenarios must be
# available in aitlds_path and the AIT-NDS must be available in aitnds_path
alertsfile = "alerts.json"
counts = {}
all_phases = set()
all_descriptions = set()
all_shorts = set()
with open(alertsfile, "w+") as alerts_file: # , open(tsfile, 'w+') as ts_file:
for scenario in scenarios:
print("Processing scenario " + scenario)
wazuhfile = scenario + "_wazuh.json"
aminerfile = scenario + "_aminer.json"
configfile = scenario + ".yaml"
alertscsv = "alerts_csv/" + scenario + "_alerts.csv"
if do_event_labeling:
print(" Reading labels...")
labels = {}
labels[("/var/log/dnsmasq.log", "inet-firewall")] = get_labels(
scenario, aitlds_path, "/inet-firewall/logs/dnsmasq.log"
)
labels[("/var/log/apache2/intranet-access.log", "intranet_server")] = (
get_labels(
scenario,
aitlds_path,
"/intranet_server/logs/apache2/intranet.*-access.log*",
)
)
labels[("/var/log/apache2/intranet-error.log", "intranet_server")] = (
get_labels(
scenario,
aitlds_path,
"/intranet_server/logs/apache2/intranet.*-error.log*",
)
)
labels[("/var/log/audit/audit.log", "internal_share")] = get_labels(
scenario, aitlds_path, "/internal_share/logs/audit/audit.log"
)
labels[("/var/log/audit/audit.log", "intranet_server")] = get_labels(
scenario, aitlds_path, "/intranet_server/logs/audit/audit.log"
)
labels[("/var/log/auth.log", "intranet_server")] = get_labels(
scenario, aitlds_path, "/intranet_server/logs/auth.log*"
)
if scenario == "wheeler":
labels[
("/var/log/logstash/intranet-server/system.cpu.log", "monitoring")
] = []
else:
labels[
("/var/log/logstash/intranet-server/system.cpu.log", "monitoring")
] = get_labels(
scenario,
aitlds_path,
"/monitoring/logs/logstash/intranet-server/*-system.cpu.log",
)
labels[("/var/log/openvpn.log", "vpn")] = get_labels(
scenario, aitlds_path, "/vpn/logs/openvpn.log"
)
# for k, v in labels.items():
# print(k)
# print(str(k) + ': ' + str(len(v)))
netflows = get_netflows(scenario, aitnds_path)
print(" Processing log files...")
with (
open("alerts_json/" + wazuhfile) as wazuh_in,
open("alerts_json/" + aminerfile) as aminer_in,
open("server_configs/" + configfile) as server_config_file,
open(alertscsv, "w+") as alertscsv_out,
):
alertscsv_out.write("time,name,ip,host,short,time_label,event_label\n")
ips = {}
server_config = yaml.safe_load(server_config_file)
for hostname in server_config:
name = hostname
ips[server_config[hostname]["default_ipv4_address"]] = name
# alerts_file.write('time,name,ip,host,short,time_label,event_label\n')
for line in wazuh_in:
j = json.loads(line)
if "The average number" in line:
continue
if "data" in j and "timestamp" in j["data"]:
log_time = datetime.strptime(
j["data"]["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z"
) # 2022-01-21T00:17:54.308261+0000
log_time = log_time.replace(tzinfo=pytz.utc)
log_time = log_time.timestamp()
elif "predecoder" in j and "timestamp" in j["predecoder"]:
if "/" in j["predecoder"]["timestamp"]:
log_time = datetime.strptime(
j["predecoder"]["timestamp"], "%m/%d/%Y-%H:%M:%S.%f"
) # 01/21/2022-00:17:54.308214
log_time = log_time.replace(tzinfo=pytz.utc)
log_time = log_time.timestamp()
else:
log_time = datetime.strptime(
"2022 " + j["predecoder"]["timestamp"], "%Y %b %d %H:%M:%S"
) # Jan 21 01:03:08
log_time = log_time.replace(tzinfo=pytz.utc)
log_time = log_time.timestamp()
else:
if "full_log" in j:
logline = j["full_log"]
else:
print("Unknown alert format: " + str(j))
filename = j["location"].split("/")[-1]
if filename.endswith("-access.log"):
filename = "-access.log"
elif filename.endswith("-error.log"):
filename = "-error.log"
elif filename.endswith("syslog"):
filename = "logs/syslog"
else:
filename = "/" + filename
log_time = timestampExtractor.timestampExtractor[filename](
logline
).timestamp()
description = j["rule"]["description"]
short = get_short(description)
all_shorts.add(short)
if not description.startswith("Suricata: "):
description = "Wazuh: " + description
all_descriptions.add(description)
event_label = "-"
if do_event_labeling:
if "full_log" in j:
# Check if the line appears in the label file; if yes, assign the corresponding label
log_line_from_alert = j["full_log"]
if "audit" in j["location"]:
# Wazuh aggregates multiple audit lines into one, needs to be split
parts = log_line_from_alert.split("type=")
log_line_from_alert_list = [
"type=" + part for part in parts if part != ""
]
else:
log_line_from_alert_list = [log_line_from_alert]
file_name_from_alert = (j["location"], ips[j["agent"]["ip"]])
if file_name_from_alert in labels:
for log_line_from_alert in log_line_from_alert_list:
if log_line_from_alert in labels[file_name_from_alert]:
event_label = labels[file_name_from_alert][
log_line_from_alert
]
break
elif description.startswith("Suricata: "):
# Check if the netflow attributes correspond to a labeled netflow in the AIT-NDS; if yes, assign the corresponding label
proto = j["data"]["proto"]
srcip = j["data"]["src_ip"]
srcport = j["data"]["src_port"]
destip = j["data"]["dest_ip"]
destport = j["data"]["dest_port"]
if (proto, srcip, srcport, destip, destport) in netflows:
alert_time = (
datetime.strptime(
j["data"]["timestamp"], "%Y-%m-%dT%H:%M:%S.%f+0000"
)
.replace(tzinfo=timezone.utc)
.timestamp()
) # "2022-01-24T03:57:01.687867+0000"
for netflow_time, netflow_label in netflows[
(proto, srcip, srcport, destip, destport)
]:
if abs(alert_time - netflow_time) < 2:
event_label = [netflow_label]
break
if (proto, destip, destport, srcip, srcport) in netflows:
alert_time = (
datetime.strptime(
j["data"]["timestamp"], "%Y-%m-%dT%H:%M:%S.%f+0000"
)
.replace(tzinfo=timezone.utc)
.timestamp()
) # "2022-01-24T03:57:01.687867+0000"
for netflow_time, netflow_label in netflows[
(proto, destip, destport, srcip, srcport)
]:
if abs(alert_time - netflow_time) < 2:
event_label = [netflow_label]
break
else:
print("full_log missing from alert: " + str(j))
phase = get_phase(scenario, log_time)
all_phases.add(phase)
if short not in counts:
counts[short] = {}
if phase not in counts[short]:
counts[short][phase] = {}
if scenario not in counts[short][phase]:
counts[short][phase][scenario] = 1
else:
counts[short][phase][scenario] += 1
location = j["agent"]["ip"]
time_label = phase
if time_label.startswith("false_positive"):
time_label = "false_positive"
event_label = get_ts_label(event_label)
alertscsv_out.write(
str(int(log_time))
+ ","
+ str(description)
+ ","
+ str(location)
+ ","
+ str(ips[location])
+ ","
+ short
+ ","
+ time_label
+ ","
+ event_label
+ "\n"
)
j = {
"scenario": scenario,
"time": log_time,
"type": description,
"location": location,
"label": event_label,
}
# ts_file.write(
# scenario
# + ","
# + str(int(log_time))
# + ","
# + str(description)
# + ","
# + str(location)
# + ","
# + str(ips[location])
# + ","
# + ts_label
# + "\n"
# )
# alerts_file.write(json.dumps(j) + '\n')
for line in aminer_in:
j = json.loads(line)
log_time = j["LogData"]["DetectionTimestamp"][-1]
description = j["AnalysisComponent"]["AnalysisComponentName"]
all_descriptions.add(description)
location = j["AMiner"]["ID"]
short = get_short(description)
all_shorts.add(short)
log_line_from_alert = j["LogData"]["RawLogData"][0]
file_name_from_alert = (
j["LogData"]["LogResources"][0],
ips[j["AMiner"]["ID"]],
)
event_label = "-"
if do_event_labeling:
if file_name_from_alert in labels:
if log_line_from_alert in labels[file_name_from_alert]:
event_label = labels[file_name_from_alert][
log_line_from_alert
]
phase = get_phase(scenario, log_time)
all_phases.add(phase)
if short not in counts:
counts[short] = {}
if phase not in counts[short]:
counts[short][phase] = {}
if scenario not in counts[short][phase]:
counts[short][phase][scenario] = 1
else:
counts[short][phase][scenario] += 1
time_label = phase
if time_label.startswith("false_positive"):
time_label = "false_positive"
event_label = get_ts_label(event_label)
alertscsv_out.write(
str(int(log_time))
+ ","
+ str(description)
+ ","
+ str(location)
+ ","
+ str(ips[location])
+ ","
+ short
+ ","
+ time_label
+ ","
+ event_label
+ "\n"
)
j = {
"scenario": scenario,
"time": log_time,
"type": description,
"location": location,
"label": event_label,
}
# ts_file.write(
# scenario
# + ","
# + str(int(log_time))
# + ","
# + str(description)
# + ","
# + str(location)
# + ","
# + str(ips[location])
# + ","
# + ts_label
# + "\n"
# )
# alerts_file.write(json.dumps(j) + '\n')