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 
33 int DetectHelperBufferRegister(const char *name, AppProto alproto, bool toclient, bool toserver)
34 {
35  if (toserver) {
38  }
39  if (toclient) {
42  }
44 }
45 
47  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
48  const int list_id,
49  bool (*GetBuf)(void *txv, const uint8_t flow_flags, const uint8_t **buf, uint32_t *buf_len))
50 {
52  if (buffer->inspect == NULL) {
53  const uint8_t *b = NULL;
54  uint32_t b_len = 0;
55 
56  if (!GetBuf(txv, flow_flags, &b, &b_len))
57  return NULL;
58 
60  }
61  return buffer;
62 }
63 
64 int DetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto,
65  bool toclient, bool toserver, InspectionBufferGetDataPtr GetData)
66 {
67  if (toserver) {
71  name, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, GetData, alproto, 0);
72  }
73  if (toclient) {
77  name, SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister, GetData, alproto, 0);
78  }
81 }
82 
83 int DetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppProto alproto,
84  bool toclient, bool toserver, InspectionMultiBufferGetDataPtr GetData)
85 {
86  if (toserver) {
87  DetectAppLayerMultiRegister(name, alproto, SIG_FLAG_TOSERVER, 0, GetData, 2, 0);
88  }
89  if (toclient) {
90  DetectAppLayerMultiRegister(name, alproto, SIG_FLAG_TOCLIENT, 0, GetData, 2, 0);
91  }
95 }
96 
97 int DetectHelperKeywordRegister(const SCSigTableElmt *kw)
98 {
100  void *tmp = SCRealloc(
102  if (unlikely(tmp == NULL)) {
103  return -1;
104  }
105  sigmatch_table = tmp;
107  }
108 
114  (int (*)(DetectEngineThreadCtx * det_ctx, Flow * f, uint8_t flags, void *alstate,
115  void *txv, const Signature *s, const SigMatchCtx *ctx)) kw->AppLayerTxMatch;
117  (int (*)(DetectEngineCtx * de, Signature * s, const char *raw)) kw->Setup;
118  sigmatch_table[DETECT_TBLSIZE_IDX].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;
120  return DETECT_TBLSIZE_IDX - 1;
121 }
122 
123 int DetectHelperTransformRegister(const SCTransformTableElmt *kw)
124 {
126  void *tmp = SCRealloc(
128  if (unlikely(tmp == NULL)) {
129  return -1;
130  }
131  sigmatch_table = tmp;
133  }
134 
140  (void (*)(InspectionBuffer * buffer, void *options)) kw->Transform;
142  const uint8_t *content, uint16_t content_len, void *context))kw->TransformValidate;
144  (int (*)(DetectEngineCtx * de, Signature * s, const char *raw)) kw->Setup;
145  sigmatch_table[DETECT_TBLSIZE_IDX].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;
147  return DETECT_TBLSIZE_IDX - 1;
148 }
149 
151  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
152  const int list_id, uint32_t index, MultiGetTxBuffer GetBuf)
153 {
155  if (buffer == NULL) {
156  return NULL;
157  }
158  if (buffer->initialized) {
159  return buffer;
160  }
161 
162  const uint8_t *data = NULL;
163  uint32_t data_len = 0;
164 
165  if (!GetBuf(txv, flow_flags, index, &data, &data_len)) {
167  return NULL;
168  }
169  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
170  buffer->flags = DETECT_CI_FLAGS_SINGLE;
171  return buffer;
172 }
173 
175 {
176  return buf->inspect;
177 }
178 
180 {
181  return buf->inspect_len;
182 }
SigTableElmt_::url
const char * url
Definition: detect.h:1322
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1321
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
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:2180
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1309
DetectHelperGetMultiData
InspectionBuffer * DetectHelperGetMultiData(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id, uint32_t index, MultiGetTxBuffer GetBuf)
Definition: detect-engine-helper.c:150
SigTableElmt_::name
const char * name
Definition: detect.h:1319
InspectionBuffer::initialized
bool initialized
Definition: detect.h:379
InspectionBufferSetupAndApplyTransforms
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine.c:1647
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:410
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:85
InspectionMultiBufferGetDataPtr
InspectionBuffer *(* InspectionMultiBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id, const uint32_t local_id)
Definition: detect.h:421
InspectionBuffer
Definition: detect.h:375
FrameStreamData::list_id
int list_id
Definition: detect-engine-frame.c:48
Flow_
Flow data structure.
Definition: flow.h:354
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.h:416
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1313
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:854
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1049
FrameStreamData::det_ctx
DetectEngineThreadCtx * det_ctx
Definition: detect-engine-frame.c:45
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1290
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:380
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:270
SigTableElmt_
element in sigmatch type table.
Definition: detect.h:1285
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1304
DetectHelperBufferMpmRegister
int DetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto, bool toclient, bool toserver, InspectionBufferGetDataPtr GetData)
Definition: detect-engine-helper.c:64
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1506
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1099
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority, int tx_min_progress)
Definition: detect-engine.c:2230
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1594
SigTableElmt_::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, void *context)
Definition: detect.h:1301
MultiGetTxBuffer
bool(* MultiGetTxBuffer)(void *, uint8_t, uint32_t, const uint8_t **, uint32_t *)
Definition: detect-engine-helper.h:35
DetectEngineThreadCtx_
Definition: detect.h:1109
de
uint8_t de
Definition: app-layer-htp.c:560
detect-engine-mpm.h
DETECT_TBLSIZE_IDX
int DETECT_TBLSIZE_IDX
Definition: detect-engine-register.c:285
InspectionBufferLength
uint32_t InspectionBufferLength(InspectionBuffer *buf)
Definition: detect-engine-helper.c:179
FrameStreamData::transforms
const DetectEngineTransforms * transforms
Definition: detect-engine-frame.c:46
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
detect-engine-helper.h
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
name
const char * name
Definition: tm-threads.c:2081
DetectHelperMultiBufferMpmRegister
int DetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppProto alproto, bool toclient, bool toserver, InspectionMultiBufferGetDataPtr GetData)
Definition: detect-engine-helper.c:83
DetectHelperBufferRegister
int DetectHelperBufferRegister(const char *name, AppProto alproto, bool toclient, bool toserver)
Definition: detect-engine-helper.c:33
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:346
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:49
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1035
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
DETECT_TBLSIZE_STEP
#define DETECT_TBLSIZE_STEP
Definition: detect-engine-register.h:348
DetectHelperKeywordRegister
int DetectHelperKeywordRegister(const SCSigTableElmt *kw)
Definition: detect-engine-helper.c:97
SigTableElmt_::Transform
void(* Transform)(InspectionBuffer *, void *context)
Definition: detect.h:1300
DetectHelperGetData
InspectionBuffer * DetectHelperGetData(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id, bool(*GetBuf)(void *txv, const uint8_t flow_flags, const uint8_t **buf, uint32_t *buf_len))
Definition: detect-engine-helper.c:46
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1607
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:2137
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:378
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:376
DETECT_TBLSIZE
int DETECT_TBLSIZE
Definition: detect-engine-register.c:284
DetectHelperTransformRegister
int DetectHelperTransformRegister(const SCTransformTableElmt *kw)
Definition: detect-engine-helper.c:123
detect-parse.h
Signature_
Signature container.
Definition: detect.h:614
FrameStreamData::s
const Signature * s
Definition: detect-engine-frame.c:53
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1526
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:245
InspectionBufferPtr
const uint8_t * InspectionBufferPtr(InspectionBuffer *buf)
Definition: detect-engine-helper.c:174
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1196