Skip to content

Commit 11ee933

Browse files
authored
Merge pull request #208 from SigmaHQ:taxonomies
Introduction of rule taxonomy attribute and processing condition
2 parents 851321b + 0c2f452 commit 11ee933

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

sigma/processing/conditions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,22 @@ def match(
257257
return isinstance(rule, SigmaCorrelationRule)
258258

259259

260+
@dataclass
261+
class TaxonomyCondition(RuleProcessingCondition):
262+
"""
263+
Matches on rule taxonomy.
264+
"""
265+
266+
taxonomy: str
267+
268+
def match(
269+
self,
270+
pipeline: "sigma.processing.pipeline.ProcessingPipeline",
271+
rule: Union[SigmaRule, SigmaCorrelationRule],
272+
) -> bool:
273+
return rule.taxonomy == self.taxonomy
274+
275+
260276
### Field Name Condition Classes ###
261277
@dataclass
262278
class IncludeFieldCondition(FieldNameProcessingCondition):
@@ -395,6 +411,7 @@ def match_detection_item(
395411
"processing_item_applied": RuleProcessingItemAppliedCondition,
396412
"is_sigma_rule": IsSigmaRuleCondition,
397413
"is_sigma_correlation_rule": IsSigmaCorrelationRuleCondition,
414+
"taxonomy": TaxonomyCondition,
398415
}
399416
detection_item_conditions: Dict[str, DetectionItemProcessingCondition] = {
400417
"match_string": MatchStringCondition,

sigma/rule.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ class SigmaRuleBase:
736736
title: str = ""
737737
id: Optional[UUID] = None
738738
name: Optional[str] = None
739+
taxonomy: str = "sigma"
739740
related: Optional[SigmaRelated] = None
740741
status: Optional[SigmaStatus] = None
741742
description: Optional[str] = None
@@ -823,6 +824,25 @@ class instantiation of an object derived from the SigmaRuleBase class and the er
823824
else:
824825
rule_name = rule_name
825826

827+
# Rule taxonomy
828+
rule_taxonomy = rule.get("taxonomy", "sigma")
829+
if rule_taxonomy is not None:
830+
if not isinstance(rule_taxonomy, str):
831+
errors.append(
832+
sigma_exceptions.SigmaTaxonomyError(
833+
"Sigma rule taxonomy must be a string", source=source
834+
)
835+
)
836+
else:
837+
if rule_taxonomy == "":
838+
errors.append(
839+
sigma_exceptions.SigmaTaxonomyError(
840+
"Sigma rule taxonomy must not be empty", source=source
841+
)
842+
)
843+
else:
844+
rule_taxonomy = rule_taxonomy
845+
826846
# Rule related validation
827847
rule_related = rule.get("related")
828848
if rule_related is not None:
@@ -985,6 +1005,7 @@ class instantiation of an object derived from the SigmaRuleBase class and the er
9851005
"title": rule_title,
9861006
"id": rule_id,
9871007
"name": rule_name,
1008+
"taxonomy": rule_taxonomy,
9881009
"related": rule_related,
9891010
"level": level,
9901011
"status": status,

tests/test_processing_conditions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
MatchStringCondition,
1515
RuleContainsDetectionItemCondition,
1616
RuleProcessingItemAppliedCondition,
17+
TaxonomyCondition,
1718
)
1819
from sigma.rule import SigmaDetectionItem, SigmaLogSource, SigmaRule
1920
from tests.test_processing_pipeline import processing_item
@@ -41,6 +42,7 @@ def sigma_rule():
4142
"""
4243
title: Test
4344
status: test
45+
taxonomy: test
4446
logsource:
4547
category: test_category
4648
product: test_product
@@ -155,6 +157,10 @@ def test_is_sigma_correlation_rule_with_rule(dummy_processing_pipeline, sigma_ru
155157
assert not IsSigmaCorrelationRuleCondition().match(dummy_processing_pipeline, sigma_rule)
156158

157159

160+
def test_taxonomy_condition_match(dummy_processing_pipeline, sigma_rule):
161+
assert TaxonomyCondition("test").match(dummy_processing_pipeline, sigma_rule)
162+
163+
158164
def test_include_field_condition_match(dummy_processing_pipeline, detection_item):
159165
assert IncludeFieldCondition(["field", "otherfield"]).match_field_name(
160166
dummy_processing_pipeline, "field"

tests/test_rule.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ def sigma_rule():
10161016
title="Test",
10171017
id=UUID("9a6cafa7-1481-4e64-89a1-1f69ed08618c"),
10181018
name="test",
1019+
taxonomy="test",
10191020
status=SigmaStatus.TEST,
10201021
description="This is a test",
10211022
references=[
@@ -1087,6 +1088,7 @@ def test_sigmarule_fromyaml(sigma_rule):
10871088
title: Test
10881089
id: 9a6cafa7-1481-4e64-89a1-1f69ed08618c
10891090
name: test
1091+
taxonomy: test
10901092
status: test
10911093
description: This is a test
10921094
references:
@@ -1127,6 +1129,7 @@ def test_sigmarule_fromyaml_with_custom_attribute(sigma_rule):
11271129
title: Test
11281130
id: 9a6cafa7-1481-4e64-89a1-1f69ed08618c
11291131
name: test
1132+
taxonomy: test
11301133
status: test
11311134
description: This is a test
11321135
references:

0 commit comments

Comments
 (0)