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/bsize: Validate against content buffer when available #4953

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions doc/userguide/rules/payload-keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,40 @@ You can also use the negation (!) before isdataat.
bsize
-----

With the bsize keyword, you can match on the length of the buffer. This adds precision to the content match, previously this could have been done with isdataat.
With the ``bsize`` keyword, you can match on the length of the buffer. This adds precision to the content match, previously this could have been done with isdataat.

An optional operator can be specified; if no operator is present, the operator will default to '='. When a relational
operator is used, e.g., '<', '>' or '<>' (range), the bsize value will be compared using the relational operator.

If the ``content`` keyword immediately precedes ``bsize``, an error will be raised if the content length
and the bsize value prevent a match.

Format::

bsize:<number>;
bsize:=<number>;
bsize:<<number>;
bsize:><number>;
bsize:<lo-number><><hi-number>;

Example of bsize in a rule:
Examples of ``bsize`` in a rule:

.. container:: example-rule

alert dns any any -> any any (msg:"test bsize rule"; dns.query; content:"google.com"; bsize:10; sid:123; rev:1;)

.. container:: example-rule

alert dns any any -> any any (msg:"test bsize rule"; dns.query; content:"short"; bsize:<10; sid:124; rev:1;)

.. container:: example-rule

alert dns any any -> any any (msg:"test bsize rule"; dns.query; content:"longer string"; bsize:>10; sid:125; rev:1;)

.. container:: example-rule

alert dns any any -> any any (msg:"test bsize rule"; dns.query; content:"middle"; bsize:5<>15; sid:126; rev:1;)

dsize
-----

Expand Down
34 changes: 34 additions & 0 deletions src/detect-bsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ void DetectBsizeRegister(void)
#define DETECT_BSIZE_RA 2
#define DETECT_BSIZE_EQ 3

const char *bsize_mode_strings[] = { "<", ">", "<>", "="};

typedef struct DetectBsizeData {
uint8_t mode;
uint64_t lo;
Expand Down Expand Up @@ -291,6 +293,38 @@ static int DetectBsizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *
DetectBsizeData *bsz = DetectBsizeParse(sizestr);
if (bsz == NULL)
goto error;

SigMatch *pm = NULL;
pm = DetectGetLastSMFromLists(s, DETECT_CONTENT, -1);
jlucovsky marked this conversation as resolved.
Show resolved Hide resolved
jlucovsky marked this conversation as resolved.
Show resolved Hide resolved
if (pm != NULL) {
DetectContentData *cd = (DetectContentData *) pm->ctx;
SCLogDebug("Content %.*s, content length %"PRIu16, cd->content_len, cd->content, cd->content_len);
/* Check if match with content is possible */
bool possible = false;
switch (bsz->mode) {
case DETECT_BSIZE_EQ:
possible = bsz->lo == cd->content_len;
break;
case DETECT_BSIZE_GT:
possible = bsz->lo < cd->content_len;
break;
case DETECT_BSIZE_LT:
possible = cd->content_len < bsz->lo;
break;
case DETECT_BSIZE_RA:
possible = bsz->lo < cd->content_len && cd->content_len < bsz->hi;
break;
}
if (!possible) {
SCLogError(SC_ERR_INVALID_SIGNATURE,
"bsize match impossible: content len %d and bsize op '%s' "
"values lo=%"PRIu64"; hi=%"PRIu64,
cd->content_len,
bsize_mode_strings[bsz->mode], bsz->lo,bsz->hi);
goto error;
}
}

sm = SigMatchAlloc();
if (sm == NULL)
goto error;
Expand Down
8 changes: 7 additions & 1 deletion src/tests/detect-bsize.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2017 Open Information Security Foundation
/* Copyright (C) 2017-2020 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
Expand Down Expand Up @@ -119,6 +119,12 @@ static int DetectBsizeSigTest01(void)
TEST_FAIL("alert tcp any any -> any any (content:\"abc\"; bsize:10; sid:3;)");
TEST_FAIL("alert http any any -> any any (content:\"GET\"; http_method; bsize:10; sid:4;)");
TEST_FAIL("alert http any any -> any any (http_request_line; content:\"GET\"; bsize:<10>; sid:5;)");
TEST_FAIL("alert http any any -> any any (http.uri; content:\"abcdefgh123456\";"
"bsize:2; sid:1; rev:1;)");
TEST_OK("alert http any any -> any any (http.uri; content:\"abcdefgh123456\"; bsize:>2; sid:2; rev:1;)");
TEST_FAIL("alert http any any -> any any (http.uri; content:\"abcdefgh123456\"; bsize:<13; sid:3; rev:1;)");
TEST_FAIL("alert http any any -> any any (http.uri; content:\"abcdefgh123456\"; bsize:>15; sid:4; rev:1;)");
TEST_OK("alert http any any -> any any (http.uri; content:\"abcdefgh123456\"; bsize:10<>15; sid:5; rev:1;)");
jlucovsky marked this conversation as resolved.
Show resolved Hide resolved
PASS;
}

Expand Down