suricata
detect-bytemath.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-2026 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 Jeff Lucovsky <jeff@lucovsky.org>
22  */
23 
24 /*
25  * Refer to the Snort manual, section 3.5.34 for details.
26  */
27 
28 #include "suricata-common.h"
29 #include "threads.h"
30 #include "decode.h"
31 
32 #include "rust.h"
33 #include "app-layer-parser.h"
34 #include "app-layer-protos.h"
35 
36 #include "detect.h"
37 #include "detect-parse.h"
38 #include "detect-engine.h"
39 #include "detect-engine-buffer.h"
40 #include "detect-engine-mpm.h"
41 #include "detect-engine-state.h"
42 #include "detect-engine-build.h"
43 
44 #include "detect-content.h"
45 #include "detect-pcre.h"
46 #include "detect-byte.h"
47 #include "detect-bytemath.h"
48 
49 #include "flow.h"
50 #include "flow-var.h"
51 #include "flow-util.h"
52 
53 #include "util-byte.h"
54 #include "util-debug.h"
55 #include "util-unittest.h"
56 #include "util-unittest-helper.h"
57 #include "util-spm.h"
58 
59 static int DetectByteMathSetup(DetectEngineCtx *, Signature *, const char *);
60 #ifdef UNITTESTS
61 #define DETECT_BYTEMATH_ENDIAN_DEFAULT (uint8_t) BigEndian
62 #define DETECT_BYTEMATH_BASE_DEFAULT (uint8_t) BaseDec
63 
64 static void DetectByteMathRegisterTests(void);
65 #endif
66 static void DetectByteMathFree(DetectEngineCtx *, void *);
67 
68 /**
69  * \brief Registers the keyword handlers for the "byte_math" keyword.
70  */
72 {
73  sigmatch_table[DETECT_BYTEMATH].name = "byte_math";
75  sigmatch_table[DETECT_BYTEMATH].Setup = DetectByteMathSetup;
76  sigmatch_table[DETECT_BYTEMATH].Free = DetectByteMathFree;
77  sigmatch_table[DETECT_BYTEMATH].desc = "used to perform mathematical operations on byte values";
78  sigmatch_table[DETECT_BYTEMATH].url = "/rules/payload-keywords.html#byte-math";
79 #ifdef UNITTESTS
80  sigmatch_table[DETECT_BYTEMATH].RegisterTests = DetectByteMathRegisterTests;
81 #endif
82 }
83 
84 static inline bool DetectByteMathValidateNbytesOnly(const DetectByteMathData *data, int32_t nbytes)
85 {
86  return nbytes >= 1 &&
87  (((data->flags & DETECT_BYTEMATH_FLAG_STRING) && nbytes <= 10) || (nbytes <= 4));
88 }
89 
90 int DetectByteMathDoMatch(DetectEngineThreadCtx *det_ctx, const DetectByteMathData *data,
91  const Signature *s, const uint8_t *payload, const uint32_t payload_len, uint8_t nbytes,
92  uint64_t rvalue, uint64_t *value, uint8_t endian)
93 {
94  if (payload_len == 0) {
95  return 0;
96  }
97 
98  if (!DetectByteMathValidateNbytesOnly(data, nbytes)) {
99  return 0;
100  }
101 
102  const uint8_t *ptr;
103  int32_t len;
104  uint64_t val;
105  int extbytes;
106 
107  /* Calculate the ptr value for the byte-math op and length remaining in
108  * the packet from that point.
109  */
110  if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
111  SCLogDebug("relative, working with det_ctx->buffer_offset %" PRIu32 ", "
112  "data->offset %" PRIi32 "",
113  det_ctx->buffer_offset, data->offset);
114 
115  ptr = payload + det_ctx->buffer_offset;
116  len = payload_len - det_ctx->buffer_offset;
117 
118  ptr += data->offset;
119  len -= data->offset;
120 
121  /* No match if there is no relative base */
122  if (len <= 0) {
123  return 0;
124  }
125  } else {
126  SCLogDebug("absolute, data->offset %" PRIi32 "", data->offset);
127 
128  ptr = payload + data->offset;
129  len = payload_len - data->offset;
130  }
131 
132  /* Validate that the to-be-extracted is within the packet */
133  if (ptr < payload || nbytes > len) {
134  SCLogDebug("Data not within payload pkt=%p, ptr=%p, len=%" PRIu32 ", nbytes=%d", payload,
135  ptr, len, nbytes);
136  return 0;
137  }
138 
139  /* Extract the byte data */
140  if (data->flags & DETECT_BYTEMATH_FLAG_STRING) {
141  extbytes = ByteExtractStringUint64(&val, data->base, nbytes, (const char *)ptr);
142  if (extbytes <= 0) {
143  if (val == 0) {
144  SCLogDebug("No Numeric value");
145  return 0;
146  } else {
147  SCLogDebug("error extracting %d bytes of string data: %d", nbytes, extbytes);
148  return -1;
149  }
150  }
151  } else {
152  ByteEndian bme = endian;
153  int endianness = (bme == BigEndian) ? BYTE_BIG_ENDIAN : BYTE_LITTLE_ENDIAN;
154  extbytes = ByteExtractUint64(&val, endianness, nbytes, ptr);
155  if (extbytes != nbytes) {
156  SCLogDebug("error extracting %d bytes of numeric data: %d", nbytes, extbytes);
157  return 0;
158  }
159  }
160 
161  DEBUG_VALIDATE_BUG_ON(extbytes > len);
162 
163  ptr += extbytes;
164 
165  switch (data->oper) {
166  case OperatorNone:
167  break;
168  case Addition:
169  val += rvalue;
170  break;
171  case Subtraction:
172  val -= rvalue;
173  break;
174  case Division:
175  if (rvalue == 0) {
176  SCLogDebug("avoiding division by zero");
177  return 0;
178  }
179  val /= rvalue;
180  break;
181  case Multiplication:
182  val *= rvalue;
183  break;
184  case LeftShift:
185  if (rvalue < 64) {
186  val <<= rvalue;
187  } else {
188  val = 0;
189  }
190  break;
191  case RightShift:
192  if (rvalue < 64) {
193  val >>= rvalue;
194  } else {
195  val = 0;
196  }
197  break;
198  }
199 
200  det_ctx->buffer_offset = (uint32_t)(ptr - payload);
201 
202  if (data->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
203  val &= data->bitmask_val;
204  if (val && data->bitmask_shift_count) {
205  val = val >> data->bitmask_shift_count;
206  }
207  }
208 
209  *value = val;
210  return 1;
211 }
212 
213 /**
214  * \internal
215  * \brief Used to parse byte_math arg.
216  *
217  * \param arg The argument to parse.
218  * \param rvalue May be NULL. When non-null, will contain the variable
219  * name of rvalue (iff rvalue is not a scalar value)
220  *
221  * \retval bmd On success an instance containing the parsed data.
222  * On failure, NULL.
223  */
224 static DetectByteMathData *DetectByteMathParse(
225  DetectEngineCtx *de_ctx, const char *arg, char **nbytes, char **rvalue)
226 {
227  DetectByteMathData *bmd;
228  if ((bmd = SCByteMathParse(arg)) == NULL) {
229  SCLogError("invalid bytemath values");
230  return NULL;
231  }
232 
233  if (bmd->nbytes_str) {
234  if (nbytes == NULL) {
235  SCLogError("byte_math supplied with "
236  "var name for nbytes. \"nbytes\" argument supplied to "
237  "this function must be non-NULL");
238  goto error;
239  }
240  *nbytes = SCStrdup(bmd->nbytes_str);
241  if (*nbytes == NULL) {
242  goto error;
243  }
244  }
245 
246  if (bmd->rvalue_str) {
247  if (rvalue == NULL) {
248  SCLogError("byte_math supplied with "
249  "var name for rvalue. \"rvalue\" argument supplied to "
250  "this function must be non-NULL");
251  goto error;
252  }
253  *rvalue = SCStrdup(bmd->rvalue_str);
254  if (*rvalue == NULL) {
255  goto error;
256  }
257  }
258 
259  if (bmd->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
260  if (bmd->bitmask_val) {
261  uint32_t bmask = bmd->bitmask_val;
262  while (!(bmask & 0x1)){
263  bmask = bmask >> 1;
264  bmd->bitmask_shift_count++;
265  }
266  }
267  }
268 
269  return bmd;
270 
271  error:
272  if (bmd != NULL)
273  DetectByteMathFree(de_ctx, bmd);
274  return NULL;
275 }
276 
277 /**
278  * \brief The setup function for the byte_math keyword for a signature.
279  *
280  * \param de_ctx Pointer to the detection engine context.
281  * \param s Pointer to signature for the current Signature being parsed
282  * from the rules.
283  * \param arg Pointer to the string holding the keyword value.
284  *
285  * \retval 0 On success.
286  * \retval -1 On failure.
287  */
288 static int DetectByteMathSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
289 {
290  SigMatch *prev_pm = NULL;
291  DetectByteMathData *data;
292  char *rvalue = NULL;
293  char *nbytes = NULL;
294  int ret = -1;
295 
296  data = DetectByteMathParse(de_ctx, arg, &nbytes, &rvalue);
297  if (data == NULL)
298  goto error;
299 
300  int sm_list;
301  if (s->init_data->list != DETECT_SM_LIST_NOTSET) {
302  if (DetectBufferGetActiveList(de_ctx, s) == -1)
303  goto error;
304 
305  sm_list = s->init_data->list;
306 
307  if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
309  if (!prev_pm) {
310  SCLogError("relative specified without "
311  "previous pattern match");
312  goto error;
313  }
314  }
315  } else if (data->endian == EndianDCE) {
316  if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
319  if (prev_pm == NULL) {
320  sm_list = DETECT_SM_LIST_PMATCH;
321  } else {
322  sm_list = SigMatchListSMBelongsTo(s, prev_pm);
323  if (sm_list < 0)
324  goto error;
325  }
326  } else {
327  sm_list = DETECT_SM_LIST_PMATCH;
328  }
329 
331  goto error;
332 
333  } else if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
336  if (prev_pm == NULL) {
337  sm_list = DETECT_SM_LIST_PMATCH;
338  } else {
339  sm_list = SigMatchListSMBelongsTo(s, prev_pm);
340  if (sm_list < 0)
341  goto error;
342  }
343 
344  } else {
345  sm_list = DETECT_SM_LIST_PMATCH;
346  }
347 
348  if (data->endian == EndianDCE) {
350  goto error;
351 
352  if ((data->flags & DETECT_BYTEMATH_FLAG_STRING) || (data->base == BaseDec) ||
353  (data->base == BaseHex) || (data->base == BaseOct)) {
354  SCLogError("Invalid option. "
355  "A bytemath keyword with dce holds other invalid modifiers.");
356  goto error;
357  }
358  }
359 
360  if (nbytes != NULL) {
361  DetectByteIndexType index;
362  if (!DetectByteRetrieveSMVar(nbytes, s, sm_list, &index)) {
363  SCLogError("unknown byte_ keyword var seen in byte_math - %s", nbytes);
364  goto error;
365  }
366  data->nbytes = index;
367  data->flags |= DETECT_BYTEMATH_FLAG_NBYTES_VAR;
368  SCFree(nbytes);
369  nbytes = NULL;
370  }
371 
372  if (rvalue != NULL) {
373  DetectByteIndexType index;
374  if (!DetectByteRetrieveSMVar(rvalue, s, sm_list, &index)) {
375  SCLogError("unknown byte_ keyword var seen in byte_math - %s", rvalue);
376  goto error;
377  }
378  data->rvalue = index;
379  data->flags |= DETECT_BYTEMATH_FLAG_RVALUE_VAR;
380  SCFree(rvalue);
381  rvalue = NULL;
382  }
383 
384  SigMatch *prev_bmd_sm = DetectGetLastSMByListId(s, sm_list,
385  DETECT_BYTEMATH, -1);
386  if (prev_bmd_sm == NULL) {
387  data->local_id = 0;
388  } else {
389  data->local_id = ((DetectByteMathData *)prev_bmd_sm->ctx)->local_id + 1;
390  }
391  if (data->local_id > de_ctx->byte_extract_max_local_id) {
392  de_ctx->byte_extract_max_local_id = data->local_id;
393  }
394 
395  if (SCSigMatchAppendSMToList(de_ctx, s, DETECT_BYTEMATH, (SigMatchCtx *)data, sm_list) ==
396  NULL) {
397  goto error;
398  }
399 
400  if (!(data->flags & DETECT_BYTEMATH_FLAG_RELATIVE))
401  goto okay;
402 
403  if (prev_pm == NULL)
404  goto okay;
405 
406  if (prev_pm->type == DETECT_CONTENT) {
407  DetectContentData *cd = (DetectContentData *)prev_pm->ctx;
409  } else if (prev_pm->type == DETECT_PCRE) {
410  DetectPcreData *pd = (DetectPcreData *)prev_pm->ctx;
412  }
413 
414  okay:
415  return 0;
416 
417  error:
418  if (rvalue)
419  SCFree(rvalue);
420  if (nbytes)
421  SCFree(nbytes);
422  DetectByteMathFree(de_ctx, data);
423  return ret;
424 }
425 
426 /**
427  * \brief Used to free instances of DetectByteMathractData.
428  *
429  * \param ptr Instance of DetectByteMathData to be freed.
430  */
431 static void DetectByteMathFree(DetectEngineCtx *de_ctx, void *ptr)
432 {
433  SCByteMathFree(ptr);
434 }
435 
436 /**
437  * \brief Lookup the SigMatch for a named byte_math variable.
438  *
439  * \param arg The name of the byte_math variable to lookup.
440  * \param s Pointer the signature to look in.
441  *
442  * \retval A pointer to the SigMatch if found, otherwise NULL.
443  */
444 SigMatch *DetectByteMathRetrieveSMVar(const char *arg, int sm_list, const Signature *s)
445 {
446  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
447  SigMatch *sm = s->init_data->buffers[x].head;
448  while (sm != NULL) {
449  if (sm->type == DETECT_BYTEMATH) {
450  const DetectByteMathData *bmd = (const DetectByteMathData *)sm->ctx;
451  if (strcmp(bmd->result, arg) == 0) {
452  SCLogDebug("Retrieved SM for \"%s\"", arg);
453  return sm;
454  }
455  }
456  sm = sm->next;
457  }
458  }
459 
460  for (int list = 0; list < DETECT_SM_LIST_MAX; list++) {
461  SigMatch *sm = s->init_data->smlists[list];
462  while (sm != NULL) {
463  // Make sure that the linked buffers ore on the same list
464  if (sm->type == DETECT_BYTEMATH && (sm_list == -1 || sm_list == list)) {
465  const DetectByteMathData *bmd = (const DetectByteMathData *)sm->ctx;
466  if (strcmp(bmd->result, arg) == 0) {
467  SCLogDebug("Retrieved SM for \"%s\"", arg);
468  return sm;
469  }
470  }
471  sm = sm->next;
472  }
473  }
474 
475  return NULL;
476 }
477 
478 /*************************************Unittests********************************/
479 #ifdef UNITTESTS
480 #include "detect-engine-alert.h"
481 
482 static int DetectByteMathParseTest01(void)
483 {
484 
485  DetectByteMathData *bmd = DetectByteMathParse(NULL,
486  "bytes 4, offset 2, oper +,"
487  "rvalue 10, result bar",
488  NULL, NULL);
489  FAIL_IF(bmd == NULL);
490 
491  FAIL_IF_NOT(bmd->nbytes == 4);
492  FAIL_IF_NOT(bmd->offset == 2);
493  FAIL_IF_NOT(bmd->oper == Addition);
494  FAIL_IF_NOT(bmd->rvalue == 10);
495  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
498 
499  DetectByteMathFree(NULL, bmd);
500 
501  PASS;
502 }
503 
504 static int DetectByteMathParseTest02(void)
505 {
506  /* bytes value invalid */
507  DetectByteMathData *bmd = DetectByteMathParse(NULL,
508  "bytes 257, offset 2, oper +, "
509  "rvalue 39, result bar",
510  NULL, NULL);
511 
512  FAIL_IF_NOT(bmd == NULL);
513 
514  PASS;
515 }
516 
517 static int DetectByteMathParseTest03(void)
518 {
519  /* bytes value invalid */
520  DetectByteMathData *bmd = DetectByteMathParse(NULL,
521  "bytes 11, offset 2, oper +, "
522  "rvalue 39, result bar",
523  NULL, NULL);
524  FAIL_IF_NOT(bmd == NULL);
525 
526  PASS;
527 }
528 
529 static int DetectByteMathParseTest04(void)
530 {
531  /* offset value invalid */
532  DetectByteMathData *bmd = DetectByteMathParse(NULL,
533  "bytes 4, offset 70000, oper +,"
534  " rvalue 39, result bar",
535  NULL, NULL);
536 
537  FAIL_IF_NOT(bmd == NULL);
538 
539  PASS;
540 }
541 
542 static int DetectByteMathParseTest05(void)
543 {
544  /* oper value invalid */
545  DetectByteMathData *bmd = DetectByteMathParse(NULL,
546  "bytes 11, offset 16, oper &,"
547  "rvalue 39, result bar",
548  NULL, NULL);
549  FAIL_IF_NOT(bmd == NULL);
550 
551  PASS;
552 }
553 
554 static int DetectByteMathParseTest06(void)
555 {
556  uint8_t flags = DETECT_BYTEMATH_FLAG_RELATIVE;
557  char *rvalue = NULL;
558 
559  DetectByteMathData *bmd = DetectByteMathParse(NULL,
560  "bytes 4, offset 0, oper +,"
561  "rvalue 248, result var, relative",
562  NULL, &rvalue);
563 
564  FAIL_IF(bmd == NULL);
565  FAIL_IF_NOT(bmd->nbytes == 4);
566  FAIL_IF_NOT(bmd->offset == 0);
567  FAIL_IF_NOT(bmd->oper == Addition);
568  FAIL_IF_NOT(bmd->rvalue == 248);
569  FAIL_IF_NOT(strcmp(bmd->result, "var") == 0);
570  FAIL_IF_NOT(bmd->flags == flags);
573 
574  DetectByteMathFree(NULL, bmd);
575 
576  PASS;
577 }
578 
579 static int DetectByteMathParseTest07(void)
580 {
581  char *rvalue = NULL;
582 
583  DetectByteMathData *bmd = DetectByteMathParse(NULL,
584  "bytes 4, offset 2, oper +,"
585  "rvalue foo, result bar",
586  NULL, &rvalue);
587  FAIL_IF_NOT(rvalue);
588  FAIL_IF_NOT(bmd->nbytes == 4);
589  FAIL_IF_NOT(bmd->offset == 2);
590  FAIL_IF_NOT(bmd->oper == Addition);
591  FAIL_IF_NOT(strcmp(rvalue, "foo") == 0);
592  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
595 
596  DetectByteMathFree(NULL, bmd);
597 
598  SCFree(rvalue);
599 
600  PASS;
601 }
602 
603 static int DetectByteMathParseTest08(void)
604 {
605  /* ensure Parse checks the pointer value when rvalue is a var */
606  DetectByteMathData *bmd = DetectByteMathParse(NULL,
607  "bytes 4, offset 2, oper +,"
608  "rvalue foo, result bar",
609  NULL, NULL);
610  FAIL_IF_NOT(bmd == NULL);
611 
612  PASS;
613 }
614 
615 static int DetectByteMathParseTest09(void)
616 {
617  uint8_t flags = DETECT_BYTEMATH_FLAG_RELATIVE;
618 
619  DetectByteMathData *bmd = DetectByteMathParse(NULL,
620  "bytes 4, offset 2, oper +,"
621  "rvalue 39, result bar, relative",
622  NULL, NULL);
623  FAIL_IF(bmd == NULL);
624 
625  FAIL_IF_NOT(bmd->nbytes == 4);
626  FAIL_IF_NOT(bmd->offset == 2);
627  FAIL_IF_NOT(bmd->oper == Addition);
628  FAIL_IF_NOT(bmd->rvalue == 39);
629  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
630  FAIL_IF_NOT(bmd->flags == flags);
633 
634  DetectByteMathFree(NULL, bmd);
635 
636  PASS;
637 }
638 
639 static int DetectByteMathParseTest10(void)
640 {
641  uint8_t flags = DETECT_BYTEMATH_FLAG_ENDIAN;
642 
643  DetectByteMathData *bmd = DetectByteMathParse(NULL,
644  "bytes 4, offset 2, oper +,"
645  "rvalue 39, result bar, endian"
646  " big",
647  NULL, NULL);
648 
649  FAIL_IF(bmd == NULL);
650  FAIL_IF_NOT(bmd->nbytes == 4);
651  FAIL_IF_NOT(bmd->offset == 2);
652  FAIL_IF_NOT(bmd->oper == Addition);
653  FAIL_IF_NOT(bmd->rvalue == 39);
654  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
655  FAIL_IF_NOT(bmd->flags == flags);
656  FAIL_IF_NOT(bmd->endian == BigEndian);
658 
659  DetectByteMathFree(NULL, bmd);
660 
661  PASS;
662 }
663 
664 static int DetectByteMathParseTest11(void)
665 {
666  uint8_t flags = DETECT_BYTEMATH_FLAG_ENDIAN;
667 
668  DetectByteMathData *bmd = DetectByteMathParse(NULL,
669  "bytes 4, offset 2, oper +, "
670  "rvalue 39, result bar, dce",
671  NULL, NULL);
672 
673  FAIL_IF(bmd == NULL);
674  FAIL_IF_NOT(bmd->nbytes == 4);
675  FAIL_IF_NOT(bmd->offset == 2);
676  FAIL_IF_NOT(bmd->oper == Addition);
677  FAIL_IF_NOT(bmd->rvalue == 39);
678  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
679  FAIL_IF_NOT(bmd->flags == flags);
680  FAIL_IF_NOT(bmd->endian == EndianDCE);
682 
683  DetectByteMathFree(NULL, bmd);
684 
685  PASS;
686 }
687 
688 static int DetectByteMathParseTest12(void)
689 {
690  uint8_t flags = DETECT_BYTEMATH_FLAG_RELATIVE | DETECT_BYTEMATH_FLAG_STRING;
691 
692  DetectByteMathData *bmd = DetectByteMathParse(NULL,
693  "bytes 4, offset 2, oper +,"
694  "rvalue 39, result bar, "
695  "relative, string dec",
696  NULL, NULL);
697 
698  FAIL_IF(bmd == NULL);
699  FAIL_IF_NOT(bmd->nbytes == 4);
700  FAIL_IF_NOT(bmd->offset == 2);
701  FAIL_IF_NOT(bmd->oper == Addition);
702  FAIL_IF_NOT(bmd->rvalue == 39);
703  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
704  FAIL_IF_NOT(bmd->flags == flags);
705  FAIL_IF_NOT(bmd->endian == BigEndian);
706  FAIL_IF_NOT(bmd->base == BaseDec);
707 
708  DetectByteMathFree(NULL, bmd);
709 
710  PASS;
711 }
712 
713 static int DetectByteMathParseTest13(void)
714 {
715  uint8_t flags = DETECT_BYTEMATH_FLAG_STRING |
716  DETECT_BYTEMATH_FLAG_RELATIVE |
717  DETECT_BYTEMATH_FLAG_BITMASK;
718 
719  DetectByteMathData *bmd = DetectByteMathParse(NULL,
720  "bytes 4, offset 2, oper +, "
721  "rvalue 39, result bar, "
722  "relative, string dec, bitmask "
723  "0x8f40",
724  NULL, NULL);
725 
726  FAIL_IF(bmd == NULL);
727  FAIL_IF_NOT(bmd->nbytes == 4);
728  FAIL_IF_NOT(bmd->offset == 2);
729  FAIL_IF_NOT(bmd->oper == Addition);
730  FAIL_IF_NOT(bmd->rvalue == 39);
731  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
732  FAIL_IF_NOT(bmd->bitmask_val == 0x8f40);
733  FAIL_IF_NOT(bmd->bitmask_shift_count == 6);
734  FAIL_IF_NOT(bmd->flags == flags);
735  FAIL_IF_NOT(bmd->endian == BigEndian);
736  FAIL_IF_NOT(bmd->base == BaseDec);
737 
738  DetectByteMathFree(NULL, bmd);
739 
740  PASS;
741 }
742 
743 
744 static int DetectByteMathParseTest14(void)
745 {
746  /* incomplete */
747  DetectByteMathData *bmd = DetectByteMathParse(NULL,
748  "bytes 4, offset 2, oper +,"
749  "rvalue foo",
750  NULL, NULL);
751 
752  FAIL_IF_NOT(bmd == NULL);
753 
754  PASS;
755 }
756 
757 static int DetectByteMathParseTest15(void)
758 {
759 
760  /* incomplete */
761  DetectByteMathData *bmd = DetectByteMathParse(NULL,
762  "bytes 4, offset 2, oper +, "
763  "result bar",
764  NULL, NULL);
765 
766  FAIL_IF_NOT(bmd == NULL);
767 
768  PASS;
769 }
770 
771 static int DetectByteMathParseTest16(void)
772 {
773  uint8_t flags = DETECT_BYTEMATH_FLAG_STRING | DETECT_BYTEMATH_FLAG_RELATIVE |
774  DETECT_BYTEMATH_FLAG_BITMASK;
775 
776  DetectByteMathData *bmd = DetectByteMathParse(NULL,
777  "bytes 4, offset -2, oper +, "
778  "rvalue 39, result bar, "
779  "relative, string dec, bitmask "
780  "0x8f40",
781  NULL, NULL);
782 
783  FAIL_IF(bmd == NULL);
784  FAIL_IF_NOT(bmd->nbytes == 4);
785  FAIL_IF_NOT(bmd->offset == -2);
786  FAIL_IF_NOT(bmd->oper == Addition);
787  FAIL_IF_NOT(bmd->rvalue == 39);
788  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
789  FAIL_IF_NOT(bmd->bitmask_val == 0x8f40);
790  FAIL_IF_NOT(bmd->bitmask_shift_count == 6);
791  FAIL_IF_NOT(bmd->flags == flags);
792  FAIL_IF_NOT(bmd->endian == BigEndian);
793  FAIL_IF_NOT(bmd->base == BaseDec);
794 
795  DetectByteMathFree(NULL, bmd);
796 
797  PASS;
798 }
799 
800 static int DetectByteMathPacket01(void)
801 {
802  uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01,
803  0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
804  0x00, 0x00, 0x6d, 0x00, 0x01, 0x00 };
805  Flow f;
806  void *dns_state = NULL;
807  Packet *p = NULL;
808  Signature *s = NULL;
809  ThreadVars tv;
810  DetectEngineThreadCtx *det_ctx = NULL;
812 
813  memset(&tv, 0, sizeof(ThreadVars));
815  memset(&f, 0, sizeof(Flow));
816 
817  p = UTHBuildPacketReal(buf, sizeof(buf), IPPROTO_UDP,
818  "192.168.1.5", "192.168.1.1",
819  41424, 53);
820  FAIL_IF_NULL(p);
821 
822  FLOW_INITIALIZE(&f);
823  f.flags |= FLOW_IPV4;
824  f.proto = IPPROTO_UDP;
826 
827  p->flow = &f;
828  p->flags |= PKT_HAS_FLOW;
830  f.alproto = ALPROTO_DNS;
831 
834 
836  de_ctx->flags |= DE_QUIET;
837 
838  /*
839  * byte_extract: Extract 1 byte from offset 0 --> 0x0038
840  * byte_math: Extract 1 byte from offset 2 (0x35)
841  * Add 0x35 + 0x38 = 109 (0x6d)
842  * byte_test: Compare 2 bytes at offset 13 bytes from last
843  * match and compare with 0x6d
844  */
845  s = DetectEngineAppendSig(de_ctx, "alert udp any any -> any any "
846  "(byte_extract: 1, 0, extracted_val, relative;"
847  "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
848  "byte_test: 2, =, var, 13;"
849  "msg:\"Byte extract and byte math with byte test verification\";"
850  "sid:1;)");
851  FAIL_IF_NULL(s);
852 
853  /* this rule should not alert */
854  s = DetectEngineAppendSig(de_ctx, "alert udp any any -> any any "
855  "(byte_extract: 1, 0, extracted_val, relative;"
856  "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
857  "byte_test: 2, !=, var, 13;"
858  "msg:\"Byte extract and byte math with byte test verification\";"
859  "sid:2;)");
860  FAIL_IF_NULL(s);
861 
862  /*
863  * this rule should alert:
864  * compares offset 15 with var ... 1 (offset 15) < 0x6d (var)
865  */
866  s = DetectEngineAppendSig(de_ctx, "alert udp any any -> any any "
867  "(byte_extract: 1, 0, extracted_val, relative;"
868  "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
869  "byte_test: 2, <, var, 15;"
870  "msg:\"Byte extract and byte math with byte test verification\";"
871  "sid:3;)");
872  FAIL_IF_NULL(s);
873 
875  DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
876  FAIL_IF_NULL(det_ctx);
877 
878  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_DNS,
879  STREAM_TOSERVER, buf, sizeof(buf));
880  FAIL_IF_NOT(r == 0);
881 
882  dns_state = f.alstate;
883  FAIL_IF_NULL(dns_state);
884 
885  /* do detect */
886  SigMatchSignatures(&tv, de_ctx, det_ctx, p);
887 
888  /* ensure sids 1 & 3 alerted */
892 
894  DetectEngineThreadCtxDeinit(&tv, det_ctx);
896 
897  FLOW_DESTROY(&f);
898  UTHFreePacket(p);
900  PASS;
901 }
902 
903 static int DetectByteMathPacket02(void)
904 {
905  uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
906  0x00, 0x70, 0x00, 0x01, 0x00 };
907  Flow f;
908  void *dns_state = NULL;
909  Packet *p = NULL;
910  Signature *s = NULL;
911  ThreadVars tv;
912  DetectEngineThreadCtx *det_ctx = NULL;
914 
915  memset(&tv, 0, sizeof(ThreadVars));
917  memset(&f, 0, sizeof(Flow));
918 
919  p = UTHBuildPacketReal(buf, sizeof(buf), IPPROTO_UDP, "192.168.1.5", "192.168.1.1", 41424, 53);
920  FAIL_IF_NULL(p);
921 
922  FLOW_INITIALIZE(&f);
923  f.flags |= FLOW_IPV4;
924  f.proto = IPPROTO_UDP;
926 
927  p->flow = &f;
928  p->flags |= PKT_HAS_FLOW;
930  f.alproto = ALPROTO_DNS;
931 
934 
936  de_ctx->flags |= DE_QUIET;
937 
938  /*
939  * byte_extract: Extract 1 byte from offset 0 --> 0x38
940  * byte_math: Extract 1 byte from offset -1 (0x38)
941  * Add 0x38 + 0x38 = 112 (0x70)
942  * byte_test: Compare 2 bytes at offset 13 bytes from last
943  * match and compare with 0x70
944  */
946  "alert udp any any -> any any "
947  "(byte_extract: 1, 0, extracted_val, relative;"
948  "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
949  "byte_test: 2, =, var, 13;"
950  "msg:\"Byte extract and byte math with byte test verification\";"
951  "sid:1;)");
952  FAIL_IF_NULL(s);
953 
954  /* this rule should not alert */
956  "alert udp any any -> any any "
957  "(byte_extract: 1, 0, extracted_val, relative;"
958  "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
959  "byte_test: 2, !=, var, 13;"
960  "msg:\"Byte extract and byte math with byte test verification\";"
961  "sid:2;)");
962  FAIL_IF_NULL(s);
963 
964  /*
965  * this rule should alert:
966  * compares offset 15 with var ... 1 (offset 15) < 0x70 (var)
967  */
969  "alert udp any any -> any any "
970  "(byte_extract: 1, 0, extracted_val, relative;"
971  "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
972  "byte_test: 2, <, var, 15;"
973  "msg:\"Byte extract and byte math with byte test verification\";"
974  "sid:3;)");
975  FAIL_IF_NULL(s);
976 
978  DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
979  FAIL_IF_NULL(det_ctx);
980 
981  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_DNS, STREAM_TOSERVER, buf, sizeof(buf));
982  FAIL_IF_NOT(r == 0);
983 
984  dns_state = f.alstate;
985  FAIL_IF_NULL(dns_state);
986 
987  /* do detect */
988  SigMatchSignatures(&tv, de_ctx, det_ctx, p);
989 
990  /* ensure sids 1 & 3 alerted */
994 
996  DetectEngineThreadCtxDeinit(&tv, det_ctx);
998 
999  FLOW_DESTROY(&f);
1000  UTHFreePacket(p);
1001 
1003  PASS;
1004 }
1005 
1006 /**
1007  * \test A payload-supplied shift count of 64 or more yields 0 instead of
1008  * shifting a uint64_t by its own width.
1009  */
1010 static int DetectByteMathPacket03(void)
1011 {
1012  /* byte 0 is the shift count (64), byte 1 the value shifted, byte 2 the
1013  * expected result */
1014  uint8_t buf[] = { 0x40, 0xff, 0x00 };
1015 
1016  Packet *p = UTHBuildPacket(buf, sizeof(buf), IPPROTO_UDP);
1017  FAIL_IF_NULL(p);
1018 
1019  /* 0xff >> 64 is 0 */
1020  FAIL_IF_NOT(UTHPacketMatchSig(p, "alert udp any any -> any any "
1021  "(byte_extract: 1, 0, shift;"
1022  "byte_math: bytes 1, offset 1, oper >>, rvalue shift, result "
1023  "var;"
1024  "byte_test: 1, =, var, 2;"
1025  "sid:1;)"));
1026  UTHFreePacket(p);
1027 
1028  PASS;
1029 }
1030 
1031 /**
1032  * \test A literal shift count of 64 or more is rejected at parse time.
1033  */
1034 static int DetectByteMathParseTest17(void)
1035 {
1036  DetectByteMathData *bmd = DetectByteMathParse(
1037  NULL, "bytes 4, offset 2, oper >>, rvalue 64, result foo", NULL, NULL);
1038  FAIL_IF_NOT_NULL(bmd);
1039 
1040  bmd = DetectByteMathParse(
1041  NULL, "bytes 4, offset 2, oper <<, rvalue 64, result foo", NULL, NULL);
1042  FAIL_IF_NOT_NULL(bmd);
1043 
1044  bmd = DetectByteMathParse(
1045  NULL, "bytes 4, offset 2, oper >>, rvalue 63, result foo", NULL, NULL);
1046  FAIL_IF_NULL(bmd);
1047  DetectByteMathFree(NULL, bmd);
1048 
1049  PASS;
1050 }
1051 
1052 static int DetectByteMathContext01(void)
1053 {
1054  DetectEngineCtx *de_ctx = NULL;
1055  Signature *s = NULL;
1056  SigMatch *sm = NULL;
1057  DetectContentData *cd = NULL;
1058  DetectByteMathData *bmd = NULL;
1059 
1061  FAIL_IF(de_ctx == NULL);
1062 
1063  de_ctx->flags |= DE_QUIET;
1064  s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1065  "(msg:\"Testing bytemath_body\"; "
1066  "content:\"|00 04 93 F3|\"; "
1067  "content:\"|00 00 00 07|\"; distance:4; within:4;"
1068  "byte_math:bytes 4, offset 0, oper +, rvalue "
1069  "248, result var, relative; sid:1;)");
1070 
1071  FAIL_IF(de_ctx->sig_list == NULL);
1072 
1074 
1076  FAIL_IF(sm->type != DETECT_CONTENT);
1077  cd = (DetectContentData *)sm->ctx;
1080  FAIL_IF(cd->content_len != 4);
1081 
1082  sm = sm->next;
1083  FAIL_IF(sm->type != DETECT_CONTENT);
1084  sm = sm->next;
1085  FAIL_IF(sm->type != DETECT_BYTEMATH);
1086 
1087  FAIL_IF(sm->ctx == NULL);
1088 
1089  bmd = (DetectByteMathData *)sm->ctx;
1090  FAIL_IF_NOT(bmd->nbytes == 4);
1091  FAIL_IF_NOT(bmd->offset == 0);
1092  FAIL_IF_NOT(bmd->rvalue == 248);
1093  FAIL_IF_NOT(strcmp(bmd->result, "var") == 0);
1094  FAIL_IF_NOT(bmd->flags == DETECT_BYTEMATH_FLAG_RELATIVE);
1095  FAIL_IF_NOT(bmd->endian == BigEndian);
1096  FAIL_IF_NOT(bmd->oper == Addition);
1097  FAIL_IF_NOT(bmd->base == BaseDec);
1098 
1100 
1101  PASS;
1102 }
1103 
1104 static void DetectByteMathRegisterTests(void)
1105 {
1106  UtRegisterTest("DetectByteMathParseTest01", DetectByteMathParseTest01);
1107  UtRegisterTest("DetectByteMathParseTest02", DetectByteMathParseTest02);
1108  UtRegisterTest("DetectByteMathParseTest03", DetectByteMathParseTest03);
1109  UtRegisterTest("DetectByteMathParseTest04", DetectByteMathParseTest04);
1110  UtRegisterTest("DetectByteMathParseTest05", DetectByteMathParseTest05);
1111  UtRegisterTest("DetectByteMathParseTest06", DetectByteMathParseTest06);
1112  UtRegisterTest("DetectByteMathParseTest07", DetectByteMathParseTest07);
1113  UtRegisterTest("DetectByteMathParseTest08", DetectByteMathParseTest08);
1114  UtRegisterTest("DetectByteMathParseTest09", DetectByteMathParseTest09);
1115  UtRegisterTest("DetectByteMathParseTest10", DetectByteMathParseTest10);
1116  UtRegisterTest("DetectByteMathParseTest11", DetectByteMathParseTest11);
1117  UtRegisterTest("DetectByteMathParseTest12", DetectByteMathParseTest12);
1118  UtRegisterTest("DetectByteMathParseTest13", DetectByteMathParseTest13);
1119  UtRegisterTest("DetectByteMathParseTest14", DetectByteMathParseTest14);
1120  UtRegisterTest("DetectByteMathParseTest15", DetectByteMathParseTest15);
1121  UtRegisterTest("DetectByteMathParseTest16", DetectByteMathParseTest16);
1122  UtRegisterTest("DetectByteMathParseTest17", DetectByteMathParseTest17);
1123  UtRegisterTest("DetectByteMathPacket01", DetectByteMathPacket01);
1124  UtRegisterTest("DetectByteMathPacket02", DetectByteMathPacket02);
1125  UtRegisterTest("DetectByteMathPacket03", DetectByteMathPacket03);
1126  UtRegisterTest("DetectByteMathContext01", DetectByteMathContext01);
1127 }
1128 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1527
DETECT_CONTENT_RELATIVE_NEXT
#define DETECT_CONTENT_RELATIVE_NEXT
Definition: detect-content.h:66
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:539
detect-content.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1330
detect-engine.h
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:119
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:662
SigTableElmt_::desc
const char * desc
Definition: detect.h:1526
ByteExtractUint64
int ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:75
Flow_::flags
uint64_t flags
Definition: flow.h:404
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1311
ALPROTO_DCERPC
@ ALPROTO_DCERPC
Definition: app-layer-protos.h:44
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1511
flow-util.h
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:47
SigTableElmt_::name
const char * name
Definition: detect.h:1524
SignatureInitData_::smlists_tail
struct SigMatch_ * smlists_tail[DETECT_SM_LIST_MAX]
Definition: detect.h:664
DETECT_BYTEJUMP
@ DETECT_BYTEJUMP
Definition: detect-engine-register.h:92
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:78
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
Flow_::proto
uint8_t proto
Definition: flow.h:377
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:144
Packet_::flags
uint32_t flags
Definition: decode.h:562
threads.h
SCDetectGetLastSMFromLists
SigMatch * SCDetectGetLastSMFromLists(const Signature *s,...)
Returns the sm with the largest index (added latest) from the lists passed to us.
Definition: detect-parse.c:596
Flow_
Flow data structure.
Definition: flow.h:355
Flow_::protomap
uint8_t protomap
Definition: flow.h:446
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:987
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2878
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:356
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:232
rust.h
DE_QUIET
#define DE_QUIET
Definition: detect.h:333
UTHPacketMatchSig
int UTHPacketMatchSig(Packet *p, const char *sig)
Definition: util-unittest-helper.c:835
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:243
mpm_default_matcher
uint8_t mpm_default_matcher
Definition: util-mpm.c:48
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:3064
DetectContentData_
Definition: detect-content.h:93
p
Packet * p
Definition: fuzz_iprep.c:21
DetectPcreData_::flags
uint16_t flags
Definition: detect-pcre.h:52
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2518
ByteExtractStringUint64
int ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:190
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3859
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:547
UTHBuildPacketReal
Packet * UTHBuildPacketReal(uint8_t *payload, uint16_t payload_len, uint8_t ipproto, const char *src, const char *dst, uint16_t sport, uint16_t dport)
UTHBuildPacketReal is a function that create tcp/udp packets for unittests specifying ip and port sou...
Definition: util-unittest-helper.c:138
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1506
detect-pcre.h
DETECT_BYTEMATH_ENDIAN_DEFAULT
#define DETECT_BYTEMATH_ENDIAN_DEFAULT
Definition: detect-bytemath.c:61
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:100
DetectByteIndexType
uint8_t DetectByteIndexType
Definition: detect-byte.h:28
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DetectGetLastSMByListId
SigMatch * DetectGetLastSMByListId(const Signature *s, int list_id,...)
Returns the sm with the largest index (added last) from the list passed to us as an id.
Definition: detect-parse.c:690
DetectByteMathRetrieveSMVar
SigMatch * DetectByteMathRetrieveSMVar(const char *arg, int sm_list, const Signature *s)
Lookup the SigMatch for a named byte_math variable.
Definition: detect-bytemath.c:444
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:38
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
DETECT_CONTENT_DISTANCE
#define DETECT_CONTENT_DISTANCE
Definition: detect-content.h:30
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1306
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:24
SignatureInitData_::list
int list
Definition: detect.h:641
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:420
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3626
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:363
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:990
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:3512
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:362
BYTE_BIG_ENDIAN
#define BYTE_BIG_ENDIAN
Definition: util-byte.h:29
FlowGetProtoMapping
uint8_t FlowGetProtoMapping(uint8_t proto)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:100
Packet_
Definition: decode.h:516
detect-engine-build.h
detect-engine-alert.h
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:767
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1486
detect-byte.h
DetectEngineCtx_::byte_extract_max_local_id
int32_t byte_extract_max_local_id
Definition: detect.h:1069
DETECT_PCRE
@ DETECT_PCRE
Definition: detect-engine-register.h:80
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2300
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1333
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:329
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:354
DETECT_SM_LIST_NOTSET
#define DETECT_SM_LIST_NOTSET
Definition: detect.h:144
DETECT_BYTETEST
@ DETECT_BYTETEST
Definition: detect-engine-register.h:91
BYTE_LITTLE_ENDIAN
#define BYTE_LITTLE_ENDIAN
Definition: util-byte.h:30
Packet_::flow
struct Flow_ * flow
Definition: decode.h:564
DetectByteMathDoMatch
int DetectByteMathDoMatch(DetectEngineThreadCtx *det_ctx, const DetectByteMathData *data, const Signature *s, const uint8_t *payload, const uint32_t payload_len, uint8_t nbytes, uint64_t rvalue, uint64_t *value, uint8_t endian)
Definition: detect-bytemath.c:90
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
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1554
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:360
DETECT_BYTEMATH_BASE_DEFAULT
#define DETECT_BYTEMATH_BASE_DEFAULT
Definition: detect-bytemath.c:62
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3871
util-spm.h
detect-engine-buffer.h
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:997
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
DetectBytemathRegister
void DetectBytemathRegister(void)
Registers the keyword handlers for the "byte_math" keyword.
Definition: detect-bytemath.c:71
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:667
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SigMatchListSMBelongsTo
int SigMatchListSMBelongsTo(const Signature *s, const SigMatch *key_sm)
Definition: detect-parse.c:795
SCFree
#define SCFree(p)
Definition: util-mem.h:61
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:472
Flow_::alstate
void * alstate
Definition: flow.h:480
DETECT_BYTE_EXTRACT
@ DETECT_BYTE_EXTRACT
Definition: detect-engine-register.h:94
detect-parse.h
Signature_
Signature container.
Definition: detect.h:688
SigMatch_
a single match condition for a signature
Definition: detect.h:359
payload_len
uint16_t payload_len
Definition: stream-tcp-private.h:1
DETECT_ISDATAAT
@ DETECT_ISDATAAT
Definition: detect-engine-register.h:102
DETECT_SM_LIST_MAX
@ DETECT_SM_LIST_MAX
Definition: detect.h:135
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2839
DETECT_PCRE_RELATIVE_NEXT
#define DETECT_PCRE_RELATIVE_NEXT
Definition: detect-pcre.h:34
app-layer-protos.h
DetectPcreData_
Definition: detect-pcre.h:48
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DETECT_BYTEMATH
@ DETECT_BYTEMATH
Definition: detect-engine-register.h:93
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:989
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:60
DetectByteRetrieveSMVar
bool DetectByteRetrieveSMVar(const char *arg, const Signature *s, int sm_list, DetectByteIndexType *index)
Used to retrieve args from BM.
Definition: detect-byte.c:41
flow.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:451
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
DetectBufferGetActiveList
int DetectBufferGetActiveList(DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine-buffer.c:109
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:668
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1429
flow-var.h
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:119
DETECT_CONTENT_WITHIN
#define DETECT_CONTENT_WITHIN
Definition: detect-content.h:31
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1513
detect-bytemath.h