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

detect/entropy: Add entropy keyword #12539

Closed
wants to merge 5 commits into from
Closed

Conversation

jlucovsky
Copy link
Contributor

Continuation of #12385

The entropy keyword syntax is the keyword entropy followed by options
and the entropy value for comparison.

The minimum entropy keyword specification is:
entropy: value <entropy-spec>

This results in the calculated entropy value being compared with
with the equality operator.

A match occurs when the values and operator agree. This example matches
if the calculated and entropy value are the same.

When entropy keyword options are specified, all options and "value" must
be comma-separated. Options and value may be specified in any order.

Options have default values:

  • bytes is equal to the current content length
  • offset is 0
  • comparison with value is equality

entropy: [bytes <byteval>] [offset <offsetval>] value <entropy-spec>

Using default values:
entropy: bytes 0, offset 0, value =<entropy-spec>

is: (see below) and a value, e.g., "< 4.1"

The following operators are available from the float crate introduced with this pr:
- = (default): Match when calculated entropy value equals specified entropy value
- < Match when calculated entropy value is strictly less than specified entropy value
- <= Match when calculated entropy value is less than or equal to the specified entropy value
- > Match when calculated entropy value is strictly greater than specified entropy value
- >= Match when calculated entropy value is greater than or equal to the specified entropy value
- != Match when the calculated entropy value is not equal to the specified entropy value
- x-y Match when calculated entropy value is in the range, exclusive
- !x-y Match when calculated entropy value is not in the range, exclusive
Link to ticket: https://redmine.openinfosecfoundation.org/issues/4162

Describe changes:

  • New float crate -- similar to unit crate -- for floating-point usage
  • Entropy parsing/calculation logic
  • Entropy keyword
  • Add entropy handling to content inspection
  • Documentation

Provide values to any of the below to override the defaults.

  • To use an LibHTP, Suricata-Verify or Suricata-Update pull request,
    link to the pull request in the respective _BRANCH variable.

SV_BRANCH=OISF/suricata-verify#2232

Issue: 4162

This commit adds a floating-point analog to `uint.rs` targeted
initially to be used by the forthcoming entropy keyword.
This commit adds
- Parser for the entropy keyword
- Calculation of content the Shannon entropy value

Issue: 4162

The entropy keyword syntax is the keyword entropy followed by options
and the entropy value for comparison.

The minimum entropy keyword specification is:
entropy: value <entropy-spec>

This results in the calculated entropy value being compared with
<entropy-spec> with the equality operator.

A match occurs when the values and operator agree. This example matches
if the calculated and entropy value are the same.

When entropy keyword options are specified, all options and "value" must
be comma-separated. Options and value may be specified in any order.

Options have default values:
- bytes is equal to the current content length
- offset is 0
- comparison with value is equality

entropy: [bytes <byteval>] [offset <offsetval>] value <entropy-spec>

Using default values:
entropy: bytes 0, offset 0, value =<entropy-spec>

<entropy-spec> is: <operator> (see below) and a value, e.g., "< 4.1"

The following operators are available from the float crate:
    - =  (default): Match when calculated entropy value equals specified entropy value
    - <  Match when calculated entropy value is strictly less than specified entropy value
    - <= Match when calculated entropy value is less than or equal to specified entropy value
    - >  Match when calculated entropy value is strictly greater than specified entropy value
    - >= Match when calculated entropy value is greater than or equal to specified entropy value
    - != Match when calculated entropy value is not equal to specified entropy value
    - x-y Match when calculated entropy value is in the range, exclusive
    - !x-y Match when calculated entropy value is not in the range, exclusive
This commit adds keyword/build support for the entropy keyword. The
entropy keyword compares an entropy value with a value calculated
according to the Shannon entropy on the available content.

Issue: 4162
This commit updates the
- Upgrade notes for 7 to 8
- Payload keyword section

Both are update to document the new entropy keyword.
This commit causes the content inspection engine to recognize and
invoke the entropy "match" function when the entropy keyword is used.

Issue: 4162
Copy link

codecov bot commented Feb 6, 2025

Codecov Report

Attention: Patch coverage is 90.69021% with 58 lines in your changes missing coverage. Please review.

Project coverage is 80.72%. Comparing base (d4330ef) to head (83fbba8).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12539      +/-   ##
==========================================
+ Coverage   80.68%   80.72%   +0.03%     
==========================================
  Files         925      928       +3     
  Lines      258914   259537     +623     
==========================================
+ Hits       208914   209511     +597     
- Misses      50000    50026      +26     
Flag Coverage Δ
fuzzcorpus 56.78% <2.75%> (-0.05%) ⬇️
livemode 19.38% <2.75%> (-0.03%) ⬇️
pcap 44.10% <2.75%> (-0.10%) ⬇️
suricata-verify 63.41% <71.42%> (+0.02%) ⬆️
unittests 58.44% <83.78%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@suricata-qa
Copy link

Information: QA ran without warnings.

Pipeline 24649

#[cfg(test)]
mod tests {
use super::*;
use assert_float_eq::*;
Copy link
Member

Choose a reason for hiding this comment

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

Just a little hesitation on adding another crate. Would have formatting to a string with 2 or 3 decimal points and comparing that have worked just as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll work things so the crate's no longer required.


This example shows all possible options with default values::

entropy: bytes 0, offset 0, value =<entropy-val>
Copy link
Member

Choose a reason for hiding this comment

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

For an example, can we use a real value so we're not mixing in chars like < and > in the placeholder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks ... will do.

@jlucovsky
Copy link
Contributor Author

Continued in #12542

@jlucovsky jlucovsky closed this Feb 7, 2025
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.

3 participants