fix: combine matching mapping preconditions - #244
Open
a-bonfim-tech wants to merge 1 commit into
Open
a-bonfim-tech wants to merge 1 commit into
a-bonfim-tech wants to merge 1 commit into
Conversation
Collaborator
|
Hi @a-bonfim-tech, Thanks for raising but #107 is still open because the final piece of work there would be about applying preconditions to a subset of rules. Whereas this PR looks to be about addressing a Are we sure we would want to let mut preconds = FxHashMap::default();
if let Some(extensions) = &mapping.extensions && let Some(preconditions) = &extensions.preconditions
{
for (rid, rule) in &rules {
if let Rule::Sigma(sigma) = rule {
let mut filters = Vec::with_capacity(preconditions.len());
for precondition in preconditions {
if precondition.for_.is_empty() {
continue;
}
let mut matched = true;
for (f, v) in &precondition.for_ {
match sigma.find(f) {
Some(value) => {
if value.as_str() != Some(v.as_str()) {
matched = false;
break;
}
}
None => {
matched = false;
break;
}
}
}
if matched {
filters.push(precondition.filter.clone());
}
}
if !filters.is_empty() {
preconds.insert(*rid, Expression::BooleanGroup(BoolSym::And, filters));
}
}
}
} |
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.
Summary
Fix the remaining multi-match behavior tracked in #107.
When more than one mapping precondition matches the same Sigma rule, Chainsaw currently stores preconditions in a map keyed by rule ID and the later match overwrites the earlier one.
This change preserves every matching precondition for a rule and requires all of them to pass before the Sigma rule is evaluated.
Changes
Vec<Expression>per rule ID.Scope
This PR only addresses the multiple-matching-preconditions overwrite behavior discussed in #107. It does not change the mapping schema, Sigma rule parsing, negation syntax, or existing mappings.
Validation
Static diff review and regression tests were added. I was not able to run the Rust test suite locally in the current environment because external dependency resolution via GitHub was unavailable.