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 #include "app-layer-parser.h"
34 #include "util-validate.h"
35 
37  const char *name, AppProto alproto, uint8_t direction, uint8_t progress)
38 {
40  if (direction & STREAM_TOSERVER) {
42  name, alproto, SIG_FLAG_TOSERVER, progress, DetectEngineInspectGenericList, NULL);
43  }
44  if (direction & STREAM_TOCLIENT) {
46  name, alproto, SIG_FLAG_TOCLIENT, progress, DetectEngineInspectGenericList, NULL);
47  }
49 }
50 
52  const char *name, AppProto alproto, uint8_t direction, uint8_t sub_state, uint8_t progress)
53 {
54  DEBUG_VALIDATE_BUG_ON(AppLayerParserSupportsSubStates(alproto) && sub_state == 0);
55  if (direction & STREAM_TOSERVER) {
57  (uint8_t)progress, DetectEngineInspectGenericList, NULL);
58  }
59  if (direction & STREAM_TOCLIENT) {
61  (uint8_t)progress, DetectEngineInspectGenericList, NULL);
62  }
64 }
65 
66 int SCDetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto,
67  uint8_t direction, InspectionSingleBufferGetDataPtr GetData)
68 {
70  if (direction & STREAM_TOSERVER) {
74  name, SIG_FLAG_TOSERVER, 2, PrefilterSingleMpmRegister, GetData, alproto, 0);
75  }
76  if (direction & STREAM_TOCLIENT) {
80  name, SIG_FLAG_TOCLIENT, 2, PrefilterSingleMpmRegister, GetData, alproto, 0);
81  }
84 }
85 
86 int SCDetectRegisterMpmGeneric(const char *name, const char *desc, AppProto alproto,
87  uint8_t direction, InspectionBufferGetDataPtr GetData, uint8_t progress)
88 {
90  if (direction & STREAM_TOSERVER) {
94  alproto, progress);
95  }
96  if (direction & STREAM_TOCLIENT) {
100  alproto, progress);
101  }
104 }
105 
106 int SCDetectHelperBufferProgressMpmRegister(const char *name, const char *desc, AppProto alproto,
107  uint8_t direction, InspectionSingleBufferGetDataPtr GetData, uint8_t progress)
108 {
110  if (direction & STREAM_TOSERVER) {
114  name, SIG_FLAG_TOSERVER, 2, PrefilterSingleMpmRegister, GetData, alproto, progress);
115  }
116  if (direction & STREAM_TOCLIENT) {
120  name, SIG_FLAG_TOCLIENT, 2, PrefilterSingleMpmRegister, GetData, alproto, progress);
121  }
124 }
125 
126 int SCDetectHelperMultiBufferProgressMpmRegister(const char *name, const char *desc,
127  AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData,
128  uint8_t progress)
129 {
131  if (direction & STREAM_TOSERVER) {
132  DetectAppLayerMultiRegister(name, alproto, SIG_FLAG_TOSERVER, progress, GetData, 2);
133  }
134  if (direction & STREAM_TOCLIENT) {
135  DetectAppLayerMultiRegister(name, alproto, SIG_FLAG_TOCLIENT, progress, GetData, 2);
136  }
140 }
141 
143  AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData,
144  uint8_t sub_state, uint8_t progress)
145 {
146  DEBUG_VALIDATE_BUG_ON(AppLayerParserSupportsSubStates(alproto) && sub_state == 0);
147  if (direction & STREAM_TOSERVER) {
149  name, alproto, SIG_FLAG_TOSERVER, sub_state, progress, GetData, 2);
150  }
151  if (direction & STREAM_TOCLIENT) {
153  name, alproto, SIG_FLAG_TOCLIENT, sub_state, progress, GetData, 2);
154  }
158 }
159 
160 int SCDetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppProto alproto,
161  uint8_t direction, InspectionMultiBufferGetDataPtr GetData)
162 {
164  return SCDetectHelperMultiBufferProgressMpmRegister(name, desc, alproto, direction, GetData, 0);
165 }
166 
168 {
170  void *tmp = SCRealloc(
172  if (unlikely(tmp == NULL)) {
173  return -1;
174  }
175  sigmatch_table = tmp;
178  }
179 
181  return DETECT_TBLSIZE_IDX - 1;
182 }
183 
185 {
186  int keyword_id = SCDetectHelperNewKeywordId();
187  if (keyword_id < 0) {
188  return -1;
189  }
190 
191  sigmatch_table[keyword_id].name = kw->name;
192  sigmatch_table[keyword_id].desc = kw->desc;
193  sigmatch_table[keyword_id].url = kw->url;
194  sigmatch_table[keyword_id].flags = kw->flags;
195  sigmatch_table[keyword_id].AppLayerTxMatch =
196  (int (*)(DetectEngineThreadCtx * det_ctx, Flow * f, uint8_t flags, void *alstate,
197  void *txv, const Signature *s, const SigMatchCtx *ctx)) kw->AppLayerTxMatch;
198  sigmatch_table[keyword_id].Setup =
199  (int (*)(DetectEngineCtx * de, Signature * s, const char *raw)) kw->Setup;
200  sigmatch_table[keyword_id].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;
201 
202  return (uint16_t)keyword_id;
203 }
204 
205 void SCDetectHelperKeywordAliasRegister(uint16_t kwid, const char *alias)
206 {
207  sigmatch_table[kwid].alias = alias;
208 }
209 
210 void SCDetectHelperKeywordJsonInfoRegister(uint16_t kwid, void (*cb)(struct SCJsonBuilder *))
211 {
213 }
214 
216 {
217  int transform_id = SCDetectHelperNewKeywordId();
218  if (transform_id < 0) {
219  return -1;
220  }
221 
222  sigmatch_table[transform_id].name = kw->name;
223  sigmatch_table[transform_id].desc = kw->desc;
224  sigmatch_table[transform_id].url = kw->url;
225  sigmatch_table[transform_id].flags = kw->flags;
226  sigmatch_table[transform_id].Transform = (void (*)(DetectEngineThreadCtx * det_ctx,
227  InspectionBuffer * buffer, const void *options)) kw->Transform;
229  sigmatch_table[transform_id].Setup =
230  (int (*)(DetectEngineCtx * de, Signature * s, const char *raw)) kw->Setup;
231  sigmatch_table[transform_id].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;
232  sigmatch_table[transform_id].TransformId = kw->TransformId;
233 
234  return transform_id;
235 }
236 
238 {
241 }
SCJsonBuilder
struct SCJsonBuilder SCJsonBuilder
Definition: detect-engine-helper.h:83
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:1545
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:1657
SCSigTableAppLiteElmt::name
const char * name
keyword name
Definition: detect-engine-helper.h:51
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SCDetectHelperNewKeywordId
int SCDetectHelperNewKeywordId(void)
Definition: detect-engine-helper.c:167
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(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:2267
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
DetectAppLayerMpmRegisterSingle
void DetectAppLayerMpmRegisterSingle(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionSingleBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
Definition: detect-engine-mpm.c:176
SigTableElmt_::name
const char * name
Definition: detect.h:1542
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:2207
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
name
const char * name
Definition: detect-engine-proto.c:47
PrefilterSingleMpmRegister
int PrefilterSingleMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1729
SCTransformTableElmt::url
const char * url
Definition: detect-engine-helper.h:70
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
SCTransformTableElmt::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect-engine-helper.h:72
SCDetectHelperMultiBufferProgressMpmRegister
int SCDetectHelperMultiBufferProgressMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData, uint8_t progress)
Definition: detect-engine-helper.c:126
SCTransformTableElmt
Definition: detect-engine-helper.h:67
Flow_
Flow data structure.
Definition: flow.h:359
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1675
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1402
SCDetectHelperKeywordAliasRegister
void SCDetectHelperKeywordAliasRegister(uint16_t kwid, const char *alias)
Definition: detect-engine-helper.c:205
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:1507
DetectAppLayerMultiRegisterSubState
void DetectAppLayerMultiRegisterSubState(const char *name, AppProto alproto, uint32_t dir, uint8_t sub_state, uint8_t progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2317
rust.h
SigTableElmt_::TransformId
void(* TransformId)(const uint8_t **data, uint32_t *length, const void *context)
Definition: detect.h:1521
SCDetectHelperMultiBufferProgressMpmRegisterSubState
int SCDetectHelperMultiBufferProgressMpmRegisterSubState(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData, uint8_t sub_state, uint8_t progress)
Definition: detect-engine-helper.c:142
SCDetectHelperBufferMpmRegister
int SCDetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionSingleBufferGetDataPtr GetData)
Definition: detect-engine-helper.c:66
SCTransformTableElmt::TransformId
void(* TransformId)(const uint8_t **id_data, uint32_t *id_length, const void *context)
Definition: detect-engine-helper.h:76
SCSigTableAppLiteElmt::desc
const char * desc
keyword description
Definition: detect-engine-helper.h:53
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:275
DetectMd5ValidateCallback
bool DetectMd5ValidateCallback(const Signature *s, const char **sigerror, const DetectBufferType *map)
Definition: detect-engine.c:5333
SigTableElmt_
element in sigmatch type table.
Definition: detect.h:1502
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
SCTransformTableElmt::desc
const char * desc
Definition: detect-engine-helper.h:69
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
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:275
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1452
SCSigTableAppLiteElmt
App-layer light version of SigTableElmt.
Definition: detect-engine-helper.h:49
SCTransformTableElmt::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, const void *context)
Definition: detect-engine-helper.h:75
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:274
SCDetectHelperBufferProgressRegister
int SCDetectHelperBufferProgressRegister(const char *name, AppProto alproto, uint8_t direction, uint8_t progress)
Definition: detect-engine-helper.c:36
SigTableElmt_::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, const void *context)
Definition: detect.h:1518
DetectEngineThreadCtx_
Definition: detect.h:1316
SCDetectHelperTransformRegister
int SCDetectHelperTransformRegister(const SCTransformTableElmt *kw)
Definition: detect-engine-helper.c:215
detect-engine-mpm.h
DETECT_TBLSIZE_IDX
int DETECT_TBLSIZE_IDX
Definition: detect-engine-register.c:262
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
SCDetectHelperKeywordJsonInfoRegister
void SCDetectHelperKeywordJsonInfoRegister(uint16_t kwid, void(*cb)(struct SCJsonBuilder *))
Definition: detect-engine-helper.c:210
SCTransformTableElmt::Transform
void(* Transform)(DetectEngineThreadCtx *, InspectionBuffer *, const void *context)
Definition: detect-engine-helper.h:74
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1707
SCSigTableAppLiteElmt::flags
uint32_t flags
flags SIGMATCH_*
Definition: detect-engine-helper.h:57
detect-engine-helper.h
app-layer-parser.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:184
SigTableElmt_::JsonAdditionalInfo
void(* JsonAdditionalInfo)(struct SCJsonBuilder *)
Definition: detect.h:1551
SCDetectHelperBufferProgressRegisterSubState
int SCDetectHelperBufferProgressRegisterSubState(const char *name, AppProto alproto, uint8_t direction, uint8_t sub_state, uint8_t progress)
Definition: detect-engine-helper.c:51
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:355
SCDetectRegisterMpmGeneric
int SCDetectRegisterMpmGeneric(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionBufferGetDataPtr GetData, uint8_t progress)
Definition: detect-engine-helper.c:86
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1388
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:1543
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2329
suricata-common.h
DETECT_TBLSIZE_STEP
#define DETECT_TBLSIZE_STEP
Definition: detect-engine-register.h:304
SCDetectHelperMultiBufferMpmRegister
int SCDetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData)
Definition: detect-engine-helper.c:160
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:159
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)
Definition: detect-engine.c:2155
DetectAppLayerInspectEngineRegisterSubState
void DetectAppLayerInspectEngineRegisterSubState(const char *name, AppProto alproto, uint32_t dir, uint8_t sub_state, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register an app inspection engine for a tx type
Definition: detect-engine.c:298
util-validate.h
DETECT_TBLSIZE
int DETECT_TBLSIZE
Definition: detect-engine-register.c:261
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
SCTransformTableElmt::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect-engine-helper.h:73
FrameStreamData::s
const Signature * s
Definition: detect-engine-frame.c:54
InspectionBufferGetDataPtr
InspectionBuffer *(* InspectionBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id)
Definition: detect-engine-helper.h:39
AppLayerParserSupportsSubStates
bool AppLayerParserSupportsSubStates(const AppProto alproto)
Definition: app-layer-parser.c:1356
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1549
DetectAppLayerInspectEngineRegisterSingle
void DetectAppLayerInspectEngineRegisterSingle(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionSingleBufferGetDataPtr GetData)
Definition: detect-engine.c:321
SCDetectHelperBufferProgressMpmRegister
int SCDetectHelperBufferProgressMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionSingleBufferGetDataPtr GetData, uint8_t progress)
Definition: detect-engine-helper.c:106
SigTableElmt_::Transform
void(* Transform)(DetectEngineThreadCtx *, InspectionBuffer *, const void *context)
Definition: detect.h:1517
DetectLowerSetupCallback
void DetectLowerSetupCallback(const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
Definition: detect-engine.c:5374
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
SCDetectRegisterBufferLowerMd5Callbacks
void SCDetectRegisterBufferLowerMd5Callbacks(const char *name)
Definition: detect-engine-helper.c:237
f
Flow f
Definition: fuzz_dataset.c:32