[Bug] Rule Toml Write Formatting Wrongly Formats \\\\x#4978
[Bug] Rule Toml Write Formatting Wrongly Formats \\\\x#4978eric-forte-elastic merged 13 commits intomainfrom
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
|
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.
I think we will want a new unit test in test_toml_formatter.py
There was a problem hiding this comment.
++ 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.
can we add another test to make sure \\\\x is not converted to \\u00?
There was a problem hiding this comment.
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"
|




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
\\\\xwhich would trigger an unwanted replacement to\\u00and calls python's string formatting directly instead of toml's string dump.How To Test
2d2c5b4rules/windows/privilege_escalation_printspooler_service_suspicious_file.tomlfrom this commit as well.python -m detection_rules dev trim-version-lock 8.14.0that would same_toml on a rule with\\\\x[rule.filters.query.wildcard."file.path"]values are do not have\\\\xreplaced with\\u00whereas they did in Prep main for 9.1 #4555Checklist
bug,enhancement,schema,maintenance,Rule: New,Rule: Deprecation,Rule: Tuning,Hunt: New, orHunt: Tuningso guidelines can be generatedmeta:rapid-mergelabel if planning to merge within 24 hoursContributor checklist