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)
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 (buffer->initialized)
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 AppLayerTxData *_txd, 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 = IkeVendorGetData(det_ctx, ctx->transforms, f, &cbdata, list_id);
102  if (buffer == NULL)
103  break;
104 
105  if (buffer->inspect_len >= mpm_ctx->minlen) {
106  (void)mpm_table[mpm_ctx->mpm_type].Search(
107  mpm_ctx, &det_ctx->mtcu, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
108  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
109  }
110  local_id++;
111  }
112 
113  SCReturn;
114 }
115 
116 static void PrefilterMpmIkeVendorFree(void *ptr)
117 {
118  if (ptr != NULL)
119  SCFree(ptr);
120 }
121 
122 static int PrefilterMpmIkeVendorRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
123  MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
124 {
125  PrefilterMpmIkeVendor *pectx = SCCalloc(1, sizeof(*pectx));
126  if (pectx == NULL)
127  return -1;
128  pectx->list_id = list_id;
129  pectx->mpm_ctx = mpm_ctx;
130  pectx->transforms = &mpm_reg->transforms;
131 
132  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxIkeVendor, mpm_reg->app_v2.alproto,
133  mpm_reg->app_v2.tx_min_progress, pectx, PrefilterMpmIkeVendorFree, mpm_reg->pname);
134 }
135 
136 static uint8_t DetectEngineInspectIkeVendor(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
137  const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
138  void *alstate, void *txv, uint64_t tx_id)
139 {
140  uint32_t local_id = 0;
141 
142  const DetectEngineTransforms *transforms = NULL;
143  if (!engine->mpm) {
144  transforms = engine->v2.transforms;
145  }
146 
147  while (1) {
148  struct IkeVendorGetDataArgs cbdata = {
149  local_id,
150  txv,
151  };
152  InspectionBuffer *buffer =
153  IkeVendorGetData(det_ctx, transforms, f, &cbdata, engine->sm_list);
154  if (buffer == NULL || buffer->inspect == NULL)
155  break;
156 
157  det_ctx->buffer_offset = 0;
158  det_ctx->discontinue_matching = 0;
159  det_ctx->inspection_recursion_counter = 0;
160 
161  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
162  (uint8_t *)buffer->inspect, buffer->inspect_len, buffer->inspect_offset,
164  if (match == 1) {
166  }
167  local_id++;
168  }
170 }
171 
172 /**
173  * \brief Registration function for ike.vendor keyword.
174  */
176 {
177  sigmatch_table[DETECT_AL_IKE_VENDOR].name = "ike.vendor";
178  sigmatch_table[DETECT_AL_IKE_VENDOR].desc = "match IKE Vendor";
179  sigmatch_table[DETECT_AL_IKE_VENDOR].url = "/rules/ike-keywords.html#ike-vendor";
180  sigmatch_table[DETECT_AL_IKE_VENDOR].Setup = DetectIkeVendorSetup;
183 
184  DetectAppLayerMpmRegister2("ike.vendor", SIG_FLAG_TOSERVER, 1, PrefilterMpmIkeVendorRegister,
185  NULL, ALPROTO_IKE, 1);
186 
188  "ike.vendor", ALPROTO_IKE, SIG_FLAG_TOSERVER, 1, DetectEngineInspectIkeVendor, NULL);
189 
190  g_ike_vendor_buffer_id = DetectBufferTypeGetByName("ike.vendor");
191 }
192 
193 /**
194  * \brief setup the sticky buffer keyword used in the rule
195  *
196  * \param de_ctx Pointer to the Detection Engine Context
197  * \param s Pointer to the Signature to which the current keyword belongs
198  * \param str Should hold an empty string always
199  *
200  * \retval 0 On success
201  * \retval -1 On failure
202  */
203 
204 static int DetectIkeVendorSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
205 {
206  if (DetectBufferSetActiveList(s, g_ike_vendor_buffer_id) < 0)
207  return -1;
209  return -1;
210  return 0;
211 }
util-byte.h
DetectEngineAppInspectionEngine_
Definition: detect.h:390
SigTableElmt_::url
const char * url
Definition: detect.h:1241
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1498
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:1053
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1447
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:49
SigTableElmt_::desc
const char * desc
Definition: detect.h:1240
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:1238
InspectionBuffer::initialized
bool initialized
Definition: detect.h:341
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1395
DetectEngineTransforms
Definition: detect.h:372
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:640
InspectionBuffer
Definition: detect.h:337
Flow_
Flow data structure.
Definition: flow.h:357
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1134
PrefilterMpmIkeVendor::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-ike-vendor.c:51
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1232
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:1223
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:1132
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1079
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:396
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1025
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
DetectIkeVendorRegister
void DetectIkeVendorRegister(void)
Registration function for ike.vendor keyword.
Definition: detect-ike-vendor.c:175
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:339
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:273
Packet_
Definition: decode.h:428
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:224
conf.h
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
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:1092
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:106
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
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1303
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:307
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:76
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1111
DETECT_AL_IKE_VENDOR
@ DETECT_AL_IKE_VENDOR
Definition: detect-engine-register.h:327
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:1431
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, PrefilterTxFn PrefilterTxFunc, AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:270
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:340
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:338
str
#define str(s)
Definition: suricata-common.h:280
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
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:1384
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1423
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1293
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