Skip to content

Commit

Permalink
Merge pull request #285 from SigmaHQ:issue-284
Browse files Browse the repository at this point in the history
Preserve placeholder in ReplaceStringTransformation
  • Loading branch information
thomaspatzke authored Oct 9, 2024
2 parents ba25155 + 2bcbbd7 commit b84ca95
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sigma/processing/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,10 @@ def apply_string_value(self, field: str, val: SigmaString) -> SigmaString:
sigma_string_plain = str(val)
replaced = self.re.sub(self.replacement, sigma_string_plain)
postprocessed_backslashes = re.sub(r"\\(?![*?])", r"\\\\", replaced)
return SigmaString(postprocessed_backslashes)
if val.contains_placeholder(): # Preserve placeholders
return SigmaString(postprocessed_backslashes).insert_placeholders()
else:
return SigmaString(postprocessed_backslashes)


@dataclass
Expand Down
44 changes: 44 additions & 0 deletions tests/test_processing_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,50 @@ def test_replace_string_specials(dummy_pipeline):
)


def test_replace_string_placeholder(dummy_pipeline):
sigma_rule = SigmaRule.from_dict(
{
"title": "Test",
"logsource": {"category": "test"},
"detection": {
"test": {
"field|expand": "foo%var%bar",
},
"condition": "test",
},
}
)
s_before = sigma_rule.detection.detections["test"].detection_items[0].value[0]
assert s_before == SigmaString("foo%var%bar").insert_placeholders()

transformation = ReplaceStringTransformation("bar", "test")
transformation.apply(dummy_pipeline, sigma_rule)
s = sigma_rule.detection.detections["test"].detection_items[0].value[0]
assert s == SigmaString("foo%var%test").insert_placeholders()


def test_replace_string_no_placeholder(dummy_pipeline):
sigma_rule = SigmaRule.from_dict(
{
"title": "Test",
"logsource": {"category": "test"},
"detection": {
"test": {
"field": "foo%var%bar",
},
"condition": "test",
},
}
)
s_before = sigma_rule.detection.detections["test"].detection_items[0].value[0]
assert s_before == SigmaString("foo%var%bar")

transformation = ReplaceStringTransformation("bar", "test")
transformation.apply(dummy_pipeline, sigma_rule)
s = sigma_rule.detection.detections["test"].detection_items[0].value[0]
assert s == SigmaString("foo%var%test")


def test_replace_string_skip_specials(dummy_pipeline):
sigma_rule = SigmaRule.from_dict(
{
Expand Down

0 comments on commit b84ca95

Please sign in to comment.