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-bindings.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 ret = 0, res = 0;
134  size_t pcre2len;
135  char attribute[100];
136  char value[100];
137 
138  ret = DetectParsePcreExec(&parse_regex, 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(parse_regex.match, 1, (PCRE2_UCHAR8 *)attribute, &pcre2len);
149  if (res < 0) {
150  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
151  goto error;
152  }
153 
154  pcre2len = sizeof(value);
155  res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)value, &pcre2len);
156  if (res < 0) {
157  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "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(SC_ERR_INVALID_SIGNATURE, "invalid input as arg "
171  "to ike.chosen_sa_attribute keyword");
172  goto error;
173  }
174 
175  return dd;
176 
177 error:
178  if (dd) {
179  if (dd->sa_type != NULL)
180  SCFree(dd->sa_type);
181  SCFree(dd);
182  }
183  return NULL;
184 }
185 
186 /**
187  * \brief Function to add the parsed IKE SA attribute query into the current signature.
188  *
189  * \param de_ctx Pointer to the Detection Engine Context.
190  * \param s Pointer to the Current Signature.
191  * \param rawstr Pointer to the user provided flags options.
192  *
193  * \retval 0 on Success.
194  * \retval -1 on Failure.
195  */
196 static int DetectIkeChosenSaSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
197 {
199  return -1;
200 
201  DetectIkeChosenSaData *dd = DetectIkeChosenSaParse(rawstr);
202  if (dd == NULL) {
203  SCLogError(SC_ERR_INVALID_ARGUMENT, "Parsing \'%s\' failed", rawstr);
204  goto error;
205  }
206 
207  /* okay so far so good, lets get this into a SigMatch
208  * and put it in the Signature. */
209  SigMatch *sm = SigMatchAlloc();
210  if (sm == NULL)
211  goto error;
212 
214  sm->ctx = (void *)dd;
215 
216  SigMatchAppendSMToList(s, sm, g_ike_chosen_sa_buffer_id);
217  return 0;
218 
219 error:
220  DetectIkeChosenSaFree(de_ctx, dd);
221  return -1;
222 }
223 
224 /**
225  * \internal
226  * \brief Function to free memory associated with DetectIkeChosenSaData.
227  *
228  * \param de_ptr Pointer to DetectIkeChosenSaData.
229  */
230 static void DetectIkeChosenSaFree(DetectEngineCtx *de_ctx, void *ptr)
231 {
233  if (dd == NULL)
234  return;
235  if (dd->sa_type != NULL)
236  SCFree(dd->sa_type);
237 
238  SCFree(ptr);
239 }
240 
241 /*
242  * ONLY TESTS BELOW THIS COMMENT
243  */
244 
245 #ifdef UNITTESTS
246 
247 /**
248  * \test IKEChosenSaParserTest is a test for valid values
249  *
250  * \retval 1 on success
251  * \retval 0 on failure
252  */
253 static int IKEChosenSaParserTest(void)
254 {
255  DetectIkeChosenSaData *de = NULL;
256  de = DetectIkeChosenSaParse("alg_hash=2");
257 
258  FAIL_IF_NULL(de);
259  FAIL_IF(de->sa_value != 2);
260  FAIL_IF(strcmp(de->sa_type, "alg_hash") != 0);
261 
262  DetectIkeChosenSaFree(NULL, de);
263  PASS;
264 }
265 
266 #endif /* UNITTESTS */
267 
269 {
270 #ifdef UNITTESTS
271  UtRegisterTest("IKEChosenSaParserTest", IKEChosenSaParserTest);
272 #endif /* UNITTESTS */
273 }
util-byte.h
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
SigTableElmt_::url
const char * url
Definition: detect.h:1248
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
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:1247
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2475
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1235
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1245
IKEChosenSaRegisterTests
void IKEChosenSaRegisterTests(void)
Definition: detect-ike-chosen-sa.c:268
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:353
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1216
DetectIkeChosenSaData::sa_value
uint32_t sa_value
Definition: detect-ike-chosen-sa.c:51
ByteExtractStringUint32
int ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:239
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:231
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
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:1034
de
uint8_t de
Definition: app-layer-htp.c:568
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2598
SC_ERR_PCRE_COPY_SUBSTRING
@ SC_ERR_PCRE_COPY_SUBSTRING
Definition: util-error.h:358
SCEnter
#define SCEnter(...)
Definition: util-debug.h:298
detect.h
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:317
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:227
conf.h
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
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:309
DETECT_AL_IKE_CHOSEN_SA
@ DETECT_AL_IKE_CHOSEN_SA
Definition: detect-engine-register.h:325
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
SigMatch_::type
uint16_t type
Definition: detect.h:315
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
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:1951
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-ike-chosen-sa.c:42
str
#define str(s)
Definition: suricata-common.h:272
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
SigMatch_
a single match condition for a signature
Definition: detect.h:314
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:302
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1237