Repository navigation
fix: compile |re values with the rule's policy - #585
Open
cristianchiriac wants to merge 1 commit into
Open
cristianchiriac wants to merge 1 commit into
cristianchiriac wants to merge 1 commit into
Conversation
SigmaRegularExpressionModifier created SigmaRegularExpression without a policy, so |re values were always compiled with sigma.default_policy. Rules loaded with SigmaRule/SigmaCollection.from_yaml(..., policy=TrustedPolicy) were still checked with RE2 and rejected as soon as they used constructs like lookaheads that only the Python engine supports. Pass the policy from SigmaDetections.from_dict (and SigmaFilter) through SigmaDetection.from_definition and SigmaDetectionItem.from_mapping to the detection item, and use it in the |re modifier.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
According to the 2.0 release notes, full regex compatibility can be restored with
PythonRegexEngine, e.g. throughTrustedPolicy. That currently doesn't work for|revalues in rules: passing the policy when loading a rule still compiles the regex with RE2.SigmaCollection.from_yaml(..., policy=TrustedPolicy)fails the same way.SigmaRule.from_dictpasses the policy toSigmaDetections.from_dict, but it stops there. Modifiers run when eachSigmaDetectionItemis built, andSigmaRegularExpressionModifier.modify()createsSigmaRegularExpression(val.original)without a policy, socompile()falls back tosigma.default_policy.Change
SigmaDetectionItemgets an optionalpolicyfield (compare=False,repr=False).SigmaDetectionItem.from_mapping()andfrom_value(), andSigmaDetection.from_definition(), take an optionalpolicyargument and pass it on.SigmaDetections.from_dict()andSigmaFilter.from_dict()pass their policy down.|remodifier createsSigmaRegularExpression(..., policy=self.detection_item.policy).All new parameters are keyword-compatible additions with a
Nonedefault, so existing callers are unaffected. Without an explicit policy, behavior is unchanged: RE2 viasigma.default_policy.Testing
tests/test_policy.py:|re|i: 'foo(?=bar)'loads withTrustedPolicy, both viaSigmaRuleandSigmaCollection, and the value carries the policy;-m "not online").mypy sigmaandblack --checkare clean.This came up while running pySigma-validators-sigmaHQ with pySigma 2.0. The tests of
SigmahqUnsupportedRegexGroupConstructValidatorneed to load rules containing lookarounds, and they can't do that even withTrustedPolicyuntil this is fixed.