Skip to content

Commit 76f8fd4

Browse files
committed
Fix timestamp handling when paired with |gt |gte |lt and |lte modifiers.
1 parent 2eae03f commit 76f8fd4

3 files changed

Lines changed: 46 additions & 8 deletions

File tree

sigma/conversion/base.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,11 +1805,17 @@ def convert_condition_field_compare_op_val(
18051805
"Expected SigmaCompareExpression for cond.value, got {type(cond_value)}"
18061806
)
18071807

1808-
return self.compare_op_expression.format(
1809-
field=self.escape_and_quote_field(cond.field),
1810-
operator=self.compare_operators[cond_value.op],
1811-
value=cond_value.number,
1812-
)
1808+
if isinstance(cond.value, SigmaCompareExpression) and cond.value.number and isinstance(cond.value.number, SigmaTimestampPart):
1809+
return self.field_timestamp_part_expression.format(
1810+
field=self.escape_and_quote_field(cond.field),
1811+
timestamp_part=self.timestamp_part_mapping[cond.value.number.timestamp_part],
1812+
) + self.compare_operators[cond_value.op] + str(cond.value.number)
1813+
else:
1814+
return self.compare_op_expression.format(
1815+
field=self.escape_and_quote_field(cond.field),
1816+
operator=self.compare_operators[cond_value.op],
1817+
value=cond_value.number,
1818+
)
18131819

18141820
def convert_condition_field_eq_field_escape_and_quote(
18151821
self, field1: str, field2: str

tests/test_conversion_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3500,6 +3500,6 @@ def test_convert_timestamp_part_modifiers(test_backend, monkeypatch):
35003500
)
35013501
)
35023502
== [
3503-
'strftime(timestamp, "%M")=1 and strftime(timestamp, "%H")=2 and strftime(timestamp, "%d")=3 and strftime(timestamp, "%V")=4 and strftime(timestamp, "%m")=5 and strftime(timestamp, "%Y")=6 and timestamp>7 and timestamp>=8 and timestamp<9 and timestamp<=10 and timestamp>11 and timestamp>=12'
3503+
'strftime(timestamp, "%M")=1 and strftime(timestamp, "%H")=2 and strftime(timestamp, "%d")=3 and strftime(timestamp, "%V")=4 and strftime(timestamp, "%m")=5 and strftime(timestamp, "%Y")=6 and strftime(timestamp, "%M")>7 and strftime(timestamp, "%H")>=8 and strftime(timestamp, "%d")<9 and strftime(timestamp, "%V")<=10 and strftime(timestamp, "%m")>11 and strftime(timestamp, "%Y")>=12'
35043504
]
35053505
)

tests/test_rule.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
SigmaNull,
2121
SigmaRegularExpression,
2222
SigmaTimestampPart,
23-
TimestampPart,
23+
TimestampPart, SigmaCompareExpression,
2424
)
2525
from sigma.modifiers import (
2626
SigmaBase64Modifier,
@@ -1692,7 +1692,7 @@ def test_sigmarule_bad_scope():
16921692
)
16931693

16941694

1695-
def test_sigmarule_timestamp_modifiers():
1695+
def test_sigmarule_timestamp_modifiers_equals():
16961696
rule = SigmaRule.from_dict(
16971697
{
16981698
"title": "Test",
@@ -1721,3 +1721,35 @@ def test_sigmarule_timestamp_modifiers():
17211721
assert detection_items[3].value[0] == SigmaTimestampPart(TimestampPart.WEEK, 4)
17221722
assert detection_items[4].value[0] == SigmaTimestampPart(TimestampPart.MONTH, 5)
17231723
assert detection_items[5].value[0] == SigmaTimestampPart(TimestampPart.YEAR, 6)
1724+
1725+
1726+
def test_sigmarule_timestamp_modifiers_greater_than():
1727+
rule = SigmaRule.from_dict(
1728+
{
1729+
"title": "Test",
1730+
"logsource": {
1731+
"category": "process_creation",
1732+
"product": "windows",
1733+
},
1734+
"detection": {
1735+
"selection": {
1736+
"timestamp|minute|gt": 1,
1737+
"timestamp|hour|gte": 2,
1738+
"timestamp|day|lt": 3,
1739+
"timestamp|week|lte": 4,
1740+
"timestamp|month|gt": 5,
1741+
"timestamp|year|gte": 6,
1742+
},
1743+
"condition": "selection",
1744+
},
1745+
},
1746+
source=sigma_exceptions.SigmaRuleLocation("test.yml"),
1747+
)
1748+
detection_items = rule.detection["selection"].detection_items
1749+
assert isinstance(detection_items[0].value[0], SigmaCompareExpression)
1750+
assert detection_items[0].value[0].number == SigmaTimestampPart(TimestampPart.MINUTE, 1)
1751+
assert detection_items[1].value[0].number == SigmaTimestampPart(TimestampPart.HOUR, 2)
1752+
assert detection_items[2].value[0].number == SigmaTimestampPart(TimestampPart.DAY, 3)
1753+
assert detection_items[3].value[0].number == SigmaTimestampPart(TimestampPart.WEEK, 4)
1754+
assert detection_items[4].value[0].number == SigmaTimestampPart(TimestampPart.MONTH, 5)
1755+
assert detection_items[5].value[0].number == SigmaTimestampPart(TimestampPart.YEAR, 6)

0 commit comments

Comments
 (0)