Skip to content

Commit bb1e60c

Browse files
authored
Merge pull request #375 from Res260/fixtimestamps
Fix timestamp handling when paired with `|gt` `|gte` `|lt` and `|lte`…
2 parents e8294d9 + df647d8 commit bb1e60c

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

sigma/conversion/base.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,11 +1805,27 @@ 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 (
1809+
isinstance(cond.value, SigmaCompareExpression)
1810+
and cond.value.number
1811+
and isinstance(cond.value.number, SigmaTimestampPart)
1812+
and self.field_timestamp_part_expression
1813+
and self.timestamp_part_mapping
1814+
):
1815+
return (
1816+
self.field_timestamp_part_expression.format(
1817+
field=self.escape_and_quote_field(cond.field),
1818+
timestamp_part=self.timestamp_part_mapping[cond.value.number.timestamp_part],
1819+
)
1820+
+ self.compare_operators[cond_value.op]
1821+
+ str(cond.value.number)
1822+
)
1823+
else:
1824+
return self.compare_op_expression.format(
1825+
field=self.escape_and_quote_field(cond.field),
1826+
operator=self.compare_operators[cond_value.op],
1827+
value=cond_value.number,
1828+
)
18131829

18141830
def convert_condition_field_eq_field_escape_and_quote(
18151831
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
SigmaRegularExpression,
2222
SigmaTimestampPart,
2323
TimestampPart,
24+
SigmaCompareExpression,
2425
)
2526
from sigma.modifiers import (
2627
SigmaBase64Modifier,
@@ -1692,7 +1693,7 @@ def test_sigmarule_bad_scope():
16921693
)
16931694

16941695

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

0 commit comments

Comments
 (0)