suricata
detect-ike-vendor.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-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 /**
19  *
20  * \author Frank Honza <frank.honza@dcso.de>
21  */
22 
23 #include "suricata-common.h"
24 #include "conf.h"
25 #include "detect.h"
26 #include "detect-parse.h"
27 #include "detect-engine.h"
30 #include "detect-engine-mpm.h"
31 #include "detect-ike-vendor.h"
32 #include "app-layer-parser.h"
33 #include "util-byte.h"
34 
35 #include "rust-bindings.h"
36 #include "util-profiling.h"
37 
38 static int DetectIkeVendorSetup(DetectEngineCtx *, Signature *, const char *);
39 
40 typedef struct {
41  char *vendor;
43 
45  uint32_t local_id;
46  void *txv;
47 };
48 
49 typedef struct PrefilterMpmIkeVendor {
50  int list_id;
51  const MpmCtx *mpm_ctx;
54 
55 static int g_ike_vendor_buffer_id = 0;
56 
57 static InspectionBuffer *IkeVendorGetData(DetectEngineThreadCtx *det_ctx,
58  const DetectEngineTransforms *transforms, Flow *f, struct IkeVendorGetDataArgs *cbdata,
59  int list_id, bool first)
60 {
61  SCEnter();
62 
63  InspectionBuffer *buffer =
64  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
65  if (buffer == NULL)
66  return NULL;
67  if (!first && buffer->inspect != NULL)
68  return buffer;
69 
70  const uint8_t *data;
71  uint32_t data_len;
72  if (rs_ike_tx_get_vendor(cbdata->txv, cbdata->local_id, &data, &data_len) == 0) {
73  return NULL;
74  }
75 
76  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
77 
78  SCReturnPtr(buffer, "InspectionBuffer");
79 }
80 
81 /** \brief IkeVendor Mpm prefilter callback
82  *
83  * \param det_ctx detection engine thread ctx
84  * \param p packet to inspect
85  * \param f flow to inspect
86  * \param txv tx to inspect
87  * \param pectx inspection context
88  */
89 static void PrefilterTxIkeVendor(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
90  Flow *f, void *txv, const uint64_t idx, const uint8_t flags)
91 {
92  SCEnter();
93 
94  const PrefilterMpmIkeVendor *ctx = (const PrefilterMpmIkeVendor *)pectx;
95  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
96  const int list_id = ctx->list_id;
97 
98  uint32_t local_id = 0;
99  while (1) {
100  struct IkeVendorGetDataArgs cbdata = { local_id, txv };
101  InspectionBuffer *buffer =
102  IkeVendorGetData(det_ctx, ctx->transforms, f, &cbdata, list_id, true);
103  if (buffer == NULL)
104  break;
105 
106  if (buffer->inspect_len >= mpm_ctx->minlen) {
107  (void)mpm_table[mpm_ctx->mpm_type].Search(
108  mpm_ctx, &det_ctx->mtcu, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
109  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
110  }
111  local_id++;
112  }
113 
114  SCReturn;
115 }
116 
117 static void PrefilterMpmIkeVendorFree(void *ptr)
118 {
119  if (ptr != NULL)
120  SCFree(ptr);
121 }
122 
123 static int PrefilterMpmIkeVendorRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
124  MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
125 {
126  PrefilterMpmIkeVendor *pectx = SCCalloc(1, sizeof(*pectx));
127  if (pectx == NULL)
128  return -1;
129  pectx->list_id = list_id;
130  pectx->mpm_ctx = mpm_ctx;
131  pectx->transforms = &mpm_reg->transforms;
132 
133  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxIkeVendor, mpm_reg->app_v2.alproto,
134  mpm_reg->app_v2.tx_min_progress, pectx, PrefilterMpmIkeVendorFree, mpm_reg->pname);
135 }
136 
137 static uint8_t DetectEngineInspectIkeVendor(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
138  const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
139  void *alstate, void *txv, uint64_t tx_id)
140 {
141  uint32_t local_id = 0;
142 
143  const DetectEngineTransforms *transforms = NULL;
144  if (!engine->mpm) {
145  transforms = engine->v2.transforms;
146  }
147 
148  while (1) {
149  struct IkeVendorGetDataArgs cbdata = {
150  local_id,
151  txv,
152  };
153  InspectionBuffer *buffer =
154  IkeVendorGetData(det_ctx, transforms, f, &cbdata, engine->sm_list, false);
155  if (buffer == NULL || buffer->inspect == NULL)
156  break;
157 
158  det_ctx->buffer_offset = 0;
159  det_ctx->discontinue_matching = 0;
160  det_ctx->inspection_recursion_counter = 0;
161 
162  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
163  (uint8_t *)buffer->inspect, buffer->inspect_len, buffer->inspect_offset,
165  if (match == 1) {
167  }
168  local_id++;
169  }
171 }
172 
173 /**
174  * \brief Registration function for ike.vendor keyword.
175  */
177 {
178  sigmatch_table[DETECT_AL_IKE_VENDOR].name = "ike.vendor";
179  sigmatch_table[DETECT_AL_IKE_VENDOR].desc = "match IKE Vendor";
180  sigmatch_table[DETECT_AL_IKE_VENDOR].url = "/rules/ike-keywords.html#ike-vendor";
181  sigmatch_table[DETECT_AL_IKE_VENDOR].Setup = DetectIkeVendorSetup;
184 
185  DetectAppLayerMpmRegister2("ike.vendor", SIG_FLAG_TOSERVER, 1, PrefilterMpmIkeVendorRegister,
186  NULL, ALPROTO_IKE, 1);
187 
189  "ike.vendor", ALPROTO_IKE, SIG_FLAG_TOSERVER, 1, DetectEngineInspectIkeVendor, NULL);
190 
191  g_ike_vendor_buffer_id = DetectBufferTypeGetByName("ike.vendor");
192 }
193 
194 /**
195  * \brief setup the sticky buffer keyword used in the rule
196  *
197  * \param de_ctx Pointer to the Detection Engine Context
198  * \param s Pointer to the Signature to which the current keyword belongs
199  * \param str Should hold an empty string always
200  *
201  * \retval 0 On success
202  * \retval -1 On failure
203  */
204 
205 static int DetectIkeVendorSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
206 {
207  if (DetectBufferSetActiveList(s, g_ike_vendor_buffer_id) < 0)
208  return -1;
210  return -1;
211  return 0;
212 }
util-byte.h
DetectEngineAppInspectionEngine_
Definition: detect.h:390
SigTableElmt_::url
const char * url
Definition: detect.h:1248
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:394
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1062
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1455
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:49
SigTableElmt_::desc
const char * desc
Definition: detect.h:1247
PrefilterMpmIkeVendor::list_id
int list_id
Definition: detect-ike-vendor.c:50
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
IkeVendorGetDataArgs::local_id
uint32_t local_id
Definition: detect-ike-vendor.c:45
SigTableElmt_::name
const char * name
Definition: detect.h:1245
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1403
DetectEngineTransforms
Definition: detect.h:372
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:640
InspectionBuffer
Definition: detect.h:338
Flow_
Flow data structure.
Definition: flow.h:353
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1141
PrefilterMpmIkeVendor::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-ike-vendor.c:51
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1239
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@87::@89 app_v2
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:626
PrefilterMpmIkeVendor
struct PrefilterMpmIkeVendor PrefilterMpmIkeVendor
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
PrefilterMpmIkeVendor::transforms
const DetectEngineTransforms * transforms
Definition: detect-ike-vendor.c:52
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@84 v2
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1139
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:396
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:230
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1034
SCEnter
#define SCEnter(...)
Definition: util-debug.h:298
detect-engine-mpm.h
detect.h
DetectIkeVendorRegister
void DetectIkeVendorRegister(void)
Registration function for ike.vendor keyword.
Definition: detect-ike-vendor.c:176
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:39
DetectIkeVendorData::vendor
char * vendor
Definition: detect-ike-vendor.c:41
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:340
detect-ike-vendor.h
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
util-profiling.h
SCReturn
#define SCReturn
Definition: util-debug.h:300
Packet_
Definition: decode.h:434
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:227
conf.h
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:314
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:165
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1101
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:407
DetectEngineContentInspection
uint8_t DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode)
Run the actual payload match functions.
Definition: detect-engine-content-inspection.c:104
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:304
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:47
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:628
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1118
DETECT_AL_IKE_VENDOR
@ DETECT_AL_IKE_VENDOR
Definition: detect-engine-register.h:324
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:1436
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:38
PrefilterMpmIkeVendor
Definition: detect-ike-vendor.c:49
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:341
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:339
str
#define str(s)
Definition: suricata-common.h:272
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
IkeVendorGetDataArgs
Definition: detect-ike-vendor.c:44
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags), AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:270
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:404
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
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:1389
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1431
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1300
DetectIkeVendorData
Definition: detect-ike-vendor.c:40
MpmCtx_
Definition: util-mpm.h:88
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
IkeVendorGetDataArgs::txv
void * txv
Definition: detect-ike-vendor.c:46