Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where SigmaRegularExpression.escape() was buggy when using … #154

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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