Skip to content

Commit

Permalink
Merge pull request #205 from kelnage/add-negated-condition
Browse files Browse the repository at this point in the history
Enable AddCondition to add negated conditions
  • Loading branch information
thomaspatzke authored Mar 28, 2024
2 parents 73b0597 + 16f02ea commit 4858a71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sigma/processing/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ class AddConditionTransformation(ConditionTransformation):
conditions: Dict[str, Union[str, List[str]]] = field(default_factory=dict)
name: Optional[str] = field(default=None, compare=False)
template: bool = False
negated: bool = False

def __post_init__(self):
if self.name is None: # generate random detection item name if none is given
Expand Down Expand Up @@ -648,7 +649,7 @@ def apply(
super().apply(pipeline, rule)

def apply_condition(self, cond: SigmaCondition) -> None:
cond.condition = f"{self.name} and ({cond.condition})"
cond.condition = ("not " if self.negated else "") + f"{self.name} and ({cond.condition})"


@dataclass
Expand Down
24 changes: 24 additions & 0 deletions tests/test_processing_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,30 @@ def test_addconditiontransformation_random_name():
assert len(name) > 6 and name.startswith("_cond_")


def test_addconditiontransformation_negated(dummy_pipeline, sigma_rule: SigmaRule):
transformation = AddConditionTransformation(
{
"newfield1": "test",
"newfield2": 123,
"newfield3": "$category",
"listfield": ["value1", "value2"],
},
"additional",
negated=True,
)
transformation.set_processing_item(
ProcessingItem(
transformation,
identifier="test",
)
)
transformation.apply(dummy_pipeline, sigma_rule)
assert (
sigma_rule.detection.parsed_condition[0].condition
== "not additional and (test)" # negated condition expression was added
)


### ChangeLogsourceTransformation ###
def test_changelogsource(dummy_pipeline, sigma_rule: SigmaRule):
processing_item = ProcessingItem(
Expand Down

0 comments on commit 4858a71

Please sign in to comment.