suricata
detect-flowbits.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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 Victor Julien <victor@inliniac.net>
22  * \author Breno Silva <breno.silva@gmail.com>
23  *
24  * Implements the flowbits keyword
25  */
26 
27 #include "suricata-common.h"
28 #include "decode.h"
29 #include "detect.h"
30 #include "threads.h"
31 #include "flow.h"
32 #include "flow-bit.h"
33 #include "flow-util.h"
34 #include "detect-flowbits.h"
35 #include "util-spm.h"
36 
37 #include "app-layer-parser.h"
38 
39 #include "detect-parse.h"
40 #include "detect-engine.h"
41 #include "detect-engine-mpm.h"
42 #include "detect-engine-state.h"
43 #include "detect-engine-build.h"
44 
45 #include "util-var-name.h"
46 #include "util-unittest.h"
47 #include "util-debug.h"
48 #include "util-conf.h"
49 
50 #define PARSE_REGEX "^([a-z]+)(?:,\\s*(.*))?"
51 static DetectParseRegex parse_regex;
52 
53 #define MAX_TOKENS 100
54 
56  const Signature *, const SigMatchCtx *);
57 static int DetectFlowbitSetup (DetectEngineCtx *, Signature *, const char *);
58 static int FlowbitOrAddData(DetectEngineCtx *, DetectFlowbitsData *, char *);
59 void DetectFlowbitFree (DetectEngineCtx *, void *);
60 #ifdef UNITTESTS
61 void FlowBitsRegisterTests(void);
62 #endif
63 
65 {
66  sigmatch_table[DETECT_FLOWBITS].name = "flowbits";
67  sigmatch_table[DETECT_FLOWBITS].desc = "operate on flow flag";
68  sigmatch_table[DETECT_FLOWBITS].url = "/rules/flow-keywords.html#flowbits";
70  sigmatch_table[DETECT_FLOWBITS].Setup = DetectFlowbitSetup;
72 #ifdef UNITTESTS
74 #endif
75  /* this is compatible to ip-only signatures */
77 
78  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
79 }
80 
81 static int FlowbitOrAddData(DetectEngineCtx *de_ctx, DetectFlowbitsData *cd, char *arrptr)
82 {
83  char *strarr[MAX_TOKENS];
84  char *token;
85  char *saveptr = NULL;
86  uint8_t i = 0;
87 
88  while ((token = strtok_r(arrptr, "|", &saveptr))) {
89  // Check for leading/trailing spaces in the token
90  while(isspace((unsigned char)*token))
91  token++;
92  if (*token == 0)
93  goto next;
94  char *end = token + strlen(token) - 1;
95  while(end > token && isspace((unsigned char)*end))
96  *(end--) = '\0';
97 
98  // Check for spaces in between the flowbit names
99  if (strchr(token, ' ') != NULL) {
100  SCLogError("Spaces are not allowed in flowbit names.");
101  return -1;
102  }
103 
104  if (i == MAX_TOKENS) {
105  SCLogError("Number of flowbits exceeds "
106  "maximum allowed: %d.",
107  MAX_TOKENS);
108  return -1;
109  }
110  strarr[i++] = token;
111  next:
112  arrptr = NULL;
113  }
114  if (i == 0) {
115  SCLogError("No valid flowbits specified");
116  return -1;
117  }
118 
119  cd->or_list_size = i;
120  cd->or_list = SCCalloc(cd->or_list_size, sizeof(uint32_t));
121  if (unlikely(cd->or_list == NULL))
122  return -1;
123  for (uint8_t j = 0; j < cd->or_list_size ; j++) {
124  cd->or_list[j] = VarNameStoreSetupAdd(strarr[j], VAR_TYPE_FLOW_BIT);
126  }
127 
128  return 1;
129 }
130 
131 static int DetectFlowbitMatchToggle (Packet *p, const DetectFlowbitsData *fd)
132 {
133  if (p->flow == NULL)
134  return 0;
135 
136  FlowBitToggle(p->flow,fd->idx);
137 
138  return 1;
139 }
140 
141 static int DetectFlowbitMatchUnset (Packet *p, const DetectFlowbitsData *fd)
142 {
143  if (p->flow == NULL)
144  return 0;
145 
146  FlowBitUnset(p->flow,fd->idx);
147 
148  return 1;
149 }
150 
151 static int DetectFlowbitMatchSet (Packet *p, const DetectFlowbitsData *fd)
152 {
153  if (p->flow == NULL)
154  return 0;
155 
156  FlowBitSet(p->flow,fd->idx);
157 
158  return 1;
159 }
160 
161 static int DetectFlowbitMatchIsset (Packet *p, const DetectFlowbitsData *fd)
162 {
163  if (p->flow == NULL)
164  return 0;
165  if (fd->or_list_size > 0) {
166  for (uint8_t i = 0; i < fd->or_list_size; i++) {
167  if (FlowBitIsset(p->flow, fd->or_list[i]) == 1)
168  return 1;
169  }
170  return 0;
171  }
172 
173  return FlowBitIsset(p->flow,fd->idx);
174 }
175 
176 static int DetectFlowbitMatchIsnotset (Packet *p, const DetectFlowbitsData *fd)
177 {
178  if (p->flow == NULL)
179  return 0;
180  if (fd->or_list_size > 0) {
181  for (uint8_t i = 0; i < fd->or_list_size; i++) {
182  if (FlowBitIsnotset(p->flow, fd->or_list[i]) == 1)
183  return 1;
184  }
185  return 0;
186  }
187  return FlowBitIsnotset(p->flow,fd->idx);
188 }
189 
190 /*
191  * returns 0: no match
192  * 1: match
193  * -1: error
194  */
195 
197  const Signature *s, const SigMatchCtx *ctx)
198 {
199  const DetectFlowbitsData *fd = (const DetectFlowbitsData *)ctx;
200  if (fd == NULL)
201  return 0;
202 
203  switch (fd->cmd) {
205  return DetectFlowbitMatchIsset(p,fd);
207  return DetectFlowbitMatchIsnotset(p,fd);
209  return DetectFlowbitMatchSet(p,fd);
211  return DetectFlowbitMatchUnset(p,fd);
213  return DetectFlowbitMatchToggle(p,fd);
214  default:
215  SCLogError("unknown cmd %" PRIu32 "", fd->cmd);
216  return 0;
217  }
218 
219  return 0;
220 }
221 
222 static int DetectFlowbitParse(const char *str, char *cmd, int cmd_len, char *name,
223  int name_len)
224 {
225  int count, rc;
226  size_t pcre2len;
227 
228  count = DetectParsePcreExec(&parse_regex, str, 0, 0);
229  if (count != 2 && count != 3) {
230  SCLogError("\"%s\" is not a valid setting for flowbits.", str);
231  return 0;
232  }
233 
234  pcre2len = cmd_len;
235  rc = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)cmd, &pcre2len);
236  if (rc < 0) {
237  SCLogError("pcre2_substring_copy_bynumber failed");
238  return 0;
239  }
240 
241  if (count == 3) {
242  pcre2len = name_len;
243  rc = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)name, &pcre2len);
244  if (rc < 0) {
245  SCLogError("pcre2_substring_copy_bynumber failed");
246  return 0;
247  }
248 
249  /* Trim trailing whitespace. */
250  while (strlen(name) > 0 && isblank(name[strlen(name) - 1])) {
251  name[strlen(name) - 1] = '\0';
252  }
253 
254  if (strchr(name, '|') == NULL) {
255  /* Validate name, spaces are not allowed. */
256  for (size_t i = 0; i < strlen(name); i++) {
257  if (isblank(name[i])) {
258  SCLogError("spaces not allowed in flowbit names");
259  return 0;
260  }
261  }
262  }
263  }
264 
265  return 1;
266 }
267 
268 int DetectFlowbitSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
269 {
270  DetectFlowbitsData *cd = NULL;
271  SigMatch *sm = NULL;
272  uint8_t fb_cmd = 0;
273  char fb_cmd_str[16] = "", fb_name[256] = "";
274 
275  if (!DetectFlowbitParse(rawstr, fb_cmd_str, sizeof(fb_cmd_str), fb_name,
276  sizeof(fb_name))) {
277  return -1;
278  }
279 
280  if (strcmp(fb_cmd_str,"noalert") == 0) {
282  } else if (strcmp(fb_cmd_str,"isset") == 0) {
283  fb_cmd = DETECT_FLOWBITS_CMD_ISSET;
284  } else if (strcmp(fb_cmd_str,"isnotset") == 0) {
286  } else if (strcmp(fb_cmd_str,"set") == 0) {
287  fb_cmd = DETECT_FLOWBITS_CMD_SET;
288  } else if (strcmp(fb_cmd_str,"unset") == 0) {
289  fb_cmd = DETECT_FLOWBITS_CMD_UNSET;
290  } else if (strcmp(fb_cmd_str,"toggle") == 0) {
292  } else {
293  SCLogError("ERROR: flowbits action \"%s\" is not supported.", fb_cmd_str);
294  goto error;
295  }
296 
297  switch (fb_cmd) {
299  if (strlen(fb_name) != 0)
300  goto error;
301  s->flags |= SIG_FLAG_NOALERT;
302  return 0;
308  default:
309  if (strlen(fb_name) == 0)
310  goto error;
311  break;
312  }
313 
314  cd = SCCalloc(1, sizeof(DetectFlowbitsData));
315  if (unlikely(cd == NULL))
316  goto error;
317  if (strchr(fb_name, '|') != NULL) {
318  int retval = FlowbitOrAddData(de_ctx, cd, fb_name);
319  if (retval == -1) {
320  goto error;
321  }
322  cd->cmd = fb_cmd;
323  } else {
326  cd->cmd = fb_cmd;
327  cd->or_list_size = 0;
328  cd->or_list = NULL;
329  SCLogDebug("idx %" PRIu32 ", cmd %s, name %s",
330  cd->idx, fb_cmd_str, strlen(fb_name) ? fb_name : "(none)");
331  }
332  /* Okay so far so good, lets get this into a SigMatch
333  * and put it in the Signature. */
334  sm = SigMatchAlloc();
335  if (sm == NULL)
336  goto error;
337 
338  sm->type = DETECT_FLOWBITS;
339  sm->ctx = (SigMatchCtx *)cd;
340 
341  switch (fb_cmd) {
342  /* case DETECT_FLOWBITS_CMD_NOALERT can't happen here */
343 
346  /* checks, so packet list */
348  break;
349 
353  /* modifiers, only run when entire sig has matched */
355  break;
356 
357  // suppress coverity warning as scan-build-7 warns w/o this.
358  // coverity[deadcode : FALSE]
359  default:
360  goto error;
361  }
362 
363  return 0;
364 
365 error:
366  if (cd != NULL)
368  if (sm != NULL)
369  SCFree(sm);
370  return -1;
371 }
372 
374 {
376  if (fd == NULL)
377  return;
378  if (fd->or_list != NULL)
379  SCFree(fd->or_list);
380  SCFree(fd);
381 }
382 
383 struct FBAnalyze {
386 
387  uint32_t *set_sids;
388  uint32_t set_sids_idx;
389  uint32_t set_sids_size;
390 
391  uint32_t *isset_sids;
392  uint32_t isset_sids_idx;
393  uint32_t isset_sids_size;
394 
395  uint32_t *isnotset_sids;
398 
399  uint32_t *unset_sids;
400  uint32_t unset_sids_idx;
401  uint32_t unset_sids_size;
402 
403  uint32_t *toggle_sids;
404  uint32_t toggle_sids_idx;
406 };
407 
408 extern int rule_engine_analysis_set;
409 static void DetectFlowbitsAnalyzeDump(const DetectEngineCtx *de_ctx,
410  struct FBAnalyze *array, uint32_t elements);
411 
413 {
414  const uint32_t max_fb_id = de_ctx->max_fb_id;
415  if (max_fb_id == 0)
416  return 0;
417 
418 #define MAX_SIDS 8
419  uint32_t array_size = max_fb_id + 1;
420  struct FBAnalyze *array = SCCalloc(array_size, sizeof(struct FBAnalyze));
421 
422  if (array == NULL) {
423  SCLogError("Unable to allocate flowbit analyze array");
424  return -1;
425  }
426 
427  SCLogDebug("fb analyzer array size: %"PRIu64,
428  (uint64_t)(array_size * sizeof(struct FBAnalyze)));
429 
430  /* fill flowbit array, updating counters per sig */
431  for (uint32_t i = 0; i < de_ctx->sig_array_len; i++) {
432  const Signature *s = de_ctx->sig_array[i];
433 
434  /* see if the signature uses stateful matching */
435  bool has_state = (s->init_data->buffer_index != 0);
436 
437  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
438  switch (sm->type) {
439  case DETECT_FLOWBITS:
440  {
441  /* figure out the flowbit action */
442  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
443  // Handle flowbit array in case of ORed flowbits
444  for (uint8_t k = 0; k < fb->or_list_size; k++) {
445  array[fb->or_list[k]].cnts[fb->cmd]++;
446  if (has_state)
447  array[fb->or_list[k]].state_cnts[fb->cmd]++;
448  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
449  if (array[fb->or_list[k]].isset_sids_idx >= array[fb->or_list[k]].isset_sids_size) {
450  uint32_t old_size = array[fb->or_list[k]].isset_sids_size;
451  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
452 
453  void *ptr = SCRealloc(array[fb->or_list[k]].isset_sids, new_size * sizeof(uint32_t));
454  if (ptr == NULL)
455  goto end;
456  array[fb->or_list[k]].isset_sids_size = new_size;
457  array[fb->or_list[k]].isset_sids = ptr;
458  }
459 
460  array[fb->or_list[k]].isset_sids[array[fb->or_list[k]].isset_sids_idx] = s->num;
461  array[fb->or_list[k]].isset_sids_idx++;
462  } else if (fb->cmd == DETECT_FLOWBITS_CMD_ISNOTSET) {
463  if (array[fb->or_list[k]].isnotset_sids_idx >= array[fb->or_list[k]].isnotset_sids_size) {
464  uint32_t old_size = array[fb->or_list[k]].isnotset_sids_size;
465  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
466 
467  void *ptr = SCRealloc(array[fb->or_list[k]].isnotset_sids, new_size * sizeof(uint32_t));
468  if (ptr == NULL)
469  goto end;
470  array[fb->or_list[k]].isnotset_sids_size = new_size;
471  array[fb->or_list[k]].isnotset_sids = ptr;
472  }
473 
474  array[fb->or_list[k]].isnotset_sids[array[fb->or_list[k]].isnotset_sids_idx] = s->num;
475  array[fb->or_list[k]].isnotset_sids_idx++;
476  }
477  }
478  if (fb->or_list_size == 0) {
479  array[fb->idx].cnts[fb->cmd]++;
480  if (has_state)
481  array[fb->idx].state_cnts[fb->cmd]++;
482  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
483  if (array[fb->idx].isset_sids_idx >= array[fb->idx].isset_sids_size) {
484  uint32_t old_size = array[fb->idx].isset_sids_size;
485  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
486 
487  void *ptr = SCRealloc(array[fb->idx].isset_sids, new_size * sizeof(uint32_t));
488  if (ptr == NULL)
489  goto end;
490  array[fb->idx].isset_sids_size = new_size;
491  array[fb->idx].isset_sids = ptr;
492  }
493 
494  array[fb->idx].isset_sids[array[fb->idx].isset_sids_idx] = s->num;
495  array[fb->idx].isset_sids_idx++;
496  } else if (fb->cmd == DETECT_FLOWBITS_CMD_ISNOTSET) {
497  if (array[fb->idx].isnotset_sids_idx >= array[fb->idx].isnotset_sids_size) {
498  uint32_t old_size = array[fb->idx].isnotset_sids_size;
499  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
500 
501  void *ptr = SCRealloc(array[fb->idx].isnotset_sids, new_size * sizeof(uint32_t));
502  if (ptr == NULL)
503  goto end;
504  array[fb->idx].isnotset_sids_size = new_size;
505  array[fb->idx].isnotset_sids = ptr;
506  }
507 
508  array[fb->idx].isnotset_sids[array[fb->idx].isnotset_sids_idx] = s->num;
509  array[fb->idx].isnotset_sids_idx++;
510  }
511  }
512  }
513  }
514  }
515  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_POSTMATCH] ; sm != NULL; sm = sm->next) {
516  switch (sm->type) {
517  case DETECT_FLOWBITS:
518  {
519  /* figure out what flowbit action */
520  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
521  array[fb->idx].cnts[fb->cmd]++;
522  if (has_state)
523  array[fb->idx].state_cnts[fb->cmd]++;
524  if (fb->cmd == DETECT_FLOWBITS_CMD_SET) {
525  if (array[fb->idx].set_sids_idx >= array[fb->idx].set_sids_size) {
526  uint32_t old_size = array[fb->idx].set_sids_size;
527  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
528 
529  void *ptr = SCRealloc(array[fb->idx].set_sids, new_size * sizeof(uint32_t));
530  if (ptr == NULL)
531  goto end;
532  array[fb->idx].set_sids_size = new_size;
533  array[fb->idx].set_sids = ptr;
534  }
535 
536  array[fb->idx].set_sids[array[fb->idx].set_sids_idx] = s->num;
537  array[fb->idx].set_sids_idx++;
538  }
539  else if (fb->cmd == DETECT_FLOWBITS_CMD_UNSET) {
540  if (array[fb->idx].unset_sids_idx >= array[fb->idx].unset_sids_size) {
541  uint32_t old_size = array[fb->idx].unset_sids_size;
542  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
543 
544  void *ptr = SCRealloc(array[fb->idx].unset_sids, new_size * sizeof(uint32_t));
545  if (ptr == NULL)
546  goto end;
547  array[fb->idx].unset_sids_size = new_size;
548  array[fb->idx].unset_sids = ptr;
549  }
550 
551  array[fb->idx].unset_sids[array[fb->idx].unset_sids_idx] = s->num;
552  array[fb->idx].unset_sids_idx++;
553  }
554  else if (fb->cmd == DETECT_FLOWBITS_CMD_TOGGLE) {
555  if (array[fb->idx].toggle_sids_idx >= array[fb->idx].toggle_sids_size) {
556  uint32_t old_size = array[fb->idx].toggle_sids_size;
557  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
558 
559  void *ptr = SCRealloc(array[fb->idx].toggle_sids, new_size * sizeof(uint32_t));
560  if (ptr == NULL)
561  goto end;
562  array[fb->idx].toggle_sids_size = new_size;
563  array[fb->idx].toggle_sids = ptr;
564  }
565 
566  array[fb->idx].toggle_sids[array[fb->idx].toggle_sids_idx] = s->num;
567  array[fb->idx].toggle_sids_idx++;
568  }
569  }
570  }
571  }
572  }
573 
574  /* walk array to see if all bits make sense */
575  for (uint32_t i = 0; i < array_size; i++) {
576  char *varname = VarNameStoreSetupLookup(i, VAR_TYPE_FLOW_BIT);
577  if (varname == NULL)
578  continue;
579 
580  bool to_state = false;
581 
582  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] &&
583  array[i].cnts[DETECT_FLOWBITS_CMD_TOGGLE] == 0 &&
584  array[i].cnts[DETECT_FLOWBITS_CMD_SET] == 0) {
585 
586  const Signature *s = de_ctx->sig_array[array[i].isset_sids[0]];
587  SCLogWarning("flowbit '%s' is checked but not "
588  "set. Checked in %u and %u other sigs",
589  varname, s->id, array[i].isset_sids_idx - 1);
590  }
591  if (array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] &&
592  array[i].state_cnts[DETECT_FLOWBITS_CMD_SET] == 0)
593  {
594  SCLogDebug("flowbit %s/%u: isset in state, set not in state", varname, i);
595  }
596 
597  /* if signature depends on 'stateful' flowbits, then turn the
598  * sig into a stateful sig itself */
599  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] > 0 &&
600  array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] == 0 &&
602  {
603  SCLogDebug("flowbit %s/%u: isset not in state, set in state", varname, i);
604  to_state = true;
605  }
606 
607  SCLogDebug("ALL flowbit %s/%u: sets %u toggles %u unsets %u isnotsets %u issets %u", varname, i,
610  array[i].cnts[DETECT_FLOWBITS_CMD_ISSET]);
611  SCLogDebug("STATE flowbit %s/%u: sets %u toggles %u unsets %u isnotsets %u issets %u", varname, i,
615  for (uint32_t x = 0; x < array[i].set_sids_idx; x++) {
616  SCLogDebug("SET flowbit %s/%u: SID %u", varname, i,
617  de_ctx->sig_array[array[i].set_sids[x]]->id);
618  }
619  for (uint32_t x = 0; x < array[i].isset_sids_idx; x++) {
620  Signature *s = de_ctx->sig_array[array[i].isset_sids[x]];
621  SCLogDebug("GET flowbit %s/%u: SID %u", varname, i, s->id);
622 
623  if (to_state) {
625  SCLogDebug("made SID %u stateful because it depends on "
626  "stateful rules that set flowbit %s", s->id, varname);
627  }
628  }
629  SCFree(varname);
630  }
631 
633  DetectFlowbitsAnalyzeDump(de_ctx, array, array_size);
634  }
635 
636 end:
637  for (uint32_t i = 0; i < array_size; i++) {
638  SCFree(array[i].set_sids);
639  SCFree(array[i].unset_sids);
640  SCFree(array[i].isset_sids);
641  SCFree(array[i].isnotset_sids);
642  SCFree(array[i].toggle_sids);
643  }
644  SCFree(array);
645 
646  return 0;
647 }
648 
650 static void DetectFlowbitsAnalyzeDump(const DetectEngineCtx *de_ctx,
651  struct FBAnalyze *array, uint32_t elements)
652 {
653  JsonBuilder *js = jb_new_object();
654  if (js == NULL)
655  return;
656 
657  jb_open_array(js, "flowbits");
658  for (uint32_t x = 0; x < elements; x++) {
659  char *varname = VarNameStoreSetupLookup(x, VAR_TYPE_FLOW_BIT);
660  if (varname == NULL)
661  continue;
662 
663  const struct FBAnalyze *e = &array[x];
664 
665  jb_start_object(js);
666  jb_set_string(js, "name", varname);
667  jb_set_uint(js, "internal_id", x);
668  jb_set_uint(js, "set_cnt", e->cnts[DETECT_FLOWBITS_CMD_SET]);
669  jb_set_uint(js, "unset_cnt", e->cnts[DETECT_FLOWBITS_CMD_UNSET]);
670  jb_set_uint(js, "toggle_cnt", e->cnts[DETECT_FLOWBITS_CMD_TOGGLE]);
671  jb_set_uint(js, "isset_cnt", e->cnts[DETECT_FLOWBITS_CMD_ISSET]);
672  jb_set_uint(js, "isnotset_cnt", e->cnts[DETECT_FLOWBITS_CMD_ISNOTSET]);
673 
674  // sets
675  if (e->cnts[DETECT_FLOWBITS_CMD_SET]) {
676  jb_open_array(js, "sets");
677  for (uint32_t i = 0; i < e->set_sids_idx; i++) {
678  const Signature *s = de_ctx->sig_array[e->set_sids[i]];
679  jb_append_uint(js, s->id);
680  }
681  jb_close(js);
682  }
683  // gets
685  jb_open_array(js, "isset");
686  for (uint32_t i = 0; i < e->isset_sids_idx; i++) {
687  const Signature *s = de_ctx->sig_array[e->isset_sids[i]];
688  jb_append_uint(js, s->id);
689  }
690  jb_close(js);
691  }
692  // isnotset
694  jb_open_array(js, "isnotset");
695  for (uint32_t i = 0; i < e->isnotset_sids_idx; i++) {
696  const Signature *s = de_ctx->sig_array[e->isnotset_sids[i]];
697  jb_append_uint(js, s->id);
698  }
699  jb_close(js);
700  }
701  // unset
703  jb_open_array(js, "unset");
704  for (uint32_t i = 0; i < e->unset_sids_idx; i++) {
705  const Signature *s = de_ctx->sig_array[e->unset_sids[i]];
706  jb_append_uint(js, s->id);
707  }
708  jb_close(js);
709  }
710  // toggle
712  jb_open_array(js, "toggle");
713  for (uint32_t i = 0; i < e->toggle_sids_idx; i++) {
714  const Signature *s = de_ctx->sig_array[e->toggle_sids[i]];
715  jb_append_uint(js, s->id);
716  }
717  jb_close(js);
718  }
719  SCFree(varname);
720  jb_close(js);
721  }
722  jb_close(js); // array
723  jb_close(js); // object
724 
725  const char *filename = "flowbits.json";
726  const char *log_dir = ConfigGetLogDirectory();
727  char log_path[PATH_MAX] = "";
728  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
729 
731  FILE *fp = fopen(log_path, "w");
732  if (fp != NULL) {
733  fwrite(jb_ptr(js), jb_len(js), 1, fp);
734  fprintf(fp, "\n");
735  fclose(fp);
736  }
738 
739  jb_free(js);
740 }
741 
742 #ifdef UNITTESTS
743 
744 static int FlowBitsTestParse01(void)
745 {
746  char command[16] = "", name[16] = "";
747 
748  /* Single argument version. */
749  FAIL_IF(!DetectFlowbitParse("noalert", command, sizeof(command), name,
750  sizeof(name)));
751  FAIL_IF(strcmp(command, "noalert") != 0);
752 
753  /* No leading or trailing spaces. */
754  FAIL_IF(!DetectFlowbitParse("set,flowbit", command, sizeof(command), name,
755  sizeof(name)));
756  FAIL_IF(strcmp(command, "set") != 0);
757  FAIL_IF(strcmp(name, "flowbit") != 0);
758 
759  /* Leading space. */
760  FAIL_IF(!DetectFlowbitParse("set, flowbit", command, sizeof(command), name,
761  sizeof(name)));
762  FAIL_IF(strcmp(command, "set") != 0);
763  FAIL_IF(strcmp(name, "flowbit") != 0);
764 
765  /* Trailing space. */
766  FAIL_IF(!DetectFlowbitParse("set,flowbit ", command, sizeof(command), name,
767  sizeof(name)));
768  FAIL_IF(strcmp(command, "set") != 0);
769  FAIL_IF(strcmp(name, "flowbit") != 0);
770 
771  /* Leading and trailing space. */
772  FAIL_IF(!DetectFlowbitParse("set, flowbit ", command, sizeof(command), name,
773  sizeof(name)));
774  FAIL_IF(strcmp(command, "set") != 0);
775  FAIL_IF(strcmp(name, "flowbit") != 0);
776 
777  /* Spaces are not allowed in the name. */
778  FAIL_IF(DetectFlowbitParse("set,namewith space", command, sizeof(command),
779  name, sizeof(name)));
780 
781  PASS;
782 }
783 
784 /**
785  * \test FlowBitsTestSig01 is a test for a valid noalert flowbits option
786  *
787  * \retval 1 on success
788  * \retval 0 on failure
789  */
790 
791 static int FlowBitsTestSig01(void)
792 {
793  Signature *s = NULL;
794  DetectEngineCtx *de_ctx = NULL;
795 
798 
799  de_ctx->flags |= DE_QUIET;
800 
801  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Noalert\"; flowbits:noalert,wrongusage; content:\"GET \"; sid:1;)");
802  FAIL_IF_NOT_NULL(s);
803 
806  PASS;
807 }
808 
809 /**
810  * \test FlowBitsTestSig02 is a test for a valid isset,set,isnotset,unset,toggle flowbits options
811  *
812  * \retval 1 on success
813  * \retval 0 on failure
814  */
815 
816 static int FlowBitsTestSig02(void)
817 {
818  Signature *s = NULL;
819  ThreadVars th_v;
820  DetectEngineCtx *de_ctx = NULL;
821 
822  memset(&th_v, 0, sizeof(th_v));
823 
826 
827  de_ctx->flags |= DE_QUIET;
828 
829  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"isset rule need an option\"; flowbits:isset; content:\"GET \"; sid:1;)");
830  FAIL_IF_NOT_NULL(s);
831 
832  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"isnotset rule need an option\"; flowbits:isnotset; content:\"GET \"; sid:2;)");
833  FAIL_IF_NOT_NULL(s);
834 
835  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"set rule need an option\"; flowbits:set; content:\"GET \"; sid:3;)");
836  FAIL_IF_NOT_NULL(s);
837 
838  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"unset rule need an option\"; flowbits:unset; content:\"GET \"; sid:4;)");
839  FAIL_IF_NOT_NULL(s);
840 
841  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"toggle rule need an option\"; flowbits:toggle; content:\"GET \"; sid:5;)");
842  FAIL_IF_NOT_NULL(s);
843 
844  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"!set is not an option\"; flowbits:!set,myerr; content:\"GET \"; sid:6;)");
845  FAIL_IF_NOT_NULL(s);
846 
849 
850  PASS;
851 }
852 
853 /**
854  * \test FlowBitsTestSig03 is a test for a invalid flowbits option
855  *
856  * \retval 1 on success
857  * \retval 0 on failure
858  */
859 
860 static int FlowBitsTestSig03(void)
861 {
862  Signature *s = NULL;
863  DetectEngineCtx *de_ctx = NULL;
864 
867 
868  de_ctx->flags |= DE_QUIET;
869 
870  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Unknown cmd\"; flowbits:wrongcmd; content:\"GET \"; sid:1;)");
871  FAIL_IF_NOT_NULL(s);
872 
875  PASS;
876 }
877 
878 /**
879  * \test FlowBitsTestSig04 is a test check idx value
880  *
881  * \retval 1 on success
882  * \retval 0 on failure
883  */
884 
885 static int FlowBitsTestSig04(void)
886 {
887  Signature *s = NULL;
888  DetectEngineCtx *de_ctx = NULL;
889  int idx = 0;
892 
893  de_ctx->flags |= DE_QUIET;
894 
895  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"isset option\"; flowbits:isset,fbt; content:\"GET \"; sid:1;)");
896  FAIL_IF_NULL(s);
897 
899  FAIL_IF(idx != 1);
900 
903  PASS;
904 }
905 
906 /**
907  * \test FlowBitsTestSig05 is a test check noalert flag
908  *
909  * \retval 1 on success
910  * \retval 0 on failure
911  */
912 
913 static int FlowBitsTestSig05(void)
914 {
915  Signature *s = NULL;
916  DetectEngineCtx *de_ctx = NULL;
917 
920 
921  de_ctx->flags |= DE_QUIET;
922 
923  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Noalert\"; flowbits:noalert; content:\"GET \"; sid:1;)");
924  FAIL_IF_NULL(s);
926 
929  PASS;
930 }
931 
932 /**
933  * \test FlowBitsTestSig06 is a test set flowbits option
934  *
935  * \retval 1 on success
936  * \retval 0 on failure
937  */
938 
939 static int FlowBitsTestSig06(void)
940 {
941  uint8_t *buf = (uint8_t *)
942  "GET /one/ HTTP/1.1\r\n"
943  "Host: one.example.org\r\n"
944  "\r\n";
945  uint16_t buflen = strlen((char *)buf);
946  Packet *p = PacketGetFromAlloc();
947  FAIL_IF_NULL(p);
948  Signature *s = NULL;
949  ThreadVars th_v;
950  DetectEngineThreadCtx *det_ctx = NULL;
951  DetectEngineCtx *de_ctx = NULL;
952  Flow f;
953  GenericVar flowvar, *gv = NULL;
954  int result = 0;
955  uint32_t idx = 0;
956 
957  memset(&th_v, 0, sizeof(th_v));
958  memset(&f, 0, sizeof(Flow));
959  memset(&flowvar, 0, sizeof(GenericVar));
960 
961  FLOW_INITIALIZE(&f);
962  p->flow = &f;
963  p->flow->flowvar = &flowvar;
964 
965  p->src.family = AF_INET;
966  p->dst.family = AF_INET;
967  p->payload = buf;
968  p->payload_len = buflen;
969  p->proto = IPPROTO_TCP;
970  p->flags |= PKT_HAS_FLOW;
972 
975 
976  de_ctx->flags |= DE_QUIET;
977 
978  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow; sid:10;)");
979  FAIL_IF_NULL(s);
980 
981  idx = VarNameStoreSetupAdd("myflow", VAR_TYPE_FLOW_BIT);
983  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
984 
985  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
986 
987  gv = p->flow->flowvar;
988  FAIL_IF_NULL(gv);
989  for ( ; gv != NULL; gv = gv->next) {
990  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
991  result = 1;
992  }
993  }
994  FAIL_IF_NOT(result);
995 
996  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
998 
999  FLOW_DESTROY(&f);
1000 
1001  SCFree(p);
1002  PASS;
1003 }
1004 
1005 /**
1006  * \test FlowBitsTestSig07 is a test unset flowbits option
1007  *
1008  * \retval 1 on success
1009  * \retval 0 on failure
1010  */
1011 
1012 static int FlowBitsTestSig07(void)
1013 {
1014  uint8_t *buf = (uint8_t *)
1015  "GET /one/ HTTP/1.1\r\n"
1016  "Host: one.example.org\r\n"
1017  "\r\n";
1018  uint16_t buflen = strlen((char *)buf);
1019  Packet *p = PacketGetFromAlloc();
1020  FAIL_IF_NULL(p);
1021  Signature *s = NULL;
1022  ThreadVars th_v;
1023  DetectEngineThreadCtx *det_ctx = NULL;
1024  DetectEngineCtx *de_ctx = NULL;
1025  Flow f;
1026  GenericVar flowvar, *gv = NULL;
1027  int result = 0;
1028  uint32_t idx = 0;
1029 
1030  memset(&th_v, 0, sizeof(th_v));
1031  memset(&f, 0, sizeof(Flow));
1032  memset(&flowvar, 0, sizeof(GenericVar));
1033 
1034  FLOW_INITIALIZE(&f);
1035  p->flow = &f;
1036  p->flow->flowvar = &flowvar;
1037 
1038  p->src.family = AF_INET;
1039  p->dst.family = AF_INET;
1040  p->payload = buf;
1041  p->payload_len = buflen;
1042  p->proto = IPPROTO_TCP;
1043 
1046 
1047  de_ctx->flags |= DE_QUIET;
1048 
1049  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow2; sid:10;)");
1050  FAIL_IF_NULL(s);
1051 
1052  s = s->next = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit unset\"; flowbits:unset,myflow2; sid:11;)");
1053  FAIL_IF_NULL(s);
1054 
1055  idx = VarNameStoreSetupAdd("myflow", VAR_TYPE_FLOW_BIT);
1057  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1058 
1059  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1060 
1061  gv = p->flow->flowvar;
1062  FAIL_IF_NULL(gv);
1063 
1064  for ( ; gv != NULL; gv = gv->next) {
1065  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
1066  result = 1;
1067  }
1068  }
1069  FAIL_IF(result);
1070 
1071  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1073 
1074  FLOW_DESTROY(&f);
1075 
1076  SCFree(p);
1077  PASS;
1078 }
1079 
1080 /**
1081  * \test FlowBitsTestSig08 is a test toggle flowbits option
1082  *
1083  * \retval 1 on success
1084  * \retval 0 on failure
1085  */
1086 
1087 static int FlowBitsTestSig08(void)
1088 {
1089  uint8_t *buf = (uint8_t *)
1090  "GET /one/ HTTP/1.1\r\n"
1091  "Host: one.example.org\r\n"
1092  "\r\n";
1093  uint16_t buflen = strlen((char *)buf);
1094  Packet *p = PacketGetFromAlloc();
1095  if (unlikely(p == NULL))
1096  return 0;
1097  Signature *s = NULL;
1098  ThreadVars th_v;
1099  DetectEngineThreadCtx *det_ctx = NULL;
1100  DetectEngineCtx *de_ctx = NULL;
1101  Flow f;
1102  GenericVar flowvar, *gv = NULL;
1103  int result = 0;
1104  uint32_t idx = 0;
1105 
1106  memset(&th_v, 0, sizeof(th_v));
1107  memset(&f, 0, sizeof(Flow));
1108  memset(&flowvar, 0, sizeof(GenericVar));
1109 
1110  FLOW_INITIALIZE(&f);
1111  p->flow = &f;
1112  p->flow->flowvar = &flowvar;
1113 
1114  p->src.family = AF_INET;
1115  p->dst.family = AF_INET;
1116  p->payload = buf;
1117  p->payload_len = buflen;
1118  p->proto = IPPROTO_TCP;
1119 
1122 
1123  de_ctx->flags |= DE_QUIET;
1124 
1125  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow2; sid:10;)");
1126  FAIL_IF_NULL(s);
1127 
1128  s = s->next = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit unset\"; flowbits:toggle,myflow2; sid:11;)");
1129  FAIL_IF_NULL(s);
1130 
1131  idx = VarNameStoreSetupAdd("myflow", VAR_TYPE_FLOW_BIT);
1133  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1134 
1135  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1136 
1137  gv = p->flow->flowvar;
1138  FAIL_IF_NULL(gv);
1139 
1140  for ( ; gv != NULL; gv = gv->next) {
1141  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
1142  result = 1;
1143  }
1144  }
1145  FAIL_IF(result);
1146 
1147  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1149 
1150  FLOW_DESTROY(&f);
1151 
1152  SCFree(p);
1153  PASS;
1154 }
1155 
1156 /**
1157  * \brief this function registers unit tests for FlowBits
1158  */
1160 {
1161  UtRegisterTest("FlowBitsTestParse01", FlowBitsTestParse01);
1162  UtRegisterTest("FlowBitsTestSig01", FlowBitsTestSig01);
1163  UtRegisterTest("FlowBitsTestSig02", FlowBitsTestSig02);
1164  UtRegisterTest("FlowBitsTestSig03", FlowBitsTestSig03);
1165  UtRegisterTest("FlowBitsTestSig04", FlowBitsTestSig04);
1166  UtRegisterTest("FlowBitsTestSig05", FlowBitsTestSig05);
1167  UtRegisterTest("FlowBitsTestSig06", FlowBitsTestSig06);
1168  UtRegisterTest("FlowBitsTestSig07", FlowBitsTestSig07);
1169  UtRegisterTest("FlowBitsTestSig08", FlowBitsTestSig08);
1170 }
1171 #endif /* UNITTESTS */
FBAnalyze::cnts
uint16_t cnts[DETECT_FLOWBITS_CMD_MAX]
Definition: detect-flowbits.c:384
GenericVar_::type
uint8_t type
Definition: util-var.h:49
FBAnalyze::isset_sids
uint32_t * isset_sids
Definition: detect-flowbits.c:391
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:47
SigTableElmt_::url
const char * url
Definition: detect.h:1245
Packet_::proto
uint8_t proto
Definition: decode.h:451
FBAnalyze::isnotset_sids_idx
uint32_t isnotset_sids_idx
Definition: detect-flowbits.c:396
g_flowbits_dump_write_m
SCMutex g_flowbits_dump_write_m
Definition: detect-flowbits.c:649
DetectFlowbitsData_::or_list_size
uint8_t or_list_size
Definition: detect-flowbits.h:39
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:382
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:542
DETECT_FLOWBITS_CMD_MAX
#define DETECT_FLOWBITS_CMD_MAX
Definition: detect-flowbits.h:34
SigTableElmt_::desc
const char * desc
Definition: detect.h:1244
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2547
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1007
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1232
flow-util.h
DetectParseRegex
Definition: detect-parse.h:44
SigTableElmt_::name
const char * name
Definition: detect.h:1242
Signature_::num
SigIntId num
Definition: detect.h:568
FBAnalyze::isnotset_sids
uint32_t * isnotset_sids
Definition: detect-flowbits.c:395
FlowBitsRegisterTests
void FlowBitsRegisterTests(void)
this function registers unit tests for FlowBits
Definition: detect-flowbits.c:1159
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
MAX_SIDS
#define MAX_SIDS
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
FBAnalyze::unset_sids_size
uint32_t unset_sids_size
Definition: detect-flowbits.c:401
MAX_TOKENS
#define MAX_TOKENS
Definition: detect-flowbits.c:53
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
DETECT_FLOWBITS_CMD_ISNOTSET
#define DETECT_FLOWBITS_CMD_ISNOTSET
Definition: detect-flowbits.h:31
Packet_::payload
uint8_t * payload
Definition: decode.h:574
Packet_::flags
uint32_t flags
Definition: decode.h:464
threads.h
Flow_
Flow data structure.
Definition: flow.h:343
DetectFlowbitsAnalyze
int DetectFlowbitsAnalyze(DetectEngineCtx *de_ctx)
Definition: detect-flowbits.c:412
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1236
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:801
VarNameStoreSetupLookup
char * VarNameStoreSetupLookup(uint32_t idx, const enum VarTypes type)
Definition: util-var-name.c:332
DetectFlowbitsData_::cmd
uint8_t cmd
Definition: detect-flowbits.h:38
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2588
flow-bit.h
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:218
util-var-name.h
DE_QUIET
#define DE_QUIET
Definition: detect.h:290
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1822
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:512
DETECT_FLOWBITS_CMD_TOGGLE
#define DETECT_FLOWBITS_CMD_TOGGLE
Definition: detect-flowbits.h:29
DETECT_FLOWBITS_CMD_ISSET
#define DETECT_FLOWBITS_CMD_ISSET
Definition: detect-flowbits.h:32
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:460
MAX
#define MAX(x, y)
Definition: suricata-common.h:390
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1227
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:575
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
Signature_::next
struct Signature_ * next
Definition: detect.h:631
FlowBitUnset
void FlowBitUnset(Flow *f, uint32_t idx)
Definition: flow-bit.c:89
DetectFlowbitsData_
Definition: detect-flowbits.h:36
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:89
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
GenericVar_::next
struct GenericVar_ * next
Definition: util-var.h:52
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-flowbits.c:50
DetectEngineThreadCtx_
Definition: detect.h:1032
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2668
FlowBitSet
void FlowBitSet(Flow *f, uint32_t idx)
Definition: flow-bit.c:84
detect-engine-mpm.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
FBAnalyze::toggle_sids_idx
uint32_t toggle_sids_idx
Definition: detect-flowbits.c:404
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:320
FBAnalyze::set_sids_size
uint32_t set_sids_size
Definition: detect-flowbits.c:389
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:79
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:2188
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:319
GenericVar_::idx
uint32_t idx
Definition: util-var.h:51
FBAnalyze::unset_sids
uint32_t * unset_sids
Definition: detect-flowbits.c:399
DetectFlowbitFree
void DetectFlowbitFree(DetectEngineCtx *, void *)
Definition: detect-flowbits.c:373
DetectFlowbitsRegister
void DetectFlowbitsRegister(void)
Definition: detect-flowbits.c:64
Signature_::flags
uint32_t flags
Definition: detect.h:558
FBAnalyze::set_sids
uint32_t * set_sids
Definition: detect-flowbits.c:387
DetectEngineCtx_::max_fb_id
uint32_t max_fb_id
Definition: detect.h:865
Packet_
Definition: decode.h:429
detect-engine-build.h
DetectFlowbitsData_::idx
uint32_t idx
Definition: detect-flowbits.h:37
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:628
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:1210
FBAnalyze::toggle_sids
uint32_t * toggle_sids
Definition: detect-flowbits.c:403
FBAnalyze::isset_sids_idx
uint32_t isset_sids_idx
Definition: detect-flowbits.c:392
detect-flowbits.h
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:267
FlowBitToggle
void FlowBitToggle(Flow *f, uint32_t idx)
Definition: flow-bit.c:94
Flow_::flowvar
GenericVar * flowvar
Definition: flow.h:478
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1934
FBAnalyze::unset_sids_idx
uint32_t unset_sids_idx
Definition: detect-flowbits.c:400
FBAnalyze::toggle_sids_size
uint32_t toggle_sids_size
Definition: detect-flowbits.c:405
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
SIG_FLAG_INIT_STATE_MATCH
#define SIG_FLAG_INIT_STATE_MATCH
Definition: detect.h:256
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:311
util-conf.h
Packet_::flow
struct Flow_ * flow
Definition: decode.h:466
DETECT_FLOWBITS_CMD_NOALERT
#define DETECT_FLOWBITS_CMD_NOALERT
Definition: detect-flowbits.h:33
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3300
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
VAR_TYPE_FLOW_BIT
@ VAR_TYPE_FLOW_BIT
Definition: util-var.h:35
FBAnalyze::state_cnts
uint16_t state_cnts[DETECT_FLOWBITS_CMD_MAX]
Definition: detect-flowbits.c:385
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3514
SigMatch_::type
uint16_t type
Definition: detect.h:317
GenericVar_
Definition: util-var.h:48
rule_engine_analysis_set
int rule_engine_analysis_set
Definition: detect-engine-loader.c:54
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:77
util-spm.h
VarNameStoreSetupAdd
uint32_t VarNameStoreSetupAdd(const char *name, const enum VarTypes type)
add to staging or return existing id if already in there
Definition: util-var-name.c:323
FBAnalyze::set_sids_idx
uint32_t set_sids_idx
Definition: detect-flowbits.c:388
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:806
SIG_FLAG_NOALERT
#define SIG_FLAG_NOALERT
Definition: detect.h:207
DETECT_FLOWBITS
@ DETECT_FLOWBITS
Definition: detect-engine-register.h:90
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:173
ConfigGetLogDirectory
const char * ConfigGetLogDirectory(void)
Definition: util-conf.c:37
FlowBitIsset
int FlowBitIsset(Flow *f, uint32_t idx)
Definition: flow-bit.c:104
str
#define str(s)
Definition: suricata-common.h:286
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
FBAnalyze::isset_sids_size
uint32_t isset_sids_size
Definition: detect-flowbits.c:393
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectFlowbitsData_::or_list
uint32_t * or_list
Definition: detect-flowbits.h:40
Signature_::id
uint32_t id
Definition: detect.h:591
DETECT_FLOWBITS_CMD_UNSET
#define DETECT_FLOWBITS_CMD_UNSET
Definition: detect-flowbits.h:30
detect-parse.h
Signature_
Signature container.
Definition: detect.h:557
SigMatch_
a single match condition for a signature
Definition: detect.h:316
FBAnalyze::isnotset_sids_size
uint32_t isnotset_sids_size
Definition: detect-flowbits.c:397
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2549
DetectFlowbitMatch
int DetectFlowbitMatch(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect-flowbits.c:196
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:815
Address_::family
char family
Definition: decode.h:116
Packet_::dst
Address dst
Definition: decode.h:434
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:848
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineCtx_::sig_array_len
uint32_t sig_array_len
Definition: detect.h:817
FBAnalyze
Definition: detect-flowbits.c:383
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:548
SIGMATCH_IPONLY_COMPAT
#define SIGMATCH_IPONLY_COMPAT
Definition: detect.h:1429
SCMutex
#define SCMutex
Definition: threads-debug.h:114
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:127
Packet_::src
Address src
Definition: decode.h:433
FlowBitIsnotset
int FlowBitIsnotset(Flow *f, uint32_t idx)
Definition: flow-bit.c:116
DETECT_FLOWBITS_CMD_SET
#define DETECT_FLOWBITS_CMD_SET
Definition: detect-flowbits.h:28
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1234