-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Flow rate initial support/v2 #12236
base: master
Are you sure you want to change the base?
Flow rate initial support/v2 #12236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* Copyright (C) 2024 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 | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* version 2 along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
/** | ||
* \file | ||
* | ||
* \author Shivani Bhardwaj <shivani@oisf.net> | ||
*/ | ||
|
||
#include "suricata-common.h" | ||
#include "rust.h" | ||
#include "detect-flow-rate.h" | ||
#include "detect-engine.h" | ||
#include "detect-engine-prefilter.h" | ||
#include "detect-engine-uint.h" | ||
#include "detect-parse.h" | ||
|
||
static int DetectFlowRateMatch( | ||
DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx) | ||
{ | ||
if (p->flow == NULL) { | ||
return 0; | ||
} | ||
|
||
uint64_t age = SCTIME_SECS(p->flow->lastts) - SCTIME_SECS(p->flow->startts); | ||
uint64_t rate = (p->flow->tosrcbytecnt + p->flow->todstbytecnt) / age; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Watch for division by zero ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed. an embarrassing mistake. thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also shows lack of tests btw. Something like this should have easily been caught |
||
|
||
const DetectFlowRate *expected = (const DetectFlowRate *)ctx; | ||
return DetectU64Match(rate, expected->rate); | ||
} | ||
|
||
static int DetectFlowRateSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr) | ||
{ | ||
DetectU64Data *du64 = DetectU64Parse(rawstr); | ||
if (du64 == NULL) | ||
return -1; | ||
|
||
DetectFlowRate *fr = SCCalloc(1, sizeof(DetectFlowRate)); | ||
if (fr == NULL) | ||
return -1; | ||
|
||
fr->rate = du64; | ||
|
||
if (SigMatchAppendSMToList( | ||
de_ctx, s, DETECT_FLOW_RATE, (SigMatchCtx *)fr, DETECT_SM_LIST_MATCH) == NULL) { | ||
return -1; | ||
} | ||
s->flags |= SIG_FLAG_REQUIRE_PACKET; | ||
|
||
return 0; | ||
} | ||
|
||
static void DetectFlowRateFree(DetectEngineCtx *de_ctx, void *ptr) | ||
{ | ||
DetectFlowRate *fr = (DetectFlowRate *)ptr; | ||
if (fr != NULL) { | ||
rs_detect_u64_free(fr->rate); | ||
SCFree(fr); | ||
} | ||
} | ||
|
||
void DetectFlowRateRegister(void) | ||
{ | ||
sigmatch_table[DETECT_FLOW_RATE].name = "flow.rate"; | ||
sigmatch_table[DETECT_FLOW_RATE].desc = "match flow rate"; | ||
sigmatch_table[DETECT_FLOW_RATE].url = "/rules/flow-keywords.html#flow-rate"; | ||
sigmatch_table[DETECT_FLOW_RATE].Match = DetectFlowRateMatch; | ||
sigmatch_table[DETECT_FLOW_RATE].Setup = DetectFlowRateSetup; | ||
sigmatch_table[DETECT_FLOW_RATE].Free = DetectFlowRateFree; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* Copyright (C) 2024 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 | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* version 2 along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
#ifndef SURICATA_DETECT_FLOW_RATE_H | ||
#define SURICATA_DETECT_FLOW_RATE_H | ||
|
||
typedef struct DetectFlowRate_ { | ||
DetectUintData_u64 *rate; | ||
} DetectFlowRate; | ||
|
||
void DetectFlowRateRegister(void); | ||
|
||
#endif /* SURICATA_DETECT_FLOW_RATE_H */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of mismatching units. Considering keeping the initial syntax something like
flow.rate:>500kb,10
where 500kb is the number of bytes and 10 the seconds with which the rate will be calculated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some other ideas, none of which I'm a fan of:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Between those, I like:
The most, I think. I imagine that any format would need documenting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may also want packets instead of bytes like
flow.rate: packets 50, seconds 10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will you also want milliseconds ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@catenacyber I don't know yet. Do you think somebody might want to express rate as 10k bytes in 5ms?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know either, but maybe more granularity/expressivity is always better ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Shall keep that in mind. Thanks a lot for your valuable feedback, both! 🙇🏽♀️