/src/suricata7/src/detect-flow.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-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 | | * \file |
20 | | * |
21 | | * \author Victor Julien <victor@inliniac.net> |
22 | | * |
23 | | * FLOW part of the detection engine. |
24 | | */ |
25 | | |
26 | | #include "suricata-common.h" |
27 | | #include "decode.h" |
28 | | |
29 | | #include "detect.h" |
30 | | #include "detect-parse.h" |
31 | | #include "detect-engine.h" |
32 | | #include "detect-engine-prefilter-common.h" |
33 | | #include "detect-engine-build.h" |
34 | | |
35 | | #include "flow.h" |
36 | | #include "flow-var.h" |
37 | | |
38 | | #include "detect-flow.h" |
39 | | |
40 | | #include "util-unittest.h" |
41 | | #include "util-unittest-helper.h" |
42 | | #include "util-debug.h" |
43 | | |
44 | | /** |
45 | | * \brief Regex for parsing our flow options |
46 | | */ |
47 | 73 | #define PARSE_REGEX "^\\s*([A-z_]+)\\s*(?:,\\s*([A-z_]+))?\\s*(?:,\\s*([A-z_]+))?\\s*$" |
48 | | |
49 | | static DetectParseRegex parse_regex; |
50 | | |
51 | | int DetectFlowMatch (DetectEngineThreadCtx *, Packet *, |
52 | | const Signature *, const SigMatchCtx *); |
53 | | static int DetectFlowSetup (DetectEngineCtx *, Signature *, const char *); |
54 | | #ifdef UNITTESTS |
55 | | static void DetectFlowRegisterTests(void); |
56 | | #endif |
57 | | void DetectFlowFree(DetectEngineCtx *, void *); |
58 | | |
59 | | static int PrefilterSetupFlow(DetectEngineCtx *de_ctx, SigGroupHead *sgh); |
60 | | static bool PrefilterFlowIsPrefilterable(const Signature *s); |
61 | | |
62 | | /** |
63 | | * \brief Registration function for flow: keyword |
64 | | */ |
65 | | void DetectFlowRegister (void) |
66 | 73 | { |
67 | 73 | sigmatch_table[DETECT_FLOW].name = "flow"; |
68 | 73 | sigmatch_table[DETECT_FLOW].desc = "match on direction and state of the flow"; |
69 | 73 | sigmatch_table[DETECT_FLOW].url = "/rules/flow-keywords.html#flow"; |
70 | 73 | sigmatch_table[DETECT_FLOW].Match = DetectFlowMatch; |
71 | 73 | sigmatch_table[DETECT_FLOW].Setup = DetectFlowSetup; |
72 | 73 | sigmatch_table[DETECT_FLOW].Free = DetectFlowFree; |
73 | | #ifdef UNITTESTS |
74 | | sigmatch_table[DETECT_FLOW].RegisterTests = DetectFlowRegisterTests; |
75 | | #endif |
76 | 73 | sigmatch_table[DETECT_FLOW].SupportsPrefilter = PrefilterFlowIsPrefilterable; |
77 | 73 | sigmatch_table[DETECT_FLOW].SetupPrefilter = PrefilterSetupFlow; |
78 | | |
79 | 73 | DetectSetupParseRegexes(PARSE_REGEX, &parse_regex); |
80 | 73 | } |
81 | | |
82 | | /** |
83 | | * \param pflags packet flags (p->flags) |
84 | | * \param pflowflags packet flow flags (p->flowflags) |
85 | | * \param tflags detection flags (det_ctx->flags) |
86 | | * \param dflags detect flow flags |
87 | | * \param match_cnt number of matches to trigger |
88 | | */ |
89 | | static inline int FlowMatch(const uint32_t pflags, const uint8_t pflowflags, const uint16_t tflags, |
90 | | const uint16_t dflags, const uint16_t match_cnt) |
91 | 89.6k | { |
92 | 89.6k | uint8_t cnt = 0; |
93 | | |
94 | 89.6k | if ((dflags & DETECT_FLOW_FLAG_NO_FRAG) && |
95 | 0 | (!(pflags & PKT_REBUILT_FRAGMENT))) { |
96 | 0 | cnt++; |
97 | 89.6k | } else if ((dflags & DETECT_FLOW_FLAG_ONLY_FRAG) && |
98 | 0 | (pflags & PKT_REBUILT_FRAGMENT)) { |
99 | 0 | cnt++; |
100 | 0 | } |
101 | | |
102 | 89.6k | if ((dflags & DETECT_FLOW_FLAG_TOSERVER) && (pflowflags & FLOW_PKT_TOSERVER)) { |
103 | 16.1k | cnt++; |
104 | 73.5k | } else if ((dflags & DETECT_FLOW_FLAG_TOCLIENT) && (pflowflags & FLOW_PKT_TOCLIENT)) { |
105 | 887 | cnt++; |
106 | 887 | } |
107 | | |
108 | 89.6k | if ((dflags & DETECT_FLOW_FLAG_ESTABLISHED) && (pflowflags & FLOW_PKT_ESTABLISHED)) { |
109 | 68.6k | cnt++; |
110 | 68.6k | } else if (dflags & DETECT_FLOW_FLAG_NOT_ESTABLISHED && (!(pflowflags & FLOW_PKT_ESTABLISHED))) { |
111 | 438 | cnt++; |
112 | 20.5k | } else if (dflags & DETECT_FLOW_FLAG_STATELESS) { |
113 | 9.40k | cnt++; |
114 | 9.40k | } |
115 | | |
116 | 89.6k | if (tflags & DETECT_ENGINE_THREAD_CTX_STREAM_CONTENT_MATCH) { |
117 | 2.25k | if (dflags & DETECT_FLOW_FLAG_ONLYSTREAM) |
118 | 0 | cnt++; |
119 | 87.4k | } else { |
120 | 87.4k | if (dflags & DETECT_FLOW_FLAG_NOSTREAM) |
121 | 0 | cnt++; |
122 | 87.4k | } |
123 | | |
124 | 89.6k | return (match_cnt == cnt) ? 1 : 0; |
125 | 89.6k | } |
126 | | |
127 | | /** |
128 | | * \brief This function is used to match flow flags set on a packet with those passed via flow: |
129 | | * |
130 | | * \param t pointer to thread vars |
131 | | * \param det_ctx pointer to the pattern matcher thread |
132 | | * \param p pointer to the current packet |
133 | | * \param m pointer to the sigmatch that we will cast into DetectFlowData |
134 | | * |
135 | | * \retval 0 no match |
136 | | * \retval 1 match |
137 | | */ |
138 | | int DetectFlowMatch (DetectEngineThreadCtx *det_ctx, Packet *p, |
139 | | const Signature *s, const SigMatchCtx *ctx) |
140 | 170k | { |
141 | 170k | SCEnter(); |
142 | | |
143 | 170k | SCLogDebug("pkt %p", p); |
144 | | |
145 | 170k | if (p->flowflags & FLOW_PKT_TOSERVER) { |
146 | 107k | SCLogDebug("FLOW_PKT_TOSERVER"); |
147 | 107k | } else if (p->flowflags & FLOW_PKT_TOCLIENT) { |
148 | 63.6k | SCLogDebug("FLOW_PKT_TOCLIENT"); |
149 | 63.6k | } |
150 | | |
151 | 170k | if (p->flowflags & FLOW_PKT_ESTABLISHED) { |
152 | 150k | SCLogDebug("FLOW_PKT_ESTABLISHED"); |
153 | 150k | } |
154 | | |
155 | 170k | const DetectFlowData *fd = (const DetectFlowData *)ctx; |
156 | | |
157 | 170k | const int ret = FlowMatch(p->flags, p->flowflags, det_ctx->flags, fd->flags, fd->match_cnt); |
158 | 170k | SCLogDebug("returning %" PRId32 " fd->match_cnt %" PRId32 " fd->flags 0x%02X p->flowflags 0x%02X", |
159 | 170k | ret, fd->match_cnt, fd->flags, p->flowflags); |
160 | 170k | SCReturnInt(ret); |
161 | 170k | } |
162 | | |
163 | | /** |
164 | | * \brief This function is used to parse flow options passed via flow: keyword |
165 | | * |
166 | | * \param de_ctx Pointer to the detection engine context |
167 | | * \param flowstr Pointer to the user provided flow options |
168 | | * |
169 | | * \retval fd pointer to DetectFlowData on success |
170 | | * \retval NULL on failure |
171 | | */ |
172 | | static DetectFlowData *DetectFlowParse (DetectEngineCtx *de_ctx, const char *flowstr) |
173 | 360k | { |
174 | 360k | DetectFlowData *fd = NULL; |
175 | 360k | char *args[3] = {NULL,NULL,NULL}; |
176 | 360k | int res = 0; |
177 | 360k | size_t pcre2len; |
178 | 360k | char str1[16] = "", str2[16] = "", str3[16] = ""; |
179 | 360k | pcre2_match_data *match = NULL; |
180 | | |
181 | 360k | int ret = DetectParsePcreExec(&parse_regex, &match, flowstr, 0, 0); |
182 | 360k | if (ret < 1 || ret > 4) { |
183 | 16.3k | SCLogError("parse error, ret %" PRId32 ", string %s", ret, flowstr); |
184 | 16.3k | goto error; |
185 | 16.3k | } |
186 | | |
187 | 344k | if (ret > 1) { |
188 | 344k | pcre2len = sizeof(str1); |
189 | 344k | res = SC_Pcre2SubstringCopy(match, 1, (PCRE2_UCHAR8 *)str1, &pcre2len); |
190 | 344k | if (res < 0) { |
191 | 1.15k | SCLogError("pcre2_substring_copy_bynumber failed"); |
192 | 1.15k | goto error; |
193 | 1.15k | } |
194 | 342k | args[0] = (char *)str1; |
195 | | |
196 | 342k | if (ret > 2) { |
197 | 144k | pcre2len = sizeof(str2); |
198 | 144k | res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)str2, &pcre2len); |
199 | 144k | if (res < 0) { |
200 | 188 | SCLogError("pcre2_substring_copy_bynumber failed"); |
201 | 188 | goto error; |
202 | 188 | } |
203 | 144k | args[1] = (char *)str2; |
204 | 144k | } |
205 | 342k | if (ret > 3) { |
206 | 1.67k | pcre2len = sizeof(str3); |
207 | 1.67k | res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)str3, &pcre2len); |
208 | 1.67k | if (res < 0) { |
209 | 421 | SCLogError("pcre2_substring_copy_bynumber failed"); |
210 | 421 | goto error; |
211 | 421 | } |
212 | 1.25k | args[2] = (char *)str3; |
213 | 1.25k | } |
214 | 342k | } |
215 | | |
216 | 342k | fd = SCMalloc(sizeof(DetectFlowData)); |
217 | 342k | if (unlikely(fd == NULL)) |
218 | 0 | goto error; |
219 | 342k | fd->flags = 0; |
220 | 342k | fd->match_cnt = 0; |
221 | | |
222 | 342k | int i; |
223 | 817k | for (i = 0; i < (ret - 1); i++) { |
224 | 485k | if (args[i]) { |
225 | | /* inspect our options and set the flags */ |
226 | 485k | if (strcasecmp(args[i], "established") == 0) { |
227 | 252k | if (fd->flags & DETECT_FLOW_FLAG_ESTABLISHED) { |
228 | 46 | SCLogError("DETECT_FLOW_FLAG_ESTABLISHED flag is already set"); |
229 | 46 | goto error; |
230 | 252k | } else if (fd->flags & DETECT_FLOW_FLAG_NOT_ESTABLISHED) { |
231 | 0 | SCLogError("cannot set DETECT_FLOW_FLAG_ESTABLISHED, " |
232 | 0 | "DETECT_FLOW_FLAG_NOT_ESTABLISHED already set"); |
233 | 0 | goto error; |
234 | 252k | } else if (fd->flags & DETECT_FLOW_FLAG_STATELESS) { |
235 | 50 | SCLogError("DETECT_FLOW_FLAG_STATELESS already set"); |
236 | 50 | goto error; |
237 | 50 | } |
238 | 252k | fd->flags |= DETECT_FLOW_FLAG_ESTABLISHED; |
239 | 252k | } else if (strcasecmp(args[i], "not_established") == 0) { |
240 | 1.30k | if (fd->flags & DETECT_FLOW_FLAG_NOT_ESTABLISHED) { |
241 | 0 | SCLogError("DETECT_FLOW_FLAG_NOT_ESTABLISHED flag is already set"); |
242 | 0 | goto error; |
243 | 1.30k | } else if (fd->flags & DETECT_FLOW_FLAG_ESTABLISHED) { |
244 | 0 | SCLogError("cannot set DETECT_FLOW_FLAG_NOT_ESTABLISHED, " |
245 | 0 | "DETECT_FLOW_FLAG_ESTABLISHED already set"); |
246 | 0 | goto error; |
247 | 0 | } |
248 | 1.30k | fd->flags |= DETECT_FLOW_FLAG_NOT_ESTABLISHED; |
249 | 231k | } else if (strcasecmp(args[i], "stateless") == 0) { |
250 | 6.91k | if (fd->flags & DETECT_FLOW_FLAG_STATELESS) { |
251 | 155 | SCLogError("DETECT_FLOW_FLAG_STATELESS flag is already set"); |
252 | 155 | goto error; |
253 | 6.76k | } else if (fd->flags & DETECT_FLOW_FLAG_ESTABLISHED) { |
254 | 3 | SCLogError("cannot set DETECT_FLOW_FLAG_STATELESS, " |
255 | 3 | "DETECT_FLOW_FLAG_ESTABLISHED already set"); |
256 | 3 | goto error; |
257 | 3 | } |
258 | 6.75k | fd->flags |= DETECT_FLOW_FLAG_STATELESS; |
259 | 224k | } else if (strcasecmp(args[i], "to_client") == 0 || strcasecmp(args[i], "from_server") == 0) { |
260 | 54.5k | if (fd->flags & DETECT_FLOW_FLAG_TOCLIENT) { |
261 | 60 | SCLogError("cannot set DETECT_FLOW_FLAG_TOCLIENT flag is already set"); |
262 | 60 | goto error; |
263 | 54.5k | } else if (fd->flags & DETECT_FLOW_FLAG_TOSERVER) { |
264 | 75 | SCLogError("cannot set to_client, DETECT_FLOW_FLAG_TOSERVER already set"); |
265 | 75 | goto error; |
266 | 75 | } |
267 | 54.4k | fd->flags |= DETECT_FLOW_FLAG_TOCLIENT; |
268 | 169k | } else if (strcasecmp(args[i], "to_server") == 0 || strcasecmp(args[i], "from_client") == 0){ |
269 | 159k | if (fd->flags & DETECT_FLOW_FLAG_TOSERVER) { |
270 | 448 | SCLogError("cannot set DETECT_FLOW_FLAG_TOSERVER flag is already set"); |
271 | 448 | goto error; |
272 | 158k | } else if (fd->flags & DETECT_FLOW_FLAG_TOCLIENT) { |
273 | 21 | SCLogError("cannot set to_server, DETECT_FLOW_FLAG_TO_CLIENT flag already set"); |
274 | 21 | goto error; |
275 | 21 | } |
276 | 158k | fd->flags |= DETECT_FLOW_FLAG_TOSERVER; |
277 | 158k | } else if (strcasecmp(args[i], "only_stream") == 0) { |
278 | 219 | if (fd->flags & DETECT_FLOW_FLAG_ONLYSTREAM) { |
279 | 0 | SCLogError("cannot set only_stream flag is already set"); |
280 | 0 | goto error; |
281 | 219 | } else if (fd->flags & DETECT_FLOW_FLAG_NOSTREAM) { |
282 | 1 | SCLogError( |
283 | 1 | "cannot set only_stream flag, DETECT_FLOW_FLAG_NOSTREAM already set"); |
284 | 1 | goto error; |
285 | 1 | } |
286 | 218 | fd->flags |= DETECT_FLOW_FLAG_ONLYSTREAM; |
287 | 10.5k | } else if (strcasecmp(args[i], "no_stream") == 0) { |
288 | 521 | if (fd->flags & DETECT_FLOW_FLAG_NOSTREAM) { |
289 | 2 | SCLogError("cannot set no_stream flag is already set"); |
290 | 2 | goto error; |
291 | 519 | } else if (fd->flags & DETECT_FLOW_FLAG_ONLYSTREAM) { |
292 | 0 | SCLogError( |
293 | 0 | "cannot set no_stream flag, DETECT_FLOW_FLAG_ONLYSTREAM already set"); |
294 | 0 | goto error; |
295 | 0 | } |
296 | 519 | fd->flags |= DETECT_FLOW_FLAG_NOSTREAM; |
297 | 10.0k | } else if (strcasecmp(args[i], "no_frag") == 0) { |
298 | 463 | if (fd->flags & DETECT_FLOW_FLAG_NO_FRAG) { |
299 | 0 | SCLogError("cannot set no_frag flag is already set"); |
300 | 0 | goto error; |
301 | 463 | } else if (fd->flags & DETECT_FLOW_FLAG_ONLY_FRAG) { |
302 | 0 | SCLogError("cannot set no_frag flag, only_frag already set"); |
303 | 0 | goto error; |
304 | 0 | } |
305 | 463 | fd->flags |= DETECT_FLOW_FLAG_NO_FRAG; |
306 | 9.58k | } else if (strcasecmp(args[i], "only_frag") == 0) { |
307 | 426 | if (fd->flags & DETECT_FLOW_FLAG_ONLY_FRAG) { |
308 | 0 | SCLogError("cannot set only_frag flag is already set"); |
309 | 0 | goto error; |
310 | 426 | } else if (fd->flags & DETECT_FLOW_FLAG_NO_FRAG) { |
311 | 0 | SCLogError("cannot set only_frag flag, no_frag already set"); |
312 | 0 | goto error; |
313 | 0 | } |
314 | 426 | fd->flags |= DETECT_FLOW_FLAG_ONLY_FRAG; |
315 | 9.16k | } else { |
316 | 9.16k | SCLogError("invalid flow option \"%s\"", args[i]); |
317 | 9.16k | goto error; |
318 | 9.16k | } |
319 | | |
320 | 475k | fd->match_cnt++; |
321 | | //printf("args[%" PRId32 "]: %s match_cnt: %" PRId32 " flags: 0x%02X\n", i, args[i], fd->match_cnt, fd->flags); |
322 | 475k | } |
323 | 485k | } |
324 | 342k | pcre2_match_data_free(match); |
325 | 332k | return fd; |
326 | | |
327 | 28.1k | error: |
328 | 28.1k | if (match) { |
329 | 28.1k | pcre2_match_data_free(match); |
330 | 28.1k | } |
331 | 28.1k | if (fd != NULL) |
332 | 10.0k | DetectFlowFree(de_ctx, fd); |
333 | 28.1k | return NULL; |
334 | | |
335 | 342k | } |
336 | | |
337 | | int DetectFlowSetupImplicit(Signature *s, uint32_t flags) |
338 | 418k | { |
339 | 836k | #define SIG_FLAG_BOTH (SIG_FLAG_TOSERVER|SIG_FLAG_TOCLIENT) |
340 | 418k | BUG_ON(flags == 0); |
341 | 418k | BUG_ON(flags & ~SIG_FLAG_BOTH); |
342 | 418k | BUG_ON((flags & SIG_FLAG_BOTH) == SIG_FLAG_BOTH); |
343 | | |
344 | 418k | SCLogDebug("want %08lx", flags & SIG_FLAG_BOTH); |
345 | 418k | SCLogDebug("have %08lx", s->flags & SIG_FLAG_BOTH); |
346 | | |
347 | 418k | if (flags & SIG_FLAG_TOSERVER) { |
348 | 350k | if ((s->flags & SIG_FLAG_BOTH) == SIG_FLAG_BOTH) { |
349 | | /* both is set if we just have 'flow:established' */ |
350 | 325k | s->flags &= ~SIG_FLAG_TOCLIENT; |
351 | 325k | } else if (s->flags & SIG_FLAG_TOCLIENT) { |
352 | 866 | return -1; |
353 | 866 | } |
354 | 350k | s->flags |= SIG_FLAG_TOSERVER; |
355 | 350k | } else { |
356 | 67.0k | if ((s->flags & SIG_FLAG_BOTH) == SIG_FLAG_BOTH) { |
357 | | /* both is set if we just have 'flow:established' */ |
358 | 56.8k | s->flags &= ~SIG_FLAG_TOSERVER; |
359 | 56.8k | } else if (s->flags & SIG_FLAG_TOSERVER) { |
360 | 3.09k | return -1; |
361 | 3.09k | } |
362 | 63.9k | s->flags |= SIG_FLAG_TOCLIENT; |
363 | 63.9k | } |
364 | 414k | return 0; |
365 | 418k | #undef SIG_FLAG_BOTH |
366 | 418k | } |
367 | | |
368 | | /** |
369 | | * \brief this function is used to add the parsed flowdata into the current signature |
370 | | * |
371 | | * \param de_ctx pointer to the Detection Engine Context |
372 | | * \param s pointer to the Current Signature |
373 | | * \param flowstr pointer to the user provided flow options |
374 | | * |
375 | | * \retval 0 on Success |
376 | | * \retval -1 on Failure |
377 | | */ |
378 | | int DetectFlowSetup (DetectEngineCtx *de_ctx, Signature *s, const char *flowstr) |
379 | 248k | { |
380 | | /* ensure only one flow option */ |
381 | 248k | if (s->init_data->init_flags & SIG_FLAG_INIT_FLOW) { |
382 | 976 | SCLogError("A signature may have only one flow option."); |
383 | 976 | return -1; |
384 | 976 | } |
385 | | |
386 | 247k | DetectFlowData *fd = DetectFlowParse(de_ctx, flowstr); |
387 | 247k | if (fd == NULL) |
388 | 19.7k | return -1; |
389 | | |
390 | 228k | SigMatch *sm = SigMatchAlloc(); |
391 | 228k | if (sm == NULL) |
392 | 0 | goto error; |
393 | | |
394 | 228k | sm->type = DETECT_FLOW; |
395 | 228k | sm->ctx = (SigMatchCtx *)fd; |
396 | | |
397 | | /* set the signature direction flags */ |
398 | 228k | if (fd->flags & DETECT_FLOW_FLAG_TOSERVER) { |
399 | 105k | s->flags |= SIG_FLAG_TOSERVER; |
400 | 122k | } else if (fd->flags & DETECT_FLOW_FLAG_TOCLIENT) { |
401 | 35.8k | s->flags |= SIG_FLAG_TOCLIENT; |
402 | 86.8k | } else { |
403 | 86.8k | s->flags |= SIG_FLAG_TOSERVER; |
404 | 86.8k | s->flags |= SIG_FLAG_TOCLIENT; |
405 | 86.8k | } |
406 | 228k | if (fd->flags & DETECT_FLOW_FLAG_ONLYSTREAM) { |
407 | 202 | s->flags |= SIG_FLAG_REQUIRE_STREAM; |
408 | 202 | } |
409 | 228k | if (fd->flags & DETECT_FLOW_FLAG_NOSTREAM) { |
410 | 487 | s->flags |= SIG_FLAG_REQUIRE_PACKET; |
411 | 227k | } else if (fd->flags == DETECT_FLOW_FLAG_TOSERVER || |
412 | 207k | fd->flags == DETECT_FLOW_FLAG_TOCLIENT) |
413 | 45.2k | { |
414 | | /* no direct flow is needed for just direction, |
415 | | * no sigmatch is needed either. */ |
416 | 45.2k | SigMatchFree(de_ctx, sm); |
417 | 45.2k | sm = NULL; |
418 | 182k | } else { |
419 | 182k | s->init_data->init_flags |= SIG_FLAG_INIT_FLOW; |
420 | 182k | } |
421 | | |
422 | 228k | if (sm != NULL) { |
423 | 182k | SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH); |
424 | 182k | } |
425 | 228k | return 0; |
426 | | |
427 | 0 | error: |
428 | 0 | if (fd != NULL) |
429 | 0 | DetectFlowFree(de_ctx, fd); |
430 | 0 | return -1; |
431 | | |
432 | 228k | } |
433 | | |
434 | | /** |
435 | | * \brief this function will free memory associated with DetectFlowData |
436 | | * |
437 | | * \param fd pointer to DetectFlowData |
438 | | */ |
439 | | void DetectFlowFree(DetectEngineCtx *de_ctx, void *ptr) |
440 | 342k | { |
441 | 342k | DetectFlowData *fd = (DetectFlowData *)ptr; |
442 | 342k | SCFree(fd); |
443 | 342k | } |
444 | | |
445 | | static void |
446 | | PrefilterPacketFlowMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx) |
447 | 10.2k | { |
448 | 10.2k | const PrefilterPacketHeaderCtx *ctx = pectx; |
449 | | |
450 | 10.2k | if (!PrefilterPacketHeaderExtraMatch(ctx, p)) |
451 | 1.34k | return; |
452 | | |
453 | 8.88k | if (FlowMatch(p->flags, p->flowflags, det_ctx->flags, ctx->v1.u16[0], ctx->v1.u16[1])) { |
454 | 8.88k | PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt); |
455 | 8.88k | } |
456 | 8.88k | } |
457 | | |
458 | | static void |
459 | | PrefilterPacketFlowSet(PrefilterPacketHeaderValue *v, void *smctx) |
460 | 8.52k | { |
461 | 8.52k | const DetectFlowData *fb = smctx; |
462 | 8.52k | v->u16[0] = fb->flags; |
463 | 8.52k | v->u16[1] = fb->match_cnt; |
464 | 8.52k | } |
465 | | |
466 | | static bool |
467 | | PrefilterPacketFlowCompare(PrefilterPacketHeaderValue v, void *smctx) |
468 | 3.05k | { |
469 | 3.05k | const DetectFlowData *fb = smctx; |
470 | 3.05k | if (v.u16[0] == fb->flags && v.u16[1] == fb->match_cnt) { |
471 | 3.05k | return true; |
472 | 3.05k | } |
473 | 0 | return false; |
474 | 3.05k | } |
475 | | |
476 | | static int PrefilterSetupFlow(DetectEngineCtx *de_ctx, SigGroupHead *sgh) |
477 | 6.06k | { |
478 | 6.06k | return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_FLOW, |
479 | 6.06k | PrefilterPacketFlowSet, |
480 | 6.06k | PrefilterPacketFlowCompare, |
481 | 6.06k | PrefilterPacketFlowMatch); |
482 | 6.06k | } |
483 | | |
484 | | static bool PrefilterFlowIsPrefilterable(const Signature *s) |
485 | 0 | { |
486 | 0 | const SigMatch *sm; |
487 | 0 | for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) { |
488 | 0 | switch (sm->type) { |
489 | 0 | case DETECT_FLOW: |
490 | 0 | return true; |
491 | 0 | } |
492 | 0 | } |
493 | 0 | return false; |
494 | 0 | } |
495 | | |
496 | | #ifdef UNITTESTS |
497 | | #include "detect-engine-alert.h" |
498 | | |
499 | | /** |
500 | | * \test DetectFlowTestParse01 is a test to make sure that we return "something" |
501 | | * when given valid flow opt |
502 | | */ |
503 | | static int DetectFlowTestParse01 (void) |
504 | | { |
505 | | DetectFlowData *fd = NULL; |
506 | | fd = DetectFlowParse(NULL, "established"); |
507 | | FAIL_IF_NULL(fd); |
508 | | DetectFlowFree(NULL, fd); |
509 | | PASS; |
510 | | } |
511 | | |
512 | | /** |
513 | | * \test DetectFlowTestParse02 is a test for setting the established flow opt |
514 | | */ |
515 | | static int DetectFlowTestParse02 (void) |
516 | | { |
517 | | DetectFlowData *fd = NULL; |
518 | | fd = DetectFlowParse(NULL, "established"); |
519 | | FAIL_IF_NULL(fd); |
520 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_ESTABLISHED && |
521 | | fd->match_cnt == 1); |
522 | | PASS; |
523 | | } |
524 | | |
525 | | /** |
526 | | * \test DetectFlowTestParse03 is a test for setting the stateless flow opt |
527 | | */ |
528 | | static int DetectFlowTestParse03 (void) |
529 | | { |
530 | | DetectFlowData *fd = NULL; |
531 | | fd = DetectFlowParse(NULL, "stateless"); |
532 | | FAIL_IF_NULL(fd); |
533 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_STATELESS && fd->match_cnt == 1); |
534 | | DetectFlowFree(NULL, fd); |
535 | | PASS; |
536 | | } |
537 | | |
538 | | /** |
539 | | * \test DetectFlowTestParse04 is a test for setting the to_client flow opt |
540 | | */ |
541 | | static int DetectFlowTestParse04 (void) |
542 | | { |
543 | | DetectFlowData *fd = NULL; |
544 | | fd = DetectFlowParse(NULL, "to_client"); |
545 | | FAIL_IF_NULL(fd); |
546 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_TOCLIENT && fd->match_cnt == 1); |
547 | | DetectFlowFree(NULL, fd); |
548 | | PASS; |
549 | | } |
550 | | |
551 | | /** |
552 | | * \test DetectFlowTestParse05 is a test for setting the to_server flow opt |
553 | | */ |
554 | | static int DetectFlowTestParse05 (void) |
555 | | { |
556 | | DetectFlowData *fd = NULL; |
557 | | fd = DetectFlowParse(NULL, "to_server"); |
558 | | FAIL_IF_NULL(fd); |
559 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_TOSERVER && fd->match_cnt == 1); |
560 | | DetectFlowFree(NULL, fd); |
561 | | PASS; |
562 | | } |
563 | | |
564 | | /** |
565 | | * \test DetectFlowTestParse06 is a test for setting the from_server flow opt |
566 | | */ |
567 | | static int DetectFlowTestParse06 (void) |
568 | | { |
569 | | DetectFlowData *fd = NULL; |
570 | | fd = DetectFlowParse(NULL, "from_server"); |
571 | | FAIL_IF_NULL(fd); |
572 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_TOCLIENT && fd->match_cnt == 1); |
573 | | DetectFlowFree(NULL, fd); |
574 | | PASS; |
575 | | } |
576 | | |
577 | | /** |
578 | | * \test DetectFlowTestParse07 is a test for setting the from_client flow opt |
579 | | */ |
580 | | static int DetectFlowTestParse07 (void) |
581 | | { |
582 | | DetectFlowData *fd = NULL; |
583 | | fd = DetectFlowParse(NULL, "from_client"); |
584 | | FAIL_IF_NULL(fd); |
585 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_TOSERVER && fd->match_cnt == 1); |
586 | | DetectFlowFree(NULL, fd); |
587 | | PASS; |
588 | | } |
589 | | |
590 | | /** |
591 | | * \test DetectFlowTestParse08 is a test for setting the established,to_client flow opts |
592 | | */ |
593 | | static int DetectFlowTestParse08 (void) |
594 | | { |
595 | | DetectFlowData *fd = NULL; |
596 | | fd = DetectFlowParse(NULL, "established,to_client"); |
597 | | FAIL_IF_NULL(fd); |
598 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_ESTABLISHED && fd->flags & DETECT_FLOW_FLAG_TOCLIENT && fd->match_cnt == 2); |
599 | | DetectFlowFree(NULL, fd); |
600 | | PASS; |
601 | | } |
602 | | |
603 | | /** |
604 | | * \test DetectFlowTestParse09 is a test for setting the to_client,stateless flow opts (order of state,dir reversed) |
605 | | */ |
606 | | static int DetectFlowTestParse09 (void) |
607 | | { |
608 | | DetectFlowData *fd = NULL; |
609 | | fd = DetectFlowParse(NULL, "to_client,stateless"); |
610 | | FAIL_IF_NULL(fd); |
611 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_STATELESS && |
612 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
613 | | fd->match_cnt == 2); |
614 | | DetectFlowFree(NULL, fd); |
615 | | PASS; |
616 | | } |
617 | | |
618 | | /** |
619 | | * \test DetectFlowTestParse10 is a test for setting the from_server,stateless flow opts (order of state,dir reversed) |
620 | | */ |
621 | | static int DetectFlowTestParse10 (void) |
622 | | { |
623 | | DetectFlowData *fd = NULL; |
624 | | fd = DetectFlowParse(NULL, "from_server,stateless"); |
625 | | FAIL_IF_NULL(fd); |
626 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_STATELESS && |
627 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
628 | | fd->match_cnt == 2); |
629 | | DetectFlowFree(NULL, fd); |
630 | | PASS; |
631 | | } |
632 | | |
633 | | /** |
634 | | * \test DetectFlowTestParse11 is a test for setting the from_server,stateless flow opts with spaces all around |
635 | | */ |
636 | | static int DetectFlowTestParse11 (void) |
637 | | { |
638 | | DetectFlowData *fd = NULL; |
639 | | fd = DetectFlowParse(NULL, " from_server , stateless "); |
640 | | FAIL_IF_NULL(fd); |
641 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_STATELESS && |
642 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
643 | | fd->match_cnt == 2); |
644 | | DetectFlowFree(NULL, fd); |
645 | | PASS; |
646 | | } |
647 | | |
648 | | /** |
649 | | * \test DetectFlowTestParseNocase01 is a test to make sure that we return "something" |
650 | | * when given valid flow opt |
651 | | */ |
652 | | static int DetectFlowTestParseNocase01 (void) |
653 | | { |
654 | | DetectFlowData *fd = NULL; |
655 | | fd = DetectFlowParse(NULL, "ESTABLISHED"); |
656 | | FAIL_IF_NULL(fd); |
657 | | DetectFlowFree(NULL, fd); |
658 | | PASS; |
659 | | } |
660 | | |
661 | | /** |
662 | | * \test DetectFlowTestParseNocase02 is a test for setting the established flow opt |
663 | | */ |
664 | | static int DetectFlowTestParseNocase02 (void) |
665 | | { |
666 | | DetectFlowData *fd = NULL; |
667 | | fd = DetectFlowParse(NULL, "ESTABLISHED"); |
668 | | FAIL_IF_NULL(fd); |
669 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_ESTABLISHED && |
670 | | fd->match_cnt == 1); |
671 | | DetectFlowFree(NULL, fd); |
672 | | PASS; |
673 | | } |
674 | | |
675 | | /** |
676 | | * \test DetectFlowTestParseNocase03 is a test for setting the stateless flow opt |
677 | | */ |
678 | | static int DetectFlowTestParseNocase03 (void) |
679 | | { |
680 | | DetectFlowData *fd = NULL; |
681 | | fd = DetectFlowParse(NULL, "STATELESS"); |
682 | | FAIL_IF_NULL(fd); |
683 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_STATELESS && fd->match_cnt == 1); DetectFlowFree(NULL, fd); |
684 | | PASS; |
685 | | } |
686 | | |
687 | | /** |
688 | | * \test DetectFlowTestParseNocase04 is a test for setting the to_client flow opt |
689 | | */ |
690 | | static int DetectFlowTestParseNocase04 (void) |
691 | | { |
692 | | DetectFlowData *fd = NULL; |
693 | | fd = DetectFlowParse(NULL, "TO_CLIENT"); |
694 | | FAIL_IF_NULL(fd); |
695 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_TOCLIENT && fd->match_cnt == 1); |
696 | | DetectFlowFree(NULL, fd); |
697 | | PASS; |
698 | | } |
699 | | |
700 | | /** |
701 | | * \test DetectFlowTestParseNocase05 is a test for setting the to_server flow opt |
702 | | */ |
703 | | static int DetectFlowTestParseNocase05 (void) |
704 | | { |
705 | | DetectFlowData *fd = NULL; |
706 | | fd = DetectFlowParse(NULL, "TO_SERVER"); |
707 | | FAIL_IF_NULL(fd); |
708 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_TOSERVER && fd->match_cnt == 1); |
709 | | DetectFlowFree(NULL, fd); |
710 | | PASS; |
711 | | } |
712 | | |
713 | | /** |
714 | | * \test DetectFlowTestParseNocase06 is a test for setting the from_server flow opt |
715 | | */ |
716 | | static int DetectFlowTestParseNocase06 (void) |
717 | | { |
718 | | DetectFlowData *fd = NULL; |
719 | | fd = DetectFlowParse(NULL, "FROM_SERVER"); |
720 | | FAIL_IF_NULL(fd); |
721 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_TOCLIENT && fd->match_cnt == 1); |
722 | | DetectFlowFree(NULL, fd); |
723 | | PASS; |
724 | | } |
725 | | |
726 | | /** |
727 | | * \test DetectFlowTestParseNocase07 is a test for setting the from_client flow opt |
728 | | */ |
729 | | static int DetectFlowTestParseNocase07 (void) |
730 | | { |
731 | | DetectFlowData *fd = NULL; |
732 | | fd = DetectFlowParse(NULL, "FROM_CLIENT"); |
733 | | FAIL_IF_NULL(fd); |
734 | | FAIL_IF_NOT(fd->flags == DETECT_FLOW_FLAG_TOSERVER && fd->match_cnt == 1); |
735 | | DetectFlowFree(NULL, fd); |
736 | | PASS; |
737 | | } |
738 | | |
739 | | /** |
740 | | * \test DetectFlowTestParseNocase08 is a test for setting the established,to_client flow opts |
741 | | */ |
742 | | static int DetectFlowTestParseNocase08 (void) |
743 | | { |
744 | | DetectFlowData *fd = NULL; |
745 | | fd = DetectFlowParse(NULL, "ESTABLISHED,TO_CLIENT"); |
746 | | FAIL_IF_NULL(fd); |
747 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_ESTABLISHED && |
748 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
749 | | fd->match_cnt == 2); |
750 | | DetectFlowFree(NULL, fd); |
751 | | PASS; |
752 | | } |
753 | | |
754 | | /** |
755 | | * \test DetectFlowTestParseNocase09 is a test for setting the to_client,stateless flow opts (order of state,dir reversed) |
756 | | */ |
757 | | static int DetectFlowTestParseNocase09 (void) |
758 | | { |
759 | | DetectFlowData *fd = NULL; |
760 | | fd = DetectFlowParse(NULL, "TO_CLIENT,STATELESS"); |
761 | | FAIL_IF_NULL(fd); |
762 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_STATELESS && |
763 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
764 | | fd->match_cnt == 2); |
765 | | DetectFlowFree(NULL, fd); |
766 | | PASS; |
767 | | } |
768 | | |
769 | | /** |
770 | | * \test DetectFlowTestParseNocase10 is a test for setting the from_server,stateless flow opts (order of state,dir reversed) |
771 | | */ |
772 | | static int DetectFlowTestParseNocase10 (void) |
773 | | { |
774 | | DetectFlowData *fd = NULL; |
775 | | fd = DetectFlowParse(NULL, "FROM_SERVER,STATELESS"); |
776 | | FAIL_IF_NULL(fd); |
777 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_STATELESS && |
778 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
779 | | fd->match_cnt == 2); |
780 | | DetectFlowFree(NULL, fd); |
781 | | PASS; |
782 | | } |
783 | | |
784 | | /** |
785 | | * \test DetectFlowTestParseNocase11 is a test for setting the from_server,stateless flow opts with spaces all around |
786 | | */ |
787 | | static int DetectFlowTestParseNocase11 (void) |
788 | | { |
789 | | DetectFlowData *fd = NULL; |
790 | | fd = DetectFlowParse(NULL, " FROM_SERVER , STATELESS "); |
791 | | FAIL_IF_NULL(fd); |
792 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_STATELESS && |
793 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
794 | | fd->match_cnt == 2); |
795 | | DetectFlowFree(NULL, fd); |
796 | | PASS; |
797 | | } |
798 | | |
799 | | /** |
800 | | * \test DetectFlowTestParse12 is a test for setting an invalid separator : |
801 | | */ |
802 | | static int DetectFlowTestParse12 (void) |
803 | | { |
804 | | DetectFlowData *fd = NULL; |
805 | | fd = DetectFlowParse(NULL, "from_server:stateless"); |
806 | | FAIL_IF_NOT_NULL(fd); |
807 | | PASS; |
808 | | } |
809 | | |
810 | | /** |
811 | | * \test DetectFlowTestParse13 is a test for an invalid option |
812 | | */ |
813 | | static int DetectFlowTestParse13 (void) |
814 | | { |
815 | | DetectFlowData *fd = NULL; |
816 | | fd = DetectFlowParse(NULL, "invalidoptiontest"); |
817 | | FAIL_IF_NOT_NULL(fd); |
818 | | PASS; |
819 | | } |
820 | | |
821 | | /** |
822 | | * \test DetectFlowTestParse14 is a test for a empty option |
823 | | */ |
824 | | static int DetectFlowTestParse14 (void) |
825 | | { |
826 | | DetectFlowData *fd = NULL; |
827 | | fd = DetectFlowParse(NULL, ""); |
828 | | FAIL_IF_NOT_NULL(fd); |
829 | | PASS; |
830 | | } |
831 | | |
832 | | /** |
833 | | * \test DetectFlowTestParse15 is a test for an invalid combo of options established,stateless |
834 | | */ |
835 | | static int DetectFlowTestParse15 (void) |
836 | | { |
837 | | DetectFlowData *fd = NULL; |
838 | | fd = DetectFlowParse(NULL, "established,stateless"); |
839 | | FAIL_IF_NOT_NULL(fd); |
840 | | PASS; |
841 | | } |
842 | | |
843 | | /** |
844 | | * \test DetectFlowTestParse16 is a test for an invalid combo of options to_client,to_server |
845 | | */ |
846 | | static int DetectFlowTestParse16 (void) |
847 | | { |
848 | | DetectFlowData *fd = NULL; |
849 | | fd = DetectFlowParse(NULL, "to_client,to_server"); |
850 | | FAIL_IF_NOT_NULL(fd); |
851 | | PASS; |
852 | | } |
853 | | |
854 | | /** |
855 | | * \test DetectFlowTestParse16 is a test for an invalid combo of options to_client,from_server |
856 | | * flowbit flags are the same |
857 | | */ |
858 | | static int DetectFlowTestParse17 (void) |
859 | | { |
860 | | DetectFlowData *fd = NULL; |
861 | | fd = DetectFlowParse(NULL, "to_client,from_server"); |
862 | | FAIL_IF_NOT_NULL(fd); |
863 | | PASS; |
864 | | } |
865 | | |
866 | | /** |
867 | | * \test DetectFlowTestParse18 is a test for setting the from_server,stateless,only_stream flow opts (order of state,dir reversed) |
868 | | */ |
869 | | static int DetectFlowTestParse18 (void) |
870 | | { |
871 | | DetectFlowData *fd = NULL; |
872 | | fd = DetectFlowParse(NULL, "from_server,established,only_stream"); |
873 | | FAIL_IF_NULL(fd); |
874 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_ESTABLISHED && |
875 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
876 | | fd->flags & DETECT_FLOW_FLAG_ONLYSTREAM && |
877 | | fd->match_cnt == 3); |
878 | | DetectFlowFree(NULL, fd); |
879 | | PASS; |
880 | | } |
881 | | |
882 | | /** |
883 | | * \test DetectFlowTestParseNocase18 is a test for setting the from_server,stateless,only_stream flow opts (order of state,dir reversed) |
884 | | */ |
885 | | static int DetectFlowTestParseNocase18 (void) |
886 | | { |
887 | | DetectFlowData *fd = NULL; |
888 | | fd = DetectFlowParse(NULL, "FROM_SERVER,ESTABLISHED,ONLY_STREAM"); |
889 | | FAIL_IF_NULL(fd); |
890 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_ESTABLISHED && |
891 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
892 | | fd->flags & DETECT_FLOW_FLAG_ONLYSTREAM && |
893 | | fd->match_cnt == 3); |
894 | | DetectFlowFree(NULL, fd); |
895 | | PASS; |
896 | | } |
897 | | |
898 | | |
899 | | /** |
900 | | * \test DetectFlowTestParse19 is a test for one to many options passed to DetectFlowParse |
901 | | */ |
902 | | static int DetectFlowTestParse19 (void) |
903 | | { |
904 | | DetectFlowData *fd = NULL; |
905 | | fd = DetectFlowParse(NULL, "from_server,established,only_stream,a"); |
906 | | FAIL_IF_NOT_NULL(fd); |
907 | | PASS; |
908 | | } |
909 | | |
910 | | /** |
911 | | * \test DetectFlowTestParse20 is a test for setting from_server, established, no_stream |
912 | | */ |
913 | | static int DetectFlowTestParse20 (void) |
914 | | { |
915 | | DetectFlowData *fd = NULL; |
916 | | fd = DetectFlowParse(NULL, "from_server,established,no_stream"); |
917 | | FAIL_IF_NULL(fd); |
918 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_ESTABLISHED && |
919 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
920 | | fd->flags & DETECT_FLOW_FLAG_NOSTREAM && |
921 | | fd->match_cnt == 3); |
922 | | DetectFlowFree(NULL, fd); |
923 | | PASS; |
924 | | } |
925 | | |
926 | | /** |
927 | | * \test DetectFlowTestParse20 is a test for setting from_server, established, no_stream |
928 | | */ |
929 | | static int DetectFlowTestParseNocase20 (void) |
930 | | { |
931 | | DetectFlowData *fd = NULL; |
932 | | fd = DetectFlowParse(NULL, "FROM_SERVER,ESTABLISHED,NO_STREAM"); |
933 | | FAIL_IF_NULL(fd); |
934 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_ESTABLISHED && |
935 | | fd->flags & DETECT_FLOW_FLAG_TOCLIENT && |
936 | | fd->flags & DETECT_FLOW_FLAG_NOSTREAM && |
937 | | fd->match_cnt == 3); |
938 | | DetectFlowFree(NULL, fd); |
939 | | PASS; |
940 | | } |
941 | | |
942 | | /** |
943 | | * \test DetectFlowTestParse21 is a test for an invalid opt between to valid opts |
944 | | */ |
945 | | static int DetectFlowTestParse21 (void) |
946 | | { |
947 | | DetectFlowData *fd = NULL; |
948 | | fd = DetectFlowParse(NULL, "from_server,a,no_stream"); |
949 | | FAIL_IF_NOT_NULL(fd); |
950 | | PASS; |
951 | | } |
952 | | |
953 | | /** |
954 | | * \test DetectFlowTestParse22 is a test for setting the established,not_established flow opts both |
955 | | */ |
956 | | static int DetectFlowTestParse22(void) |
957 | | { |
958 | | DetectFlowData *fd = NULL; |
959 | | fd = DetectFlowParse(NULL, "established,not_established"); |
960 | | FAIL_IF_NOT_NULL(fd); |
961 | | fd = DetectFlowParse(NULL, "not_established,established"); |
962 | | FAIL_IF_NOT_NULL(fd); |
963 | | PASS; |
964 | | } |
965 | | |
966 | | static int DetectFlowSigTest01(void) |
967 | | { |
968 | | uint8_t *buf = (uint8_t *)"supernovaduper"; |
969 | | uint16_t buflen = strlen((char *)buf); |
970 | | ThreadVars th_v; |
971 | | DecodeThreadVars dtv; |
972 | | memset(&dtv, 0, sizeof(DecodeThreadVars)); |
973 | | memset(&th_v, 0, sizeof(th_v)); |
974 | | |
975 | | Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP); |
976 | | FAIL_IF_NULL(p); |
977 | | |
978 | | const char *sig1 = "alert tcp any any -> any any (msg:\"dummy\"; " |
979 | | "content:\"nova\"; flow:no_stream; sid:1;)"; |
980 | | |
981 | | DetectEngineCtx *de_ctx = DetectEngineCtxInit(); |
982 | | FAIL_IF_NULL(de_ctx); |
983 | | de_ctx->flags |= DE_QUIET; |
984 | | |
985 | | de_ctx->sig_list = SigInit(de_ctx, sig1); |
986 | | FAIL_IF_NULL(de_ctx->sig_list); |
987 | | |
988 | | SigGroupBuild(de_ctx); |
989 | | DetectEngineThreadCtx *det_ctx = NULL; |
990 | | DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx); |
991 | | FAIL_IF_NULL(det_ctx); |
992 | | |
993 | | SigMatchSignatures(&th_v, de_ctx, det_ctx, p); |
994 | | FAIL_IF(PacketAlertCheck(p, 1) != 1); |
995 | | |
996 | | DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); |
997 | | DetectEngineCtxFree(de_ctx); |
998 | | UTHFreePacket(p); |
999 | | |
1000 | | PASS; |
1001 | | } |
1002 | | |
1003 | | /** |
1004 | | * \test Test parsing of the not_established keyword. |
1005 | | */ |
1006 | | static int DetectFlowTestParseNotEstablished(void) |
1007 | | { |
1008 | | DetectFlowData *fd = NULL; |
1009 | | fd = DetectFlowParse(NULL, "not_established"); |
1010 | | FAIL_IF_NULL(fd); |
1011 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_NOT_ESTABLISHED); |
1012 | | DetectFlowFree(NULL, fd); |
1013 | | PASS; |
1014 | | } |
1015 | | |
1016 | | /** |
1017 | | * \test Test parsing of the "no_frag" flow argument. |
1018 | | */ |
1019 | | static int DetectFlowTestParseNoFrag(void) |
1020 | | { |
1021 | | DetectFlowData *fd = NULL; |
1022 | | fd = DetectFlowParse(NULL, "no_frag"); |
1023 | | FAIL_IF_NULL(fd); |
1024 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_NO_FRAG); |
1025 | | DetectFlowFree(NULL, fd); |
1026 | | PASS; |
1027 | | } |
1028 | | |
1029 | | /** |
1030 | | * \test Test parsing of the "only_frag" flow argument. |
1031 | | */ |
1032 | | static int DetectFlowTestParseOnlyFrag(void) |
1033 | | { |
1034 | | DetectFlowData *fd = NULL; |
1035 | | fd = DetectFlowParse(NULL, "only_frag"); |
1036 | | FAIL_IF_NULL(fd); |
1037 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_ONLY_FRAG); |
1038 | | DetectFlowFree(NULL, fd); |
1039 | | PASS; |
1040 | | } |
1041 | | |
1042 | | /** |
1043 | | * \test Test that parsing of only_frag and no_frag together fails. |
1044 | | */ |
1045 | | static int DetectFlowTestParseNoFragOnlyFrag(void) |
1046 | | { |
1047 | | DetectFlowData *fd = NULL; |
1048 | | fd = DetectFlowParse(NULL, "no_frag,only_frag"); |
1049 | | FAIL_IF_NOT_NULL(fd); |
1050 | | PASS; |
1051 | | } |
1052 | | |
1053 | | /** |
1054 | | * \test Test no_frag matching. |
1055 | | */ |
1056 | | static int DetectFlowTestNoFragMatch(void) |
1057 | | { |
1058 | | uint32_t pflags = 0; |
1059 | | DetectFlowData *fd = DetectFlowParse(NULL, "no_frag"); |
1060 | | FAIL_IF_NULL(fd); |
1061 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_NO_FRAG); |
1062 | | FAIL_IF_NOT(fd->match_cnt == 1); |
1063 | | FAIL_IF_NOT(FlowMatch(pflags, 0, 0, fd->flags, fd->match_cnt)); |
1064 | | pflags |= PKT_REBUILT_FRAGMENT; |
1065 | | FAIL_IF(FlowMatch(pflags, 0, 0, fd->flags, fd->match_cnt)); |
1066 | | PASS; |
1067 | | } |
1068 | | |
1069 | | /** |
1070 | | * \test Test only_frag matching. |
1071 | | */ |
1072 | | static int DetectFlowTestOnlyFragMatch(void) |
1073 | | { |
1074 | | uint32_t pflags = 0; |
1075 | | DetectFlowData *fd = DetectFlowParse(NULL, "only_frag"); |
1076 | | FAIL_IF_NULL(fd); |
1077 | | FAIL_IF_NOT(fd->flags & DETECT_FLOW_FLAG_ONLY_FRAG); |
1078 | | FAIL_IF_NOT(fd->match_cnt == 1); |
1079 | | FAIL_IF(FlowMatch(pflags, 0, 0, fd->flags, fd->match_cnt)); |
1080 | | pflags |= PKT_REBUILT_FRAGMENT; |
1081 | | FAIL_IF_NOT(FlowMatch(pflags, 0, 0, fd->flags, fd->match_cnt)); |
1082 | | PASS; |
1083 | | } |
1084 | | |
1085 | | /** |
1086 | | * \brief this function registers unit tests for DetectFlow |
1087 | | */ |
1088 | | static void DetectFlowRegisterTests(void) |
1089 | | { |
1090 | | UtRegisterTest("DetectFlowTestParse01", DetectFlowTestParse01); |
1091 | | UtRegisterTest("DetectFlowTestParse02", DetectFlowTestParse02); |
1092 | | UtRegisterTest("DetectFlowTestParse03", DetectFlowTestParse03); |
1093 | | UtRegisterTest("DetectFlowTestParse04", DetectFlowTestParse04); |
1094 | | UtRegisterTest("DetectFlowTestParse05", DetectFlowTestParse05); |
1095 | | UtRegisterTest("DetectFlowTestParse06", DetectFlowTestParse06); |
1096 | | UtRegisterTest("DetectFlowTestParse07", DetectFlowTestParse07); |
1097 | | UtRegisterTest("DetectFlowTestParse08", DetectFlowTestParse08); |
1098 | | UtRegisterTest("DetectFlowTestParse09", DetectFlowTestParse09); |
1099 | | UtRegisterTest("DetectFlowTestParse10", DetectFlowTestParse10); |
1100 | | UtRegisterTest("DetectFlowTestParse11", DetectFlowTestParse11); |
1101 | | UtRegisterTest("DetectFlowTestParseNocase01", DetectFlowTestParseNocase01); |
1102 | | UtRegisterTest("DetectFlowTestParseNocase02", DetectFlowTestParseNocase02); |
1103 | | UtRegisterTest("DetectFlowTestParseNocase03", DetectFlowTestParseNocase03); |
1104 | | UtRegisterTest("DetectFlowTestParseNocase04", DetectFlowTestParseNocase04); |
1105 | | UtRegisterTest("DetectFlowTestParseNocase05", DetectFlowTestParseNocase05); |
1106 | | UtRegisterTest("DetectFlowTestParseNocase06", DetectFlowTestParseNocase06); |
1107 | | UtRegisterTest("DetectFlowTestParseNocase07", DetectFlowTestParseNocase07); |
1108 | | UtRegisterTest("DetectFlowTestParseNocase08", DetectFlowTestParseNocase08); |
1109 | | UtRegisterTest("DetectFlowTestParseNocase09", DetectFlowTestParseNocase09); |
1110 | | UtRegisterTest("DetectFlowTestParseNocase10", DetectFlowTestParseNocase10); |
1111 | | UtRegisterTest("DetectFlowTestParseNocase11", DetectFlowTestParseNocase11); |
1112 | | UtRegisterTest("DetectFlowTestParse12", DetectFlowTestParse12); |
1113 | | UtRegisterTest("DetectFlowTestParse13", DetectFlowTestParse13); |
1114 | | UtRegisterTest("DetectFlowTestParse14", DetectFlowTestParse14); |
1115 | | UtRegisterTest("DetectFlowTestParse15", DetectFlowTestParse15); |
1116 | | UtRegisterTest("DetectFlowTestParse16", DetectFlowTestParse16); |
1117 | | UtRegisterTest("DetectFlowTestParse17", DetectFlowTestParse17); |
1118 | | UtRegisterTest("DetectFlowTestParse18", DetectFlowTestParse18); |
1119 | | UtRegisterTest("DetectFlowTestParseNocase18", DetectFlowTestParseNocase18); |
1120 | | UtRegisterTest("DetectFlowTestParse19", DetectFlowTestParse19); |
1121 | | UtRegisterTest("DetectFlowTestParse20", DetectFlowTestParse20); |
1122 | | UtRegisterTest("DetectFlowTestParseNocase20", DetectFlowTestParseNocase20); |
1123 | | UtRegisterTest("DetectFlowTestParse21", DetectFlowTestParse21); |
1124 | | UtRegisterTest("DetectFlowTestParse22", DetectFlowTestParse22); |
1125 | | UtRegisterTest("DetectFlowTestParseNotEstablished", |
1126 | | DetectFlowTestParseNotEstablished); |
1127 | | UtRegisterTest("DetectFlowTestParseNoFrag", DetectFlowTestParseNoFrag); |
1128 | | UtRegisterTest("DetectFlowTestParseOnlyFrag", |
1129 | | DetectFlowTestParseOnlyFrag); |
1130 | | UtRegisterTest("DetectFlowTestParseNoFragOnlyFrag", |
1131 | | DetectFlowTestParseNoFragOnlyFrag); |
1132 | | UtRegisterTest("DetectFlowTestNoFragMatch", DetectFlowTestNoFragMatch); |
1133 | | UtRegisterTest("DetectFlowTestOnlyFragMatch", DetectFlowTestOnlyFragMatch); |
1134 | | |
1135 | | UtRegisterTest("DetectFlowSigTest01", DetectFlowSigTest01); |
1136 | | } |
1137 | | #endif /* UNITTESTS */ |