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_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
77  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
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  */
100 static 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 
105  const DetectIkeChosenSaData *dd = (const DetectIkeChosenSaData *)ctx;
106 
107  uint32_t value;
108  if (!rs_ike_state_get_sa_attribute(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  */
124 static 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) {
140  SCLogError(
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 
178 error:
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  */
200 static 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 
220 error:
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  */
231 static 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  */
254 static 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 }
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1314
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1765
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:55
SigTableElmt_::desc
const char * desc
Definition: detect.h:1313
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1301
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1311
IKEChosenSaRegisterTests
void IKEChosenSaRegisterTests(void)
Definition: detect-ike-chosen-sa.c:269
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:354
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:846
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1282
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:2743
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:270
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1296
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1096
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1101
de
uint8_t de
Definition: app-layer-htp.c:563
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2869
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:346
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
DETECT_IKE_CHOSEN_SA
@ DETECT_IKE_CHOSEN_SA
Definition: detect-engine-register.h:328
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:2114
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:606
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:242
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:462
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:1303