Skip to content

Commit

Permalink
Merge pull request #154 from Res260/fixescaperegex
Browse files Browse the repository at this point in the history
Fix a bug where SigmaRegularExpression.escape() was buggy when using …
  • Loading branch information
thomaspatzke committed Nov 4, 2023
2 parents b521870 + 1786ad9 commit 5938f0c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
22 changes: 13 additions & 9 deletions sigma/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from math import inf
import re
from abc import ABC
from dataclasses import dataclass, field
from enum import Enum, auto
from ipaddress import IPv4Network, IPv6Network, ip_network
from math import inf
from typing import (
ClassVar,
Dict,
Expand All @@ -14,18 +18,14 @@
Callable,
Iterator,
)
from abc import ABC
from dataclasses import dataclass, field
from enum import Enum, auto
import re

from sigma.exceptions import (
SigmaPlaceholderError,
SigmaRuleLocation,
SigmaValueError,
SigmaRegularExpressionError,
SigmaTypeError,
)
from ipaddress import IPv4Network, IPv6Network, ip_network


class SpecialChars(Enum):
Expand Down Expand Up @@ -644,9 +644,13 @@ def escape(
if e is not None
]
)
pos = [ # determine positions of matches in regular expression
m.start() for m in re.finditer(r, self.regexp)
]
pos = (
[ # determine positions of matches in regular expression
m.start() for m in re.finditer(r, self.regexp)
]
if r is not ""
else []
)
ranges = zip([None, *pos], [*pos, None]) # string chunk ranges with escapes in between
ranges = list(ranges)

Expand Down
28 changes: 21 additions & 7 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from ipaddress import IPv4Network, IPv6Network
import re
from ipaddress import IPv4Network, IPv6Network

import pytest

from sigma.exceptions import (
SigmaPlaceholderError,
SigmaTypeError,
SigmaValueError,
SigmaRegularExpressionError,
)
from sigma.types import (
SigmaBool,
SigmaCasedString,
Expand All @@ -17,12 +25,6 @@
sigma_type,
SigmaCIDRExpression,
)
from sigma.exceptions import (
SigmaPlaceholderError,
SigmaTypeError,
SigmaValueError,
SigmaRegularExpressionError,
)


@pytest.fixture
Expand Down Expand Up @@ -509,6 +511,18 @@ def test_re_escape_without_escape():
)


def test_re_escape_with_escape_escape_char_param():
# See issue #153 https://github.com/SigmaHQ/pySigma/issues/153
assert (
SigmaRegularExpression("bitsadmin\\.exe").escape(escape_escape_char=True)
== "bitsadmin\\\\.exe"
)
assert (
SigmaRegularExpression("bitsadmin\\.exe").escape(escape_escape_char=False)
== "bitsadmin\\.exe"
)


def test_bool():
assert SigmaBool(True).boolean == True

Expand Down

0 comments on commit 5938f0c

Please sign in to comment.