suricata
detect-ike-key-exchange-payload-length.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 "app-layer-parser.h"
31 #include "util-byte.h"
32 #include "detect-engine-uint.h"
33 
34 #include "rust-bindings.h"
35 
36 /**
37  * [ike.key_exchange_payload_length]:[=|<|>|<=|>=]<length>;
38  */
39 static int DetectIkeKeyExchangePayloadLengthSetup(DetectEngineCtx *, Signature *s, const char *str);
40 static void DetectIkeKeyExchangePayloadLengthFree(DetectEngineCtx *, void *);
41 static int g_ike_key_exch_payload_length_buffer_id = 0;
42 
43 static int DetectIkeKeyExchangePayloadLengthMatch(DetectEngineThreadCtx *, Flow *, uint8_t, void *,
44  void *, const Signature *, const SigMatchCtx *);
45 
46 /**
47  * \brief Registration function for ike.key_exchange_payload_length keyword.
48  */
50 {
52  "ike.key_exchange_payload_length";
54  "match IKE key exchange payload length";
56  "/rules/ike-keywords.html#ike-key-exchange-payload-length";
58  DetectIkeKeyExchangePayloadLengthMatch;
60  DetectIkeKeyExchangePayloadLengthSetup;
62  DetectIkeKeyExchangePayloadLengthFree;
63 
64  DetectAppLayerInspectEngineRegister2("ike.key_exchange_payload_length", ALPROTO_IKE,
66 
67  DetectAppLayerInspectEngineRegister2("ike.key_exchange_payload_length", ALPROTO_IKE,
69 
70  g_ike_key_exch_payload_length_buffer_id =
71  DetectBufferTypeGetByName("ike.key_exchange_payload_length");
72 }
73 
74 /**
75  * \internal
76  * \brief Function to match key exchange payload length of a IKE state
77  *
78  * \param det_ctx Pointer to the pattern matcher thread.
79  * \param f Pointer to the current flow.
80  * \param flags Flags.
81  * \param state App layer state.
82  * \param txv Pointer to the Ike Transaction.
83  * \param s Pointer to the Signature.
84  * \param ctx Pointer to the sigmatch that we will cast into DetectU32Data.
85  *
86  * \retval 0 no match.
87  * \retval 1 match.
88  */
89 static int DetectIkeKeyExchangePayloadLengthMatch(DetectEngineThreadCtx *det_ctx, Flow *f,
90  uint8_t flags, void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
91 {
92  SCEnter();
93 
94  uint32_t length;
95  if (!rs_ike_state_get_key_exchange_payload_length(txv, &length))
96  SCReturnInt(0);
97  const DetectU32Data *du32 = (const DetectU32Data *)ctx;
98  return DetectU32Match(length, du32);
99 }
100 
101 /**
102  * \brief Function to add the parsed IKE key exchange payload length query into the current
103  * signature.
104  *
105  * \param de_ctx Pointer to the Detection Engine Context.
106  * \param s Pointer to the Current Signature.
107  * \param rawstr Pointer to the user provided flags options.
108  *
109  * \retval 0 on Success.
110  * \retval -1 on Failure.
111  */
112 static int DetectIkeKeyExchangePayloadLengthSetup(
113  DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
114 {
116  return -1;
117 
118  DetectU32Data *key_exchange_payload_length = DetectU32Parse(rawstr);
119  if (key_exchange_payload_length == NULL)
120  return -1;
121 
122  /* okay so far so good, lets get this into a SigMatch
123  * and put it in the Signature. */
124  SigMatch *sm = SigMatchAlloc();
125  if (sm == NULL)
126  goto error;
127 
129  sm->ctx = (SigMatchCtx *)key_exchange_payload_length;
130 
131  SigMatchAppendSMToList(s, sm, g_ike_key_exch_payload_length_buffer_id);
132  return 0;
133 
134 error:
135  DetectIkeKeyExchangePayloadLengthFree(de_ctx, key_exchange_payload_length);
136  return -1;
137 }
138 
139 /**
140  * \internal
141  * \brief Function to free memory associated with DetectU32Data.
142  *
143  * \param de_ptr Pointer to DetectU32Data.
144  */
145 static void DetectIkeKeyExchangePayloadLengthFree(DetectEngineCtx *de_ctx, void *ptr)
146 {
147  rs_detect_u32_free(ptr);
148 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1248
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
detect-engine.h
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:49
DetectU32Match
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
Definition: detect-engine-uint.c:31
SigTableElmt_::desc
const char * desc
Definition: detect.h:1247
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1235
SigTableElmt_::name
const char * name
Definition: detect.h:1245
DetectU32Parse
DetectUintData_u32 * DetectU32Parse(const char *u32str)
This function is used to parse u32 options passed via some u32 keyword.
Definition: detect-engine-uint.c:45
Flow_
Flow data structure.
Definition: flow.h:353
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1216
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:231
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
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.h
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:317
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
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
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:309
DETECT_AL_IKE_KEY_EXCHANGE_PAYLOAD_LENGTH
@ DETECT_AL_IKE_KEY_EXCHANGE_PAYLOAD_LENGTH
Definition: detect-engine-register.h:326
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:315
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
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:1951
str
#define str(s)
Definition: suricata-common.h:272
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
SigMatch_
a single match condition for a signature
Definition: detect.h:314
detect-ike-key-exchange-payload-length.h
DetectIkeKeyExchangePayloadLengthRegister
void DetectIkeKeyExchangePayloadLengthRegister(void)
Registration function for ike.key_exchange_payload_length keyword.
Definition: detect-ike-key-exchange-payload-length.c:49
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:302
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349