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] = VarNameStoreRegister(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 rc;
226  size_t pcre2len;
227  pcre2_match_data *match = NULL;
228 
229  int count = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
230  if (count != 2 && count != 3) {
231  SCLogError("\"%s\" is not a valid setting for flowbits.", str);
232  goto error;
233  }
234 
235  pcre2len = cmd_len;
236  rc = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)cmd, &pcre2len);
237  if (rc < 0) {
238  SCLogError("pcre2_substring_copy_bynumber failed");
239  goto error;
240  }
241 
242  if (count == 3) {
243  pcre2len = name_len;
244  rc = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)name, &pcre2len);
245  if (rc < 0) {
246  SCLogError("pcre2_substring_copy_bynumber failed");
247  goto error;
248  }
249 
250  /* Trim trailing whitespace. */
251  while (strlen(name) > 0 && isblank(name[strlen(name) - 1])) {
252  name[strlen(name) - 1] = '\0';
253  }
254 
255  if (strchr(name, '|') == NULL) {
256  /* Validate name, spaces are not allowed. */
257  for (size_t i = 0; i < strlen(name); i++) {
258  if (isblank(name[i])) {
259  SCLogError("spaces not allowed in flowbit names");
260  goto error;
261  }
262  }
263  }
264  }
265 
266  pcre2_match_data_free(match);
267  return 1;
268 
269 error:
270  if (match) {
271  pcre2_match_data_free(match);
272  }
273  return 0;
274 }
275 
276 int DetectFlowbitSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
277 {
278  DetectFlowbitsData *cd = NULL;
279  uint8_t fb_cmd = 0;
280  char fb_cmd_str[16] = "", fb_name[256] = "";
281 
282  if (!DetectFlowbitParse(rawstr, fb_cmd_str, sizeof(fb_cmd_str), fb_name,
283  sizeof(fb_name))) {
284  return -1;
285  }
286 
287  if (strcmp(fb_cmd_str,"noalert") == 0) {
288  if (strlen(fb_name) != 0)
289  goto error;
290  s->flags |= SIG_FLAG_NOALERT;
291  return 0;
292  } else if (strcmp(fb_cmd_str,"isset") == 0) {
293  fb_cmd = DETECT_FLOWBITS_CMD_ISSET;
294  } else if (strcmp(fb_cmd_str,"isnotset") == 0) {
296  } else if (strcmp(fb_cmd_str,"set") == 0) {
297  fb_cmd = DETECT_FLOWBITS_CMD_SET;
298  } else if (strcmp(fb_cmd_str,"unset") == 0) {
299  fb_cmd = DETECT_FLOWBITS_CMD_UNSET;
300  } else if (strcmp(fb_cmd_str,"toggle") == 0) {
302  } else {
303  SCLogError("ERROR: flowbits action \"%s\" is not supported.", fb_cmd_str);
304  goto error;
305  }
306 
307  switch (fb_cmd) {
313  default:
314  if (strlen(fb_name) == 0)
315  goto error;
316  break;
317  }
318 
319  cd = SCCalloc(1, sizeof(DetectFlowbitsData));
320  if (unlikely(cd == NULL))
321  goto error;
322  if (strchr(fb_name, '|') != NULL) {
323  int retval = FlowbitOrAddData(de_ctx, cd, fb_name);
324  if (retval == -1) {
325  goto error;
326  }
327  cd->cmd = fb_cmd;
328  } else {
331  cd->cmd = fb_cmd;
332  cd->or_list_size = 0;
333  cd->or_list = NULL;
334  SCLogDebug("idx %" PRIu32 ", cmd %s, name %s",
335  cd->idx, fb_cmd_str, strlen(fb_name) ? fb_name : "(none)");
336  }
337  /* Okay so far so good, lets get this into a SigMatch
338  * and put it in the Signature. */
339 
340  switch (fb_cmd) {
341  /* noalert can't happen here */
344  /* checks, so packet list */
346  DETECT_SM_LIST_MATCH) == NULL) {
347  goto error;
348  }
349  break;
350 
354  /* modifiers, only run when entire sig has matched */
356  DETECT_SM_LIST_POSTMATCH) == NULL) {
357  goto error;
358  }
359  break;
360 
361  // suppress coverity warning as scan-build-7 warns w/o this.
362  // coverity[deadcode : FALSE]
363  default:
364  goto error;
365  }
366 
367  return 0;
368 
369 error:
370  if (cd != NULL)
372  return -1;
373 }
374 
376 {
378  if (fd == NULL)
379  return;
381  if (fd->or_list != NULL) {
382  for (uint8_t i = 0; i < fd->or_list_size; i++) {
384  }
385  SCFree(fd->or_list);
386  }
387  SCFree(fd);
388 }
389 
390 struct FBAnalyze {
393 
394  uint32_t *set_sids;
395  uint32_t set_sids_idx;
396  uint32_t set_sids_size;
397 
398  uint32_t *isset_sids;
399  uint32_t isset_sids_idx;
400  uint32_t isset_sids_size;
401 
402  uint32_t *isnotset_sids;
405 
406  uint32_t *unset_sids;
407  uint32_t unset_sids_idx;
408  uint32_t unset_sids_size;
409 
410  uint32_t *toggle_sids;
411  uint32_t toggle_sids_idx;
413 };
414 
415 extern bool rule_engine_analysis_set;
416 static void DetectFlowbitsAnalyzeDump(const DetectEngineCtx *de_ctx,
417  struct FBAnalyze *array, uint32_t elements);
418 
420 {
421  const uint32_t max_fb_id = de_ctx->max_fb_id;
422  if (max_fb_id == 0)
423  return 0;
424 
425 #define MAX_SIDS 8
426  uint32_t array_size = max_fb_id + 1;
427  struct FBAnalyze *array = SCCalloc(array_size, sizeof(struct FBAnalyze));
428 
429  if (array == NULL) {
430  SCLogError("Unable to allocate flowbit analyze array");
431  return -1;
432  }
433 
434  SCLogDebug("fb analyzer array size: %"PRIu64,
435  (uint64_t)(array_size * sizeof(struct FBAnalyze)));
436 
437  /* fill flowbit array, updating counters per sig */
438  for (uint32_t i = 0; i < de_ctx->sig_array_len; i++) {
439  const Signature *s = de_ctx->sig_array[i];
440 
441  /* see if the signature uses stateful matching */
442  bool has_state = (s->init_data->buffer_index != 0);
443 
444  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
445  switch (sm->type) {
446  case DETECT_FLOWBITS:
447  {
448  /* figure out the flowbit action */
449  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
450  // Handle flowbit array in case of ORed flowbits
451  for (uint8_t k = 0; k < fb->or_list_size; k++) {
452  array[fb->or_list[k]].cnts[fb->cmd]++;
453  if (has_state)
454  array[fb->or_list[k]].state_cnts[fb->cmd]++;
455  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
456  if (array[fb->or_list[k]].isset_sids_idx >= array[fb->or_list[k]].isset_sids_size) {
457  uint32_t old_size = array[fb->or_list[k]].isset_sids_size;
458  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
459 
460  void *ptr = SCRealloc(array[fb->or_list[k]].isset_sids, new_size * sizeof(uint32_t));
461  if (ptr == NULL)
462  goto end;
463  array[fb->or_list[k]].isset_sids_size = new_size;
464  array[fb->or_list[k]].isset_sids = ptr;
465  }
466 
467  array[fb->or_list[k]].isset_sids[array[fb->or_list[k]].isset_sids_idx] = s->num;
468  array[fb->or_list[k]].isset_sids_idx++;
469  } else if (fb->cmd == DETECT_FLOWBITS_CMD_ISNOTSET) {
470  if (array[fb->or_list[k]].isnotset_sids_idx >= array[fb->or_list[k]].isnotset_sids_size) {
471  uint32_t old_size = array[fb->or_list[k]].isnotset_sids_size;
472  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
473 
474  void *ptr = SCRealloc(array[fb->or_list[k]].isnotset_sids, new_size * sizeof(uint32_t));
475  if (ptr == NULL)
476  goto end;
477  array[fb->or_list[k]].isnotset_sids_size = new_size;
478  array[fb->or_list[k]].isnotset_sids = ptr;
479  }
480 
481  array[fb->or_list[k]].isnotset_sids[array[fb->or_list[k]].isnotset_sids_idx] = s->num;
482  array[fb->or_list[k]].isnotset_sids_idx++;
483  }
484  }
485  if (fb->or_list_size == 0) {
486  array[fb->idx].cnts[fb->cmd]++;
487  if (has_state)
488  array[fb->idx].state_cnts[fb->cmd]++;
489  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
490  if (array[fb->idx].isset_sids_idx >= array[fb->idx].isset_sids_size) {
491  uint32_t old_size = array[fb->idx].isset_sids_size;
492  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
493 
494  void *ptr = SCRealloc(array[fb->idx].isset_sids, new_size * sizeof(uint32_t));
495  if (ptr == NULL)
496  goto end;
497  array[fb->idx].isset_sids_size = new_size;
498  array[fb->idx].isset_sids = ptr;
499  }
500 
501  array[fb->idx].isset_sids[array[fb->idx].isset_sids_idx] = s->num;
502  array[fb->idx].isset_sids_idx++;
503  } else if (fb->cmd == DETECT_FLOWBITS_CMD_ISNOTSET) {
504  if (array[fb->idx].isnotset_sids_idx >= array[fb->idx].isnotset_sids_size) {
505  uint32_t old_size = array[fb->idx].isnotset_sids_size;
506  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
507 
508  void *ptr = SCRealloc(array[fb->idx].isnotset_sids, new_size * sizeof(uint32_t));
509  if (ptr == NULL)
510  goto end;
511  array[fb->idx].isnotset_sids_size = new_size;
512  array[fb->idx].isnotset_sids = ptr;
513  }
514 
515  array[fb->idx].isnotset_sids[array[fb->idx].isnotset_sids_idx] = s->num;
516  array[fb->idx].isnotset_sids_idx++;
517  }
518  }
519  }
520  }
521  }
522  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_POSTMATCH] ; sm != NULL; sm = sm->next) {
523  switch (sm->type) {
524  case DETECT_FLOWBITS:
525  {
526  /* figure out what flowbit action */
527  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
528  array[fb->idx].cnts[fb->cmd]++;
529  if (has_state)
530  array[fb->idx].state_cnts[fb->cmd]++;
531  if (fb->cmd == DETECT_FLOWBITS_CMD_SET) {
532  if (array[fb->idx].set_sids_idx >= array[fb->idx].set_sids_size) {
533  uint32_t old_size = array[fb->idx].set_sids_size;
534  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
535 
536  void *ptr = SCRealloc(array[fb->idx].set_sids, new_size * sizeof(uint32_t));
537  if (ptr == NULL)
538  goto end;
539  array[fb->idx].set_sids_size = new_size;
540  array[fb->idx].set_sids = ptr;
541  }
542 
543  array[fb->idx].set_sids[array[fb->idx].set_sids_idx] = s->num;
544  array[fb->idx].set_sids_idx++;
545  }
546  else if (fb->cmd == DETECT_FLOWBITS_CMD_UNSET) {
547  if (array[fb->idx].unset_sids_idx >= array[fb->idx].unset_sids_size) {
548  uint32_t old_size = array[fb->idx].unset_sids_size;
549  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
550 
551  void *ptr = SCRealloc(array[fb->idx].unset_sids, new_size * sizeof(uint32_t));
552  if (ptr == NULL)
553  goto end;
554  array[fb->idx].unset_sids_size = new_size;
555  array[fb->idx].unset_sids = ptr;
556  }
557 
558  array[fb->idx].unset_sids[array[fb->idx].unset_sids_idx] = s->num;
559  array[fb->idx].unset_sids_idx++;
560  }
561  else if (fb->cmd == DETECT_FLOWBITS_CMD_TOGGLE) {
562  if (array[fb->idx].toggle_sids_idx >= array[fb->idx].toggle_sids_size) {
563  uint32_t old_size = array[fb->idx].toggle_sids_size;
564  uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
565 
566  void *ptr = SCRealloc(array[fb->idx].toggle_sids, new_size * sizeof(uint32_t));
567  if (ptr == NULL)
568  goto end;
569  array[fb->idx].toggle_sids_size = new_size;
570  array[fb->idx].toggle_sids = ptr;
571  }
572 
573  array[fb->idx].toggle_sids[array[fb->idx].toggle_sids_idx] = s->num;
574  array[fb->idx].toggle_sids_idx++;
575  }
576  }
577  }
578  }
579  }
580 
581  /* walk array to see if all bits make sense */
582  for (uint32_t i = 0; i < array_size; i++) {
583  const char *varname = VarNameStoreSetupLookup(i, VAR_TYPE_FLOW_BIT);
584  if (varname == NULL)
585  continue;
586 
587  bool to_state = false;
588 
589  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] &&
590  array[i].cnts[DETECT_FLOWBITS_CMD_TOGGLE] == 0 &&
591  array[i].cnts[DETECT_FLOWBITS_CMD_SET] == 0) {
592 
593  const Signature *s = de_ctx->sig_array[array[i].isset_sids[0]];
594  SCLogWarning("flowbit '%s' is checked but not "
595  "set. Checked in %u and %u other sigs",
596  varname, s->id, array[i].isset_sids_idx - 1);
597  }
598  if (array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] &&
599  array[i].state_cnts[DETECT_FLOWBITS_CMD_SET] == 0)
600  {
601  SCLogDebug("flowbit %s/%u: isset in state, set not in state", varname, i);
602  }
603 
604  /* if signature depends on 'stateful' flowbits, then turn the
605  * sig into a stateful sig itself */
606  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] > 0 &&
607  array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] == 0 &&
609  {
610  SCLogDebug("flowbit %s/%u: isset not in state, set in state", varname, i);
611  to_state = true;
612  }
613 
614  SCLogDebug("ALL flowbit %s/%u: sets %u toggles %u unsets %u isnotsets %u issets %u", varname, i,
617  array[i].cnts[DETECT_FLOWBITS_CMD_ISSET]);
618  SCLogDebug("STATE flowbit %s/%u: sets %u toggles %u unsets %u isnotsets %u issets %u", varname, i,
622  for (uint32_t x = 0; x < array[i].set_sids_idx; x++) {
623  SCLogDebug("SET flowbit %s/%u: SID %u", varname, i,
624  de_ctx->sig_array[array[i].set_sids[x]]->id);
625  }
626  for (uint32_t x = 0; x < array[i].isset_sids_idx; x++) {
627  Signature *s = de_ctx->sig_array[array[i].isset_sids[x]];
628  SCLogDebug("GET flowbit %s/%u: SID %u", varname, i, s->id);
629 
630  if (to_state) {
632  SCLogDebug("made SID %u stateful because it depends on "
633  "stateful rules that set flowbit %s", s->id, varname);
634  }
635  }
636  }
637 
639  DetectFlowbitsAnalyzeDump(de_ctx, array, array_size);
640  }
641 
642 end:
643  for (uint32_t i = 0; i < array_size; i++) {
644  SCFree(array[i].set_sids);
645  SCFree(array[i].unset_sids);
646  SCFree(array[i].isset_sids);
647  SCFree(array[i].isnotset_sids);
648  SCFree(array[i].toggle_sids);
649  }
650  SCFree(array);
651 
652  return 0;
653 }
654 
656 static void DetectFlowbitsAnalyzeDump(const DetectEngineCtx *de_ctx,
657  struct FBAnalyze *array, uint32_t elements)
658 {
659  JsonBuilder *js = jb_new_object();
660  if (js == NULL)
661  return;
662 
663  jb_open_array(js, "flowbits");
664  for (uint32_t x = 0; x < elements; x++) {
665  const char *varname = VarNameStoreSetupLookup(x, VAR_TYPE_FLOW_BIT);
666  if (varname == NULL)
667  continue;
668 
669  const struct FBAnalyze *e = &array[x];
670 
671  jb_start_object(js);
672  jb_set_string(js, "name", varname);
673  jb_set_uint(js, "internal_id", x);
674  jb_set_uint(js, "set_cnt", e->cnts[DETECT_FLOWBITS_CMD_SET]);
675  jb_set_uint(js, "unset_cnt", e->cnts[DETECT_FLOWBITS_CMD_UNSET]);
676  jb_set_uint(js, "toggle_cnt", e->cnts[DETECT_FLOWBITS_CMD_TOGGLE]);
677  jb_set_uint(js, "isset_cnt", e->cnts[DETECT_FLOWBITS_CMD_ISSET]);
678  jb_set_uint(js, "isnotset_cnt", e->cnts[DETECT_FLOWBITS_CMD_ISNOTSET]);
679 
680  // sets
681  if (e->cnts[DETECT_FLOWBITS_CMD_SET]) {
682  jb_open_array(js, "sets");
683  for (uint32_t i = 0; i < e->set_sids_idx; i++) {
684  const Signature *s = de_ctx->sig_array[e->set_sids[i]];
685  jb_append_uint(js, s->id);
686  }
687  jb_close(js);
688  }
689  // gets
691  jb_open_array(js, "isset");
692  for (uint32_t i = 0; i < e->isset_sids_idx; i++) {
693  const Signature *s = de_ctx->sig_array[e->isset_sids[i]];
694  jb_append_uint(js, s->id);
695  }
696  jb_close(js);
697  }
698  // isnotset
700  jb_open_array(js, "isnotset");
701  for (uint32_t i = 0; i < e->isnotset_sids_idx; i++) {
702  const Signature *s = de_ctx->sig_array[e->isnotset_sids[i]];
703  jb_append_uint(js, s->id);
704  }
705  jb_close(js);
706  }
707  // unset
709  jb_open_array(js, "unset");
710  for (uint32_t i = 0; i < e->unset_sids_idx; i++) {
711  const Signature *s = de_ctx->sig_array[e->unset_sids[i]];
712  jb_append_uint(js, s->id);
713  }
714  jb_close(js);
715  }
716  // toggle
718  jb_open_array(js, "toggle");
719  for (uint32_t i = 0; i < e->toggle_sids_idx; i++) {
720  const Signature *s = de_ctx->sig_array[e->toggle_sids[i]];
721  jb_append_uint(js, s->id);
722  }
723  jb_close(js);
724  }
725  jb_close(js);
726  }
727  jb_close(js); // array
728  jb_close(js); // object
729 
730  const char *filename = "flowbits.json";
731  const char *log_dir = ConfigGetLogDirectory();
732  char log_path[PATH_MAX] = "";
733  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
734 
736  FILE *fp = fopen(log_path, "w");
737  if (fp != NULL) {
738  fwrite(jb_ptr(js), jb_len(js), 1, fp);
739  fprintf(fp, "\n");
740  fclose(fp);
741  }
743 
744  jb_free(js);
745 }
746 
747 #ifdef UNITTESTS
748 
749 static int FlowBitsTestParse01(void)
750 {
751  char command[16] = "", name[16] = "";
752 
753  /* Single argument version. */
754  FAIL_IF(!DetectFlowbitParse("noalert", command, sizeof(command), name,
755  sizeof(name)));
756  FAIL_IF(strcmp(command, "noalert") != 0);
757 
758  /* No leading or trailing spaces. */
759  FAIL_IF(!DetectFlowbitParse("set,flowbit", command, sizeof(command), name,
760  sizeof(name)));
761  FAIL_IF(strcmp(command, "set") != 0);
762  FAIL_IF(strcmp(name, "flowbit") != 0);
763 
764  /* Leading space. */
765  FAIL_IF(!DetectFlowbitParse("set, flowbit", command, sizeof(command), name,
766  sizeof(name)));
767  FAIL_IF(strcmp(command, "set") != 0);
768  FAIL_IF(strcmp(name, "flowbit") != 0);
769 
770  /* Trailing space. */
771  FAIL_IF(!DetectFlowbitParse("set,flowbit ", command, sizeof(command), name,
772  sizeof(name)));
773  FAIL_IF(strcmp(command, "set") != 0);
774  FAIL_IF(strcmp(name, "flowbit") != 0);
775 
776  /* Leading and trailing space. */
777  FAIL_IF(!DetectFlowbitParse("set, flowbit ", command, sizeof(command), name,
778  sizeof(name)));
779  FAIL_IF(strcmp(command, "set") != 0);
780  FAIL_IF(strcmp(name, "flowbit") != 0);
781 
782  /* Spaces are not allowed in the name. */
783  FAIL_IF(DetectFlowbitParse("set,namewith space", command, sizeof(command),
784  name, sizeof(name)));
785 
786  PASS;
787 }
788 
789 /**
790  * \test FlowBitsTestSig01 is a test for a valid noalert flowbits option
791  *
792  * \retval 1 on success
793  * \retval 0 on failure
794  */
795 
796 static int FlowBitsTestSig01(void)
797 {
798  Signature *s = NULL;
799  DetectEngineCtx *de_ctx = NULL;
800 
803 
804  de_ctx->flags |= DE_QUIET;
805 
806  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Noalert\"; flowbits:noalert,wrongusage; content:\"GET \"; sid:1;)");
807  FAIL_IF_NOT_NULL(s);
808 
811  PASS;
812 }
813 
814 /**
815  * \test FlowBitsTestSig02 is a test for a valid isset,set,isnotset,unset,toggle flowbits options
816  *
817  * \retval 1 on success
818  * \retval 0 on failure
819  */
820 
821 static int FlowBitsTestSig02(void)
822 {
823  Signature *s = NULL;
824  ThreadVars th_v;
825  DetectEngineCtx *de_ctx = NULL;
826 
827  memset(&th_v, 0, sizeof(th_v));
828 
831 
832  de_ctx->flags |= DE_QUIET;
833 
834  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;)");
835  FAIL_IF_NOT_NULL(s);
836 
837  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;)");
838  FAIL_IF_NOT_NULL(s);
839 
840  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;)");
841  FAIL_IF_NOT_NULL(s);
842 
843  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;)");
844  FAIL_IF_NOT_NULL(s);
845 
846  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;)");
847  FAIL_IF_NOT_NULL(s);
848 
849  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;)");
850  FAIL_IF_NOT_NULL(s);
851 
854 
855  PASS;
856 }
857 
858 /**
859  * \test FlowBitsTestSig03 is a test for a invalid flowbits option
860  *
861  * \retval 1 on success
862  * \retval 0 on failure
863  */
864 
865 static int FlowBitsTestSig03(void)
866 {
867  Signature *s = NULL;
868  DetectEngineCtx *de_ctx = NULL;
869 
872 
873  de_ctx->flags |= DE_QUIET;
874 
875  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Unknown cmd\"; flowbits:wrongcmd; content:\"GET \"; sid:1;)");
876  FAIL_IF_NOT_NULL(s);
877 
880  PASS;
881 }
882 
883 /**
884  * \test FlowBitsTestSig04 is a test check idx value
885  *
886  * \retval 1 on success
887  * \retval 0 on failure
888  */
889 
890 static int FlowBitsTestSig04(void)
891 {
892  Signature *s = NULL;
893  DetectEngineCtx *de_ctx = NULL;
894  int idx = 0;
897 
898  de_ctx->flags |= DE_QUIET;
899 
900  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"isset option\"; flowbits:isset,fbt; content:\"GET \"; sid:1;)");
901  FAIL_IF_NULL(s);
902 
904  FAIL_IF(idx == 0);
905 
908  PASS;
909 }
910 
911 /**
912  * \test FlowBitsTestSig05 is a test check noalert flag
913  *
914  * \retval 1 on success
915  * \retval 0 on failure
916  */
917 
918 static int FlowBitsTestSig05(void)
919 {
920  Signature *s = NULL;
921  DetectEngineCtx *de_ctx = NULL;
922 
925 
926  de_ctx->flags |= DE_QUIET;
927 
928  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Noalert\"; flowbits:noalert; content:\"GET \"; sid:1;)");
929  FAIL_IF_NULL(s);
931 
934  PASS;
935 }
936 
937 /**
938  * \test FlowBitsTestSig06 is a test set flowbits option
939  *
940  * \retval 1 on success
941  * \retval 0 on failure
942  */
943 
944 static int FlowBitsTestSig06(void)
945 {
946  uint8_t *buf = (uint8_t *)
947  "GET /one/ HTTP/1.1\r\n"
948  "Host: one.example.org\r\n"
949  "\r\n";
950  uint16_t buflen = strlen((char *)buf);
951  Packet *p = PacketGetFromAlloc();
952  FAIL_IF_NULL(p);
953  Signature *s = NULL;
954  ThreadVars th_v;
955  DetectEngineThreadCtx *det_ctx = NULL;
956  DetectEngineCtx *de_ctx = NULL;
957  Flow f;
958  GenericVar flowvar, *gv = NULL;
959  int result = 0;
960  uint32_t idx = 0;
961 
962  memset(&th_v, 0, sizeof(th_v));
963  memset(&f, 0, sizeof(Flow));
964  memset(&flowvar, 0, sizeof(GenericVar));
965 
966  FLOW_INITIALIZE(&f);
967  p->flow = &f;
968  p->flow->flowvar = &flowvar;
969 
970  p->src.family = AF_INET;
971  p->dst.family = AF_INET;
972  p->payload = buf;
973  p->payload_len = buflen;
974  p->proto = IPPROTO_TCP;
975  p->flags |= PKT_HAS_FLOW;
977 
980 
981  de_ctx->flags |= DE_QUIET;
982 
983  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow; sid:10;)");
984  FAIL_IF_NULL(s);
985 
986  idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
988  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
989 
990  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
991 
992  gv = p->flow->flowvar;
993  FAIL_IF_NULL(gv);
994  for ( ; gv != NULL; gv = gv->next) {
995  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
996  result = 1;
997  }
998  }
999  FAIL_IF_NOT(result);
1000 
1001  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1003 
1004  FLOW_DESTROY(&f);
1005 
1006  SCFree(p);
1007  PASS;
1008 }
1009 
1010 /**
1011  * \test FlowBitsTestSig07 is a test unset flowbits option
1012  *
1013  * \retval 1 on success
1014  * \retval 0 on failure
1015  */
1016 
1017 static int FlowBitsTestSig07(void)
1018 {
1019  uint8_t *buf = (uint8_t *)
1020  "GET /one/ HTTP/1.1\r\n"
1021  "Host: one.example.org\r\n"
1022  "\r\n";
1023  uint16_t buflen = strlen((char *)buf);
1024  Packet *p = PacketGetFromAlloc();
1025  FAIL_IF_NULL(p);
1026  Signature *s = NULL;
1027  ThreadVars th_v;
1028  DetectEngineThreadCtx *det_ctx = NULL;
1029  DetectEngineCtx *de_ctx = NULL;
1030  Flow f;
1031  GenericVar flowvar, *gv = NULL;
1032  int result = 0;
1033  uint32_t idx = 0;
1034 
1035  memset(&th_v, 0, sizeof(th_v));
1036  memset(&f, 0, sizeof(Flow));
1037  memset(&flowvar, 0, sizeof(GenericVar));
1038 
1039  FLOW_INITIALIZE(&f);
1040  p->flow = &f;
1041  p->flow->flowvar = &flowvar;
1042 
1043  p->src.family = AF_INET;
1044  p->dst.family = AF_INET;
1045  p->payload = buf;
1046  p->payload_len = buflen;
1047  p->proto = IPPROTO_TCP;
1048 
1051 
1052  de_ctx->flags |= DE_QUIET;
1053 
1054  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow2; sid:10;)");
1055  FAIL_IF_NULL(s);
1056 
1057  s = s->next = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit unset\"; flowbits:unset,myflow2; sid:11;)");
1058  FAIL_IF_NULL(s);
1059 
1060  idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1062  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1063 
1064  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1065 
1066  gv = p->flow->flowvar;
1067  FAIL_IF_NULL(gv);
1068 
1069  for ( ; gv != NULL; gv = gv->next) {
1070  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
1071  result = 1;
1072  }
1073  }
1074  FAIL_IF(result);
1075 
1076  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1078 
1079  FLOW_DESTROY(&f);
1080 
1081  SCFree(p);
1082  PASS;
1083 }
1084 
1085 /**
1086  * \test FlowBitsTestSig08 is a test toggle flowbits option
1087  *
1088  * \retval 1 on success
1089  * \retval 0 on failure
1090  */
1091 
1092 static int FlowBitsTestSig08(void)
1093 {
1094  uint8_t *buf = (uint8_t *)
1095  "GET /one/ HTTP/1.1\r\n"
1096  "Host: one.example.org\r\n"
1097  "\r\n";
1098  uint16_t buflen = strlen((char *)buf);
1099  Packet *p = PacketGetFromAlloc();
1100  if (unlikely(p == NULL))
1101  return 0;
1102  Signature *s = NULL;
1103  ThreadVars th_v;
1104  DetectEngineThreadCtx *det_ctx = NULL;
1105  DetectEngineCtx *de_ctx = NULL;
1106  Flow f;
1107  GenericVar flowvar, *gv = NULL;
1108  int result = 0;
1109  uint32_t idx = 0;
1110 
1111  memset(&th_v, 0, sizeof(th_v));
1112  memset(&f, 0, sizeof(Flow));
1113  memset(&flowvar, 0, sizeof(GenericVar));
1114 
1115  FLOW_INITIALIZE(&f);
1116  p->flow = &f;
1117  p->flow->flowvar = &flowvar;
1118 
1119  p->src.family = AF_INET;
1120  p->dst.family = AF_INET;
1121  p->payload = buf;
1122  p->payload_len = buflen;
1123  p->proto = IPPROTO_TCP;
1124 
1127 
1128  de_ctx->flags |= DE_QUIET;
1129 
1130  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow2; sid:10;)");
1131  FAIL_IF_NULL(s);
1132 
1133  s = s->next = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit unset\"; flowbits:toggle,myflow2; sid:11;)");
1134  FAIL_IF_NULL(s);
1135 
1136  idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1138  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1139 
1140  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1141 
1142  gv = p->flow->flowvar;
1143  FAIL_IF_NULL(gv);
1144 
1145  for ( ; gv != NULL; gv = gv->next) {
1146  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
1147  result = 1;
1148  }
1149  }
1150  FAIL_IF(result);
1151 
1152  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1154 
1155  FLOW_DESTROY(&f);
1156 
1157  SCFree(p);
1158  PASS;
1159 }
1160 
1161 /**
1162  * \brief this function registers unit tests for FlowBits
1163  */
1165 {
1166  UtRegisterTest("FlowBitsTestParse01", FlowBitsTestParse01);
1167  UtRegisterTest("FlowBitsTestSig01", FlowBitsTestSig01);
1168  UtRegisterTest("FlowBitsTestSig02", FlowBitsTestSig02);
1169  UtRegisterTest("FlowBitsTestSig03", FlowBitsTestSig03);
1170  UtRegisterTest("FlowBitsTestSig04", FlowBitsTestSig04);
1171  UtRegisterTest("FlowBitsTestSig05", FlowBitsTestSig05);
1172  UtRegisterTest("FlowBitsTestSig06", FlowBitsTestSig06);
1173  UtRegisterTest("FlowBitsTestSig07", FlowBitsTestSig07);
1174  UtRegisterTest("FlowBitsTestSig08", FlowBitsTestSig08);
1175 }
1176 #endif /* UNITTESTS */
FBAnalyze::cnts
uint16_t cnts[DETECT_FLOWBITS_CMD_MAX]
Definition: detect-flowbits.c:391
GenericVar_::type
uint8_t type
Definition: util-var.h:49
FBAnalyze::isset_sids
uint32_t * isset_sids
Definition: detect-flowbits.c:398
SigTableElmt_::url
const char * url
Definition: detect.h:1299
Packet_::proto
uint8_t proto
Definition: decode.h:459
FBAnalyze::isnotset_sids_idx
uint32_t isnotset_sids_idx
Definition: detect-flowbits.c:403
g_flowbits_dump_write_m
SCMutex g_flowbits_dump_write_m
Definition: detect-flowbits.c:655
DetectFlowbitsData_::or_list_size
uint8_t or_list_size
Definition: detect-flowbits.h:38
detect-engine.h
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:581
DETECT_FLOWBITS_CMD_MAX
#define DETECT_FLOWBITS_CMD_MAX
Definition: detect-flowbits.h:33
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1022
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1286
flow-util.h
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1296
Signature_::num
SigIntId num
Definition: detect.h:608
FBAnalyze::isnotset_sids
uint32_t * isnotset_sids
Definition: detect-flowbits.c:402
FlowBitsRegisterTests
void FlowBitsRegisterTests(void)
this function registers unit tests for FlowBits
Definition: detect-flowbits.c:1164
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:408
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:586
Packet_::flags
uint32_t flags
Definition: decode.h:474
threads.h
Flow_
Flow data structure.
Definition: flow.h:351
DetectFlowbitsAnalyze
int DetectFlowbitsAnalyze(DetectEngineCtx *de_ctx)
Definition: detect-flowbits.c:419
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
DetectFlowbitsData_::cmd
uint8_t cmd
Definition: detect-flowbits.h:37
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2533
flow-bit.h
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:223
util-var-name.h
DE_QUIET
#define DE_QUIET
Definition: detect.h:324
VarNameStoreSetupLookup
const char * VarNameStoreSetupLookup(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:188
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1897
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:548
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2674
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
VarNameStoreRegister
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
Definition: util-var-name.c:155
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:468
MAX
#define MAX(x, y)
Definition: suricata-common.h:395
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:587
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:668
FlowBitUnset
void FlowBitUnset(Flow *f, uint32_t idx)
Definition: flow-bit.c:89
DetectFlowbitsData_
Definition: detect-flowbits.h:35
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:124
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:1095
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2791
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
VarNameStoreUnregister
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:201
FBAnalyze::toggle_sids_idx
uint32_t toggle_sids_idx
Definition: detect-flowbits.c:411
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:354
FBAnalyze::set_sids_size
uint32_t set_sids_size
Definition: detect-flowbits.c:396
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:114
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:2314
app-layer-parser.h
GenericVar_::idx
uint32_t idx
Definition: util-var.h:51
FBAnalyze::unset_sids
uint32_t * unset_sids
Definition: detect-flowbits.c:406
DetectFlowbitFree
void DetectFlowbitFree(DetectEngineCtx *, void *)
Definition: detect-flowbits.c:375
DetectFlowbitsRegister
void DetectFlowbitsRegister(void)
Definition: detect-flowbits.c:64
Signature_::flags
uint32_t flags
Definition: detect.h:597
FBAnalyze::set_sids
uint32_t * set_sids
Definition: detect-flowbits.c:394
DetectEngineCtx_::max_fb_id
uint32_t max_fb_id
Definition: detect.h:899
Packet_
Definition: decode.h:437
detect-engine-build.h
DetectFlowbitsData_::idx
uint32_t idx
Definition: detect-flowbits.h:36
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:665
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:1264
FBAnalyze::toggle_sids
uint32_t * toggle_sids
Definition: detect-flowbits.c:410
FBAnalyze::isset_sids_idx
uint32_t isset_sids_idx
Definition: detect-flowbits.c:399
detect-flowbits.h
FlowBitToggle
void FlowBitToggle(Flow *f, uint32_t idx)
Definition: flow-bit.c:94
Flow_::flowvar
GenericVar * flowvar
Definition: flow.h:486
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2149
FBAnalyze::unset_sids_idx
uint32_t unset_sids_idx
Definition: detect-flowbits.c:407
FBAnalyze::toggle_sids_size
uint32_t toggle_sids_size
Definition: detect-flowbits.c:412
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
SIG_FLAG_INIT_STATE_MATCH
#define SIG_FLAG_INIT_STATE_MATCH
Definition: detect.h:291
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:345
util-conf.h
Packet_::flow
struct Flow_ * flow
Definition: decode.h:476
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3244
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:392
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3454
GenericVar_
Definition: util-var.h:48
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
util-spm.h
FBAnalyze::set_sids_idx
uint32_t set_sids_idx
Definition: detect-flowbits.c:395
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:847
SIG_FLAG_NOALERT
#define SIG_FLAG_NOALERT
Definition: detect.h:243
DETECT_FLOWBITS
@ DETECT_FLOWBITS
Definition: detect-engine-register.h:90
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:229
ConfigGetLogDirectory
const char * ConfigGetLogDirectory(void)
Definition: util-conf.c:38
FlowBitIsset
int FlowBitIsset(Flow *f, uint32_t idx)
Definition: flow-bit.c:104
str
#define str(s)
Definition: suricata-common.h:291
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:400
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectFlowbitsData_::or_list
uint32_t * or_list
Definition: detect-flowbits.h:39
Signature_::id
uint32_t id
Definition: detect.h:631
DETECT_FLOWBITS_CMD_UNSET
#define DETECT_FLOWBITS_CMD_UNSET
Definition: detect-flowbits.h:30
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
SigMatch_
a single match condition for a signature
Definition: detect.h:350
FBAnalyze::isnotset_sids_size
uint32_t isnotset_sids_size
Definition: detect-flowbits.c:404
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2494
DetectFlowbitMatch
int DetectFlowbitMatch(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect-flowbits.c:196
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:856
Address_::family
char family
Definition: decode.h:117
Packet_::dst
Address dst
Definition: decode.h:442
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:841
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineCtx_::sig_array_len
uint32_t sig_array_len
Definition: detect.h:857
FBAnalyze
Definition: detect-flowbits.c:390
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:587
SIGMATCH_IPONLY_COMPAT
#define SIGMATCH_IPONLY_COMPAT
Definition: detect.h:1478
SCMutex
#define SCMutex
Definition: threads-debug.h:114
rule_engine_analysis_set
bool rule_engine_analysis_set
Definition: detect-engine-loader.c:56
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:121
Packet_::src
Address src
Definition: decode.h:441
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:1288