Skip to content
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

matching: multi match support #331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

securitym0nkey
Copy link

Make sure these boxes are signed before submitting your Pull Request
-- thank you.

This is an implementation of redmine 2509

Describe changes:

This is a resubmit of pr #37. After reworking to fit the 2023 code base.

It adds support to specify multiple conditions to match a rule.

Syntax:
multi:<match_condition1>;<match_condition2>;...<match_conditionN>;

Examples:
Match all rules including the term "nmap" but just from the "emerging-scan.rules" file.
multi:filename:rule/emerging-scan.rules; re:nmap;

Match all rules with a recent cve reference and a perimeter deployment
multi:re:cve-202[23];metadata: deployment perimeter;

This adds support to specify multiple conditions to match a rule.

Syntax:
multi:<match_condition1>;<match_condition2>;...<match_conditionN>;

Examples:
Match all rules including the term "nmap" but just from the "emerging-scan.rules" file.
multi:filename:rule/emerging-scan.rules; re:nmap;

Match all rules with a recent cve reference and a perimeter deployment
multi:re:cve-202[23];metadata: deployment perimeter;

Ticket: #2509
@securitym0nkey securitym0nkey requested a review from jasonish as a code owner July 31, 2023 12:56
@jasonish
Copy link
Member

jasonish commented Jan 9, 2025

Sorry for taking so long to get to this. One issue I have is not all valid regex's are supported. For example:

re: sid:\s*2100498;\s*rev:7;

works...

but:

multi: re: sid:\s*2100498;\s*rev:7;

appears to work, but actually fails to properly load as it will only take up to the ;, then silently fail to load the remainder. So I think that would need to be addressed with some test cases.

Copy link
Member

@jasonish jasonish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@securitym0nkey
Copy link
Author

That's a good point. ; is definitely not a good delimiter. So what could be a better one? Almost every character might be part of a valid regex, right? Though other delimiters might be less likely to be used in someones regex.

My quick idea would be to use a [TAB] as a delimiter. A good regex would use \t instead of a real [TAB]. But i'm not really satisfied with that solution myself. Any thoughts on what would be acceptable.

Certainly its a good idea to throw some warning or error if a child matcher is not a valid one. I think that would happen with with your example.

@jasonish
Copy link
Member

jasonish commented Feb 1, 2025

That's a good point. ; is definitely not a good delimiter. So what could be a better one? Almost every character might be part of a valid regex, right? Though other delimiters might be less likely to be used in someones regex.

My quick idea would be to use a [TAB] as a delimiter. A good regex would use \t instead of a real [TAB]. But i'm not really satisfied with that solution myself. Any thoughts on what would be acceptable.

Certainly its a good idea to throw some warning or error if a child matcher is not a valid one. I think that would happen with with your example.

Yeah, I'm not sure either. It was not created with more parameters on a line than one in mind... Proper quoting could work, but also a breaking change as we don't currently require quoting now.

@securitym0nkey
Copy link
Author

After some thoughts i would suggest to use && [space][andsign][andsign][space] as the delimiter. I can't come up with a situation where this causes a conflict with other matchers except when the regular expression as exactly && in it. I can't think of a situation where one would write such regex. Obviously it needs to be documented as invalid when using the multi matcher. The workaround would be to encode && as \x26\x26 within the regex.

So your example combined with some other matchers would look something like this.

multi: re: sid:\s*2100498;\s*rev:7; && multi:filename:rule/emerging-scan.rules && metadata: deployment perimeter

Why i like this solution:

  • no quoting, no breaking change
  • && indicates that the multiple matchers are logical AND
  • even the edge case where the delimiter is part of the regex can be addressed

If that's an acceptable solution i would go ahead and implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants