-
Notifications
You must be signed in to change notification settings - Fork 70
/
siem.py
executable file
·427 lines (361 loc) · 12.4 KB
/
siem.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
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
#!/usr/bin/env python3
# Copyright 2019-2021 Sophos Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing permissions and limitations under the
# License.
#
import sys
import json
import logging
import logging.handlers
import datetime
import logging_config #Make sure to have here, before import state!
import os
import re
import state
from optparse import OptionParser
import name_mapping
import config
import api_client
import vercheck
VERSION = "2.1.0"
QUIET = False
MISSING_VALUE = "NA"
DEFAULT_ENDPOINT = "event"
SEVERITY_MAP = {"none": 0, "low": 1, "medium": 5, "high": 8, "very_high": 10}
CEF_CONFIG = {
"cef.version": "0",
"cef.device_vendor": "sophos",
"cef.device_product": "sophos central",
"cef.device_version": 1.0,
}
# CEF format from https://www.protect724.hpe.com/docs/DOC-1072
CEF_FORMAT = (
"CEF:%(version)s|%(device_vendor)s|%(device_product)s|"
"%(device_version)s|%(device_event_class_id)s|%(name)s|%(severity)s|"
)
CEF_MAPPING = {
# This is used for mapping CEF header prefix and extension to json returned by server
# CEF header prefix to json mapping
# Format
# CEF_header_prefix: JSON_key
"device_event_class_id": "type",
"name": "name",
"severity": "severity",
# json to CEF extension mapping
# Format
# JSON_key: CEF_extension
"source": "suser",
"when": "end",
"user_id": "duid",
"created_at": "rt",
"full_file_path": "filePath",
"location": "dhost",
}
# Initialize the SIEM_LOGGER
SIEM_LOGGER = logging.getLogger("SIEM")
SIEM_LOGGER.setLevel(logging.INFO)
SIEM_LOGGER.propagate = False
logging.basicConfig(format="%(message)s")
def is_valid_fqdn(fqdn):
fqdn = fqdn.strip()
fqdn = fqdn[:-1] if fqdn.endswith(".") else fqdn # chomp trailing period
return fqdn and len(fqdn) < 256 and all(part and len(part) < 64 and re.match(r"^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$", part) for part in fqdn.split("."))
def convert_to_valid_fqdn(value):
return ".".join([re.sub("[^-a-z0-9]+", "-", x.strip()).strip("-") for x in value.lower().split(".") if x.strip()])
def write_json_format(results, config):
"""Write JSON format data.
Arguments:
results {list}: data
"""
for i in results:
i = remove_null_values(i)
update_cef_keys(i, config)
name_mapping.update_fields(log, i)
SIEM_LOGGER.info(json.dumps(i, ensure_ascii=False).strip())
def write_keyvalue_format(results, config):
"""Write key value format data.
Arguments:
results {dict}: results
"""
for i in results:
i = remove_null_values(i)
update_cef_keys(i, config)
name_mapping.update_fields(log, i)
date = i[u"rt"]
# TODO: Spaces/quotes/semicolons are not escaped here, does it matter?
events = list('%s="%s";' % (k, v) for k, v in i.items())
SIEM_LOGGER.info(
" ".join(
[
date,
]
+ events
).strip()
)
def write_cef_format(results, config):
"""Write CEF format data.
Arguments:
results {list}: data
"""
for i in results:
i = remove_null_values(i)
name_mapping.update_fields(log, i)
SIEM_LOGGER.info(format_cef(flatten_json(i), config).strip())
# Flattening JSON objects in Python
# https://medium.com/@amirziai/flattening-json-objects-in-python-f5343c794b10#.37u7axqta
def flatten_json(y):
out = {}
def flatten(x, name=""):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + "_")
else:
out[name[:-1]] = x
flatten(y)
return out
def log(s):
"""Write the log.
Arguments:
log_message {string} -- log content
"""
if not QUIET:
sys.stderr.write("%s\n" % s)
def format_prefix(data):
""" pipe and backslash in header must be escaped. escape group with backslash
Arguments:
data {string}: data
Returns:
string -- backslash escape string
"""
# pipe and backslash in header must be escaped
# escape group with backslash
return re.compile(r"([|\\])").sub(r"\\\1", data)
def format_extension(data):
""" equal sign and backslash in extension value must be escaped. escape group with backslash.
Arguments:
data : data
Returns:
string/list -- backslash escape string or return same value
"""
if type(data) is str:
return re.compile(r"([=\\])").sub(r"\\\1", data)
else:
return data
def map_severity(severity):
if severity in SEVERITY_MAP:
return SEVERITY_MAP[severity]
else:
msg = 'The "%s" severity can not be mapped, defaulting to 0' % severity
log(msg)
return SEVERITY_MAP["none"]
def extract_prefix_fields(data):
""" extract prefix fields and remove those from data dictionary
Arguments:
data {dict}: data
Returns:
fields {dict} -- fields object
"""
name_field = CEF_MAPPING["name"]
device_event_class_id_field = CEF_MAPPING["device_event_class_id"]
severity_field = CEF_MAPPING["severity"]
name = data.get(name_field, MISSING_VALUE)
name = format_prefix(name)
data.pop(name_field, None)
device_event_class_id = data.get(device_event_class_id_field, MISSING_VALUE)
device_event_class_id = format_prefix(device_event_class_id)
data.pop(device_event_class_id_field, None)
severity = data.get(severity_field, MISSING_VALUE)
severity = map_severity(severity)
data.pop(severity_field, None)
fields = {
"name": name,
"device_event_class_id": device_event_class_id,
"severity": severity,
"version": CEF_CONFIG["cef.version"],
"device_vendor": CEF_CONFIG["cef.device_vendor"],
"device_version": CEF_CONFIG["cef.device_version"],
"device_product": CEF_CONFIG["cef.device_product"],
}
return fields
def update_cef_keys(data, config):
""" Replace if there is a mapped CEF key
Arguments:
data {dict}: data
"""
# Replace if there is a mapped CEF key
for key, value in list(data.items()):
new_key = CEF_MAPPING.get(key, key)
if new_key == key:
continue
if config.convert_dhost_field_to_valid_fqdn.lower() == "true" and new_key == "dhost" and not is_valid_fqdn(value):
value = convert_to_valid_fqdn(value)
data[new_key] = value
del data[key]
def format_cef(data, config):
""" Message CEF formatted
Arguments:
data {dict}: data
Returns:
data {str}: message
"""
fields = extract_prefix_fields(data)
msg = CEF_FORMAT % fields
update_cef_keys(data, config)
for index, (key, value) in enumerate(data.items()):
value = format_extension(value)
if index > 0:
msg += " %s=%s" % (key, value)
else:
msg += "%s=%s" % (key, value)
return msg
def remove_null_values(data):
""" Removed null value
Arguments:
data {dict}: data
Returns:
data {dict}: update data
"""
return {k: v for k, v in data.items() if v is not None}
def parse_args_options():
""" Parsed the command line arguments
Returns:
options {dict}: options data
"""
global QUIET
if "SOPHOS_SIEM_HOME" in os.environ:
app_path = os.environ["SOPHOS_SIEM_HOME"]
else:
# Setup path
app_path = os.path.join(os.getcwd())
config_file = os.path.join(app_path, "config.ini")
parser = OptionParser(
description="Download event and/or alert data and output to various formats. "
"config.ini is a configuration file that exists by default in the siem-scripts "
"folder."
"Script keeps tab of its state, it will always pick-up from where it left-off "
"based on a state file stored in state folder. Set SOPHOS_SIEM_HOME environment "
"variable to point to the folder where config.ini, mapping files, state "
"and log folders will be located. state and log folders are created when the "
"script is run for the first time. "
)
parser.add_option(
"-s",
"--since",
default=False,
action="store",
help="Return results since specified Unix "
"Timestamp, max last 24 hours, defaults to "
"last 12 hours if there is no state file",
)
parser.add_option(
"-c",
"--config",
default=config_file,
action="store",
help="Specify a configuration file, " "defaults to config.ini",
)
parser.add_option(
"-l",
"--light",
default=False,
action="store_true",
help="Ignore noisy events - web control, "
"device control, update failure, "
"application allowed, (non)compliant",
)
parser.add_option(
"-d", "--debug", default=False, action="store_true", help="Print debug logs"
)
parser.add_option(
"-v", "--version", default=False, action="store_true", help="Print version"
)
parser.add_option(
"-q",
"--quiet",
default=False,
action="store_true",
help="Suppress status messages",
)
options, args = parser.parse_args()
if options.config is None:
parser.error("Need a config file specified")
if options.version:
log(VERSION)
sys.exit(0)
if options.quiet:
QUIET = True
return options
def load_config(config_path):
""" Get config file data
Arguments:
config_path {str}: config file path
Returns:
cfg {dice}: config.ini data
"""
cfg = config.Config(config_path)
cfg.format = cfg.format.lower()
cfg.endpoint = cfg.endpoint.lower()
validate_format(cfg.format)
validate_endpoint(cfg.endpoint)
return cfg
def validate_format(format):
if format not in ("json", "keyvalue", "cef"):
raise Exception("Invalid format in config.ini, format can be json, cef or keyvalue")
def validate_endpoint(endpoint):
endpoint_map = api_client.ENDPOINT_MAP
if endpoint not in endpoint_map:
raise Exception("Invalid endpoint in config.ini, endpoint can be event, alert or all")
def get_alerts_or_events(endpoint, options, config, state):
""" Get alerts/events data
Arguments:
endpoint {str}: endpoint name
options {dict}: options
config {dict}: config file details
state {dict}: state file details
"""
api_client_obj = api_client.ApiClient(endpoint, options, config, state)
results = api_client_obj.get_alerts_or_events()
if config.format == "json":
write_json_format(results, config)
elif config.format == "keyvalue":
write_keyvalue_format(results, config)
elif config.format == "cef":
write_cef_format(results, config)
else:
write_json_format(results, config)
def run(options, config_data, state):
""" Call the fetch alerts/events method
Arguments:
options {dict}: options
config_data {dict}: config file details
state {dict}: state file details
"""
endpoint_map = api_client.ENDPOINT_MAP
if config_data.endpoint in endpoint_map:
tuple_endpoint = endpoint_map[config_data.endpoint]
else:
tuple_endpoint = endpoint_map[DEFAULT_ENDPOINT]
for endpoint in tuple_endpoint:
get_alerts_or_events(
endpoint, options, config_data, state
)
def main():
options = parse_args_options()
logging.Formatter.formatTime = (lambda self, record, datefmt=None: datetime.datetime.fromtimestamp(record.created, datetime.timezone.utc).astimezone().isoformat(sep="T",timespec="milliseconds"))
config_data = load_config(options.config)
logging.info("Logging Level is set as: "+config_data.logging_level)
logger = logging.getLogger()
logger.setLevel(config_data.logging_level)
if (logger.level <= logging.DEBUG):
logger.handlers[0].setFormatter(logging.Formatter(logging_config.DEBUG_FORMAT))
state_data = state.State(options, config_data.state_file_path)
run(options, config_data, state_data)
if __name__ == "__main__":
main()