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 #12542

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

jlucovsky
Copy link
Contributor

Continuation of #12539

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

Updates:

  • Removed crate added for float assertions
  • Clarified documentation example showing default values.

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 causes the content inspection engine to recognize and
invoke the entropy "match" function when the entropy keyword is used.

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

Both are update to document the new entropy keyword.
Copy link

codecov bot commented Feb 7, 2025

Codecov Report

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

Project coverage is 80.73%. Comparing base (d4330ef) to head (37d9a5b).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12542      +/-   ##
==========================================
+ Coverage   80.68%   80.73%   +0.04%     
==========================================
  Files         925      928       +3     
  Lines      258914   259518     +604     
==========================================
+ Hits       208914   209516     +602     
- Misses      50000    50002       +2     
Flag Coverage Δ
fuzzcorpus 56.80% <2.75%> (-0.03%) ⬇️
livemode 19.38% <2.75%> (-0.04%) ⬇️
pcap 44.14% <2.75%> (-0.05%) ⬇️
suricata-verify 63.42% <71.42%> (+0.03%) ⬆️
unittests 58.43% <83.27%> (+0.05%) ⬆️

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

@suricata-qa
Copy link

WARNING:

field baseline test %
SURI_TLPR1_stats_chk
.uptime 636 658 103.46%

Pipeline 24668

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.

Sorry I didn't include these in yesterdays review, but got distracted after reading just the documentation.

Comment on lines +33 to +40
#[repr(C)]
#[derive(Debug)]
pub struct DetectEntropyData {
offset: i32,
nbytes: i32,
value: DetectFloatData<f64>,
flags: u8,
}
Copy link
Member

Choose a reason for hiding this comment

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

This looks like its used opaquely from C? If so, we can drop the repr here to avoid providing the full structure to C, which IMO is a good idea unless C needs to get at the internals.

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.

Comment on lines +143 to +149
fn calculate_entropy(data: *const u8, length: i32) -> f64 {
if data.is_null() || length <= 0 {
return 0.0;
}

// Convert the raw pointer to a slice safely
let data_slice = unsafe { slice::from_raw_parts(data, length as usize) };
Copy link
Member

Choose a reason for hiding this comment

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

This conversion should be done in the calling function, then it can just pass the &[u8] here without length and avoid the unsafe here, since SCDetectEntropyMatch is already unsafe. Also makes its more usable from Rust if ever needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I'll make this change.

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