suricata
detect-ike-nonce-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.nonce_payload_length]:[=|<|>|<=|>=]<length>;
38  */
39 static int DetectIkeNoncePayloadLengthSetup(DetectEngineCtx *, Signature *s, const char *str);
40 static void DetectIkeNoncePayloadLengthFree(DetectEngineCtx *, void *);
41 static int g_ike_nonce_payload_length_buffer_id = 0;
42 
43 static int DetectIkeNoncePayloadLengthMatch(DetectEngineThreadCtx *, Flow *, uint8_t, void *,
44  void *, const Signature *, const SigMatchCtx *);
45 
46 /**
47  * \brief Registration function for ike.nonce_payload_length keyword.
48  */
50 {
51  sigmatch_table[DETECT_AL_IKE_NONCE_PAYLOAD_LENGTH].name = "ike.nonce_payload_length";
52  sigmatch_table[DETECT_AL_IKE_NONCE_PAYLOAD_LENGTH].desc = "match IKE nonce payload length";
54  "/rules/ike-keywords.html#ike-nonce-payload-length";
56  DetectIkeNoncePayloadLengthMatch;
57  sigmatch_table[DETECT_AL_IKE_NONCE_PAYLOAD_LENGTH].Setup = DetectIkeNoncePayloadLengthSetup;
58  sigmatch_table[DETECT_AL_IKE_NONCE_PAYLOAD_LENGTH].Free = DetectIkeNoncePayloadLengthFree;
59 
62 
65 
66  g_ike_nonce_payload_length_buffer_id = DetectBufferTypeGetByName("ike.nonce_payload_length");
67 }
68 
69 /**
70  * \internal
71  * \brief Function to match nonce length of a IKE state
72  *
73  * \param det_ctx Pointer to the pattern matcher thread.
74  * \param f Pointer to the current flow.
75  * \param flags Flags.
76  * \param state App layer state.
77  * \param txv Pointer to the Ike Transaction.
78  * \param s Pointer to the Signature.
79  * \param ctx Pointer to the sigmatch that we will cast into DetectU32Data.
80  *
81  * \retval 0 no match.
82  * \retval 1 match.
83  */
84 static int DetectIkeNoncePayloadLengthMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
85  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
86 {
87  SCEnter();
88 
89  uint32_t length;
90  if (!rs_ike_state_get_nonce_payload_length(txv, &length))
91  SCReturnInt(0);
92  const DetectU32Data *du32 = (const DetectU32Data *)ctx;
93  return DetectU32Match(length, du32);
94 }
95 
96 /**
97  * \brief Function to add the parsed IKE nonce length field into the current signature.
98  *
99  * \param de_ctx Pointer to the Detection Engine Context.
100  * \param s Pointer to the Current Signature.
101  * \param rawstr Pointer to the user provided flags options.
102  *
103  * \retval 0 on Success.
104  * \retval -1 on Failure.
105  */
106 static int DetectIkeNoncePayloadLengthSetup(
107  DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
108 {
110  return -1;
111 
112  DetectU32Data *nonce_payload_length = DetectU32Parse(rawstr);
113  if (nonce_payload_length == NULL)
114  return -1;
115 
116  /* okay so far so good, lets get this into a SigMatch
117  * and put it in the Signature. */
118 
120  (SigMatchCtx *)nonce_payload_length,
121  g_ike_nonce_payload_length_buffer_id) == NULL) {
122  goto error;
123  }
124  return 0;
125 
126 error:
127  DetectIkeNoncePayloadLengthFree(de_ctx, nonce_payload_length);
128  return -1;
129 }
130 
131 /**
132  * \internal
133  * \brief Function to free memory associated with DetectU32Data.
134  *
135  * \param de_ptr Pointer to DetectU32Data.
136  */
137 static void DetectIkeNoncePayloadLengthFree(DetectEngineCtx *de_ctx, void *ptr)
138 {
139  rs_detect_u32_free(ptr);
140 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
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:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1294
SigTableElmt_::name
const char * name
Definition: detect.h:1304
detect-ike-nonce-payload-length.h
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:356
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1275
DETECT_AL_IKE_NONCE_PAYLOAD_LENGTH
@ DETECT_AL_IKE_NONCE_PAYLOAD_LENGTH
Definition: detect-engine-register.h:325
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect.h
app-layer-parser.h
conf.h
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:344
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
DetectIkeNoncePayloadLengthRegister
void DetectIkeNoncePayloadLengthRegister(void)
Registration function for ike.nonce_payload_length keyword.
Definition: detect-ike-nonce-payload-length.c:49
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:2111
str
#define str(s)
Definition: suricata-common.h:291
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:240
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:436
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275