-
Notifications
You must be signed in to change notification settings - Fork 604
[Bug] Rule Toml Write Formatting Wrongly Formats \\\\x #4978
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
[Bug] Rule Toml Write Formatting Wrongly Formats \\\\x #4978
Conversation
Bug - GuidelinesThese guidelines serve as a reminder set of considerations when addressing a bug in the code. Documentation and Context
Code Standards and Practices
Testing
Additional Checks
|
⛔️ Test failed Results
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you discover why this didn't appear in the past with rule rules/windows/privilege_escalation_printspooler_service_suspicious_file.toml ? We should figure that out in case it influences how we address the root issue.
Yes, the key for this is that this issue occurs when the string contains Following the commit history of the rule, the string had The one of the next updates then added a min stack, which would not be impacted by the trim until #4555. The only other change prior to #4555, was in generating investigation guides. The generation appears not to have used a dev function, but some custom code as mentioned in: #4358. This custom code used the At which point, the PR #4555 looks to be the first time we run this rule through the Additionally, this looks to be the first time we have sent a rule that had |
⛔️ Test failed Results
|
detection_rules/rule_formatter.py
Outdated
if "\\\\x" in v: | ||
return f'"{v!s}"' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we will want a new unit test in test_toml_formatter.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++ agreed, added new unit test that looks for this specifically. It also will fail if the path behavior changes. E.g. currently we expect \\\\
to be formatted to \\
. If this changes the unit test will fail on purpose as we want to match Query DSL's path handling which does this as well.
tests/test_toml_formatter.py
Outdated
self.compare_test_data(self.test_data[1:]) | ||
self.compare_test_data(self.test_data[2:]) | ||
|
||
def test_formatter_paths(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add another test to make sure \\\\x
is not converted to \\u00
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this test not accomplish that?
def test_formatter_paths(self):
"""Test that paths are handled as expected with toml lib."""
with self.assertRaisesRegex(
AssertionError,
r'\+ {"metadata": {"field": "value"}, "rule": {"path": "\?:\\\\Windows\\\\Sys\?\?\?\?\?\\\\x5lrs\.dll"}}',
):
self.compare_test_data([self.test_data[1]])
This output checks the need for "?:\\\\Windows\\\\Sys?????\\\\x5lrs.dll"
to explicitly become "?:\\Windows\\Sys?????\\x5lrs.dll"
If the \\\\x
is converted to \\u00
it will fail. It will fail if \\\\x
is not converted to exactly \\x
(e.g. \\\\x
-> \\\\x
will also fail as is its intention)
The regex filter to catch the specific transformation
The +
line needs to match exactly in order for this unit test to pass.
Is this accomplishing what you are looking for?
…of github.com:elastic/detection-rules into 4977-bug-rule-toml-write-formatting-wrongly-formats-x
Updated to catch and additional issue where where backslashes in certain strings are not properly escaped in self._old_dump_str(v). The input and output paths should now match for the filters. As a note, Query DSL does require 4 The unit test now includes a path that otherwise would have been escaped or transformed with the magic To test that the fix does not introduce any other concerns, checkout the commit before the change Expected Output
Needs to match the path formatting from https://github.com/elastic/detection-rules/pull/4976/files Which in our testing it does. ❯ diff my_patch_without_fix.patch my_patch_with_fix.patch
28856c28856
< index 30d052601..bbfa62985 100644
---
> index 30d052601..4f0f3fb4c 100644
28966c28966
< +value = "?:\\Windows\\Sys?????\\u005lrs.dll"
---
> +value = "?:\\\\Windows\\\\Sys?????\\\\x5lrs.dll"
28976c28976
< +value = "?:\\Windows\\system32\\spool\\DRIVERS\\u0064\\\\*.dll"
---
> +value = "?:\\\\Windows\\\\system32\\\\spool\\\\DRIVERS\\\\x64\\\\*.dll"
28996c28996
< +value = "?:\\Windows\\system32\\spool\\PRTPROCS\\u0064\\\\*.dll"
---
> +value = "?:\\\\Windows\\\\system32\\\\spool\\\\PRTPROCS\\\\x64\\\\*.dll"
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
Pull Request
Issue link(s):
Relates Issue: #4968
Resolves #4977
Summary - What I changed
Note: Please review this fairly carefully as we need to check for unintended consequences and whether or not this is the true root cause of the issue that broke the rule in #4977
I added a hardcoded check to mitigate an unwated behavior of Python's toml library string dump formatting (see Additional Context from #4977).
This checks for the presence of
\\\\x
which would trigger an unwanted replacement to\\u00
and calls python's string formatting directly instead of toml's string dump.How To Test
2d2c5b4
rules/windows/privilege_escalation_printspooler_service_suspicious_file.toml
from this commit as well.python -m detection_rules dev trim-version-lock 8.14.0
that would same_toml on a rule with\\\\x
[rule.filters.query.wildcard."file.path"]
values are do not have\\\\x
replaced with\\u00
whereas they did in Prep main for 9.1 #4555Checklist
bug
,enhancement
,schema
,maintenance
,Rule: New
,Rule: Deprecation
,Rule: Tuning
,Hunt: New
, orHunt: Tuning
so guidelines can be generatedmeta:rapid-merge
label if planning to merge within 24 hoursContributor checklist