suricata
detect-engine-helper.c
Go to the documentation of this file.
1 /* Copyright (C) 2023 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 /**
19  * \file
20  *
21  * \author Philippe Antoine <p.antoine@catenacyber.fr>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "detect-engine.h"
27 #include "detect-engine-helper.h"
28 #include "detect-engine-mpm.h"
30 #include "detect-parse.h"
32 #include "rust.h"
33 
34 int SCDetectHelperBufferRegister(const char *name, AppProto alproto, uint8_t direction)
35 {
36  if (direction & STREAM_TOSERVER) {
39  }
40  if (direction & STREAM_TOCLIENT) {
43  }
45 }
46 
47 int SCDetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto,
48  uint8_t direction, InspectionSingleBufferGetDataPtr GetData)
49 {
50  if (direction & STREAM_TOSERVER) {
54  name, SIG_FLAG_TOSERVER, 2, PrefilterSingleMpmRegister, GetData, alproto, 0);
55  }
56  if (direction & STREAM_TOCLIENT) {
60  name, SIG_FLAG_TOCLIENT, 2, PrefilterSingleMpmRegister, GetData, alproto, 0);
61  }
64 }
65 
66 int SCDetectHelperBufferProgressMpmRegister(const char *name, const char *desc, AppProto alproto,
67  uint8_t direction, InspectionSingleBufferGetDataPtr GetData, int progress)
68 {
69  if (direction & STREAM_TOSERVER) {
73  name, SIG_FLAG_TOSERVER, 2, PrefilterSingleMpmRegister, GetData, alproto, progress);
74  }
75  if (direction & STREAM_TOCLIENT) {
79  name, SIG_FLAG_TOCLIENT, 2, PrefilterSingleMpmRegister, GetData, alproto, progress);
80  }
83 }
84 
85 int SCDetectHelperMultiBufferProgressMpmRegister(const char *name, const char *desc,
86  AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData, int progress)
87 {
88  if (direction & STREAM_TOSERVER) {
89  DetectAppLayerMultiRegister(name, alproto, SIG_FLAG_TOSERVER, progress, GetData, 2);
90  }
91  if (direction & STREAM_TOCLIENT) {
92  DetectAppLayerMultiRegister(name, alproto, SIG_FLAG_TOCLIENT, progress, GetData, 2);
93  }
97 }
98 
99 int SCDetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppProto alproto,
100  uint8_t direction, InspectionMultiBufferGetDataPtr GetData)
101 {
102  return SCDetectHelperMultiBufferProgressMpmRegister(name, desc, alproto, direction, GetData, 0);
103 }
104 
106 {
108  void *tmp = SCRealloc(
110  if (unlikely(tmp == NULL)) {
111  return -1;
112  }
113  sigmatch_table = tmp;
116  }
117 
119  return DETECT_TBLSIZE_IDX - 1;
120 }
121 
123 {
124  int keyword_id = SCDetectHelperNewKeywordId();
125  if (keyword_id < 0) {
126  return -1;
127  }
128 
129  sigmatch_table[keyword_id].name = kw->name;
130  sigmatch_table[keyword_id].desc = kw->desc;
131  sigmatch_table[keyword_id].url = kw->url;
132  sigmatch_table[keyword_id].flags = kw->flags;
133  sigmatch_table[keyword_id].AppLayerTxMatch =
134  (int (*)(DetectEngineThreadCtx * det_ctx, Flow * f, uint8_t flags, void *alstate,
135  void *txv, const Signature *s, const SigMatchCtx *ctx)) kw->AppLayerTxMatch;
136  sigmatch_table[keyword_id].Setup =
137  (int (*)(DetectEngineCtx * de, Signature * s, const char *raw)) kw->Setup;
138  sigmatch_table[keyword_id].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;
139 
140  return (uint16_t)keyword_id;
141 }
142 
143 void SCDetectHelperKeywordAliasRegister(uint16_t kwid, const char *alias)
144 {
145  sigmatch_table[kwid].alias = alias;
146 }
147 
149 {
150  int transform_id = SCDetectHelperNewKeywordId();
151  if (transform_id < 0) {
152  return -1;
153  }
154 
155  sigmatch_table[transform_id].name = kw->name;
156  sigmatch_table[transform_id].desc = kw->desc;
157  sigmatch_table[transform_id].url = kw->url;
158  sigmatch_table[transform_id].flags = kw->flags;
159  sigmatch_table[transform_id].Transform =
160  (void (*)(DetectEngineThreadCtx * det_ctx, InspectionBuffer * buffer, void *options))
161  kw->Transform;
162  sigmatch_table[transform_id].TransformValidate = (bool (*)(
163  const uint8_t *content, uint16_t content_len, void *context))kw->TransformValidate;
164  sigmatch_table[transform_id].Setup =
165  (int (*)(DetectEngineCtx * de, Signature * s, const char *raw)) kw->Setup;
166  sigmatch_table[transform_id].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;
167  sigmatch_table[transform_id].TransformId =
168  (void (*)(const uint8_t **id_data, uint32_t *length, void *context))kw->TransformId;
169 
170  return transform_id;
171 }
172 
174 {
177 }
SCSigTableAppLiteElmt::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
function callback to match on an app-layer transaction
Definition: detect-engine-helper.h:63
SigTableElmt_::url
const char * url
Definition: detect.h:1461
detect-engine.h
SCSigTableAppLiteElmt::url
const char * url
keyword documentation url
Definition: detect-engine-helper.h:55
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *, const DetectBufferType *))
Definition: detect-engine.c:1463
SCSigTableAppLiteElmt::name
const char * name
keyword name
Definition: detect-engine-helper.h:51
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SCDetectHelperNewKeywordId
int SCDetectHelperNewKeywordId(void)
Definition: detect-engine-helper.c:105
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1445
SigTableElmt_::name
const char * name
Definition: detect.h:1458
DetectEngineInspectBufferSingle
uint8_t DetectEngineInspectBufferSingle(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1988
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectAppLayerInspectEngineRegisterSingle
void DetectAppLayerInspectEngineRegisterSingle(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionSingleBufferGetDataPtr GetData)
Definition: detect-engine.c:295
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
name
const char * name
Definition: detect-engine-proto.c:48
PrefilterSingleMpmRegister
int PrefilterSingleMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1598
SCTransformTableElmt::url
const char * url
Definition: detect-engine-helper.h:70
SCTransformTableElmt::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, void *context)
Definition: detect-engine-helper.h:75
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:86
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
SCTransformTableElmt::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect-engine-helper.h:72
SCTransformTableElmt
Definition: detect-engine-helper.h:67
Flow_
Flow data structure.
Definition: flow.h:347
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1481
SCDetectHelperMultiBufferProgressMpmRegister
int SCDetectHelperMultiBufferProgressMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData, int progress)
Definition: detect-engine-helper.c:85
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1228
SCDetectHelperKeywordAliasRegister
void SCDetectHelperKeywordAliasRegister(uint16_t kwid, const char *alias)
Definition: detect-engine-helper.c:143
FrameStreamData::det_ctx
DetectEngineThreadCtx * det_ctx
Definition: detect-engine-frame.c:46
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1423
rust.h
SCTransformTableElmt::TransformId
void(* TransformId)(const uint8_t **id_data, uint32_t *id_length, void *context)
Definition: detect-engine-helper.h:76
SCDetectHelperBufferMpmRegister
int SCDetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionSingleBufferGetDataPtr GetData)
Definition: detect-engine-helper.c:47
SCSigTableAppLiteElmt::desc
const char * desc
keyword description
Definition: detect-engine-helper.h:53
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
DetectMd5ValidateCallback
bool DetectMd5ValidateCallback(const Signature *s, const char **sigerror, const DetectBufferType *map)
Definition: detect-engine.c:5079
SigTableElmt_
element in sigmatch type table.
Definition: detect.h:1418
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
SCTransformTableElmt::desc
const char * desc
Definition: detect-engine-helper.h:69
SCDetectHelperBufferRegister
int SCDetectHelperBufferRegister(const char *name, AppProto alproto, uint8_t direction)
Definition: detect-engine-helper.c:34
SCTransformTableElmt::name
const char * name
Definition: detect-engine-helper.h:68
SCTransformTableElmt::flags
uint32_t flags
Definition: detect-engine-helper.h:71
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1278
SCSigTableAppLiteElmt
App-layer light version of SigTableElmt.
Definition: detect-engine-helper.h:49
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
SigTableElmt_::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, void *context)
Definition: detect.h:1434
DetectEngineThreadCtx_
Definition: detect.h:1245
SCDetectHelperBufferProgressMpmRegister
int SCDetectHelperBufferProgressMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionSingleBufferGetDataPtr GetData, int progress)
Definition: detect-engine-helper.c:66
SCDetectHelperTransformRegister
int SCDetectHelperTransformRegister(const SCTransformTableElmt *kw)
Definition: detect-engine-helper.c:148
detect-engine-mpm.h
DETECT_TBLSIZE_IDX
int DETECT_TBLSIZE_IDX
Definition: detect-engine-register.c:277
InspectionSingleBufferGetDataPtr
bool(* InspectionSingleBufferGetDataPtr)(const void *txv, const uint8_t flow_flags, const uint8_t **buf, uint32_t *buf_len)
Definition: detect-engine-helper.h:45
SCSigTableAppLiteElmt::flags
uint32_t flags
flags SIGMATCH_*
Definition: detect-engine-helper.h:57
detect-engine-helper.h
SCSigTableAppLiteElmt::Free
void(* Free)(DetectEngineCtx *, void *)
function callback to free structure allocated by setup if any
Definition: detect-engine-helper.h:61
SCDetectHelperKeywordRegister
uint16_t SCDetectHelperKeywordRegister(const SCSigTableAppLiteElmt *kw)
Definition: detect-engine-helper.c:122
InspectionMultiBufferGetDataPtr
bool(* InspectionMultiBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const void *txv, const uint8_t flow_flags, uint32_t local_id, const uint8_t **buf, uint32_t *buf_len)
Definition: detect-engine-helper.h:42
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:350
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1214
flags
uint8_t flags
Definition: decode-gre.h:0
SCSigTableAppLiteElmt::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
function callback to parse and setup keyword in rule
Definition: detect-engine-helper.h:59
SigTableElmt_::alias
const char * alias
Definition: detect.h:1459
DetectAppLayerMpmRegisterSingle
void DetectAppLayerMpmRegisterSingle(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionSingleBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
Definition: detect-engine-mpm.c:160
suricata-common.h
DETECT_TBLSIZE_STEP
#define DETECT_TBLSIZE_STEP
Definition: detect-engine-register.h:323
SCDetectHelperMultiBufferMpmRegister
int SCDetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData)
Definition: detect-engine-helper.c:99
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1946
DETECT_TBLSIZE
int DETECT_TBLSIZE
Definition: detect-engine-register.c:276
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
SCTransformTableElmt::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect-engine-helper.h:73
FrameStreamData::s
const Signature * s
Definition: detect-engine-frame.c:54
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:273
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1375
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2099
SigTableElmt_::Transform
void(* Transform)(DetectEngineThreadCtx *, InspectionBuffer *, void *context)
Definition: detect.h:1433
SigTableElmt_::TransformId
void(* TransformId)(const uint8_t **data, uint32_t *length, void *context)
Definition: detect.h:1437
DetectLowerSetupCallback
void DetectLowerSetupCallback(const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
Definition: detect-engine.c:5120
SCDetectRegisterBufferLowerMd5Callbacks
void SCDetectRegisterBufferLowerMd5Callbacks(const char *name)
Definition: detect-engine-helper.c:173
SCTransformTableElmt::Transform
void(* Transform)(DetectEngineThreadCtx *, InspectionBuffer *, void *context)
Definition: detect-engine-helper.h:74