Skip to content

Commit

Permalink
Additional escape characters in SigmaString.to_regex()
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspatzke committed Oct 14, 2024
1 parent f24860b commit be3e034
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sigma/conversion/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ def convert_condition_field_eq_val_str(
return expr.format(
field=self.escape_and_quote_field(cond.field),
value=self.convert_value_str(value, state),
regex=self.convert_value_re(value.to_regex(), state),
regex=self.convert_value_re(value.to_regex(self.add_escaped_re), state),
backend=self,
)
except TypeError: # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions sigma/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,14 +572,14 @@ def convert(
)
return s

def to_regex(self) -> "SigmaRegularExpression":
def to_regex(self, custom_escaped: str = "") -> "SigmaRegularExpression":
"""Convert SigmaString into a regular expression."""
return SigmaRegularExpression(
self.convert(
escape_char="\\",
wildcard_multi=".*",
wildcard_single=".",
add_escaped=".*+?^$[](){}\\|",
add_escaped=".*+?^$[](){}\\|" + custom_escaped,
)
)

Expand Down
12 changes: 9 additions & 3 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,16 +389,22 @@ def test_strings_convert_invalid_part():


def test_strings_to_regex():
s = SigmaString("Test*Special?(Plain)[\\*\\?]")
s = SigmaString("Test*Special?(Plain)/[\\*\\?]")
assert s.s == (
"Test",
SpecialChars.WILDCARD_MULTI,
"Special",
SpecialChars.WILDCARD_SINGLE,
"(Plain)[*?]",
"(Plain)/[*?]",
)
r = s.to_regex()
assert r.regexp == "Test.*Special.\\(Plain\\)\\[\\*\\?\\]"
assert r.regexp == "Test.*Special.\\(Plain\\)/\\[\\*\\?\\]"


def test_strings_to_regex_with_additional_escape_chars():
s = SigmaString("Test*Special?(Plain)/[\\*\\?]")
r = s.to_regex("/")
assert r.regexp == "Test.*Special.\\(Plain\\)\\/\\[\\*\\?\\]"


def test_string_index(sigma_string):
Expand Down

0 comments on commit be3e034

Please sign in to comment.