suricata
detect-flow-age.c
Go to the documentation of this file.
1/* Copyright (C) 2022 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#include "suricata-common.h"
19#include "rust.h"
20#include "detect-flow-age.h"
21#include "detect-engine.h"
23#include "detect-engine-uint.h"
24#include "detect-parse.h"
25
26static int DetectFlowAgeMatch(
27 DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
28{
29 if (p->flow == NULL) {
30 return 0;
31 }
32 uint64_t age = SCTIME_SECS(p->flow->lastts) - SCTIME_SECS(p->flow->startts);
33 if (age > UINT32_MAX) {
34 age = UINT32_MAX;
35 }
36
37 const DetectU32Data *du32 = (const DetectU32Data *)ctx;
38 return DetectU32Match((uint32_t)age, du32);
39}
40
41static void DetectFlowAgeFree(DetectEngineCtx *de_ctx, void *ptr)
42{
43 SCDetectU32Free(ptr);
44}
45
46static int DetectFlowAgeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
47{
48 DetectU32Data *du32 = DetectU32Parse(rawstr);
49 if (du32 == NULL)
50 return -1;
51
54 DetectFlowAgeFree(de_ctx, du32);
55 return -1;
56 }
58
59 return 0;
60}
61
62static void PrefilterPacketFlowAgeMatch(
63 DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
64{
65 const PrefilterPacketHeaderCtx *ctx = pectx;
66 if (!PrefilterPacketHeaderExtraMatch(ctx, p))
67 return;
68
69 DetectU32Data du32;
70 du32.mode = ctx->v1.u8[0];
71 du32.arg1 = ctx->v1.u32[1];
72 du32.arg2 = ctx->v1.u32[2];
73 if (DetectFlowAgeMatch(det_ctx, p, NULL, (const SigMatchCtx *)&du32)) {
74 PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
75 }
76}
77
78static int PrefilterSetupFlowAge(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
79{
81 PrefilterPacketU32Set, PrefilterPacketU32Compare, PrefilterPacketFlowAgeMatch);
82}
83
84static bool PrefilterFlowAgeIsPrefilterable(const Signature *s)
85{
86 return PrefilterIsPrefilterableById(s, DETECT_FLOW_AGE);
87}
88
90{
92 sigmatch_table[DETECT_FLOW_AGE].desc = "match flow age";
93 sigmatch_table[DETECT_FLOW_AGE].url = "/rules/flow-keywords.html#flow-age";
94 sigmatch_table[DETECT_FLOW_AGE].Match = DetectFlowAgeMatch;
95 sigmatch_table[DETECT_FLOW_AGE].Setup = DetectFlowAgeSetup;
96 sigmatch_table[DETECT_FLOW_AGE].Free = DetectFlowAgeFree;
97 sigmatch_table[DETECT_FLOW_AGE].SupportsPrefilter = PrefilterFlowAgeIsPrefilterable;
98 sigmatch_table[DETECT_FLOW_AGE].SetupPrefilter = PrefilterSetupFlowAge;
99}
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, SignatureMask mask, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
DetectUintData_u32 * DetectU32Parse(const char *u32str)
This function is used to parse u32 options passed via some u32 keyword.
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
void PrefilterPacketU32Set(PrefilterPacketHeaderValue *v, void *smctx)
bool PrefilterPacketU32Compare(PrefilterPacketHeaderValue v, void *smctx)
DetectUintData_u32 DetectU32Data
void DetectFlowAgeRegister(void)
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
#define SIG_MASK_REQUIRE_FLOW
Definition detect.h:312
#define SIG_FLAG_REQUIRE_PACKET
Definition detect.h:254
@ DETECT_SM_LIST_MATCH
Definition detect.h:117
DetectEngineCtx * de_ctx
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
PrefilterRuleStore pmq
Definition detect.h:1349
SCTime_t lastts
Definition flow.h:410
SCTime_t startts
Definition flow.h:493
struct Flow_ * flow
Definition decode.h:546
Container for matching data for a signature group.
Definition detect.h:1629
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition detect.h:1444
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
bool(* SupportsPrefilter)(const Signature *s)
Definition detect.h:1443
Signature container.
Definition detect.h:668
uint32_t flags
Definition detect.h:669
#define SCTIME_SECS(t)
Definition util-time.h:57