suricata
detect-bytemath.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-2022 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  val >>= rvalue;
193  break;
194  }
195 
196  det_ctx->buffer_offset = (uint32_t)(ptr - payload);
197 
198  if (data->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
199  val &= data->bitmask_val;
200  if (val && data->bitmask_shift_count) {
201  val = val >> data->bitmask_shift_count;
202  }
203  }
204 
205  *value = val;
206  return 1;
207 }
208 
209 /**
210  * \internal
211  * \brief Used to parse byte_math arg.
212  *
213  * \param arg The argument to parse.
214  * \param rvalue May be NULL. When non-null, will contain the variable
215  * name of rvalue (iff rvalue is not a scalar value)
216  *
217  * \retval bmd On success an instance containing the parsed data.
218  * On failure, NULL.
219  */
220 static DetectByteMathData *DetectByteMathParse(
221  DetectEngineCtx *de_ctx, const char *arg, char **nbytes, char **rvalue)
222 {
223  DetectByteMathData *bmd;
224  if ((bmd = SCByteMathParse(arg)) == NULL) {
225  SCLogError("invalid bytemath values");
226  return NULL;
227  }
228 
229  if (bmd->nbytes_str) {
230  if (nbytes == NULL) {
231  SCLogError("byte_math supplied with "
232  "var name for nbytes. \"nbytes\" argument supplied to "
233  "this function must be non-NULL");
234  goto error;
235  }
236  *nbytes = SCStrdup(bmd->nbytes_str);
237  if (*nbytes == NULL) {
238  goto error;
239  }
240  }
241 
242  if (bmd->rvalue_str) {
243  if (rvalue == NULL) {
244  SCLogError("byte_math supplied with "
245  "var name for rvalue. \"rvalue\" argument supplied to "
246  "this function must be non-NULL");
247  goto error;
248  }
249  *rvalue = SCStrdup(bmd->rvalue_str);
250  if (*rvalue == NULL) {
251  goto error;
252  }
253  }
254 
255  if (bmd->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
256  if (bmd->bitmask_val) {
257  uint32_t bmask = bmd->bitmask_val;
258  while (!(bmask & 0x1)){
259  bmask = bmask >> 1;
260  bmd->bitmask_shift_count++;
261  }
262  }
263  }
264 
265  return bmd;
266 
267  error:
268  if (bmd != NULL)
269  DetectByteMathFree(de_ctx, bmd);
270  return NULL;
271 }
272 
273 /**
274  * \brief The setup function for the byte_math keyword for a signature.
275  *
276  * \param de_ctx Pointer to the detection engine context.
277  * \param s Pointer to signature for the current Signature being parsed
278  * from the rules.
279  * \param arg Pointer to the string holding the keyword value.
280  *
281  * \retval 0 On success.
282  * \retval -1 On failure.
283  */
284 static int DetectByteMathSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
285 {
286  SigMatch *prev_pm = NULL;
287  DetectByteMathData *data;
288  char *rvalue = NULL;
289  char *nbytes = NULL;
290  int ret = -1;
291 
292  data = DetectByteMathParse(de_ctx, arg, &nbytes, &rvalue);
293  if (data == NULL)
294  goto error;
295 
296  int sm_list;
297  if (s->init_data->list != DETECT_SM_LIST_NOTSET) {
298  if (DetectBufferGetActiveList(de_ctx, s) == -1)
299  goto error;
300 
301  sm_list = s->init_data->list;
302 
303  if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
305  if (!prev_pm) {
306  SCLogError("relative specified without "
307  "previous pattern match");
308  goto error;
309  }
310  }
311  } else if (data->endian == EndianDCE) {
312  if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
315  if (prev_pm == NULL) {
316  sm_list = DETECT_SM_LIST_PMATCH;
317  } else {
318  sm_list = SigMatchListSMBelongsTo(s, prev_pm);
319  if (sm_list < 0)
320  goto error;
321  }
322  } else {
323  sm_list = DETECT_SM_LIST_PMATCH;
324  }
325 
327  goto error;
328 
329  } else if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
332  if (prev_pm == NULL) {
333  sm_list = DETECT_SM_LIST_PMATCH;
334  } else {
335  sm_list = SigMatchListSMBelongsTo(s, prev_pm);
336  if (sm_list < 0)
337  goto error;
338  }
339 
340  } else {
341  sm_list = DETECT_SM_LIST_PMATCH;
342  }
343 
344  if (data->endian == EndianDCE) {
346  goto error;
347 
348  if ((data->flags & DETECT_BYTEMATH_FLAG_STRING) || (data->base == BaseDec) ||
349  (data->base == BaseHex) || (data->base == BaseOct)) {
350  SCLogError("Invalid option. "
351  "A bytemath keyword with dce holds other invalid modifiers.");
352  goto error;
353  }
354  }
355 
356  if (nbytes != NULL) {
357  DetectByteIndexType index;
358  if (!DetectByteRetrieveSMVar(nbytes, s, sm_list, &index)) {
359  SCLogError("unknown byte_ keyword var seen in byte_math - %s", nbytes);
360  goto error;
361  }
362  data->nbytes = index;
363  data->flags |= DETECT_BYTEMATH_FLAG_NBYTES_VAR;
364  SCFree(nbytes);
365  nbytes = NULL;
366  }
367 
368  if (rvalue != NULL) {
369  DetectByteIndexType index;
370  if (!DetectByteRetrieveSMVar(rvalue, s, sm_list, &index)) {
371  SCLogError("unknown byte_ keyword var seen in byte_math - %s", rvalue);
372  goto error;
373  }
374  data->rvalue = index;
375  data->flags |= DETECT_BYTEMATH_FLAG_RVALUE_VAR;
376  SCFree(rvalue);
377  rvalue = NULL;
378  }
379 
380  SigMatch *prev_bmd_sm = DetectGetLastSMByListId(s, sm_list,
381  DETECT_BYTEMATH, -1);
382  if (prev_bmd_sm == NULL) {
383  data->local_id = 0;
384  } else {
385  data->local_id = ((DetectByteMathData *)prev_bmd_sm->ctx)->local_id + 1;
386  }
387  if (data->local_id > de_ctx->byte_extract_max_local_id) {
388  de_ctx->byte_extract_max_local_id = data->local_id;
389  }
390 
391  if (SCSigMatchAppendSMToList(de_ctx, s, DETECT_BYTEMATH, (SigMatchCtx *)data, sm_list) ==
392  NULL) {
393  goto error;
394  }
395 
396  if (!(data->flags & DETECT_BYTEMATH_FLAG_RELATIVE))
397  goto okay;
398 
399  if (prev_pm == NULL)
400  goto okay;
401 
402  if (prev_pm->type == DETECT_CONTENT) {
403  DetectContentData *cd = (DetectContentData *)prev_pm->ctx;
405  } else if (prev_pm->type == DETECT_PCRE) {
406  DetectPcreData *pd = (DetectPcreData *)prev_pm->ctx;
408  }
409 
410  okay:
411  return 0;
412 
413  error:
414  if (rvalue)
415  SCFree(rvalue);
416  if (nbytes)
417  SCFree(nbytes);
418  DetectByteMathFree(de_ctx, data);
419  return ret;
420 }
421 
422 /**
423  * \brief Used to free instances of DetectByteMathractData.
424  *
425  * \param ptr Instance of DetectByteMathData to be freed.
426  */
427 static void DetectByteMathFree(DetectEngineCtx *de_ctx, void *ptr)
428 {
429  SCByteMathFree(ptr);
430 }
431 
432 /**
433  * \brief Lookup the SigMatch for a named byte_math variable.
434  *
435  * \param arg The name of the byte_math variable to lookup.
436  * \param s Pointer the signature to look in.
437  *
438  * \retval A pointer to the SigMatch if found, otherwise NULL.
439  */
440 SigMatch *DetectByteMathRetrieveSMVar(const char *arg, int sm_list, const Signature *s)
441 {
442  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
443  SigMatch *sm = s->init_data->buffers[x].head;
444  while (sm != NULL) {
445  if (sm->type == DETECT_BYTEMATH) {
446  const DetectByteMathData *bmd = (const DetectByteMathData *)sm->ctx;
447  if (strcmp(bmd->result, arg) == 0) {
448  SCLogDebug("Retrieved SM for \"%s\"", arg);
449  return sm;
450  }
451  }
452  sm = sm->next;
453  }
454  }
455 
456  for (int list = 0; list < DETECT_SM_LIST_MAX; list++) {
457  SigMatch *sm = s->init_data->smlists[list];
458  while (sm != NULL) {
459  // Make sure that the linked buffers ore on the same list
460  if (sm->type == DETECT_BYTEMATH && (sm_list == -1 || sm_list == list)) {
461  const DetectByteMathData *bmd = (const DetectByteMathData *)sm->ctx;
462  if (strcmp(bmd->result, arg) == 0) {
463  SCLogDebug("Retrieved SM for \"%s\"", arg);
464  return sm;
465  }
466  }
467  sm = sm->next;
468  }
469  }
470 
471  return NULL;
472 }
473 
474 /*************************************Unittests********************************/
475 #ifdef UNITTESTS
476 #include "detect-engine-alert.h"
477 
478 static int DetectByteMathParseTest01(void)
479 {
480 
481  DetectByteMathData *bmd = DetectByteMathParse(NULL,
482  "bytes 4, offset 2, oper +,"
483  "rvalue 10, result bar",
484  NULL, NULL);
485  FAIL_IF(bmd == NULL);
486 
487  FAIL_IF_NOT(bmd->nbytes == 4);
488  FAIL_IF_NOT(bmd->offset == 2);
489  FAIL_IF_NOT(bmd->oper == Addition);
490  FAIL_IF_NOT(bmd->rvalue == 10);
491  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
494 
495  DetectByteMathFree(NULL, bmd);
496 
497  PASS;
498 }
499 
500 static int DetectByteMathParseTest02(void)
501 {
502  /* bytes value invalid */
503  DetectByteMathData *bmd = DetectByteMathParse(NULL,
504  "bytes 257, offset 2, oper +, "
505  "rvalue 39, result bar",
506  NULL, NULL);
507 
508  FAIL_IF_NOT(bmd == NULL);
509 
510  PASS;
511 }
512 
513 static int DetectByteMathParseTest03(void)
514 {
515  /* bytes value invalid */
516  DetectByteMathData *bmd = DetectByteMathParse(NULL,
517  "bytes 11, offset 2, oper +, "
518  "rvalue 39, result bar",
519  NULL, NULL);
520  FAIL_IF_NOT(bmd == NULL);
521 
522  PASS;
523 }
524 
525 static int DetectByteMathParseTest04(void)
526 {
527  /* offset value invalid */
528  DetectByteMathData *bmd = DetectByteMathParse(NULL,
529  "bytes 4, offset 70000, oper +,"
530  " rvalue 39, result bar",
531  NULL, NULL);
532 
533  FAIL_IF_NOT(bmd == NULL);
534 
535  PASS;
536 }
537 
538 static int DetectByteMathParseTest05(void)
539 {
540  /* oper value invalid */
541  DetectByteMathData *bmd = DetectByteMathParse(NULL,
542  "bytes 11, offset 16, oper &,"
543  "rvalue 39, result bar",
544  NULL, NULL);
545  FAIL_IF_NOT(bmd == NULL);
546 
547  PASS;
548 }
549 
550 static int DetectByteMathParseTest06(void)
551 {
552  uint8_t flags = DETECT_BYTEMATH_FLAG_RELATIVE;
553  char *rvalue = NULL;
554 
555  DetectByteMathData *bmd = DetectByteMathParse(NULL,
556  "bytes 4, offset 0, oper +,"
557  "rvalue 248, result var, relative",
558  NULL, &rvalue);
559 
560  FAIL_IF(bmd == NULL);
561  FAIL_IF_NOT(bmd->nbytes == 4);
562  FAIL_IF_NOT(bmd->offset == 0);
563  FAIL_IF_NOT(bmd->oper == Addition);
564  FAIL_IF_NOT(bmd->rvalue == 248);
565  FAIL_IF_NOT(strcmp(bmd->result, "var") == 0);
566  FAIL_IF_NOT(bmd->flags == flags);
569 
570  DetectByteMathFree(NULL, bmd);
571 
572  PASS;
573 }
574 
575 static int DetectByteMathParseTest07(void)
576 {
577  char *rvalue = NULL;
578 
579  DetectByteMathData *bmd = DetectByteMathParse(NULL,
580  "bytes 4, offset 2, oper +,"
581  "rvalue foo, result bar",
582  NULL, &rvalue);
583  FAIL_IF_NOT(rvalue);
584  FAIL_IF_NOT(bmd->nbytes == 4);
585  FAIL_IF_NOT(bmd->offset == 2);
586  FAIL_IF_NOT(bmd->oper == Addition);
587  FAIL_IF_NOT(strcmp(rvalue, "foo") == 0);
588  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
591 
592  DetectByteMathFree(NULL, bmd);
593 
594  SCFree(rvalue);
595 
596  PASS;
597 }
598 
599 static int DetectByteMathParseTest08(void)
600 {
601  /* ensure Parse checks the pointer value when rvalue is a var */
602  DetectByteMathData *bmd = DetectByteMathParse(NULL,
603  "bytes 4, offset 2, oper +,"
604  "rvalue foo, result bar",
605  NULL, NULL);
606  FAIL_IF_NOT(bmd == NULL);
607 
608  PASS;
609 }
610 
611 static int DetectByteMathParseTest09(void)
612 {
613  uint8_t flags = DETECT_BYTEMATH_FLAG_RELATIVE;
614 
615  DetectByteMathData *bmd = DetectByteMathParse(NULL,
616  "bytes 4, offset 2, oper +,"
617  "rvalue 39, result bar, relative",
618  NULL, NULL);
619  FAIL_IF(bmd == NULL);
620 
621  FAIL_IF_NOT(bmd->nbytes == 4);
622  FAIL_IF_NOT(bmd->offset == 2);
623  FAIL_IF_NOT(bmd->oper == Addition);
624  FAIL_IF_NOT(bmd->rvalue == 39);
625  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
626  FAIL_IF_NOT(bmd->flags == flags);
629 
630  DetectByteMathFree(NULL, bmd);
631 
632  PASS;
633 }
634 
635 static int DetectByteMathParseTest10(void)
636 {
637  uint8_t flags = DETECT_BYTEMATH_FLAG_ENDIAN;
638 
639  DetectByteMathData *bmd = DetectByteMathParse(NULL,
640  "bytes 4, offset 2, oper +,"
641  "rvalue 39, result bar, endian"
642  " big",
643  NULL, NULL);
644 
645  FAIL_IF(bmd == NULL);
646  FAIL_IF_NOT(bmd->nbytes == 4);
647  FAIL_IF_NOT(bmd->offset == 2);
648  FAIL_IF_NOT(bmd->oper == Addition);
649  FAIL_IF_NOT(bmd->rvalue == 39);
650  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
651  FAIL_IF_NOT(bmd->flags == flags);
652  FAIL_IF_NOT(bmd->endian == BigEndian);
654 
655  DetectByteMathFree(NULL, bmd);
656 
657  PASS;
658 }
659 
660 static int DetectByteMathParseTest11(void)
661 {
662  uint8_t flags = DETECT_BYTEMATH_FLAG_ENDIAN;
663 
664  DetectByteMathData *bmd = DetectByteMathParse(NULL,
665  "bytes 4, offset 2, oper +, "
666  "rvalue 39, result bar, dce",
667  NULL, NULL);
668 
669  FAIL_IF(bmd == NULL);
670  FAIL_IF_NOT(bmd->nbytes == 4);
671  FAIL_IF_NOT(bmd->offset == 2);
672  FAIL_IF_NOT(bmd->oper == Addition);
673  FAIL_IF_NOT(bmd->rvalue == 39);
674  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
675  FAIL_IF_NOT(bmd->flags == flags);
676  FAIL_IF_NOT(bmd->endian == EndianDCE);
678 
679  DetectByteMathFree(NULL, bmd);
680 
681  PASS;
682 }
683 
684 static int DetectByteMathParseTest12(void)
685 {
686  uint8_t flags = DETECT_BYTEMATH_FLAG_RELATIVE | DETECT_BYTEMATH_FLAG_STRING;
687 
688  DetectByteMathData *bmd = DetectByteMathParse(NULL,
689  "bytes 4, offset 2, oper +,"
690  "rvalue 39, result bar, "
691  "relative, string dec",
692  NULL, NULL);
693 
694  FAIL_IF(bmd == NULL);
695  FAIL_IF_NOT(bmd->nbytes == 4);
696  FAIL_IF_NOT(bmd->offset == 2);
697  FAIL_IF_NOT(bmd->oper == Addition);
698  FAIL_IF_NOT(bmd->rvalue == 39);
699  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
700  FAIL_IF_NOT(bmd->flags == flags);
701  FAIL_IF_NOT(bmd->endian == BigEndian);
702  FAIL_IF_NOT(bmd->base == BaseDec);
703 
704  DetectByteMathFree(NULL, bmd);
705 
706  PASS;
707 }
708 
709 static int DetectByteMathParseTest13(void)
710 {
711  uint8_t flags = DETECT_BYTEMATH_FLAG_STRING |
712  DETECT_BYTEMATH_FLAG_RELATIVE |
713  DETECT_BYTEMATH_FLAG_BITMASK;
714 
715  DetectByteMathData *bmd = DetectByteMathParse(NULL,
716  "bytes 4, offset 2, oper +, "
717  "rvalue 39, result bar, "
718  "relative, string dec, bitmask "
719  "0x8f40",
720  NULL, NULL);
721 
722  FAIL_IF(bmd == NULL);
723  FAIL_IF_NOT(bmd->nbytes == 4);
724  FAIL_IF_NOT(bmd->offset == 2);
725  FAIL_IF_NOT(bmd->oper == Addition);
726  FAIL_IF_NOT(bmd->rvalue == 39);
727  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
728  FAIL_IF_NOT(bmd->bitmask_val == 0x8f40);
729  FAIL_IF_NOT(bmd->bitmask_shift_count == 6);
730  FAIL_IF_NOT(bmd->flags == flags);
731  FAIL_IF_NOT(bmd->endian == BigEndian);
732  FAIL_IF_NOT(bmd->base == BaseDec);
733 
734  DetectByteMathFree(NULL, bmd);
735 
736  PASS;
737 }
738 
739 
740 static int DetectByteMathParseTest14(void)
741 {
742  /* incomplete */
743  DetectByteMathData *bmd = DetectByteMathParse(NULL,
744  "bytes 4, offset 2, oper +,"
745  "rvalue foo",
746  NULL, NULL);
747 
748  FAIL_IF_NOT(bmd == NULL);
749 
750  PASS;
751 }
752 
753 static int DetectByteMathParseTest15(void)
754 {
755 
756  /* incomplete */
757  DetectByteMathData *bmd = DetectByteMathParse(NULL,
758  "bytes 4, offset 2, oper +, "
759  "result bar",
760  NULL, NULL);
761 
762  FAIL_IF_NOT(bmd == NULL);
763 
764  PASS;
765 }
766 
767 static int DetectByteMathParseTest16(void)
768 {
769  uint8_t flags = DETECT_BYTEMATH_FLAG_STRING | DETECT_BYTEMATH_FLAG_RELATIVE |
770  DETECT_BYTEMATH_FLAG_BITMASK;
771 
772  DetectByteMathData *bmd = DetectByteMathParse(NULL,
773  "bytes 4, offset -2, oper +, "
774  "rvalue 39, result bar, "
775  "relative, string dec, bitmask "
776  "0x8f40",
777  NULL, NULL);
778 
779  FAIL_IF(bmd == NULL);
780  FAIL_IF_NOT(bmd->nbytes == 4);
781  FAIL_IF_NOT(bmd->offset == -2);
782  FAIL_IF_NOT(bmd->oper == Addition);
783  FAIL_IF_NOT(bmd->rvalue == 39);
784  FAIL_IF_NOT(strcmp(bmd->result, "bar") == 0);
785  FAIL_IF_NOT(bmd->bitmask_val == 0x8f40);
786  FAIL_IF_NOT(bmd->bitmask_shift_count == 6);
787  FAIL_IF_NOT(bmd->flags == flags);
788  FAIL_IF_NOT(bmd->endian == BigEndian);
789  FAIL_IF_NOT(bmd->base == BaseDec);
790 
791  DetectByteMathFree(NULL, bmd);
792 
793  PASS;
794 }
795 
796 static int DetectByteMathPacket01(void)
797 {
798  uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01,
799  0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
800  0x00, 0x00, 0x6d, 0x00, 0x01, 0x00 };
801  Flow f;
802  void *dns_state = NULL;
803  Packet *p = NULL;
804  Signature *s = NULL;
805  ThreadVars tv;
806  DetectEngineThreadCtx *det_ctx = NULL;
808 
809  memset(&tv, 0, sizeof(ThreadVars));
811  memset(&f, 0, sizeof(Flow));
812 
813  p = UTHBuildPacketReal(buf, sizeof(buf), IPPROTO_UDP,
814  "192.168.1.5", "192.168.1.1",
815  41424, 53);
816  FAIL_IF_NULL(p);
817 
818  FLOW_INITIALIZE(&f);
819  f.flags |= FLOW_IPV4;
820  f.proto = IPPROTO_UDP;
822 
823  p->flow = &f;
824  p->flags |= PKT_HAS_FLOW;
826  f.alproto = ALPROTO_DNS;
827 
830 
832  de_ctx->flags |= DE_QUIET;
833 
834  /*
835  * byte_extract: Extract 1 byte from offset 0 --> 0x0038
836  * byte_math: Extract 1 byte from offset 2 (0x35)
837  * Add 0x35 + 0x38 = 109 (0x6d)
838  * byte_test: Compare 2 bytes at offset 13 bytes from last
839  * match and compare with 0x6d
840  */
841  s = DetectEngineAppendSig(de_ctx, "alert udp any any -> any any "
842  "(byte_extract: 1, 0, extracted_val, relative;"
843  "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
844  "byte_test: 2, =, var, 13;"
845  "msg:\"Byte extract and byte math with byte test verification\";"
846  "sid:1;)");
847  FAIL_IF_NULL(s);
848 
849  /* this rule should not alert */
850  s = DetectEngineAppendSig(de_ctx, "alert udp any any -> any any "
851  "(byte_extract: 1, 0, extracted_val, relative;"
852  "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
853  "byte_test: 2, !=, var, 13;"
854  "msg:\"Byte extract and byte math with byte test verification\";"
855  "sid:2;)");
856  FAIL_IF_NULL(s);
857 
858  /*
859  * this rule should alert:
860  * compares offset 15 with var ... 1 (offset 15) < 0x6d (var)
861  */
862  s = DetectEngineAppendSig(de_ctx, "alert udp any any -> any any "
863  "(byte_extract: 1, 0, extracted_val, relative;"
864  "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
865  "byte_test: 2, <, var, 15;"
866  "msg:\"Byte extract and byte math with byte test verification\";"
867  "sid:3;)");
868  FAIL_IF_NULL(s);
869 
871  DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
872  FAIL_IF_NULL(det_ctx);
873 
874  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_DNS,
875  STREAM_TOSERVER, buf, sizeof(buf));
876  FAIL_IF_NOT(r == 0);
877 
878  dns_state = f.alstate;
879  FAIL_IF_NULL(dns_state);
880 
881  /* do detect */
882  SigMatchSignatures(&tv, de_ctx, det_ctx, p);
883 
884  /* ensure sids 1 & 3 alerted */
886  FAIL_IF(PacketAlertCheck(p, 2));
888 
890  DetectEngineThreadCtxDeinit(&tv, det_ctx);
892 
893  FLOW_DESTROY(&f);
894  UTHFreePacket(p);
896  PASS;
897 }
898 
899 static int DetectByteMathPacket02(void)
900 {
901  uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
902  0x00, 0x70, 0x00, 0x01, 0x00 };
903  Flow f;
904  void *dns_state = NULL;
905  Packet *p = NULL;
906  Signature *s = NULL;
907  ThreadVars tv;
908  DetectEngineThreadCtx *det_ctx = NULL;
910 
911  memset(&tv, 0, sizeof(ThreadVars));
913  memset(&f, 0, sizeof(Flow));
914 
915  p = UTHBuildPacketReal(buf, sizeof(buf), IPPROTO_UDP, "192.168.1.5", "192.168.1.1", 41424, 53);
916  FAIL_IF_NULL(p);
917 
918  FLOW_INITIALIZE(&f);
919  f.flags |= FLOW_IPV4;
920  f.proto = IPPROTO_UDP;
922 
923  p->flow = &f;
924  p->flags |= PKT_HAS_FLOW;
926  f.alproto = ALPROTO_DNS;
927 
930 
932  de_ctx->flags |= DE_QUIET;
933 
934  /*
935  * byte_extract: Extract 1 byte from offset 0 --> 0x38
936  * byte_math: Extract 1 byte from offset -1 (0x38)
937  * Add 0x38 + 0x38 = 112 (0x70)
938  * byte_test: Compare 2 bytes at offset 13 bytes from last
939  * match and compare with 0x70
940  */
942  "alert udp any any -> any any "
943  "(byte_extract: 1, 0, extracted_val, relative;"
944  "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
945  "byte_test: 2, =, var, 13;"
946  "msg:\"Byte extract and byte math with byte test verification\";"
947  "sid:1;)");
948  FAIL_IF_NULL(s);
949 
950  /* this rule should not alert */
952  "alert udp any any -> any any "
953  "(byte_extract: 1, 0, extracted_val, relative;"
954  "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
955  "byte_test: 2, !=, var, 13;"
956  "msg:\"Byte extract and byte math with byte test verification\";"
957  "sid:2;)");
958  FAIL_IF_NULL(s);
959 
960  /*
961  * this rule should alert:
962  * compares offset 15 with var ... 1 (offset 15) < 0x70 (var)
963  */
965  "alert udp any any -> any any "
966  "(byte_extract: 1, 0, extracted_val, relative;"
967  "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
968  "byte_test: 2, <, var, 15;"
969  "msg:\"Byte extract and byte math with byte test verification\";"
970  "sid:3;)");
971  FAIL_IF_NULL(s);
972 
974  DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
975  FAIL_IF_NULL(det_ctx);
976 
977  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_DNS, STREAM_TOSERVER, buf, sizeof(buf));
978  FAIL_IF_NOT(r == 0);
979 
980  dns_state = f.alstate;
981  FAIL_IF_NULL(dns_state);
982 
983  /* do detect */
984  SigMatchSignatures(&tv, de_ctx, det_ctx, p);
985 
986  /* ensure sids 1 & 3 alerted */
988  FAIL_IF(PacketAlertCheck(p, 2));
990 
992  DetectEngineThreadCtxDeinit(&tv, det_ctx);
994 
995  FLOW_DESTROY(&f);
996  UTHFreePacket(p);
997 
999  PASS;
1000 }
1001 
1002 static int DetectByteMathContext01(void)
1003 {
1004  DetectEngineCtx *de_ctx = NULL;
1005  Signature *s = NULL;
1006  SigMatch *sm = NULL;
1007  DetectContentData *cd = NULL;
1008  DetectByteMathData *bmd = NULL;
1009 
1011  FAIL_IF(de_ctx == NULL);
1012 
1013  de_ctx->flags |= DE_QUIET;
1014  s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
1015  "(msg:\"Testing bytemath_body\"; "
1016  "content:\"|00 04 93 F3|\"; "
1017  "content:\"|00 00 00 07|\"; distance:4; within:4;"
1018  "byte_math:bytes 4, offset 0, oper +, rvalue "
1019  "248, result var, relative; sid:1;)");
1020 
1021  FAIL_IF(de_ctx->sig_list == NULL);
1022 
1024 
1026  FAIL_IF(sm->type != DETECT_CONTENT);
1027  cd = (DetectContentData *)sm->ctx;
1030  FAIL_IF(cd->content_len != 4);
1031 
1032  sm = sm->next;
1033  FAIL_IF(sm->type != DETECT_CONTENT);
1034  sm = sm->next;
1035  FAIL_IF(sm->type != DETECT_BYTEMATH);
1036 
1037  FAIL_IF(sm->ctx == NULL);
1038 
1039  bmd = (DetectByteMathData *)sm->ctx;
1040  FAIL_IF_NOT(bmd->nbytes == 4);
1041  FAIL_IF_NOT(bmd->offset == 0);
1042  FAIL_IF_NOT(bmd->rvalue == 248);
1043  FAIL_IF_NOT(strcmp(bmd->result, "var") == 0);
1044  FAIL_IF_NOT(bmd->flags == DETECT_BYTEMATH_FLAG_RELATIVE);
1045  FAIL_IF_NOT(bmd->endian == BigEndian);
1046  FAIL_IF_NOT(bmd->oper == Addition);
1047  FAIL_IF_NOT(bmd->base == BaseDec);
1048 
1050 
1051  PASS;
1052 }
1053 
1054 static void DetectByteMathRegisterTests(void)
1055 {
1056  UtRegisterTest("DetectByteMathParseTest01", DetectByteMathParseTest01);
1057  UtRegisterTest("DetectByteMathParseTest02", DetectByteMathParseTest02);
1058  UtRegisterTest("DetectByteMathParseTest03", DetectByteMathParseTest03);
1059  UtRegisterTest("DetectByteMathParseTest04", DetectByteMathParseTest04);
1060  UtRegisterTest("DetectByteMathParseTest05", DetectByteMathParseTest05);
1061  UtRegisterTest("DetectByteMathParseTest06", DetectByteMathParseTest06);
1062  UtRegisterTest("DetectByteMathParseTest07", DetectByteMathParseTest07);
1063  UtRegisterTest("DetectByteMathParseTest08", DetectByteMathParseTest08);
1064  UtRegisterTest("DetectByteMathParseTest09", DetectByteMathParseTest09);
1065  UtRegisterTest("DetectByteMathParseTest10", DetectByteMathParseTest10);
1066  UtRegisterTest("DetectByteMathParseTest11", DetectByteMathParseTest11);
1067  UtRegisterTest("DetectByteMathParseTest12", DetectByteMathParseTest12);
1068  UtRegisterTest("DetectByteMathParseTest13", DetectByteMathParseTest13);
1069  UtRegisterTest("DetectByteMathParseTest14", DetectByteMathParseTest14);
1070  UtRegisterTest("DetectByteMathParseTest15", DetectByteMathParseTest15);
1071  UtRegisterTest("DetectByteMathParseTest16", DetectByteMathParseTest16);
1072  UtRegisterTest("DetectByteMathPacket01", DetectByteMathPacket01);
1073  UtRegisterTest("DetectByteMathPacket02", DetectByteMathPacket02);
1074  UtRegisterTest("DetectByteMathContext01", DetectByteMathContext01);
1075 }
1076 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1461
DETECT_CONTENT_RELATIVE_NEXT
#define DETECT_CONTENT_RELATIVE_NEXT
Definition: detect-content.h:66
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:534
detect-content.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1269
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:642
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
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:396
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1268
ALPROTO_DCERPC
@ ALPROTO_DCERPC
Definition: app-layer-protos.h:44
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1445
flow-util.h
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:47
SigTableElmt_::name
const char * name
Definition: detect.h:1458
SignatureInitData_::smlists_tail
struct SigMatch_ * smlists_tail[DETECT_SM_LIST_MAX]
Definition: detect.h:644
DETECT_BYTEJUMP
@ DETECT_BYTEJUMP
Definition: detect-engine-register.h:83
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:69
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
Flow_::proto
uint8_t proto
Definition: flow.h:369
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:143
Packet_::flags
uint32_t flags
Definition: decode.h:547
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:563
Flow_
Flow data structure.
Definition: flow.h:347
Flow_::protomap
uint8_t protomap
Definition: flow.h:438
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2684
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:324
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:224
rust.h
DE_QUIET
#define DE_QUIET
Definition: detect.h:329
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:2420
DetectContentData_
Definition: detect-content.h:93
DetectPcreData_::flags
uint16_t flags
Definition: detect-pcre.h:51
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2235
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:3446
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:532
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:260
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
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:99
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:657
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:440
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:38
decode.h
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:19
DetectEngineThreadCtx_
Definition: detect.h:1245
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:24
SignatureInitData_::list
int list
Definition: detect.h:628
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:387
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:3414
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:359
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:936
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:3104
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:358
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:99
Packet_
Definition: decode.h:501
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:747
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:1420
detect-byte.h
DetectEngineCtx_::byte_extract_max_local_id
int32_t byte_extract_max_local_id
Definition: detect.h:1011
DETECT_PCRE
@ DETECT_PCRE
Definition: detect-engine-register.h:71
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2207
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1331
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:297
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:350
DETECT_SM_LIST_NOTSET
#define DETECT_SM_LIST_NOTSET
Definition: detect.h:144
DETECT_BYTETEST
@ DETECT_BYTETEST
Definition: detect-engine-register.h:82
BYTE_LITTLE_ENDIAN
#define BYTE_LITTLE_ENDIAN
Definition: util-byte.h:30
Packet_::flow
struct Flow_ * flow
Definition: decode.h:549
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:1297
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:356
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:3651
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:942
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
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:647
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:762
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:473
Flow_::alstate
void * alstate
Definition: flow.h:472
DETECT_BYTE_EXTRACT
@ DETECT_BYTE_EXTRACT
Definition: detect-engine-register.h:85
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
SigMatch_
a single match condition for a signature
Definition: detect.h:355
payload_len
uint16_t payload_len
Definition: stream-tcp-private.h:1
DETECT_ISDATAAT
@ DETECT_ISDATAAT
Definition: detect-engine-register.h:93
DETECT_SM_LIST_MAX
@ DETECT_SM_LIST_MAX
Definition: detect.h:135
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2645
DETECT_PCRE_RELATIVE_NEXT
#define DETECT_PCRE_RELATIVE_NEXT
Definition: detect-pcre.h:34
app-layer-protos.h
DetectPcreData_
Definition: detect-pcre.h:47
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DETECT_BYTEMATH
@ DETECT_BYTEMATH
Definition: detect-engine-register.h:84
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:935
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:443
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:648
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1427
flow-var.h
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
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:1447
detect-bytemath.h