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 */
39static int DetectIkeKeyExchangePayloadLengthSetup(DetectEngineCtx *, Signature *s, const char *str);
40static void DetectIkeKeyExchangePayloadLengthFree(DetectEngineCtx *, void *);
41static int g_ike_key_exch_payload_length_buffer_id = 0;
42
43static 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{
51 sigmatch_table[DETECT_IKE_KEY_EXCHANGE_PAYLOAD_LENGTH].name = "ike.key_exchange_payload_length";
53 "match IKE key exchange payload length";
55 "/rules/ike-keywords.html#ike-key-exchange-payload-length";
57 DetectIkeKeyExchangePayloadLengthMatch;
59 DetectIkeKeyExchangePayloadLengthSetup;
61 DetectIkeKeyExchangePayloadLengthFree;
62
63 DetectAppLayerInspectEngineRegister("ike.key_exchange_payload_length", ALPROTO_IKE,
65
66 DetectAppLayerInspectEngineRegister("ike.key_exchange_payload_length", ALPROTO_IKE,
68
69 g_ike_key_exch_payload_length_buffer_id =
70 DetectBufferTypeGetByName("ike.key_exchange_payload_length");
71}
72
73/**
74 * \internal
75 * \brief Function to match key exchange payload length of a IKE state
76 *
77 * \param det_ctx Pointer to the pattern matcher thread.
78 * \param f Pointer to the current flow.
79 * \param flags Flags.
80 * \param state App layer state.
81 * \param txv Pointer to the Ike Transaction.
82 * \param s Pointer to the Signature.
83 * \param ctx Pointer to the sigmatch that we will cast into DetectU32Data.
84 *
85 * \retval 0 no match.
86 * \retval 1 match.
87 */
88static int DetectIkeKeyExchangePayloadLengthMatch(DetectEngineThreadCtx *det_ctx, Flow *f,
89 uint8_t flags, void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
90{
91 SCEnter();
92
93 uint32_t length;
94 if (!SCIkeStateGetKeyExchangePayloadLength(txv, &length))
95 SCReturnInt(0);
96 const DetectU32Data *du32 = (const DetectU32Data *)ctx;
97 return DetectU32Match(length, du32);
98}
99
100/**
101 * \brief Function to add the parsed IKE key exchange payload length query into the current
102 * signature.
103 *
104 * \param de_ctx Pointer to the Detection Engine Context.
105 * \param s Pointer to the Current Signature.
106 * \param rawstr Pointer to the user provided flags options.
107 *
108 * \retval 0 on Success.
109 * \retval -1 on Failure.
110 */
111static int DetectIkeKeyExchangePayloadLengthSetup(
112 DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
113{
115 return -1;
116
117 DetectU32Data *key_exchange_payload_length = DetectU32Parse(rawstr);
118 if (key_exchange_payload_length == NULL)
119 return -1;
120
121 /* okay so far so good, lets get this into a SigMatch
122 * and put it in the Signature. */
123
125 (SigMatchCtx *)key_exchange_payload_length,
126 g_ike_key_exch_payload_length_buffer_id) == NULL) {
127 goto error;
128 }
129 return 0;
130
131error:
132 DetectIkeKeyExchangePayloadLengthFree(de_ctx, key_exchange_payload_length);
133 return -1;
134}
135
136/**
137 * \internal
138 * \brief Function to free memory associated with DetectU32Data.
139 *
140 * \param de_ptr Pointer to DetectU32Data.
141 */
142static void DetectIkeKeyExchangePayloadLengthFree(DetectEngineCtx *de_ctx, void *ptr)
143{
144 SCDetectU32Free(ptr);
145}
@ ALPROTO_IKE
uint8_t flags
Definition decode-gre.h:0
@ DETECT_IKE_KEY_EXCHANGE_PAYLOAD_LENGTH
DetectUintData_u32 * DetectU32Parse(const char *u32str)
This function is used to parse u32 options passed via some u32 keyword.
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
DetectUintData_u32 DetectU32Data
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
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.
void DetectIkeKeyExchangePayloadLengthRegister(void)
Registration function for ike.key_exchange_payload_length keyword.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition detect.h:1424
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
#define SCEnter(...)
Definition util-debug.h:277
#define SCReturnInt(x)
Definition util-debug.h:281