Skip to content

Commit 851321b

Browse files
authored
Merge pull request #207 from SigmaHQ/issue-179
Ignore detections with leading underscore "_" in "of" condition pattern matching
2 parents 70e0b11 + e222785 commit 851321b

File tree

4 files changed

+66
-25
lines changed

4 files changed

+66
-25
lines changed

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Reformatting by black
22
a6b11efb3455b0d13d4d712413052061580d43c4
3+
6b2893d0a7a7771984937d68d3f0306fe164d94a
34
# Set line-length to 100 for black
45
17185b23e5d18f0977eaf0f96bd941fb6c8d53f1
56

poetry.lock

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sigma/conditions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ class ConditionSelector(ConditionItem):
179179
def __post_init__(self):
180180
if self.args[0] in ["1", "any"]:
181181
self.cond_class = ConditionOR
182-
else:
182+
elif self.args[0] == "all":
183183
self.cond_class = ConditionAND
184+
else:
185+
raise SigmaConditionError("Invalid quantifier in selector", source=self.source)
184186
self.pattern = self.args[1]
185187

186188
def resolve_referenced_detections(self, detections: "sigma.rule.SigmaDetections") -> List[str]:
@@ -195,7 +197,7 @@ def resolve_referenced_detections(self, detections: "sigma.rule.SigmaDetections"
195197
return [
196198
ConditionIdentifier([identifier])
197199
for identifier in detections.detections.keys()
198-
if r.match(identifier)
200+
if r.match(identifier) and not identifier.startswith("_")
199201
]
200202

201203
def postprocess(

tests/test_conditions.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ def sigma_invalid_detections():
160160
)
161161

162162

163+
@pytest.fixture
164+
def sigma_underscore_detections():
165+
return SigmaDetections(
166+
{
167+
"detection_1": SigmaDetection(
168+
[
169+
SigmaDetectionItem(None, [], [SigmaString("val1")]),
170+
]
171+
),
172+
"detection_2": SigmaDetection(
173+
[
174+
SigmaDetectionItem(None, [], [SigmaString("val2")]),
175+
]
176+
),
177+
"_detection_3": SigmaDetection(
178+
[
179+
SigmaDetectionItem(None, [], [SigmaString("val3")]),
180+
]
181+
),
182+
},
183+
list(),
184+
)
185+
186+
163187
def test_or(sigma_simple_detections):
164188
assert SigmaCondition(
165189
"detection1 or detection2", sigma_simple_detections
@@ -381,6 +405,20 @@ def test_selector_all_of_them(sigma_simple_detections):
381405
)
382406

383407

408+
def test_selector_underscore_filter(sigma_underscore_detections):
409+
assert SigmaCondition("any of them", sigma_underscore_detections).parsed == ConditionOR(
410+
[
411+
ConditionValueExpression(SigmaString("val1")),
412+
ConditionValueExpression(SigmaString("val2")),
413+
]
414+
)
415+
416+
417+
def test_selector_invalid_quantifier(sigma_simple_detections):
418+
with pytest.raises(SigmaConditionError, match="Invalid quantifier"):
419+
ConditionSelector("invalid", "them")
420+
421+
384422
def test_keyword_detection(sigma_detections):
385423
assert SigmaCondition("keywords", sigma_detections).parsed == ConditionOR(
386424
[

0 commit comments

Comments
 (0)