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) {
74  return NULL;
75  }
76 
77  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
78 
79  SCReturnPtr(buffer, "InspectionBuffer");
80 }
81 
82 /** \brief IkeVendor Mpm prefilter callback
83  *
84  * \param det_ctx detection engine thread ctx
85  * \param p packet to inspect
86  * \param f flow to inspect
87  * \param txv tx to inspect
88  * \param pectx inspection context
89  */
90 static void PrefilterTxIkeVendor(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
91  Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
92 {
93  SCEnter();
94 
95  const PrefilterMpmIkeVendor *ctx = (const PrefilterMpmIkeVendor *)pectx;
96  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
97  const int list_id = ctx->list_id;
98 
99  uint32_t local_id = 0;
100  while (1) {
101  struct IkeVendorGetDataArgs cbdata = { local_id, txv };
102  InspectionBuffer *buffer = IkeVendorGetData(det_ctx, ctx->transforms, f, &cbdata, list_id);
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->mtc, &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 DetectBufferMpmRegistry *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);
155  if (buffer == NULL || buffer->inspect == NULL)
156  break;
157 
158  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
159  buffer->inspect, buffer->inspect_len, buffer->inspect_offset,
161  if (match) {
163  }
164  local_id++;
165  }
167 }
168 
169 /**
170  * \brief Registration function for ike.vendor keyword.
171  */
173 {
174  sigmatch_table[DETECT_AL_IKE_VENDOR].name = "ike.vendor";
175  sigmatch_table[DETECT_AL_IKE_VENDOR].desc = "match IKE Vendor";
176  sigmatch_table[DETECT_AL_IKE_VENDOR].url = "/rules/ike-keywords.html#ike-vendor";
177  sigmatch_table[DETECT_AL_IKE_VENDOR].Setup = DetectIkeVendorSetup;
180 
181  DetectAppLayerMpmRegister("ike.vendor", SIG_FLAG_TOSERVER, 1, PrefilterMpmIkeVendorRegister,
182  NULL, ALPROTO_IKE, 1);
183 
185  "ike.vendor", ALPROTO_IKE, SIG_FLAG_TOSERVER, 1, DetectEngineInspectIkeVendor, NULL);
186 
187  g_ike_vendor_buffer_id = DetectBufferTypeGetByName("ike.vendor");
188 
190 }
191 
192 /**
193  * \brief setup the sticky buffer keyword used in the rule
194  *
195  * \param de_ctx Pointer to the Detection Engine Context
196  * \param s Pointer to the Signature to which the current keyword belongs
197  * \param str Should hold an empty string always
198  *
199  * \retval 0 On success
200  * \retval -1 On failure
201  */
202 
203 static int DetectIkeVendorSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
204 {
205  if (DetectBufferSetActiveList(de_ctx, s, g_ike_vendor_buffer_id) < 0)
206  return -1;
208  return -1;
209  return 0;
210 }
util-byte.h
DetectEngineAppInspectionEngine_
Definition: detect.h:427
SigTableElmt_::url
const char * url
Definition: detect.h:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:431
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1500
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:49
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
PrefilterMpmIkeVendor::list_id
int list_id
Definition: detect-ike-vendor.c:50
IkeVendorGetDataArgs::local_id
uint32_t local_id
Definition: detect-ike-vendor.c:45
SigTableElmt_::name
const char * name
Definition: detect.h:1296
InspectionBuffer::initialized
bool initialized
Definition: detect.h:378
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1448
DetectEngineTransforms
Definition: detect.h:409
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1335
InspectionBuffer
Definition: detect.h:374
Flow_
Flow data structure.
Definition: flow.h:351
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1204
PrefilterMpmIkeVendor::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-ike-vendor.c:51
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1022
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:680
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@88::@90 app_v2
PrefilterMpmIkeVendor
struct PrefilterMpmIkeVendor PrefilterMpmIkeVendor
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:693
PrefilterMpmIkeVendor::transforms
const DetectEngineTransforms * transforms
Definition: detect-ike-vendor.c:52
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:433
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1546
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
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:172
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:38
DetectIkeVendorData::vendor
char * vendor
Definition: detect-ike-vendor.c:41
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:376
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
detect-ike-vendor.h
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
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:437
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:168
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@85 v2
DetectEngineThreadCtx_::mtc
MpmThreadCtx mtc
Definition: detect.h:1203
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:444
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1358
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:287
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:49
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
DETECT_AL_IKE_VENDOR
@ DETECT_AL_IKE_VENDOR
Definition: detect-engine-register.h:348
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:1559
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:37
PrefilterMpmIkeVendor
Definition: detect-ike-vendor.c:49
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:377
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
str
#define str(s)
Definition: suricata-common.h:291
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
IkeVendorGetDataArgs
Definition: detect-ike-vendor.c:44
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:441
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
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:1499
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1476
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
DetectIkeVendorData
Definition: detect-ike-vendor.c:40
MpmCtx_
Definition: util-mpm.h:88
DetectEngineContentInspection
bool DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, const uint32_t buffer_len, const uint32_t stream_start_offset, const uint8_t flags, const enum DetectContentInspectionType inspection_mode)
wrapper around DetectEngineContentInspectionInternal to return true/false only
Definition: detect-engine-content-inspection.c:723
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
IkeVendorGetDataArgs::txv
void * txv
Definition: detect-ike-vendor.c:46
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:682