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 DetectEngineInspectIkeChosenSaGeneric(DetectEngineCtx *de_ctx,
60  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
61  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
62 
63 static int DetectIkeChosenSaMatch(DetectEngineThreadCtx *, Flow *, uint8_t, void *, void *,
64  const Signature *, const SigMatchCtx *);
65 void IKEChosenSaRegisterTests(void);
66 
67 /**
68  * \brief Registration function for ike.ChosenSa keyword.
69  */
71 {
72  sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].name = "ike.chosen_sa_attribute";
73  sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].desc = "match IKE chosen SA Attribute";
75  "/rules/ike-keywords.html#ike-chosen_sa_attribute";
76  sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].AppLayerTxMatch = DetectIkeChosenSaMatch;
77  sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].Setup = DetectIkeChosenSaSetup;
78  sigmatch_table[DETECT_AL_IKE_CHOSEN_SA].Free = DetectIkeChosenSaFree;
79 #ifdef UNITTESTS
81 #endif
82  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
83 
85  1, DetectEngineInspectIkeChosenSaGeneric, NULL);
86 
87  g_ike_chosen_sa_buffer_id = DetectBufferTypeGetByName("ike.chosen_sa_attribute");
88 }
89 
90 static int DetectEngineInspectIkeChosenSaGeneric(DetectEngineCtx *de_ctx,
91  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
92  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
93 {
95  de_ctx, det_ctx, s, engine->smd, f, flags, alstate, txv, tx_id);
96 }
97 
98 /**
99  * \internal
100  * \brief Function to match SA attributes of a IKE state
101  *
102  * \param det_ctx Pointer to the pattern matcher thread.
103  * \param f Pointer to the current flow.
104  * \param flags Flags.
105  * \param state App layer state.
106  * \param txv Pointer to the Ike Transaction.
107  * \param s Pointer to the Signature.
108  * \param ctx Pointer to the sigmatch that we will cast into DetectIkeChosenSaData.
109  *
110  * \retval 0 no match.
111  * \retval 1 match.
112  */
113 static int DetectIkeChosenSaMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
114  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
115 {
116  SCEnter();
117 
118  const DetectIkeChosenSaData *dd = (const DetectIkeChosenSaData *)ctx;
119 
120  uint32_t value;
121  if (!rs_ike_state_get_sa_attribute(txv, dd->sa_type, &value))
122  SCReturnInt(0);
123  if (value == dd->sa_value)
124  SCReturnInt(1);
125  SCReturnInt(0);
126 }
127 
128 /**
129  * \internal
130  * \brief Function to parse options passed via ike.chosen_sa_attribute keywords.
131  *
132  * \param rawstr Pointer to the user provided options.
133  *
134  * \retval dd pointer to DetectIkeChosenSaData on success.
135  * \retval NULL on failure.
136  */
137 static DetectIkeChosenSaData *DetectIkeChosenSaParse(const char *rawstr)
138 {
139  /*
140  * idea: do not implement one c file per type, invent an own syntax:
141  * ike.chosen_sa_attribute:"encryption_algorithm=4"
142  * ike.chosen_sa_attribute:"hash_algorithm=8"
143  */
144  DetectIkeChosenSaData *dd = NULL;
145  int ret = 0, res = 0;
146  size_t pcre2len;
147  char attribute[100];
148  char value[100];
149 
150  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
151  if (ret < 3 || ret > 5) {
153  "pcre match for ike.chosen_sa_attribute failed, should be: <sa_attribute>=<type>, "
154  "but was: %s; error code %d",
155  rawstr, ret);
156  goto error;
157  }
158 
159  pcre2len = sizeof(attribute);
160  res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)attribute, &pcre2len);
161  if (res < 0) {
162  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
163  goto error;
164  }
165 
166  pcre2len = sizeof(value);
167  res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)value, &pcre2len);
168  if (res < 0) {
169  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
170  goto error;
171  }
172 
173  dd = SCCalloc(1, sizeof(DetectIkeChosenSaData));
174  if (unlikely(dd == NULL))
175  goto error;
176 
177  dd->sa_type = SCStrdup(attribute);
178  if (dd->sa_type == NULL)
179  goto error;
180 
181  if (ByteExtractStringUint32(&dd->sa_value, 10, strlen(value), value) <= 0) {
182  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid input as arg "
183  "to ike.chosen_sa_attribute keyword");
184  goto error;
185  }
186 
187  return dd;
188 
189 error:
190  if (dd) {
191  if (dd->sa_type != NULL)
192  SCFree(dd->sa_type);
193  SCFree(dd);
194  }
195  return NULL;
196 }
197 
198 /**
199  * \brief Function to add the parsed IKE SA attribute query into the current signature.
200  *
201  * \param de_ctx Pointer to the Detection Engine Context.
202  * \param s Pointer to the Current Signature.
203  * \param rawstr Pointer to the user provided flags options.
204  *
205  * \retval 0 on Success.
206  * \retval -1 on Failure.
207  */
208 static int DetectIkeChosenSaSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
209 {
211  return -1;
212 
213  DetectIkeChosenSaData *dd = DetectIkeChosenSaParse(rawstr);
214  if (dd == NULL) {
215  SCLogError(SC_ERR_INVALID_ARGUMENT, "Parsing \'%s\' failed", rawstr);
216  goto error;
217  }
218 
219  /* okay so far so good, lets get this into a SigMatch
220  * and put it in the Signature. */
221  SigMatch *sm = SigMatchAlloc();
222  if (sm == NULL)
223  goto error;
224 
226  sm->ctx = (void *)dd;
227 
228  SigMatchAppendSMToList(s, sm, g_ike_chosen_sa_buffer_id);
229  return 0;
230 
231 error:
232  DetectIkeChosenSaFree(de_ctx, dd);
233  return -1;
234 }
235 
236 /**
237  * \internal
238  * \brief Function to free memory associated with DetectIkeChosenSaData.
239  *
240  * \param de_ptr Pointer to DetectIkeChosenSaData.
241  */
242 static void DetectIkeChosenSaFree(DetectEngineCtx *de_ctx, void *ptr)
243 {
245  if (dd == NULL)
246  return;
247  if (dd->sa_type != NULL)
248  SCFree(dd->sa_type);
249 
250  SCFree(ptr);
251 }
252 
253 /*
254  * ONLY TESTS BELOW THIS COMMENT
255  */
256 
257 #ifdef UNITTESTS
258 
259 /**
260  * \test IKEChosenSaParserTest is a test for valid values
261  *
262  * \retval 1 on success
263  * \retval 0 on failure
264  */
265 static int IKEChosenSaParserTest(void)
266 {
267  DetectIkeChosenSaData *de = NULL;
268  de = DetectIkeChosenSaParse("alg_hash=2");
269 
270  FAIL_IF_NULL(de);
271  FAIL_IF(de->sa_value != 2);
272  FAIL_IF(strcmp(de->sa_type, "alg_hash") != 0);
273 
274  DetectIkeChosenSaFree(NULL, de);
275  PASS;
276 }
277 
278 #endif /* UNITTESTS */
279 
281 {
282 #ifdef UNITTESTS
283  UtRegisterTest("IKEChosenSaParserTest", IKEChosenSaParserTest);
284 #endif /* UNITTESTS */
285 }
util-byte.h
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
DetectEngineAppInspectionEngine_
Definition: detect.h:397
SigTableElmt_::url
const char * url
Definition: detect.h:1204
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1489
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:1203
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2435
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1191
DetectEngineInspectGenericList
int DetectEngineInspectGenericList(const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, const uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1575
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1201
IKEChosenSaRegisterTests
void IKEChosenSaRegisterTests(void)
Definition: detect-ike-chosen-sa.c:280
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:758
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:1172
DetectIkeChosenSaData::sa_value
uint32_t sa_value
Definition: detect-ike-chosen-sa.c:51
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:829
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:1003
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2558
SC_ERR_PCRE_COPY_SUBSTRING
@ SC_ERR_PCRE_COPY_SUBSTRING
Definition: util-error.h:358
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
de
int de
Definition: app-layer-htp.c:527
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
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:174
conf.h
ByteExtractStringUint32
int ByteExtractStringUint32(uint32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:239
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:321
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:315
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:414
DETECT_AL_IKE_CHOSEN_SA
@ DETECT_AL_IKE_CHOSEN_SA
Definition: detect-engine-register.h:306
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_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:257
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
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:517
SigMatch_
a single match condition for a signature
Definition: detect.h:320
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:70
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
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:1193