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