Repository navigation
Expand file tree
/
Copy pathexception.py
More file actions
358 lines (288 loc) · 13.1 KB
/
Copy pathexception.py
File metadata and controls
358 lines (288 loc) · 13.1 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
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
"""Rule exceptions data."""
import copy
from collections import defaultdict
from dataclasses import dataclass, field
from datetime import UTC, datetime
from pathlib import Path
from typing import Any, get_args
import pytoml # type: ignore[reportMissingTypeStubs]
from marshmallow import EXCLUDE, ValidationError, validates_schema
from .config import parse_rules_config
from .mixins import MarshmallowDataclassMixin
from .schemas import definitions
from .utils import ensure_yaml_suffix, save_yaml
RULES_CONFIG = parse_rules_config()
# https://www.elastic.co/guide/en/security/current/exceptions-api-overview.html
@dataclass(frozen=True)
class ExceptionMeta(MarshmallowDataclassMixin):
"""Data stored in an exception's [metadata] section of TOML."""
creation_date: definitions.Date
list_name: str
rule_ids: list[definitions.UUIDString]
rule_names: list[str]
updated_date: definitions.Date
# Optional fields
deprecation_date: definitions.Date | None = None
comments: str | None = None
maturity: definitions.Maturity | None = None
@dataclass(frozen=True)
class BaseExceptionItemEntry(MarshmallowDataclassMixin):
"""Shared object between nested and non-nested exception items."""
field: str
type: definitions.ExceptionEntryType
@dataclass(frozen=True)
class NestedExceptionItemEntry(BaseExceptionItemEntry, MarshmallowDataclassMixin):
"""Nested exception item entry."""
entries: list["ExceptionItemEntry"]
@validates_schema
def validate_nested_entry(self, data: dict[str, Any], **_: Any) -> None:
"""More specific validation."""
if data.get("list"):
raise ValidationError("Nested entries cannot define a list")
@dataclass(frozen=True)
class ExceptionItemEntry(BaseExceptionItemEntry, MarshmallowDataclassMixin):
"""Exception item entry."""
@dataclass(frozen=True)
class ListObject:
"""List object for exception item entry."""
id: str
type: definitions.EsDataTypes
operator: definitions.ExceptionEntryOperator
list_vals: ListObject | None = field(default=None, metadata={"data_key": "list"})
value: str | list[str] | None = None
@validates_schema
def validate_entry(self, data: dict[str, Any], **_: Any) -> None:
"""Validate the entry based on its type."""
value = data.get("value", "")
if data["type"] in ("exists", "list") and value is not None:
raise ValidationError(f"Entry of type {data['type']} cannot have a value")
if data["type"] in ("match", "wildcard") and not isinstance(value, str):
raise ValidationError(f"Entry of type {data['type']} must have a string value")
if data["type"] == "match_any" and not isinstance(value, list):
raise ValidationError(f"Entry of type {data['type']} must have a list of strings as a value")
@dataclass(frozen=True)
class ExceptionItem(MarshmallowDataclassMixin):
"""Base exception item."""
@dataclass(frozen=True)
class Comment:
"""Comment object for exception item."""
comment: str
comments: list[Comment | None]
description: str
entries: list[ExceptionItemEntry | NestedExceptionItemEntry]
list_id: str
item_id: str | None # api sets field when not provided
meta: dict[str, Any] | None
name: str
namespace_type: definitions.ExceptionNamespaceType | None # defaults to "single" if not provided
tags: list[str] | None
type: definitions.ExceptionItemType
@dataclass(frozen=True)
class EndpointException(ExceptionItem, MarshmallowDataclassMixin):
"""Endpoint exception item."""
_tags: list[definitions.ExceptionItemEndpointTags]
@validates_schema
def validate_endpoint(self, data: dict[str, Any], **_: Any) -> None:
"""Validate the endpoint exception."""
for entry in data["entries"]:
if entry["operator"] == "excluded":
raise ValidationError("Endpoint exceptions cannot have an `excluded` operator")
@dataclass(frozen=True)
class DetectionException(ExceptionItem, MarshmallowDataclassMixin):
"""Detection exception item."""
expire_time: str | None # fields.DateTime] # maybe this is isoformat?
@dataclass(frozen=True)
class ExceptionContainer(MarshmallowDataclassMixin):
"""Exception container."""
description: str
list_id: str | None
meta: dict[str, Any] | None
name: str
namespace_type: definitions.ExceptionNamespaceType | None
tags: list[str] | None
type: definitions.ExceptionContainerType
def to_rule_entry(self) -> dict[str, Any]:
"""Returns a dict of the format required in rule.exception_list."""
# requires KSO id to be consider valid structure
return {"namespace_type": self.namespace_type, "type": self.type, "list_id": self.list_id}
@dataclass(frozen=True)
class Data(MarshmallowDataclassMixin):
"""Data stored in an exception's [exception] section of TOML."""
container: ExceptionContainer
items: list[DetectionException] | None
@dataclass(frozen=True)
class TOMLExceptionContents(MarshmallowDataclassMixin):
"""Data stored in an exception file."""
metadata: ExceptionMeta
exceptions: list[Data]
@classmethod
def from_exceptions_dict(
cls, exceptions_dict: dict[str, Any], rule_list: list[dict[str, Any]]
) -> "TOMLExceptionContents":
"""Create a TOMLExceptionContents from a kibana rule resource."""
rule_ids: list[str] = []
rule_names: list[str] = []
for rule in rule_list:
rule_ids.append(rule["id"])
rule_names.append(rule["name"])
# Format date to match schema
container = exceptions_dict["container"]
now_date = datetime.now(UTC).strftime("%Y/%m/%d")
created_at = container.get("created_at")
updated_at = container.get("updated_at")
creation_date = (
datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y/%m/%d") # noqa: DTZ007
if created_at
else now_date
)
updated_date = (
datetime.strptime(updated_at, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y/%m/%d") # noqa: DTZ007
if updated_at
else now_date
)
metadata = {
"creation_date": creation_date,
"list_name": exceptions_dict["container"]["name"],
"rule_ids": rule_ids,
"rule_names": rule_names,
"updated_date": updated_date,
}
return cls.from_dict({"metadata": metadata, "exceptions": [exceptions_dict]}, unknown=EXCLUDE)
def to_api_format(self) -> list[dict[str, Any]]:
"""Convert the TOML Exception to the API format."""
converted: list[dict[str, Any]] = []
for exception in self.exceptions:
converted.append(exception.container.to_dict())
if exception.items:
converted.extend([item.to_dict() for item in exception.items])
return converted
@dataclass(frozen=True)
class TOMLException:
"""TOML exception object."""
contents: TOMLExceptionContents
path: Path | None = None
@property
def name(self) -> str:
"""Return the name of the exception list."""
return self.contents.metadata.list_name
def save_toml(self) -> None:
"""Save the exception to a TOML file."""
if not self.path:
raise ValueError(f"Can't save exception {self.name} without a path")
# Check if self.path has a .toml extension
path = self.path
if path.suffix != ".toml":
# If it doesn't, add one
path = path.with_suffix(".toml")
with path.open("w", encoding="utf-8") as f:
contents_dict = self.contents.to_dict()
# Sort the dictionary so that 'metadata' is at the top
sorted_dict = dict(sorted(contents_dict.items(), key=lambda item: item[0] != "metadata"))
pytoml.dump(sorted_dict, f) # type: ignore[reportUnknownMemberType]
def save_yaml(self, path: Path | None = None) -> None:
"""Save the exception to a YAML file."""
target_path = path or self.path
if not target_path:
raise ValueError(f"Can't save exception {self.name} without a path")
api_format = self.contents.to_api_format()
# If single item, write as dict; if multiple, write as list
content = api_format[0] if len(api_format) == 1 else api_format
save_yaml(ensure_yaml_suffix(target_path), content)
def _comment_id(comment: dict[str, Any] | str) -> str:
"""Return a stable id for deduplication. API comments are dicts with 'id'; fallback str() for other types."""
return comment.get("id", "Null") if isinstance(comment, dict) else str(comment)
def _deduplicate_comments(item: dict[str, Any]) -> dict[str, Any]:
"""Remove duplicate comments from an exception item by comment id (order preserved)."""
item = copy.deepcopy(item)
comments: list[Any] | None = item.get("comments", [])
if not isinstance(comments, list) or not comments:
return item
seen: set[str] = set()
seen.add("Null")
deduplicated: list[Any] = []
for comment in comments:
cid = _comment_id(comment)
if cid not in seen:
seen.add(cid)
deduplicated.append(comment)
item["comments"] = deduplicated
return item
def parse_exceptions_results_from_api(
results: list[dict[str, Any]],
) -> tuple[dict[str, Any], dict[str, Any], list[str], list[dict[str, Any]]]:
"""Parse exceptions results from the API into containers and items."""
exceptions_containers: dict[str, Any] = {}
exceptions_items: dict[str, list[Any]] = defaultdict(list)
unparsed_results: list[dict[str, Any]] = []
seen_list_item_pairs: set[tuple[str, str]] = set()
for result in results:
result_type = result.get("type")
list_id = result.get("list_id")
if result_type and list_id:
if result_type in get_args(definitions.ExceptionContainerType):
exceptions_containers[list_id] = result
elif result_type in get_args(definitions.ExceptionItemType):
item_id = result.get("item_id")
pair = (list_id, item_id) if item_id is not None else None
if pair is not None and pair in seen_list_item_pairs:
print(f"Duplicate item found for list_id {list_id} and item_id {item_id}, skipping...")
continue
item = _deduplicate_comments(result)
if pair is not None:
seen_list_item_pairs.add(pair)
exceptions_items[list_id].append(item)
else:
unparsed_results.append(result)
return exceptions_containers, exceptions_items, [], unparsed_results
def build_exception_objects( # noqa: PLR0913, PLR0917
exceptions_containers: dict[str, Any],
exceptions_items: dict[str, Any],
exception_list_rule_table: dict[str, Any],
exceptions_directory: Path | None,
save_toml: bool = False,
skip_errors: bool = False,
verbose: bool = False,
) -> tuple[list[TOMLException], list[str], list[str]]:
"""Build TOMLException objects from a list of exception dictionaries."""
output: list[str] = []
errors: list[str] = []
toml_exceptions: list[TOMLException] = []
for container in exceptions_containers.values():
try:
list_id = container["list_id"]
items = exceptions_items[list_id]
contents = TOMLExceptionContents.from_exceptions_dict(
{"container": container, "items": items},
exception_list_rule_table[list_id],
)
filename = f"{list_id}_exceptions.toml"
if RULES_CONFIG.exception_dir is None and not exceptions_directory:
raise FileNotFoundError( # noqa: TRY301
"No Exceptions directory is specified. Please specify either in the config or CLI."
)
exceptions_path: Path = ( # pyright: ignore[reportUnknownVariableType]
Path(exceptions_directory) / filename if exceptions_directory else RULES_CONFIG.exception_dir / filename # pyright: ignore[reportOptionalOperand]
)
if verbose:
output.append(f"[+] Building exception(s) for {exceptions_path}")
e_object = TOMLException(
contents=contents,
path=exceptions_path, # pyright: ignore[reportUnknownArgumentType]
)
if save_toml:
e_object.save_toml()
toml_exceptions.append(e_object)
except Exception as e:
if skip_errors:
output.append(f"- skipping exceptions export - {type(e).__name__}")
if not exceptions_directory:
errors.append(f"- no exceptions directory found - {e}")
else:
errors.append(f"- exceptions export - {e}")
continue
raise
return toml_exceptions, output, errors