forked from SigmaHQ/pySigma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
487 lines (438 loc) · 18.1 KB
/
Copy pathbase.py
File metadata and controls
487 lines (438 loc) · 18.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
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
from __future__ import annotations
import datetime as dt
import re
from abc import abstractmethod
from dataclasses import dataclass, field
from datetime import date, datetime
from typing import TYPE_CHECKING, Any
from uuid import UUID
import yaml
from typing_extensions import Self
import sigma.exceptions as sigma_exceptions
from sigma.rule.attributes import SigmaLevel, SigmaRelated, SigmaRuleTag, SigmaStatus
if TYPE_CHECKING:
from sigma.conversion.state import ConversionState
from sigma.exceptions import SigmaError, SigmaRuleLocation
class SigmaYAMLLoader(yaml.CSafeLoader):
"""Custom YAML loader implementing additional functionality for Sigma."""
def construct_mapping(self, node: yaml.MappingNode, deep: bool = False) -> dict[Any, Any]:
keys = set()
for k, v in node.value:
key = self.construct_object(k, deep=deep)
if key in keys:
raise yaml.error.YAMLError("Duplicate key '{k}'")
else:
keys.add(key)
return super().construct_mapping(node, deep)
@dataclass
class SigmaRuleBase:
title: str = ""
id: UUID | None = None
name: str | None = None
taxonomy: str = "sigma"
related: SigmaRelated | None = None
status: SigmaStatus | None = None
description: str | None = None
license: str | None = None
references: list[str] = field(default_factory=list)
tags: list[SigmaRuleTag] = field(default_factory=list)
author: str | None = None
date: dt.date | None = None
modified: dt.date | None = None
fields: list[str] = field(default_factory=list)
falsepositives: list[str] = field(default_factory=list)
level: SigmaLevel | None = None
scope: list[str] | None = None
errors: list[sigma_exceptions.SigmaError] = field(default_factory=list)
source: SigmaRuleLocation | None = field(default=None, compare=False)
custom_attributes: dict[str, Any] = field(compare=False, default_factory=dict)
_backreferences: list[SigmaRuleBase] = field(
init=False, default_factory=list, repr=False, compare=False
)
_conversion_result: list[Any] | None = field(
init=False, default=None, repr=False, compare=False
)
_conversion_states: list[ConversionState] | None = field(
init=False, default=None, repr=False, compare=False
)
_output: bool = field(init=False, default=True, repr=False, compare=False)
def __post_init__(self: Self) -> None:
for field in ("references", "tags", "fields", "falsepositives"):
if self.__getattribute__(field) is None:
self.__setattr__(field, [])
if self.id is not None and not isinstance(
self.id, UUID
): # Try to convert rule id into UUID object, but keep it if not possible
try:
self.id = UUID(self.id)
except ValueError:
pass
@classmethod
def from_dict_common_params(
cls: type[Self],
rule: dict[str, Any],
collect_errors: bool = False,
source: SigmaRuleLocation | None = None,
) -> tuple[dict[str, Any], list[SigmaError]]:
"""
Convert Sigma rule base parsed in dict structure into kwargs dict that can be passed to the
class instantiation of an object derived from the SigmaRuleBase class and the errors list.
This is intended to be called only by to_dict() methods for processing the general
parameters defined in the base class.
if collect_errors is set to False exceptions are collected in the errors property of the resulting
SigmaRule object. Else the first recognized error is raised as exception.
"""
errors = []
def get_rule_as_date(name: str, exception_class: type[SigmaError]) -> date | None:
"""
Accepted string based date formats are in range 1000-01-01 .. 3999-12-31:
* XXXX-XX-XX -- fully corresponds to yaml date format
* XXXX/XX/XX, XXXX/XX/X, XXXX/X/XX, XXXX/X/X -- often occurs in the US-based sigmas
Not accepted are ambiguous dates such as:
2024-01-1, 24-1-24, 24/1/1, ...
"""
nonlocal errors, rule, source
value = rule.get(name)
if (
value is not None
and not isinstance(value, date)
and not isinstance(value, datetime)
):
error = True
try:
value = str(value) # forcifully convert whatever the type is into string
accepted_regexps = (
"([1-3][0-9][0-9][0-9])-([01][0-9])-([0-3][0-9])", # 1000-01-01 .. 3999-12-31
"([1-3][0-9][0-9][0-9])/([01]?[0-9])/([0-3]?[0-9])", # 1000/1/1, 1000/01/01 .. 3999/12/31
)
for date_regexp in accepted_regexps:
matcher = re.fullmatch(date_regexp, value)
if matcher:
result = date(int(matcher[1]), int(matcher[2]), int(matcher[3]))
error = False
break
except Exception:
pass
if error:
errors.append(
exception_class(
f"Rule {name} '{ value }' is invalid, use yyyy-mm-dd", source=source
)
)
return None
return result
else:
return value
# Rule identifier may be empty or must be valid UUID
rule_id = rule.get("id")
if rule_id is not None:
try:
rule_id = UUID(rule_id)
except ValueError:
errors.append(
sigma_exceptions.SigmaIdentifierError(
"Sigma rule identifier must be an UUID", source=source
)
)
# Rule name
rule_name = rule.get("name")
if rule_name is not None:
if not isinstance(rule_name, str):
errors.append(
sigma_exceptions.SigmaTypeError(
"Sigma rule name must be a string", source=source
)
)
else:
if rule_name == "":
errors.append(
sigma_exceptions.SigmaNameError(
"Sigma rule name must not be empty", source=source
)
)
else:
rule_name = rule_name
# Rule taxonomy
rule_taxonomy = rule.get("taxonomy", "sigma")
if rule_taxonomy is not None:
if not isinstance(rule_taxonomy, str):
errors.append(
sigma_exceptions.SigmaTaxonomyError(
"Sigma rule taxonomy must be a string", source=source
)
)
else:
if rule_taxonomy == "":
errors.append(
sigma_exceptions.SigmaTaxonomyError(
"Sigma rule taxonomy must not be empty", source=source
)
)
else:
rule_taxonomy = rule_taxonomy
# Rule related validation
rule_related = rule.get("related")
if rule_related is not None:
if not isinstance(rule_related, list):
errors.append(
sigma_exceptions.SigmaRelatedError(
"Sigma rule related must be a list", source=source
)
)
else:
try:
rule_related = SigmaRelated.from_dict(rule_related)
except sigma_exceptions.SigmaRelatedError as e:
errors.append(e)
# Rule level validation
rule_level = rule.get("level")
if rule_level is not None:
if not isinstance(rule_level, str):
errors.append(
sigma_exceptions.SigmaLevelError(
"Sigma rule level must be a string", source=source
)
)
else:
try:
rule_level = SigmaLevel[rule_level.upper()]
except KeyError:
errors.append(
sigma_exceptions.SigmaLevelError(
f"'{ rule_level }' is not a valid Sigma rule level", source=source
)
)
# Rule status validation
rule_status = rule.get("status")
if rule_status is not None:
if not isinstance(rule_status, str):
errors.append(
sigma_exceptions.SigmaStatusError(
"Sigma rule status cannot be a list", source=source
)
)
else:
try:
rule_status = SigmaStatus[rule_status.upper()]
except KeyError:
errors.append(
sigma_exceptions.SigmaStatusError(
f"'{ rule_status }' is not a valid Sigma rule status", source=source
)
)
# Rule tags validation
tags = rule.get("tags", list())
rule_tags = []
if tags is not None:
if not isinstance(tags, list):
errors.append(
sigma_exceptions.SigmaTagError(
"Sigma tags fields must be a list", source=source
)
)
else:
for tag in tags:
if not isinstance(tag, str):
errors.append(
sigma_exceptions.SigmaTagError(
"Sigma rule tags must be a list of strings", source=source
)
)
continue
try:
rule_tags.append(SigmaRuleTag.from_str(tag))
except sigma_exceptions.SigmaValueError as e:
errors.append(e)
# parse rule date if existing
rule_date = get_rule_as_date("date", sigma_exceptions.SigmaDateError)
# parse rule modified if existing
rule_modified = get_rule_as_date("modified", sigma_exceptions.SigmaModifiedError)
# Rule fields validation
rule_fields = rule.get("fields")
if rule_fields is not None and not isinstance(rule_fields, list):
errors.append(
sigma_exceptions.SigmaFieldsError(
"Sigma rule fields must be a list",
source=source,
)
)
# Rule falsepositives validation
rule_falsepositives = rule.get("falsepositives")
if rule_falsepositives is not None and not isinstance(rule_falsepositives, list):
errors.append(
sigma_exceptions.SigmaFalsePositivesError(
"Sigma rule falsepositives must be a list",
source=source,
)
)
# Rule author validation
rule_author = rule.get("author")
if rule_author is not None and not isinstance(rule_author, str):
errors.append(
sigma_exceptions.SigmaAuthorError(
"Sigma rule author must be a string",
source=source,
)
)
# Rule description validation
rule_description = rule.get("description")
if rule_description is not None and not isinstance(rule_description, str):
errors.append(
sigma_exceptions.SigmaDescriptionError(
"Sigma rule description must be a string",
source=source,
)
)
# Rule references validation
rule_references = rule.get("references")
if rule_references is not None and not isinstance(rule_references, list):
errors.append(
sigma_exceptions.SigmaReferencesError(
"Sigma rule references must be a list",
source=source,
)
)
# Rule title validation
rule_title = rule.get("title")
if rule_title is None:
errors.append(
sigma_exceptions.SigmaTitleError(
"Sigma rule must have a title",
source=source,
)
)
elif not isinstance(rule_title, str):
errors.append(
sigma_exceptions.SigmaTitleError(
"Sigma rule title must be a string",
source=source,
)
)
elif len(rule_title) > 256:
errors.append(
sigma_exceptions.SigmaTitleError(
"Sigma rule title length must not exceed 256 characters",
source=source,
)
)
# Rule scope validation
rule_scope = rule.get("scope")
if rule_scope is not None and not isinstance(rule_scope, list):
errors.append(
sigma_exceptions.SigmaScopeError(
"Sigma rule scope must be a list",
source=source,
)
)
# Rule license validation
rule_license = rule.get("license")
if rule_license is not None and not isinstance(rule_license, str):
errors.append(
sigma_exceptions.SigmaLicenseError(
"Sigma rule license must be a string",
source=source,
)
)
if not collect_errors and errors:
raise errors[0]
return (
{
"title": rule_title,
"id": rule_id,
"name": rule_name,
"taxonomy": rule_taxonomy,
"related": rule_related,
"level": rule_level,
"status": rule_status,
"description": rule_description,
"references": rule_references,
"tags": rule_tags,
"author": rule_author,
"date": rule_date,
"modified": rule_modified,
"fields": rule_fields,
"falsepositives": rule_falsepositives,
"scope": rule_scope,
"license": rule_license,
"source": source,
"custom_attributes": {
k: v
for k, v in rule.items()
if k
not in set(cls.__dataclass_fields__.keys())
- {"errors", "source", "applied_processing_items"}
},
},
errors,
)
@classmethod
@abstractmethod
def from_dict(cls: type[Self], rule: dict[str, Any], collect_errors: bool = False) -> Self:
"""Convert dict input into SigmaRule object."""
raise NotImplementedError(
"from_dict method must be implemented in the derived class of SigmaRuleBase"
)
@classmethod
def from_yaml(cls: type[Self], rule: str, collect_errors: bool = False) -> Self:
"""Convert YAML input string with single document into SigmaRule object."""
parsed_rule = yaml.load(rule, SigmaYAMLLoader)
return cls.from_dict(parsed_rule, collect_errors)
def to_dict(self: Self) -> dict[str, Any]:
"""Convert rule object into dict."""
d: dict[str, Any] = {
"title": self.title,
}
# Convert to string where possible
for field in ("id", "status", "level", "author", "description", "name"):
if (s := self.__getattribute__(field)) is not None:
d[field] = str(s)
# copy list of strings
for field in ("references", "fields", "falsepositives", "scope"):
if (l := self.__getattribute__(field)) is not None and len(l) > 0:
d[field] = l.copy()
# the special cases
if len(self.tags) > 0:
d["tags"] = [str(tag) for tag in self.tags]
if self.date is not None:
d["date"] = self.date.isoformat()
if self.modified is not None:
d["modified"] = self.modified.isoformat()
# custom attributes
d.update(self.custom_attributes)
return d
def add_backreference(self: Self, rule: SigmaRuleBase) -> None:
"""Add backreference to another rule."""
self._backreferences.append(rule)
def referenced_by(self: Self, rule: SigmaRuleBase) -> bool:
"""Check if rule is referenced by another rule."""
return rule in self._backreferences
def set_conversion_result(self: Self, result: list[Any]) -> None:
"""Set conversion result."""
self._conversion_result = result
def get_conversion_result(self: Self) -> list[Any]:
"""Get conversion result."""
if self._conversion_result is None:
raise sigma_exceptions.SigmaConversionError(
self,
None,
"Conversion result not available",
)
return self._conversion_result
def set_conversion_states(self: Self, state: list[ConversionState]) -> None:
"""Set conversion state."""
self._conversion_states = state
def get_conversion_states(self: Self) -> list[ConversionState]:
"""Get conversion state."""
if self._conversion_states is None:
raise sigma_exceptions.SigmaConversionError(
self,
None,
"Conversion state not available",
)
return self._conversion_states
def disable_output(self: Self) -> None:
"""Disable output of rule."""
self._output = False
def __lt__(self: Self, other: SigmaRuleBase) -> bool:
"""Sort rules by backreference. A rule referenced by another rule is smaller."""
return self.referenced_by(other)