suricata
detect-ike-vendor.c
Go to the documentation of this file.
1 /* Copyright (C) 2020 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 
37 static int DetectIkeVendorSetup(DetectEngineCtx *, Signature *, const char *);
38 
39 typedef struct {
40  char *vendor;
42 
44  uint32_t local_id;
45  void *txv;
46 };
47 
48 typedef struct PrefilterMpmIkeVendor {
49  int list_id;
50  const MpmCtx *mpm_ctx;
53 
54 static int g_ike_vendor_buffer_id = 0;
55 
56 static InspectionBuffer *IkeVendorGetData(DetectEngineThreadCtx *det_ctx,
57  const DetectEngineTransforms *transforms, Flow *f, struct IkeVendorGetDataArgs *cbdata,
58  int list_id, bool first)
59 {
60  SCEnter();
61 
62  InspectionBuffer *buffer =
63  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
64  if (buffer == NULL)
65  return NULL;
66  if (!first && buffer->inspect != NULL)
67  return buffer;
68 
69  const uint8_t *data;
70  uint32_t data_len;
71  if (rs_ike_tx_get_vendor(cbdata->txv, cbdata->local_id, &data, &data_len) == 0) {
72  return NULL;
73  }
74 
75  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
76 
77  SCReturnPtr(buffer, "InspectionBuffer");
78 }
79 
80 /** \brief IkeVendor Mpm prefilter callback
81  *
82  * \param det_ctx detection engine thread ctx
83  * \param p packet to inspect
84  * \param f flow to inspect
85  * \param txv tx to inspect
86  * \param pectx inspection context
87  */
88 static void PrefilterTxIkeVendor(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
89  Flow *f, void *txv, const uint64_t idx, const uint8_t flags)
90 {
91  SCEnter();
92 
93  const PrefilterMpmIkeVendor *ctx = (const PrefilterMpmIkeVendor *)pectx;
94  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
95  const int list_id = ctx->list_id;
96 
97  uint32_t local_id = 0;
98  while (1) {
99  struct IkeVendorGetDataArgs cbdata = { local_id, txv };
100  InspectionBuffer *buffer =
101  IkeVendorGetData(det_ctx, ctx->transforms, f, &cbdata, list_id, true);
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  }
109  local_id++;
110  }
111 
112  SCReturn;
113 }
114 
115 static void PrefilterMpmIkeVendorFree(void *ptr)
116 {
117  if (ptr != NULL)
118  SCFree(ptr);
119 }
120 
121 static int PrefilterMpmIkeVendorRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
122  MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
123 {
124  PrefilterMpmIkeVendor *pectx = SCCalloc(1, sizeof(*pectx));
125  if (pectx == NULL)
126  return -1;
127  pectx->list_id = list_id;
128  pectx->mpm_ctx = mpm_ctx;
129  pectx->transforms = &mpm_reg->transforms;
130 
131  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxIkeVendor, mpm_reg->app_v2.alproto,
132  mpm_reg->app_v2.tx_min_progress, pectx, PrefilterMpmIkeVendorFree, mpm_reg->pname);
133 }
134 
135 static int DetectEngineInspectIkeVendor(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
136  const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
137  void *alstate, void *txv, uint64_t tx_id)
138 {
139  uint32_t local_id = 0;
140 
141  const DetectEngineTransforms *transforms = NULL;
142  if (!engine->mpm) {
143  transforms = engine->v2.transforms;
144  }
145 
146  while (1) {
147  struct IkeVendorGetDataArgs cbdata = {
148  local_id,
149  txv,
150  };
151  InspectionBuffer *buffer =
152  IkeVendorGetData(det_ctx, transforms, f, &cbdata, engine->sm_list, false);
153  if (buffer == NULL || buffer->inspect == NULL)
154  break;
155 
156  det_ctx->buffer_offset = 0;
157  det_ctx->discontinue_matching = 0;
158  det_ctx->inspection_recursion_counter = 0;
159 
160  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
161  (uint8_t *)buffer->inspect, buffer->inspect_len, buffer->inspect_offset,
163  if (match == 1) {
165  }
166  local_id++;
167  }
169 }
170 
171 /**
172  * \brief Registration function for ike.vendor keyword.
173  */
175 {
176  sigmatch_table[DETECT_AL_IKE_VENDOR].name = "ike.vendor";
177  sigmatch_table[DETECT_AL_IKE_VENDOR].desc = "match IKE Vendor";
178  sigmatch_table[DETECT_AL_IKE_VENDOR].url = "/rules/ike-keywords.html#ike-vendor";
179  sigmatch_table[DETECT_AL_IKE_VENDOR].Setup = DetectIkeVendorSetup;
182 
183  DetectAppLayerMpmRegister2("ike.vendor", SIG_FLAG_TOSERVER, 1, PrefilterMpmIkeVendorRegister,
184  NULL, ALPROTO_IKE, 1);
185 
187  "ike.vendor", ALPROTO_IKE, SIG_FLAG_TOSERVER, 1, DetectEngineInspectIkeVendor, NULL);
188 
189  g_ike_vendor_buffer_id = DetectBufferTypeGetByName("ike.vendor");
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(s, g_ike_vendor_buffer_id) < 0)
206  return -1;
208  return -1;
209  return 0;
210 }
util-byte.h
DetectEngineAppInspectionEngine_
Definition: detect.h:397
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:401
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1031
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1397
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:49
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
PrefilterMpmIkeVendor::list_id
int list_id
Definition: detect-ike-vendor.c:49
IkeVendorGetDataArgs::local_id
uint32_t local_id
Definition: detect-ike-vendor.c:44
SigTableElmt_::name
const char * name
Definition: detect.h:1201
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1346
DetectEngineTransforms
Definition: detect.h:378
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:615
InspectionBuffer
Definition: detect.h:344
Flow_
Flow data structure.
Definition: flow.h:353
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1101
PrefilterMpmIkeVendor::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-ike-vendor.c:50
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:601
DetectEngineContentInspection
int 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:102
PrefilterMpmIkeVendor
struct PrefilterMpmIkeVendor PrefilterMpmIkeVendor
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
PrefilterMpmIkeVendor::transforms
const DetectEngineTransforms * transforms
Definition: detect-ike-vendor.c:51
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1099
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:403
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
DetectIkeVendorRegister
void DetectIkeVendorRegister(void)
Registration function for ike.vendor keyword.
Definition: detect-ike-vendor.c:174
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:40
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:346
detect-ike-vendor.h
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
SCReturn
#define SCReturn
Definition: util-debug.h:302
Packet_
Definition: decode.h:414
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:174
conf.h
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:165
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@85::@87 app_v2
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1066
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:414
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
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:46
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:603
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:1078
DETECT_AL_IKE_VENDOR
@ DETECT_AL_IKE_VENDOR
Definition: detect-engine-register.h:305
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:1077
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:48
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:347
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@83 v2
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
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:517
IkeVendorGetDataArgs
Definition: detect-ike-vendor.c:43
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:259
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:411
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:1030
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:35
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1373
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:941
DetectIkeVendorData
Definition: detect-ike-vendor.c:39
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:45