suricata
detect-ike-chosen-sa.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 "util-unittest.h"
33
34#include "rust.h"
35
36/**
37 * [ike.chosen_sa_attribute]:<sa_attribute>=<type>;
38 */
39
40// support the basic attributes, which are parsed as integer and life_duration, if variable length
41// is 4 it is stored as integer too
42#define PARSE_REGEX \
43 "^\\s*(alg_enc|alg_hash|alg_auth|alg_dh|\
44sa_group_type|sa_life_type|sa_life_duration|alg_prf|sa_key_length|sa_field_size)\
45\\s*=\\s*([0-9]+)\\s*$"
46
47static DetectParseRegex parse_regex;
48
49typedef struct {
50 char *sa_type;
51 uint32_t sa_value;
53
54static DetectIkeChosenSaData *DetectIkeChosenSaParse(const char *);
55static int DetectIkeChosenSaSetup(DetectEngineCtx *, Signature *s, const char *str);
56static void DetectIkeChosenSaFree(DetectEngineCtx *, void *);
57static int g_ike_chosen_sa_buffer_id = 0;
58
59static int DetectIkeChosenSaMatch(DetectEngineThreadCtx *, Flow *, uint8_t, void *, void *,
60 const Signature *, const SigMatchCtx *);
62
63/**
64 * \brief Registration function for ike.ChosenSa keyword.
65 */
67{
68 sigmatch_table[DETECT_IKE_CHOSEN_SA].name = "ike.chosen_sa_attribute";
69 sigmatch_table[DETECT_IKE_CHOSEN_SA].desc = "match IKE chosen SA Attribute";
70 sigmatch_table[DETECT_IKE_CHOSEN_SA].url = "/rules/ike-keywords.html#ike-chosen_sa_attribute";
71 sigmatch_table[DETECT_IKE_CHOSEN_SA].AppLayerTxMatch = DetectIkeChosenSaMatch;
72 sigmatch_table[DETECT_IKE_CHOSEN_SA].Setup = DetectIkeChosenSaSetup;
73 sigmatch_table[DETECT_IKE_CHOSEN_SA].Free = DetectIkeChosenSaFree;
74#ifdef UNITTESTS
76#endif
78
81
82 g_ike_chosen_sa_buffer_id = DetectBufferTypeGetByName("ike.chosen_sa_attribute");
83}
84
85/**
86 * \internal
87 * \brief Function to match SA attributes of a IKE state
88 *
89 * \param det_ctx Pointer to the pattern matcher thread.
90 * \param f Pointer to the current flow.
91 * \param flags Flags.
92 * \param state App layer state.
93 * \param txv Pointer to the Ike Transaction.
94 * \param s Pointer to the Signature.
95 * \param ctx Pointer to the sigmatch that we will cast into DetectIkeChosenSaData.
96 *
97 * \retval 0 no match.
98 * \retval 1 match.
99 */
100static int DetectIkeChosenSaMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
101 void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
102{
103 SCEnter();
104
106
107 uint32_t value;
108 if (!SCIkeStateGetSaAttribute(txv, dd->sa_type, &value))
109 SCReturnInt(0);
110 if (value == dd->sa_value)
111 SCReturnInt(1);
112 SCReturnInt(0);
113}
114
115/**
116 * \internal
117 * \brief Function to parse options passed via ike.chosen_sa_attribute keywords.
118 *
119 * \param rawstr Pointer to the user provided options.
120 *
121 * \retval dd pointer to DetectIkeChosenSaData on success.
122 * \retval NULL on failure.
123 */
124static DetectIkeChosenSaData *DetectIkeChosenSaParse(const char *rawstr)
125{
126 /*
127 * idea: do not implement one c file per type, invent an own syntax:
128 * ike.chosen_sa_attribute:"encryption_algorithm=4"
129 * ike.chosen_sa_attribute:"hash_algorithm=8"
130 */
131 DetectIkeChosenSaData *dd = NULL;
132 int res = 0;
133 size_t pcre2len;
134 char attribute[100];
135 char value[100];
136
137 pcre2_match_data *match = NULL;
138 int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
139 if (ret < 3 || ret > 5) {
141 "pcre match for ike.chosen_sa_attribute failed, should be: <sa_attribute>=<type>, "
142 "but was: %s; error code %d",
143 rawstr, ret);
144 goto error;
145 }
146
147 pcre2len = sizeof(attribute);
148 res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)attribute, &pcre2len);
149 if (res < 0) {
150 SCLogError("pcre2_substring_copy_bynumber failed");
151 goto error;
152 }
153
154 pcre2len = sizeof(value);
155 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)value, &pcre2len);
156 if (res < 0) {
157 SCLogError("pcre2_substring_copy_bynumber failed");
158 goto error;
159 }
160
161 dd = SCCalloc(1, sizeof(DetectIkeChosenSaData));
162 if (unlikely(dd == NULL))
163 goto error;
164
165 dd->sa_type = SCStrdup(attribute);
166 if (dd->sa_type == NULL)
167 goto error;
168
169 if (ByteExtractStringUint32(&dd->sa_value, 10, strlen(value), value) <= 0) {
170 SCLogError("invalid input as arg "
171 "to ike.chosen_sa_attribute keyword");
172 goto error;
173 }
174
175 pcre2_match_data_free(match);
176 return dd;
177
178error:
179 if (match) {
180 pcre2_match_data_free(match);
181 }
182 if (dd) {
183 if (dd->sa_type != NULL)
184 SCFree(dd->sa_type);
185 SCFree(dd);
186 }
187 return NULL;
188}
189
190/**
191 * \brief Function to add the parsed IKE SA attribute query into the current signature.
192 *
193 * \param de_ctx Pointer to the Detection Engine Context.
194 * \param s Pointer to the Current Signature.
195 * \param rawstr Pointer to the user provided flags options.
196 *
197 * \retval 0 on Success.
198 * \retval -1 on Failure.
199 */
200static int DetectIkeChosenSaSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
201{
203 return -1;
204
205 DetectIkeChosenSaData *dd = DetectIkeChosenSaParse(rawstr);
206 if (dd == NULL) {
207 SCLogError("Parsing \'%s\' failed", rawstr);
208 goto error;
209 }
210
211 /* okay so far so good, lets get this into a SigMatch
212 * and put it in the Signature. */
213
215 g_ike_chosen_sa_buffer_id) == NULL) {
216 goto error;
217 }
218 return 0;
219
220error:
221 DetectIkeChosenSaFree(de_ctx, dd);
222 return -1;
223}
224
225/**
226 * \internal
227 * \brief Function to free memory associated with DetectIkeChosenSaData.
228 *
229 * \param de_ptr Pointer to DetectIkeChosenSaData.
230 */
231static void DetectIkeChosenSaFree(DetectEngineCtx *de_ctx, void *ptr)
232{
234 if (dd == NULL)
235 return;
236 if (dd->sa_type != NULL)
237 SCFree(dd->sa_type);
238
239 SCFree(ptr);
240}
241
242/*
243 * ONLY TESTS BELOW THIS COMMENT
244 */
245
246#ifdef UNITTESTS
247
248/**
249 * \test IKEChosenSaParserTest is a test for valid values
250 *
251 * \retval 1 on success
252 * \retval 0 on failure
253 */
254static int IKEChosenSaParserTest(void)
255{
256 DetectIkeChosenSaData *de = NULL;
257 de = DetectIkeChosenSaParse("alg_hash=2");
258
259 FAIL_IF_NULL(de);
260 FAIL_IF(de->sa_value != 2);
261 FAIL_IF(strcmp(de->sa_type, "alg_hash") != 0);
262
263 DetectIkeChosenSaFree(NULL, de);
264 PASS;
265}
266
267#endif /* UNITTESTS */
268
270{
271#ifdef UNITTESTS
272 UtRegisterTest("IKEChosenSaParserTest", IKEChosenSaParserTest);
273#endif /* UNITTESTS */
274}
@ ALPROTO_IKE
uint8_t flags
Definition decode-gre.h:0
@ DETECT_IKE_CHOSEN_SA
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 IKEChosenSaRegisterTests(void)
#define PARSE_REGEX
void DetectIkeChosenSaRegister(void)
Registration function for ike.ChosenSa keyword.
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
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
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
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
void(* RegisterTests)(void)
Definition detect.h:1448
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
int ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
Definition util-byte.c:239
#define SCEnter(...)
Definition util-debug.h:277
#define SCReturnInt(x)
Definition util-debug.h:281
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define SCStrdup(s)
Definition util-mem.h:56
#define unlikely(expr)