suricata
detect-engine-analyzer.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Eileen Donlon <emdonlo@gmail.com>
22  * \author Victor Julien <victor@inliniac.net>
23  *
24  * Rule analyzers for the detection engine
25  */
26 
27 #include "suricata-common.h"
28 #include "suricata.h"
29 #include "rust.h"
30 #include "detect.h"
31 #include "detect-parse.h"
32 #include "detect-engine.h"
33 #include "detect-engine-analyzer.h"
34 #include "detect-engine-mpm.h"
35 #include "conf.h"
36 #include "detect-content.h"
37 #include "detect-flow.h"
38 #include "detect-tcp-flags.h"
39 #include "feature.h"
40 #include "util-print.h"
41 #include "util-time.h"
42 #include "util-validate.h"
43 #include "util-conf.h"
44 
45 static int rule_warnings_only = 0;
46 static FILE *rule_engine_analysis_FD = NULL;
47 static FILE *fp_engine_analysis_FD = NULL;
48 static pcre2_code *percent_re = NULL;
49 static pcre2_match_data *percent_re_match = NULL;
50 static char log_path[PATH_MAX];
51 
52 typedef struct FpPatternStats_ {
53  uint16_t min;
54  uint16_t max;
55  uint32_t cnt;
56  uint64_t tot;
58 
59 /* Details for each buffer being tracked */
60 typedef struct DetectEngineAnalyzerItems {
61  int16_t item_id;
62  bool item_seen;
65  const char *item_name;
66  const char *display_name;
68 
69 /* Track which items require the item_seen value to be exposed */
71  const char *bufname;
73 };
74 
76  /* request keywords */
77  { 0, false, false, true, "http_uri", "http uri" },
78  { 0, false, false, false, "http_raw_uri", "http raw uri" },
79  { 0, false, true, false, "http_method", "http method" },
80  { 0, false, false, false, "http_request_line", "http request line" },
81  { 0, false, false, false, "http_client_body", "http client body" },
82  { 0, false, false, true, "http_header", "http header" },
83  { 0, false, false, false, "http_raw_header", "http raw header" },
84  { 0, false, false, true, "http_cookie", "http cookie" },
85  { 0, false, false, false, "http_user_agent", "http user agent" },
86  { 0, false, false, false, "http_host", "http host" },
87  { 0, false, false, false, "http_raw_host", "http raw host" },
88  { 0, false, false, false, "http_accept_enc", "http accept enc" },
89  { 0, false, false, false, "http_referer", "http referer" },
90  { 0, false, false, false, "http_content_type", "http content type" },
91  { 0, false, false, false, "http_header_names", "http header names" },
92 
93  /* response keywords not listed above */
94  { 0, false, false, false, "http_stat_msg", "http stat msg" },
95  { 0, false, false, false, "http_stat_code", "http stat code" },
96  { 0, false, true, false, "file_data", "http server body"},
97 
98  /* missing request keywords */
99  { 0, false, false, false, "http_request_line", "http request line" },
100  { 0, false, false, false, "http_accept", "http accept" },
101  { 0, false, false, false, "http_accept_lang", "http accept lang" },
102  { 0, false, false, false, "http_connection", "http connection" },
103  { 0, false, false, false, "http_content_len", "http content len" },
104  { 0, false, false, false, "http_protocol", "http protocol" },
105  { 0, false, false, false, "http_start", "http start" },
106 
107  /* missing response keywords; some of the missing are listed above*/
108  { 0, false, false, false, "http_response_line", "http response line" },
109  { 0, false, false, false, "http.server", "http server" },
110  { 0, false, false, false, "http.location", "http location" },
111 };
112 
113 /*
114  * This array contains the map between the `analyzer_items` array listed above and
115  * the item ids returned by DetectBufferTypeGetByName. Iterating signature's sigmatch
116  * array provides list_ids. The map converts those ids into elements of the
117  * analyzer items array.
118  *
119  * Ultimately, the g_buffer_type_hash is searched for each buffer name. The size of that
120  * hashlist is 256, so that's the value we use here.
121  */
122 int16_t analyzer_item_map[256];
123 
124 /*
125  * Certain values must be directly accessible. This array contains items that are directly
126  * accessed when checking if they've been seen or not.
127  */
129  { .bufname = "http_method"},
130  { .bufname = "file_data"}
131 };
132 
133 static FpPatternStats fp_pattern_stats[DETECT_SM_LIST_MAX];
134 
135 static void FpPatternStatsAdd(int list, uint16_t patlen)
136 {
137  if (list < 0 || list >= DETECT_SM_LIST_MAX)
138  return;
139 
140  FpPatternStats *f = &fp_pattern_stats[list];
141 
142  if (f->min == 0)
143  f->min = patlen;
144  else if (patlen < f->min)
145  f->min = patlen;
146 
147  if (patlen > f->max)
148  f->max = patlen;
149 
150  f->cnt++;
151  f->tot += patlen;
152 }
153 
154 void EngineAnalysisFP(const DetectEngineCtx *de_ctx, const Signature *s, char *line)
155 {
156  int fast_pattern_set = 0;
157  int fast_pattern_only_set = 0;
158  int fast_pattern_chop_set = 0;
159  const DetectContentData *fp_cd = NULL;
160  const SigMatch *mpm_sm = s->init_data->mpm_sm;
161  const int mpm_sm_list = s->init_data->mpm_sm_list;
162 
163  if (mpm_sm != NULL) {
164  fp_cd = (DetectContentData *)mpm_sm->ctx;
165  if (fp_cd->flags & DETECT_CONTENT_FAST_PATTERN) {
166  fast_pattern_set = 1;
168  fast_pattern_only_set = 1;
169  } else if (fp_cd->flags & DETECT_CONTENT_FAST_PATTERN_CHOP) {
170  fast_pattern_chop_set = 1;
171  }
172  }
173  }
174 
175  fprintf(fp_engine_analysis_FD, "== Sid: %u ==\n", s->id);
176  fprintf(fp_engine_analysis_FD, "%s\n", line);
177 
178  fprintf(fp_engine_analysis_FD, " Fast Pattern analysis:\n");
179  if (s->init_data->prefilter_sm != NULL) {
180  fprintf(fp_engine_analysis_FD, " Prefilter on: %s\n",
182  fprintf(fp_engine_analysis_FD, "\n");
183  return;
184  }
185 
186  if (fp_cd == NULL) {
187  fprintf(fp_engine_analysis_FD, " No content present\n");
188  fprintf(fp_engine_analysis_FD, "\n");
189  return;
190  }
191 
192  fprintf(fp_engine_analysis_FD, " Fast pattern matcher: ");
193  int list_type = mpm_sm_list;
194  if (list_type == DETECT_SM_LIST_PMATCH)
195  fprintf(fp_engine_analysis_FD, "content\n");
196  else {
197  const char *desc = DetectEngineBufferTypeGetDescriptionById(de_ctx, list_type);
198  const char *name = DetectEngineBufferTypeGetNameById(de_ctx, list_type);
199  if (desc && name) {
200  fprintf(fp_engine_analysis_FD, "%s (%s)\n", desc, name);
201  }
202  }
203 
204  int flags_set = 0;
205  fprintf(fp_engine_analysis_FD, " Flags:");
206  if (fp_cd->flags & DETECT_CONTENT_OFFSET) {
207  fprintf(fp_engine_analysis_FD, " Offset");
208  flags_set = 1;
209  } if (fp_cd->flags & DETECT_CONTENT_DEPTH) {
210  fprintf(fp_engine_analysis_FD, " Depth");
211  flags_set = 1;
212  }
213  if (fp_cd->flags & DETECT_CONTENT_WITHIN) {
214  fprintf(fp_engine_analysis_FD, " Within");
215  flags_set = 1;
216  }
217  if (fp_cd->flags & DETECT_CONTENT_DISTANCE) {
218  fprintf(fp_engine_analysis_FD, " Distance");
219  flags_set = 1;
220  }
221  if (fp_cd->flags & DETECT_CONTENT_NOCASE) {
222  fprintf(fp_engine_analysis_FD, " Nocase");
223  flags_set = 1;
224  }
225  if (fp_cd->flags & DETECT_CONTENT_NEGATED) {
226  fprintf(fp_engine_analysis_FD, " Negated");
227  flags_set = 1;
228  }
229  if (flags_set == 0)
230  fprintf(fp_engine_analysis_FD, " None");
231  fprintf(fp_engine_analysis_FD, "\n");
232 
233  fprintf(fp_engine_analysis_FD, " Fast pattern set: %s\n", fast_pattern_set ? "yes" : "no");
234  fprintf(fp_engine_analysis_FD, " Fast pattern only set: %s\n",
235  fast_pattern_only_set ? "yes" : "no");
236  fprintf(fp_engine_analysis_FD, " Fast pattern chop set: %s\n",
237  fast_pattern_chop_set ? "yes" : "no");
238  if (fast_pattern_chop_set) {
239  fprintf(fp_engine_analysis_FD, " Fast pattern offset, length: %u, %u\n",
240  fp_cd->fp_chop_offset, fp_cd->fp_chop_len);
241  }
242 
243  uint16_t patlen = fp_cd->content_len;
244  uint8_t *pat = SCMalloc(fp_cd->content_len + 1);
245  if (unlikely(pat == NULL)) {
246  FatalError("Error allocating memory");
247  }
248  memcpy(pat, fp_cd->content, fp_cd->content_len);
249  pat[fp_cd->content_len] = '\0';
250  fprintf(fp_engine_analysis_FD, " Original content: ");
251  PrintRawUriFp(fp_engine_analysis_FD, pat, patlen);
252  fprintf(fp_engine_analysis_FD, "\n");
253 
254  if (fast_pattern_chop_set) {
255  SCFree(pat);
256  patlen = fp_cd->fp_chop_len;
257  pat = SCMalloc(fp_cd->fp_chop_len + 1);
258  if (unlikely(pat == NULL)) {
259  exit(EXIT_FAILURE);
260  }
261  memcpy(pat, fp_cd->content + fp_cd->fp_chop_offset, fp_cd->fp_chop_len);
262  pat[fp_cd->fp_chop_len] = '\0';
263  fprintf(fp_engine_analysis_FD, " Final content: ");
264  PrintRawUriFp(fp_engine_analysis_FD, pat, patlen);
265  fprintf(fp_engine_analysis_FD, "\n");
266 
267  FpPatternStatsAdd(list_type, patlen);
268  } else {
269  fprintf(fp_engine_analysis_FD, " Final content: ");
270  PrintRawUriFp(fp_engine_analysis_FD, pat, patlen);
271  fprintf(fp_engine_analysis_FD, "\n");
272 
273  FpPatternStatsAdd(list_type, patlen);
274  }
275  SCFree(pat);
276 
277  fprintf(fp_engine_analysis_FD, "\n");
278  return;
279 }
280 
281 /**
282  * \brief Sets up the fast pattern analyzer according to the config.
283  *
284  * \retval 1 If rule analyzer successfully enabled.
285  * \retval 0 If not enabled.
286  */
288 {
289  int fp_engine_analysis_set = 0;
290 
291  if ((ConfGetBool("engine-analysis.rules-fast-pattern",
292  &fp_engine_analysis_set)) == 0) {
293  return 0;
294  }
295 
296  if (fp_engine_analysis_set == 0)
297  return 0;
298 
299  const char *log_dir;
300  log_dir = ConfigGetLogDirectory();
301  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir,
302  "rules_fast_pattern.txt");
303 
304  fp_engine_analysis_FD = fopen(log_path, "w");
305  if (fp_engine_analysis_FD == NULL) {
306  SCLogError("failed to open %s: %s", log_path, strerror(errno));
307  return 0;
308  }
309 
310  SCLogInfo("Engine-Analysis for fast_pattern printed to file - %s",
311  log_path);
312 
313  struct timeval tval;
314  struct tm *tms;
315  gettimeofday(&tval, NULL);
316  struct tm local_tm;
317  tms = SCLocalTime(tval.tv_sec, &local_tm);
318  fprintf(fp_engine_analysis_FD, "----------------------------------------------"
319  "---------------------\n");
320  fprintf(fp_engine_analysis_FD, "Date: %" PRId32 "/%" PRId32 "/%04d -- "
321  "%02d:%02d:%02d\n",
322  tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour,
323  tms->tm_min, tms->tm_sec);
324  fprintf(fp_engine_analysis_FD, "----------------------------------------------"
325  "---------------------\n");
326 
327  memset(&fp_pattern_stats, 0, sizeof(fp_pattern_stats));
328  return 1;
329 }
330 
331 /**
332  * \brief Sets up the rule analyzer according to the config
333  * \retval 1 if rule analyzer successfully enabled
334  * \retval 0 if not enabled
335  */
337 {
338  ConfNode *conf = ConfGetNode("engine-analysis");
339  int enabled = 0;
340  if (conf != NULL) {
341  const char *value = ConfNodeLookupChildValue(conf, "rules");
342  if (value && ConfValIsTrue(value)) {
343  enabled = 1;
344  } else if (value && strcasecmp(value, "warnings-only") == 0) {
345  enabled = 1;
346  rule_warnings_only = 1;
347  }
348  if (enabled) {
349  const char *log_dir;
350  log_dir = ConfigGetLogDirectory();
351  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, "rules_analysis.txt");
352  rule_engine_analysis_FD = fopen(log_path, "w");
353  if (rule_engine_analysis_FD == NULL) {
354  SCLogError("failed to open %s: %s", log_path, strerror(errno));
355  return 0;
356  }
357 
358  SCLogInfo("Engine-Analysis for rules printed to file - %s",
359  log_path);
360 
361  struct timeval tval;
362  struct tm *tms;
363  gettimeofday(&tval, NULL);
364  struct tm local_tm;
365  tms = SCLocalTime(tval.tv_sec, &local_tm);
366  fprintf(rule_engine_analysis_FD, "----------------------------------------------"
367  "---------------------\n");
368  fprintf(rule_engine_analysis_FD, "Date: %" PRId32 "/%" PRId32 "/%04d -- "
369  "%02d:%02d:%02d\n",
370  tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour,
371  tms->tm_min, tms->tm_sec);
372  fprintf(rule_engine_analysis_FD, "----------------------------------------------"
373  "---------------------\n");
374 
375  /*compile regex's for rule analysis*/
376  if (PerCentEncodingSetup()== 0) {
377  fprintf(rule_engine_analysis_FD, "Error compiling regex; can't check for percent encoding in normalized http content.\n");
378  }
379  }
380  }
381  else {
382  SCLogInfo("Conf parameter \"engine-analysis.rules\" not found. "
383  "Defaulting to not printing the rules analysis report.");
384  }
385  if (!enabled) {
386  SCLogInfo("Engine-Analysis for rules disabled in conf file.");
387  return 0;
388  }
389  return 1;
390 }
391 
393 {
394  fprintf(fp_engine_analysis_FD, "============\n"
395  "Summary:\n============\n");
396  int i;
397  for (i = 0; i < DETECT_SM_LIST_MAX; i++) {
398  FpPatternStats *f = &fp_pattern_stats[i];
399  if (f->cnt == 0)
400  continue;
401 
402  fprintf(fp_engine_analysis_FD,
403  "%s, smallest pattern %u byte(s), longest pattern %u byte(s), number of patterns %u, avg pattern len %.2f byte(s)\n",
404  DetectSigmatchListEnumToString(i), f->min, f->max, f->cnt, (float)((double)f->tot/(float)f->cnt));
405  }
406 
407  if (fp_engine_analysis_FD != NULL) {
408  fclose(fp_engine_analysis_FD);
409  fp_engine_analysis_FD = NULL;
410  }
411 
412  return;
413 }
414 
415 
417 {
418  if (rule_engine_analysis_FD != NULL) {
419  SCLogInfo("Engine-Analysis for rules printed to file - %s", log_path);
420  fclose(rule_engine_analysis_FD);
421  rule_engine_analysis_FD = NULL;
422  }
423  if (percent_re != NULL) {
424  pcre2_code_free(percent_re);
425  }
426  pcre2_match_data_free(percent_re_match);
427 }
428 
429 /**
430  * \brief Compiles regex for rule analysis
431  * \retval 1 if successful
432  * \retval 0 if on error
433  */
435 {
436 #define DETECT_PERCENT_ENCODING_REGEX "%[0-9|a-f|A-F]{2}"
437  int en;
438  PCRE2_SIZE eo = 0;
439  int opts = 0; // PCRE2_NEWLINE_ANY??
440 
441  percent_re = pcre2_compile((PCRE2_SPTR8)DETECT_PERCENT_ENCODING_REGEX, PCRE2_ZERO_TERMINATED,
442  opts, &en, &eo, NULL);
443  if (percent_re == NULL) {
444  PCRE2_UCHAR errbuffer[256];
445  pcre2_get_error_message(en, errbuffer, sizeof(errbuffer));
446  SCLogError("Compile of \"%s\" failed at offset %d: %s", DETECT_PERCENT_ENCODING_REGEX,
447  (int)eo, errbuffer);
448  return 0;
449  }
450 
451  percent_re_match = pcre2_match_data_create_from_pattern(percent_re, NULL);
452  return 1;
453 }
454 
455 /**
456  * \brief Checks for % encoding in content.
457  * \param Pointer to content
458  * \retval number of matches if content has % encoding
459  * \retval 0 if it doesn't have % encoding
460  * \retval -1 on error
461  */
462 static int PerCentEncodingMatch(uint8_t *content, uint16_t content_len)
463 {
464  int ret = 0;
465 
466  ret = pcre2_match(percent_re, (PCRE2_SPTR8)content, content_len, 0, 0, percent_re_match, NULL);
467  if (ret == -1) {
468  return 0;
469  }
470  else if (ret < -1) {
471  SCLogError("Error parsing content - %s; error code is %d", content, ret);
472  return -1;
473  }
474  return ret;
475 }
476 
477 static void EngineAnalysisRulesPrintFP(const DetectEngineCtx *de_ctx, const Signature *s)
478 {
479  const DetectContentData *fp_cd = NULL;
480  const SigMatch *mpm_sm = s->init_data->mpm_sm;
481  const int mpm_sm_list = s->init_data->mpm_sm_list;
482 
483  if (mpm_sm != NULL) {
484  fp_cd = (DetectContentData *)mpm_sm->ctx;
485  }
486 
487  if (fp_cd == NULL) {
488  return;
489  }
490 
491  uint16_t patlen = fp_cd->content_len;
492  uint8_t *pat = SCMalloc(fp_cd->content_len + 1);
493  if (unlikely(pat == NULL)) {
494  FatalError("Error allocating memory");
495  }
496  memcpy(pat, fp_cd->content, fp_cd->content_len);
497  pat[fp_cd->content_len] = '\0';
498 
500  SCFree(pat);
501  patlen = fp_cd->fp_chop_len;
502  pat = SCMalloc(fp_cd->fp_chop_len + 1);
503  if (unlikely(pat == NULL)) {
504  exit(EXIT_FAILURE);
505  }
506  memcpy(pat, fp_cd->content + fp_cd->fp_chop_offset, fp_cd->fp_chop_len);
507  pat[fp_cd->fp_chop_len] = '\0';
508  fprintf(rule_engine_analysis_FD, " Fast Pattern \"");
509  PrintRawUriFp(rule_engine_analysis_FD, pat, patlen);
510  } else {
511  fprintf(rule_engine_analysis_FD, " Fast Pattern \"");
512  PrintRawUriFp(rule_engine_analysis_FD, pat, patlen);
513  }
514  SCFree(pat);
515 
516  fprintf(rule_engine_analysis_FD, "\" on \"");
517 
518  const int list_type = mpm_sm_list;
519  if (list_type == DETECT_SM_LIST_PMATCH) {
520  int payload = 0;
521  int stream = 0;
523  payload = 1;
525  stream = 1;
526  fprintf(rule_engine_analysis_FD, "%s",
527  payload ? (stream ? "payload and reassembled stream" : "payload") : "reassembled stream");
528  }
529  else {
530  const char *desc = DetectEngineBufferTypeGetDescriptionById(de_ctx, list_type);
531  const char *name = DetectEngineBufferTypeGetNameById(de_ctx, list_type);
532  if (desc && name) {
533  fprintf(rule_engine_analysis_FD, "%s (%s)", desc, name);
534  } else if (desc || name) {
535  fprintf(rule_engine_analysis_FD, "%s", desc ? desc : name);
536  }
537 
538  }
539 
540  fprintf(rule_engine_analysis_FD, "\" ");
541  const DetectBufferType *bt = DetectEngineBufferTypeGetById(de_ctx, list_type);
542  if (bt && bt->transforms.cnt) {
543  fprintf(rule_engine_analysis_FD, "(with %d transform(s)) ", bt->transforms.cnt);
544  }
545  fprintf(rule_engine_analysis_FD, "buffer.\n");
546 
547  return;
548 }
549 
550 
551 void EngineAnalysisRulesFailure(char *line, char *file, int lineno)
552 {
553  fprintf(rule_engine_analysis_FD, "== Sid: UNKNOWN ==\n");
554  fprintf(rule_engine_analysis_FD, "%s\n", line);
555  fprintf(rule_engine_analysis_FD, " FAILURE: invalid rule.\n");
556  fprintf(rule_engine_analysis_FD, " File: %s.\n", file);
557  fprintf(rule_engine_analysis_FD, " Line: %d.\n", lineno);
558  fprintf(rule_engine_analysis_FD, "\n");
559 }
560 
561 typedef struct RuleAnalyzer {
562  JsonBuilder *js; /* document root */
563 
564  JsonBuilder *js_warnings;
565  JsonBuilder *js_notes;
567 
568 static void ATTR_FMT_PRINTF(2, 3) AnalyzerNote(RuleAnalyzer *ctx, char *fmt, ...)
569 {
570  va_list ap;
571  char str[1024];
572 
573  va_start(ap, fmt);
574  vsnprintf(str, sizeof(str), fmt, ap);
575  va_end(ap);
576 
577  if (!ctx->js_notes)
578  ctx->js_notes = jb_new_array();
579  if (ctx->js_notes)
580  jb_append_string(ctx->js_notes, str);
581 }
582 
583 static void ATTR_FMT_PRINTF(2, 3) AnalyzerWarning(RuleAnalyzer *ctx, char *fmt, ...)
584 {
585  va_list ap;
586  char str[1024];
587 
588  va_start(ap, fmt);
589  vsnprintf(str, sizeof(str), fmt, ap);
590  va_end(ap);
591 
592  if (!ctx->js_warnings)
593  ctx->js_warnings = jb_new_array();
594  if (ctx->js_warnings)
595  jb_append_string(ctx->js_warnings, str);
596 }
597 
598 #define CHECK(pat) if (strlen((pat)) <= len && memcmp((pat), buf, MIN(len, strlen((pat)))) == 0) return true;
599 
600 static bool LooksLikeHTTPMethod(const uint8_t *buf, uint16_t len)
601 {
602  CHECK("GET /");
603  CHECK("POST /");
604  CHECK("HEAD /");
605  CHECK("PUT /");
606  return false;
607 }
608 
609 static bool LooksLikeHTTPUA(const uint8_t *buf, uint16_t len)
610 {
611  CHECK("User-Agent: ");
612  CHECK("\nUser-Agent: ");
613  return false;
614 }
615 
616 static void DumpContent(JsonBuilder *js, const DetectContentData *cd)
617 {
618  char pattern_str[1024] = "";
619  DetectContentPatternPrettyPrint(cd, pattern_str, sizeof(pattern_str));
620 
621  jb_set_string(js, "pattern", pattern_str);
622  jb_set_uint(js, "length", cd->content_len);
623  jb_set_bool(js, "nocase", cd->flags & DETECT_CONTENT_NOCASE);
624  jb_set_bool(js, "negated", cd->flags & DETECT_CONTENT_NEGATED);
625  jb_set_bool(js, "starts_with", cd->flags & DETECT_CONTENT_STARTS_WITH);
626  jb_set_bool(js, "ends_with", cd->flags & DETECT_CONTENT_ENDS_WITH);
627  jb_set_bool(js, "is_mpm", cd->flags & DETECT_CONTENT_MPM);
628  jb_set_bool(js, "no_double_inspect", cd->flags & DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED);
629  if (cd->flags & DETECT_CONTENT_OFFSET) {
630  jb_set_uint(js, "offset", cd->offset);
631  }
632  if (cd->flags & DETECT_CONTENT_DEPTH) {
633  jb_set_uint(js, "depth", cd->depth);
634  }
635  if (cd->flags & DETECT_CONTENT_DISTANCE) {
636  jb_set_uint(js, "distance", cd->distance);
637  }
638  if (cd->flags & DETECT_CONTENT_WITHIN) {
639  jb_set_uint(js, "within", cd->within);
640  }
641  jb_set_bool(js, "fast_pattern", cd->flags & DETECT_CONTENT_FAST_PATTERN);
642 }
643 
644 static void DumpMatches(RuleAnalyzer *ctx, JsonBuilder *js, const SigMatchData *smd)
645 {
646  if (smd == NULL)
647  return;
648 
649  jb_open_array(js, "matches");
650  do {
651  jb_start_object(js);
652  const char *mname = sigmatch_table[smd->type].name;
653  jb_set_string(js, "name", mname);
654 
655  switch (smd->type) {
656  case DETECT_CONTENT: {
657  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
658 
659  jb_open_object(js, "content");
660  DumpContent(js, cd);
662  AnalyzerNote(ctx, (char *)"'fast_pattern:only' option is silently ignored and "
663  "is interpreted as regular 'fast_pattern'");
664  }
665  if (LooksLikeHTTPMethod(cd->content, cd->content_len)) {
666  AnalyzerWarning(ctx,
667  (char *)"pattern looks like it inspects HTTP, use http.request_line or "
668  "http.method and http.uri instead for improved performance");
669  }
670  if (LooksLikeHTTPUA(cd->content, cd->content_len)) {
671  AnalyzerWarning(ctx,
672  (char *)"pattern looks like it inspects HTTP, use http.user_agent "
673  "or http.header for improved performance");
674  }
675  jb_close(js);
676  break;
677  }
678  }
679  jb_close(js);
680 
681  if (smd->is_last)
682  break;
683  smd++;
684  } while (1);
685  jb_close(js);
686 }
687 
690 {
691  SCEnter();
692 
693  RuleAnalyzer ctx = { NULL, NULL, NULL };
694 
695  ctx.js = jb_new_object();
696  if (ctx.js == NULL)
697  SCReturn;
698 
699  jb_set_string(ctx.js, "raw", s->sig_str);
700  jb_set_uint(ctx.js, "id", s->id);
701  jb_set_uint(ctx.js, "gid", s->gid);
702  jb_set_uint(ctx.js, "rev", s->rev);
703  jb_set_string(ctx.js, "msg", s->msg);
704 
705  const char *alproto = AppProtoToString(s->alproto);
706  jb_set_string(ctx.js, "app_proto", alproto);
707 
708  jb_open_array(ctx.js, "requirements");
709  if (s->mask & SIG_MASK_REQUIRE_PAYLOAD) {
710  jb_append_string(ctx.js, "payload");
711  }
713  jb_append_string(ctx.js, "no_payload");
714  }
715  if (s->mask & SIG_MASK_REQUIRE_FLOW) {
716  jb_append_string(ctx.js, "flow");
717  }
719  jb_append_string(ctx.js, "tcp_flags_init_deinit");
720  }
722  jb_append_string(ctx.js, "tcp_flags_unusual");
723  }
724  if (s->mask & SIG_MASK_REQUIRE_DCERPC) {
725  jb_append_string(ctx.js, "dcerpc");
726  }
728  jb_append_string(ctx.js, "engine_event");
729  }
730  jb_close(ctx.js);
731 
732  jb_open_array(ctx.js, "flags");
733  if (s->flags & SIG_FLAG_SRC_ANY) {
734  jb_append_string(ctx.js, "src_any");
735  }
736  if (s->flags & SIG_FLAG_DST_ANY) {
737  jb_append_string(ctx.js, "dst_any");
738  }
739  if (s->flags & SIG_FLAG_SP_ANY) {
740  jb_append_string(ctx.js, "sp_any");
741  }
742  if (s->flags & SIG_FLAG_DP_ANY) {
743  jb_append_string(ctx.js, "dp_any");
744  }
745  if (s->flags & SIG_FLAG_NOALERT) {
746  jb_append_string(ctx.js, "noalert");
747  }
748  if (s->flags & SIG_FLAG_DSIZE) {
749  jb_append_string(ctx.js, "dsize");
750  }
751  if (s->flags & SIG_FLAG_APPLAYER) {
752  jb_append_string(ctx.js, "applayer");
753  }
754  if (s->flags & SIG_FLAG_IPONLY) {
755  jb_append_string(ctx.js, "ip_only");
756  }
757  if (s->flags & SIG_FLAG_REQUIRE_PACKET) {
758  jb_append_string(ctx.js, "need_packet");
759  }
760  if (s->flags & SIG_FLAG_REQUIRE_STREAM) {
761  jb_append_string(ctx.js, "need_stream");
762  }
763  if (s->flags & SIG_FLAG_MPM_NEG) {
764  jb_append_string(ctx.js, "negated_mpm");
765  }
766  if (s->flags & SIG_FLAG_FLUSH) {
767  jb_append_string(ctx.js, "flush");
768  }
769  if (s->flags & SIG_FLAG_REQUIRE_FLOWVAR) {
770  jb_append_string(ctx.js, "need_flowvar");
771  }
772  if (s->flags & SIG_FLAG_FILESTORE) {
773  jb_append_string(ctx.js, "filestore");
774  }
775  if (s->flags & SIG_FLAG_TOSERVER) {
776  jb_append_string(ctx.js, "toserver");
777  }
778  if (s->flags & SIG_FLAG_TOCLIENT) {
779  jb_append_string(ctx.js, "toclient");
780  }
781  if (s->flags & SIG_FLAG_TLSSTORE) {
782  jb_append_string(ctx.js, "tlsstore");
783  }
784  if (s->flags & SIG_FLAG_BYPASS) {
785  jb_append_string(ctx.js, "bypass");
786  }
787  if (s->flags & SIG_FLAG_PREFILTER) {
788  jb_append_string(ctx.js, "prefilter");
789  }
790  if (s->flags & SIG_FLAG_PDONLY) {
791  jb_append_string(ctx.js, "proto_detect_only");
792  }
793  if (s->flags & SIG_FLAG_SRC_IS_TARGET) {
794  jb_append_string(ctx.js, "src_is_target");
795  }
796  if (s->flags & SIG_FLAG_DEST_IS_TARGET) {
797  jb_append_string(ctx.js, "dst_is_target");
798  }
799  jb_close(ctx.js);
800 
801  const DetectEnginePktInspectionEngine *pkt_mpm = NULL;
802  const DetectEngineAppInspectionEngine *app_mpm = NULL;
803 
804  jb_open_array(ctx.js, "pkt_engines");
806  for ( ; pkt != NULL; pkt = pkt->next) {
807  const char *name = DetectEngineBufferTypeGetNameById(de_ctx, pkt->sm_list);
808  if (name == NULL) {
809  switch (pkt->sm_list) {
811  name = "payload";
812  break;
814  name = "packet";
815  break;
816  default:
817  name = "unknown";
818  break;
819  }
820  }
821  jb_start_object(ctx.js);
822  jb_set_string(ctx.js, "name", name);
823  jb_set_bool(ctx.js, "is_mpm", pkt->mpm);
824  DumpMatches(&ctx, ctx.js, pkt->smd);
825  jb_close(ctx.js);
826  if (pkt->mpm) {
827  pkt_mpm = pkt;
828  }
829  }
830  jb_close(ctx.js);
831  jb_open_array(ctx.js, "frame_engines");
833  for (; frame != NULL; frame = frame->next) {
834  const char *name = DetectEngineBufferTypeGetNameById(de_ctx, frame->sm_list);
835  jb_start_object(ctx.js);
836  jb_set_string(ctx.js, "name", name);
837  jb_set_bool(ctx.js, "is_mpm", frame->mpm);
838  DumpMatches(&ctx, ctx.js, frame->smd);
839  jb_close(ctx.js);
840  }
841  jb_close(ctx.js);
842 
844  bool has_stream = false;
845  bool has_client_body_mpm = false;
846  bool has_file_data_mpm = false;
847 
848  jb_open_array(ctx.js, "engines");
850  for ( ; app != NULL; app = app->next) {
851  const char *name = DetectEngineBufferTypeGetNameById(de_ctx, app->sm_list);
852  if (name == NULL) {
853  switch (app->sm_list) {
855  name = "stream";
856  break;
857  default:
858  name = "unknown";
859  break;
860  }
861  }
862 
863  if (app->sm_list == DETECT_SM_LIST_PMATCH && !app->mpm) {
864  has_stream = true;
865  } else if (app->mpm && strcmp(name, "http_client_body") == 0) {
866  has_client_body_mpm = true;
867  } else if (app->mpm && strcmp(name, "file_data") == 0) {
868  has_file_data_mpm = true;
869  }
870 
871  jb_start_object(ctx.js);
872  jb_set_string(ctx.js, "name", name);
873  const char *direction = app->dir == 0 ? "toserver" : "toclient";
874  jb_set_string(ctx.js, "direction", direction);
875  jb_set_bool(ctx.js, "is_mpm", app->mpm);
876  jb_set_string(ctx.js, "app_proto", AppProtoToString(app->alproto));
877  jb_set_uint(ctx.js, "progress", app->progress);
878  DumpMatches(&ctx, ctx.js, app->smd);
879  jb_close(ctx.js);
880  if (app->mpm) {
881  app_mpm = app;
882  }
883  }
884  jb_close(ctx.js);
885 
886  if (has_stream && has_client_body_mpm)
887  AnalyzerNote(&ctx, (char *)"mpm in http_client_body combined with stream match leads to stream buffering");
888  if (has_stream && has_file_data_mpm)
889  AnalyzerNote(&ctx, (char *)"mpm in file_data combined with stream match leads to stream buffering");
890  }
891 
892  jb_open_object(ctx.js, "lists");
893  for (int i = 0; i < DETECT_SM_LIST_MAX; i++) {
894  if (s->sm_arrays[i] != NULL) {
895  jb_open_object(ctx.js, DetectSigmatchListEnumToString(i));
896  DumpMatches(&ctx, ctx.js, s->sm_arrays[i]);
897  jb_close(ctx.js);
898  }
899  }
900  jb_close(ctx.js);
901 
902  if (pkt_mpm || app_mpm) {
903  jb_open_object(ctx.js, "mpm");
904 
905  int mpm_list = pkt_mpm ? DETECT_SM_LIST_PMATCH : app_mpm->sm_list;
906  const char *name;
907  if (mpm_list < DETECT_SM_LIST_DYNAMIC_START)
908  name = DetectListToHumanString(mpm_list);
909  else
910  name = DetectEngineBufferTypeGetNameById(de_ctx, mpm_list);
911  jb_set_string(ctx.js, "buffer", name);
912 
913  SigMatchData *smd = pkt_mpm ? pkt_mpm->smd : app_mpm->smd;
914  if (smd == NULL && mpm_list == DETECT_SM_LIST_PMATCH) {
915  smd = s->sm_arrays[mpm_list];
916  }
917  do {
918  switch (smd->type) {
919  case DETECT_CONTENT: {
920  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
921  if (cd->flags & DETECT_CONTENT_MPM) {
922  DumpContent(ctx.js, cd);
923  }
924  break;
925  }
926  }
927 
928  if (smd->is_last)
929  break;
930  smd++;
931  } while (1);
932  jb_close(ctx.js);
933  } else if (s->init_data->prefilter_sm) {
934  jb_open_object(ctx.js, "prefilter");
935  int prefilter_list = SigMatchListSMBelongsTo(s, s->init_data->prefilter_sm);
936  const char *name;
937  if (prefilter_list < DETECT_SM_LIST_DYNAMIC_START)
938  name = DetectListToHumanString(prefilter_list);
939  else
940  name = DetectEngineBufferTypeGetNameById(de_ctx, prefilter_list);
941  jb_set_string(ctx.js, "buffer", name);
942  const char *mname = sigmatch_table[s->init_data->prefilter_sm->type].name;
943  jb_set_string(ctx.js, "name", mname);
944  jb_close(ctx.js);
945  }
946 
947  if (ctx.js_warnings) {
948  jb_close(ctx.js_warnings);
949  jb_set_object(ctx.js, "warnings", ctx.js_warnings);
950  jb_free(ctx.js_warnings);
951  ctx.js_warnings = NULL;
952  }
953  if (ctx.js_notes) {
954  jb_close(ctx.js_notes);
955  jb_set_object(ctx.js, "notes", ctx.js_notes);
956  jb_free(ctx.js_notes);
957  ctx.js_notes = NULL;
958  }
959  jb_close(ctx.js);
960 
961  const char *filename = "rules.json";
962  const char *log_dir = ConfigGetLogDirectory();
963  char json_path[PATH_MAX] = "";
964  snprintf(json_path, sizeof(json_path), "%s/%s", log_dir, filename);
965 
967  FILE *fp = fopen(json_path, "a");
968  if (fp != NULL) {
969  fwrite(jb_ptr(ctx.js), jb_len(ctx.js), 1, fp);
970  fprintf(fp, "\n");
971  fclose(fp);
972  }
974  jb_free(ctx.js);
975  SCReturn;
976 }
977 
979 {
980  if (de_ctx->pattern_hash_table == NULL)
981  return;
982 
983  JsonBuilder *root_jb = jb_new_object();
984  JsonBuilder *arrays[de_ctx->buffer_type_id];
985  memset(&arrays, 0, sizeof(JsonBuilder *) * de_ctx->buffer_type_id);
986 
987  jb_open_array(root_jb, "buffers");
988 
990  htb != NULL; htb = HashListTableGetListNext(htb)) {
991  char str[1024] = "";
994 
995  JsonBuilder *jb = arrays[p->sm_list];
996  if (arrays[p->sm_list] == NULL) {
997  jb = arrays[p->sm_list] = jb_new_object();
998  const char *name;
1000  name = DetectListToHumanString(p->sm_list);
1001  else
1003  jb_set_string(jb, "name", name);
1004  jb_set_uint(jb, "list_id", p->sm_list);
1005 
1006  jb_open_array(jb, "patterns");
1007  }
1008 
1009  jb_start_object(jb);
1010  jb_set_string(jb, "pattern", str);
1011  jb_set_uint(jb, "patlen", p->cd->content_len);
1012  jb_set_uint(jb, "cnt", p->cnt);
1013  jb_set_uint(jb, "mpm", p->mpm);
1014  jb_open_object(jb, "flags");
1015  jb_set_bool(jb, "nocase", p->cd->flags & DETECT_CONTENT_NOCASE);
1016  jb_set_bool(jb, "negated", p->cd->flags & DETECT_CONTENT_NEGATED);
1017  jb_set_bool(jb, "depth", p->cd->flags & DETECT_CONTENT_DEPTH);
1018  jb_set_bool(jb, "offset", p->cd->flags & DETECT_CONTENT_OFFSET);
1019  jb_set_bool(jb, "endswith", p->cd->flags & DETECT_CONTENT_ENDS_WITH);
1020  jb_close(jb);
1021  jb_close(jb);
1022  }
1023 
1024  for (uint32_t i = 0; i < de_ctx->buffer_type_id; i++) {
1025  JsonBuilder *jb = arrays[i];
1026  if (jb == NULL)
1027  continue;
1028 
1029  jb_close(jb); // array
1030  jb_close(jb); // object
1031 
1032  jb_append_object(root_jb, jb);
1033  jb_free(jb);
1034  }
1035  jb_close(root_jb);
1036  jb_close(root_jb);
1037 
1038  const char *filename = "patterns.json";
1039  const char *log_dir = ConfigGetLogDirectory();
1040  char json_path[PATH_MAX] = "";
1041  snprintf(json_path, sizeof(json_path), "%s/%s", log_dir, filename);
1042 
1044  FILE *fp = fopen(json_path, "a");
1045  if (fp != NULL) {
1046  fwrite(jb_ptr(root_jb), jb_len(root_jb), 1, fp);
1047  fprintf(fp, "\n");
1048  fclose(fp);
1049  }
1051  jb_free(root_jb);
1052 
1054  de_ctx->pattern_hash_table = NULL;
1055 }
1056 
1057 static void EngineAnalysisItemsReset(void)
1058 {
1059  for (size_t i = 0; i < ARRAY_SIZE(analyzer_items); i++) {
1060  analyzer_items[i].item_seen = false;
1061  }
1062 }
1063 
1064 static void EngineAnalysisItemsInit(void)
1065 {
1066  static bool analyzer_init = false;
1067 
1068  if (analyzer_init) {
1069  EngineAnalysisItemsReset();
1070  return;
1071  }
1072 
1073  memset(analyzer_item_map, -1, sizeof(analyzer_item_map));
1074 
1075  for (size_t i = 0; i < ARRAY_SIZE(analyzer_items); i++) {
1076  DetectEngineAnalyzerItems *analyzer_item = &analyzer_items[i];
1077 
1078  int item_id = DetectBufferTypeGetByName(analyzer_item->item_name);
1079  DEBUG_VALIDATE_BUG_ON(item_id < 0 || item_id > UINT16_MAX);
1080  analyzer_item->item_id = (uint16_t)item_id;
1081  if (analyzer_item->item_id == -1) {
1082  /* Mismatch between the analyzer_items array and what's supported */
1083  FatalError("unable to initialize engine-analysis table: detect buffer \"%s\" not "
1084  "recognized.",
1085  analyzer_item->item_name);
1086  }
1087  analyzer_item->item_seen = false;
1088 
1089  if (analyzer_item->export_item_seen) {
1090  for (size_t k = 0; k < ARRAY_SIZE(exposed_item_seen_list); k++) {
1091  if (0 == strcmp(exposed_item_seen_list[k].bufname, analyzer_item->item_name))
1092  exposed_item_seen_list[k].item_seen_ptr = &analyzer_item->item_seen;
1093  }
1094 
1095  }
1096  analyzer_item_map[analyzer_item->item_id] = (int16_t) i;
1097  }
1098 
1099  analyzer_init = true;
1100 }
1101 
1102 /**
1103  * \brief Prints analysis of loaded rules.
1104  *
1105  * Warns if potential rule issues are detected. For example,
1106  * warns if a rule uses a construct that may perform poorly,
1107  * e.g. pcre without content or with http_method content only;
1108  * warns if a rule uses a construct that may not be consistent with intent,
1109  * e.g. client side ports only, http and content without any http_* modifiers, etc.
1110  *
1111  * \param s Pointer to the signature.
1112  */
1114  const Signature *s, const char *line)
1115 {
1116  uint32_t rule_bidirectional = 0;
1117  uint32_t rule_pcre = 0;
1118  uint32_t rule_pcre_http = 0;
1119  uint32_t rule_content = 0;
1120  uint32_t rule_flow = 0;
1121  uint32_t rule_flags = 0;
1122  uint32_t rule_flow_toserver = 0;
1123  uint32_t rule_flow_toclient = 0;
1124  uint32_t rule_flow_nostream = 0;
1125  uint32_t rule_ipv4_only = 0;
1126  uint32_t rule_ipv6_only = 0;
1127  uint32_t rule_flowbits = 0;
1128  uint32_t rule_flowint = 0;
1129  uint32_t rule_content_http = 0;
1130  uint32_t rule_content_offset_depth = 0;
1131  int32_t list_id = 0;
1132  uint32_t rule_warning = 0;
1133  uint32_t stream_buf = 0;
1134  uint32_t packet_buf = 0;
1135  uint32_t file_store = 0;
1136  uint32_t warn_pcre_no_content = 0;
1137  uint32_t warn_pcre_http_content = 0;
1138  uint32_t warn_pcre_http = 0;
1139  uint32_t warn_content_http_content = 0;
1140  uint32_t warn_content_http = 0;
1141  uint32_t warn_tcp_no_flow = 0;
1142  uint32_t warn_client_ports = 0;
1143  uint32_t warn_direction = 0;
1144  uint32_t warn_method_toclient = 0;
1145  uint32_t warn_method_serverbody = 0;
1146  uint32_t warn_pcre_method = 0;
1147  uint32_t warn_encoding_norm_http_buf = 0;
1148  uint32_t warn_file_store_not_present = 0;
1149  uint32_t warn_offset_depth_pkt_stream = 0;
1150  uint32_t warn_offset_depth_alproto = 0;
1151  uint32_t warn_non_alproto_fp_for_alproto_sig = 0;
1152  uint32_t warn_no_direction = 0;
1153  uint32_t warn_both_direction = 0;
1154 
1155  EngineAnalysisItemsInit();
1156 
1157  bool *http_method_item_seen_ptr = exposed_item_seen_list[0].item_seen_ptr;
1158  bool *http_server_body_item_seen_ptr = exposed_item_seen_list[1].item_seen_ptr;
1159 
1161  rule_bidirectional = 1;
1162  }
1163 
1164  if (s->flags & SIG_FLAG_REQUIRE_PACKET) {
1165  packet_buf += 1;
1166  }
1167  if (s->flags & SIG_FLAG_FILESTORE) {
1168  file_store += 1;
1169  }
1170  if (s->flags & SIG_FLAG_REQUIRE_STREAM) {
1171  stream_buf += 1;
1172  }
1173 
1174  if (s->proto.flags & DETECT_PROTO_IPV4) {
1175  rule_ipv4_only += 1;
1176  }
1177  if (s->proto.flags & DETECT_PROTO_IPV6) {
1178  rule_ipv6_only += 1;
1179  }
1180 
1181  for (list_id = 0; list_id < (int)s->init_data->smlists_array_size; list_id++) {
1182  SigMatch *sm = NULL;
1183  for (sm = s->init_data->smlists[list_id]; sm != NULL; sm = sm->next) {
1184  int16_t item_slot = analyzer_item_map[list_id];
1185  if (sm->type == DETECT_PCRE) {
1186  if (item_slot == -1) {
1187  rule_pcre++;
1188  continue;
1189  }
1190 
1191  rule_pcre_http++;
1192  analyzer_items[item_slot].item_seen = true;
1193  } else if (sm->type == DETECT_CONTENT) {
1194  if (item_slot == -1) {
1195  rule_content++;
1196  if (list_id == DETECT_SM_LIST_PMATCH) {
1199  rule_content_offset_depth++;
1200  }
1201  }
1202  continue;
1203  }
1204 
1205  rule_content_http++;
1206  analyzer_items[item_slot].item_seen = true;
1207 
1208  if (analyzer_items[item_slot].check_encoding_match) {
1210  if (cd != NULL && PerCentEncodingMatch(cd->content, cd->content_len) > 0) {
1211  warn_encoding_norm_http_buf += 1;
1212  }
1213  }
1214  }
1215  else if (sm->type == DETECT_FLOW) {
1216  rule_flow += 1;
1217  if ((s->flags & SIG_FLAG_TOSERVER) && !(s->flags & SIG_FLAG_TOCLIENT)) {
1218  rule_flow_toserver = 1;
1219  }
1220  else if ((s->flags & SIG_FLAG_TOCLIENT) && !(s->flags & SIG_FLAG_TOSERVER)) {
1221  rule_flow_toclient = 1;
1222  }
1223  DetectFlowData *fd = (DetectFlowData *)sm->ctx;
1224  if (fd != NULL) {
1225  if (fd->flags & DETECT_FLOW_FLAG_NOSTREAM)
1226  rule_flow_nostream = 1;
1227  }
1228  }
1229  else if (sm->type == DETECT_FLOWBITS) {
1230  if (list_id == DETECT_SM_LIST_MATCH) {
1231  rule_flowbits += 1;
1232  }
1233  }
1234  else if (sm->type == DETECT_FLOWINT) {
1235  if (list_id == DETECT_SM_LIST_MATCH) {
1236  rule_flowint += 1;
1237  }
1238  }
1239  else if (sm->type == DETECT_FLAGS) {
1240  DetectFlagsData *fd = (DetectFlagsData *)sm->ctx;
1241  if (fd != NULL) {
1242  rule_flags = 1;
1243  }
1244  }
1245  } /* for (sm = s->sm_lists[list_id]; sm != NULL; sm = sm->next) */
1246 
1247  } /* for ( ; list_id < DETECT_SM_LIST_MAX; list_id++) */
1248 
1249 
1250  if (file_store && !RequiresFeature("output::file-store")) {
1251  rule_warning += 1;
1252  warn_file_store_not_present = 1;
1253  }
1254 
1255  if (rule_pcre > 0 && rule_content == 0 && rule_content_http == 0) {
1256  rule_warning += 1;
1257  warn_pcre_no_content = 1;
1258  }
1259 
1260  if (rule_content_http > 0 && rule_pcre > 0 && rule_pcre_http == 0) {
1261  rule_warning += 1;
1262  warn_pcre_http_content = 1;
1263  } else if (s->alproto == ALPROTO_HTTP1 && rule_pcre > 0 && rule_pcre_http == 0) {
1264  rule_warning += 1;
1265  warn_pcre_http = 1;
1266  }
1267 
1268  if (rule_content > 0 && rule_content_http > 0) {
1269  rule_warning += 1;
1270  warn_content_http_content = 1;
1271  }
1272  if (s->alproto == ALPROTO_HTTP1 && rule_content > 0 && rule_content_http == 0) {
1273  rule_warning += 1;
1274  warn_content_http = 1;
1275  }
1276  if (rule_content == 1) {
1277  //todo: warning if content is weak, separate warning for pcre + weak content
1278  }
1279  if (rule_flow == 0 && rule_flags == 0 && !(s->proto.flags & DETECT_PROTO_ANY) &&
1280  DetectProtoContainsProto(&s->proto, IPPROTO_TCP) &&
1281  (rule_content || rule_content_http || rule_pcre || rule_pcre_http || rule_flowbits ||
1282  rule_flowint)) {
1283  rule_warning += 1;
1284  warn_tcp_no_flow = 1;
1285  }
1286  if (rule_flow && !rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)
1287  && !((s->flags & SIG_FLAG_SP_ANY) && (s->flags & SIG_FLAG_DP_ANY))) {
1288  if (((s->flags & SIG_FLAG_TOSERVER) && !(s->flags & SIG_FLAG_SP_ANY) && (s->flags & SIG_FLAG_DP_ANY))
1289  || ((s->flags & SIG_FLAG_TOCLIENT) && !(s->flags & SIG_FLAG_DP_ANY) && (s->flags & SIG_FLAG_SP_ANY))) {
1290  rule_warning += 1;
1291  warn_client_ports = 1;
1292  }
1293  }
1294  if (rule_flow && rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)) {
1295  rule_warning += 1;
1296  warn_direction = 1;
1297  }
1298 
1299  if (*http_method_item_seen_ptr) {
1300  if (rule_flow && rule_flow_toclient) {
1301  rule_warning += 1;
1302  warn_method_toclient = 1;
1303  }
1304  if (*http_server_body_item_seen_ptr) {
1305  rule_warning += 1;
1306  warn_method_serverbody = 1;
1307  }
1308  if (rule_content == 0 && rule_content_http == 0 && (rule_pcre > 0 || rule_pcre_http > 0)) {
1309  rule_warning += 1;
1310  warn_pcre_method = 1;
1311  }
1312  }
1313  if (rule_content_offset_depth > 0 && stream_buf && packet_buf) {
1314  rule_warning += 1;
1315  warn_offset_depth_pkt_stream = 1;
1316  }
1317  if (rule_content_offset_depth > 0 && !stream_buf && packet_buf && s->alproto != ALPROTO_UNKNOWN) {
1318  rule_warning += 1;
1319  warn_offset_depth_alproto = 1;
1320  }
1321  if (s->init_data->mpm_sm != NULL && s->alproto == ALPROTO_HTTP1 &&
1323  rule_warning += 1;
1324  warn_non_alproto_fp_for_alproto_sig = 1;
1325  }
1326 
1327  if ((s->flags & (SIG_FLAG_TOSERVER|SIG_FLAG_TOCLIENT)) == 0) {
1328  warn_no_direction += 1;
1329  rule_warning += 1;
1330  }
1331 
1332  /* No warning about direction for ICMP protos */
1333  if (!(DetectProtoContainsProto(&s->proto, IPPROTO_ICMPV6) && DetectProtoContainsProto(&s->proto, IPPROTO_ICMP))) {
1335  warn_both_direction += 1;
1336  rule_warning += 1;
1337  }
1338  }
1339 
1340  if (!rule_warnings_only || (rule_warnings_only && rule_warning > 0)) {
1341  fprintf(rule_engine_analysis_FD, "== Sid: %u ==\n", s->id);
1342  fprintf(rule_engine_analysis_FD, "%s\n", line);
1343 
1344  if (s->flags & SIG_FLAG_IPONLY) fprintf(rule_engine_analysis_FD, " Rule is ip only.\n");
1345  if (s->flags & SIG_FLAG_PDONLY) fprintf(rule_engine_analysis_FD, " Rule is PD only.\n");
1346  if (rule_ipv6_only) fprintf(rule_engine_analysis_FD, " Rule is IPv6 only.\n");
1347  if (rule_ipv4_only) fprintf(rule_engine_analysis_FD, " Rule is IPv4 only.\n");
1348  if (packet_buf) fprintf(rule_engine_analysis_FD, " Rule matches on packets.\n");
1349  if (!rule_flow_nostream && stream_buf &&
1350  (rule_flow || rule_flowbits || rule_flowint || rule_content || rule_pcre)) {
1351  fprintf(rule_engine_analysis_FD, " Rule matches on reassembled stream.\n");
1352  }
1353  for(size_t i = 0; i < ARRAY_SIZE(analyzer_items); i++) {
1355  if (ai->item_seen) {
1356  fprintf(rule_engine_analysis_FD, " Rule matches on %s buffer.\n", ai->display_name);
1357  }
1358  }
1359  if (s->alproto != ALPROTO_UNKNOWN) {
1360  fprintf(rule_engine_analysis_FD, " App layer protocol is %s.\n", AppProtoToString(s->alproto));
1361  }
1362  if (rule_content || rule_content_http || rule_pcre || rule_pcre_http) {
1363  fprintf(rule_engine_analysis_FD,
1364  " Rule contains %u content options, %u http content options, %u pcre "
1365  "options, and %u pcre options with http modifiers.\n",
1366  rule_content, rule_content_http, rule_pcre, rule_pcre_http);
1367  }
1368 
1369  /* print fast pattern info */
1370  if (s->init_data->prefilter_sm) {
1371  fprintf(rule_engine_analysis_FD, " Prefilter on: %s.\n",
1373  } else {
1374  EngineAnalysisRulesPrintFP(de_ctx, s);
1375  }
1376 
1377  /* this is where the warnings start */
1378  if (warn_pcre_no_content /*rule_pcre > 0 && rule_content == 0 && rule_content_http == 0*/) {
1379  fprintf(rule_engine_analysis_FD, " Warning: Rule uses pcre without a content option present.\n"
1380  " -Consider adding a content to improve performance of this rule.\n");
1381  }
1382  if (warn_pcre_http_content /*rule_content_http > 0 && rule_pcre > 0 && rule_pcre_http == 0*/) {
1383  fprintf(rule_engine_analysis_FD, " Warning: Rule uses content options with http_* and pcre options without http modifiers.\n"
1384  " -Consider adding http pcre modifier.\n");
1385  }
1386  else if (warn_pcre_http /*s->alproto == ALPROTO_HTTP1 && rule_pcre > 0 && rule_pcre_http == 0*/) {
1387  fprintf(rule_engine_analysis_FD, " Warning: Rule app layer protocol is http, but pcre options do not have http modifiers.\n"
1388  " -Consider adding http pcre modifiers.\n");
1389  }
1390  if (warn_content_http_content /*rule_content > 0 && rule_content_http > 0*/) {
1391  fprintf(rule_engine_analysis_FD, " Warning: Rule contains content with http_* and content without http_*.\n"
1392  " -Consider adding http content modifiers.\n");
1393  }
1394  if (warn_content_http /*s->alproto == ALPROTO_HTTP1 && rule_content > 0 && rule_content_http == 0*/) {
1395  fprintf(rule_engine_analysis_FD, " Warning: Rule app layer protocol is http, but content options do not have http_* modifiers.\n"
1396  " -Consider adding http content modifiers.\n");
1397  }
1398  if (rule_content == 1) {
1399  //todo: warning if content is weak, separate warning for pcre + weak content
1400  }
1401  if (warn_encoding_norm_http_buf) {
1402  fprintf(rule_engine_analysis_FD, " Warning: Rule may contain percent encoded content for a normalized http buffer match.\n");
1403  }
1404  if (warn_tcp_no_flow /*rule_flow == 0 && rule_flags == 0
1405  && !(s->proto.flags & DETECT_PROTO_ANY) && DetectProtoContainsProto(&s->proto, IPPROTO_TCP)*/) {
1406  fprintf(rule_engine_analysis_FD, " Warning: TCP rule without a flow or flags option.\n"
1407  " -Consider adding flow or flags to improve performance of this rule.\n");
1408  }
1409  if (warn_client_ports /*rule_flow && !rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)
1410  && !((s->flags & SIG_FLAG_SP_ANY) && (s->flags & SIG_FLAG_DP_ANY)))
1411  if (((s->flags & SIG_FLAG_TOSERVER) && !(s->flags & SIG_FLAG_SP_ANY) && (s->flags & SIG_FLAG_DP_ANY))
1412  || ((s->flags & SIG_FLAG_TOCLIENT) && !(s->flags & SIG_FLAG_DP_ANY) && (s->flags & SIG_FLAG_SP_ANY))*/) {
1413  fprintf(rule_engine_analysis_FD, " Warning: Rule contains ports or port variables only on the client side.\n"
1414  " -Flow direction possibly inconsistent with rule.\n");
1415  }
1416  if (warn_direction /*rule_flow && rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)*/) {
1417  fprintf(rule_engine_analysis_FD, " Warning: Rule is bidirectional and has a flow option with a specific direction.\n");
1418  }
1419  if (warn_method_toclient /*http_method_buf && rule_flow && rule_flow_toclient*/) {
1420  fprintf(rule_engine_analysis_FD, " Warning: Rule uses content or pcre for http_method with flow:to_client or from_server\n");
1421  }
1422  if (warn_method_serverbody /*http_method_buf && http_server_body_buf*/) {
1423  fprintf(rule_engine_analysis_FD, " Warning: Rule uses content or pcre for http_method with content or pcre for http_server_body.\n");
1424  }
1425  if (warn_pcre_method /*http_method_buf && rule_content == 0 && rule_content_http == 0
1426  && (rule_pcre > 0 || rule_pcre_http > 0)*/) {
1427  fprintf(rule_engine_analysis_FD, " Warning: Rule uses pcre with only a http_method content; possible performance issue.\n");
1428  }
1429  if (warn_offset_depth_pkt_stream) {
1430  fprintf(rule_engine_analysis_FD, " Warning: Rule has depth"
1431  "/offset with raw content keywords. Please note the "
1432  "offset/depth will be checked against both packet "
1433  "payloads and stream. If you meant to have the offset/"
1434  "depth checked against just the payload, you can update "
1435  "the signature as \"alert tcp-pkt...\"\n");
1436  }
1437  if (warn_offset_depth_alproto) {
1438  fprintf(rule_engine_analysis_FD, " Warning: Rule has "
1439  "offset/depth set along with a match on a specific "
1440  "app layer protocol - %d. This can lead to FNs if we "
1441  "have a offset/depth content match on a packet payload "
1442  "before we can detect the app layer protocol for the "
1443  "flow.\n", s->alproto);
1444  }
1445  if (warn_non_alproto_fp_for_alproto_sig) {
1446  fprintf(rule_engine_analysis_FD, " Warning: Rule app layer "
1447  "protocol is http, but the fast_pattern is set on the raw "
1448  "stream. Consider adding fast_pattern over a http "
1449  "buffer for increased performance.");
1450  }
1451  if (warn_no_direction) {
1452  fprintf(rule_engine_analysis_FD, " Warning: Rule has no direction indicator.\n");
1453  }
1454  if (warn_both_direction) {
1455  fprintf(rule_engine_analysis_FD, " Warning: Rule is inspecting both the request and the response.\n");
1456  }
1457  if (warn_file_store_not_present) {
1458  fprintf(rule_engine_analysis_FD, " Warning: Rule requires file-store but the output file-store is not enabled.\n");
1459  }
1460  if (rule_warning == 0) {
1461  fprintf(rule_engine_analysis_FD, " No warnings for this rule.\n");
1462  }
1463  fprintf(rule_engine_analysis_FD, "\n");
1464  }
1465  return;
1466 }
detect-tcp-flags.h
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
SignatureHasPacketContent
int SignatureHasPacketContent(const Signature *s)
check if a signature has patterns that are to be inspected against a packets payload (as opposed to t...
Definition: detect-engine-mpm.c:772
RuleAnalyzer::js
JsonBuilder * js
Definition: detect-engine-analyzer.c:562
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:59
DetectContentData_::offset
uint16_t offset
Definition: detect-content.h:100
DetectPatternTracker
Definition: detect.h:669
DetectEngineAppInspectionEngine_
Definition: detect.h:390
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:394
detect-content.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
detect-engine.h
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:79
DETECT_CONTENT_FAST_PATTERN_CHOP
#define DETECT_CONTENT_FAST_PATTERN_CHOP
Definition: detect-content.h:36
DetectContentData_::fp_chop_len
uint16_t fp_chop_len
Definition: detect-content.h:91
Signature_::sig_str
char * sig_str
Definition: detect.h:609
DetectEnginePktInspectionEngine
Definition: detect.h:443
DetectEngineAppInspectionEngine_::next
struct DetectEngineAppInspectionEngine_ * next
Definition: detect.h:409
DETECT_PROTO_IPV6
#define DETECT_PROTO_IPV6
Definition: detect-engine-proto.h:34
SIG_FLAG_PDONLY
#define SIG_FLAG_PDONLY
Definition: detect.h:239
DetectFlowData_
Definition: detect-flow.h:37
DETECT_FLOW_FLAG_NOSTREAM
#define DETECT_FLOW_FLAG_NOSTREAM
Definition: detect-flow.h:33
SigTableElmt_::name
const char * name
Definition: detect.h:1238
DetectPatternTracker::mpm
uint32_t mpm
Definition: detect.h:673
ConfGetBool
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
Definition: conf.c:482
DetectListToHumanString
const char * DetectListToHumanString(int list)
Definition: detect-parse.c:117
DetectEngineCtx_::pattern_hash_table
HashListTable * pattern_hash_table
Definition: detect.h:827
DumpPatterns
void DumpPatterns(DetectEngineCtx *de_ctx)
Definition: detect-engine-analyzer.c:978
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectContentData_::within
int32_t within
Definition: detect-content.h:102
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:62
SignatureInitData_::prefilter_sm
SigMatch * prefilter_sm
Definition: detect.h:514
DETECT_FLOW
@ DETECT_FLOW
Definition: detect-engine-register.h:54
Signature_::alproto
AppProto alproto
Definition: detect.h:544
CleanupFPAnalyzer
void CleanupFPAnalyzer(void)
Definition: detect-engine-analyzer.c:392
RuleAnalyzer::js_warnings
JsonBuilder * js_warnings
Definition: detect-engine-analyzer.c:564
g_rules_analyzer_write_m
SCMutex g_rules_analyzer_write_m
Definition: detect-engine-analyzer.c:688
DetectEngineAnalyzerItems::display_name
const char * display_name
Definition: detect-engine-analyzer.c:66
DETECT_SM_LIST_DYNAMIC_START
@ DETECT_SM_LIST_DYNAMIC_START
Definition: detect.h:98
SIG_FLAG_DEST_IS_TARGET
#define SIG_FLAG_DEST_IS_TARGET
Definition: detect.h:243
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:325
DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED
#define DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED
Definition: detect-content.h:55
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:30
DetectFlowData_::flags
uint16_t flags
Definition: detect-flow.h:38
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
DETECT_PROTO_ANY
#define DETECT_PROTO_ANY
Definition: detect-engine-proto.h:27
DetectEnginePktInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:444
DetectEngineFrameInspectionEngine::mpm
bool mpm
Definition: detect.h:472
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:298
DetectEngineBufferTypeGetNameById
const char * DetectEngineBufferTypeGetNameById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1109
SIG_FLAG_DST_ANY
#define SIG_FLAG_DST_ANY
Definition: detect.h:201
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
SIG_FLAG_REQUIRE_STREAM
#define SIG_FLAG_REQUIRE_STREAM
Definition: detect.h:216
DetectPatternTracker::cnt
uint32_t cnt
Definition: detect.h:672
rust.h
DetectEngineAnalyzerItems
Definition: detect-engine-analyzer.c:60
DetectPatternTracker::cd
const struct DetectContentData_ * cd
Definition: detect.h:670
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
Signature_::sm_arrays
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:595
FpPatternStats_::max
uint16_t max
Definition: detect-engine-analyzer.c:54
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:503
DetectBufferType_
Definition: detect.h:412
DetectContentData_
Definition: detect-content.h:86
DetectContentData_::fp_chop_offset
uint16_t fp_chop_offset
Definition: detect-content.h:93
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:229
SIG_FLAG_SRC_ANY
#define SIG_FLAG_SRC_ANY
Definition: detect.h:200
SigMatchData_
Data needed for Match()
Definition: detect.h:322
SigMatchData_::type
uint16_t type
Definition: detect.h:323
Signature_::frame_inspect
DetectEngineFrameInspectionEngine * frame_inspect
Definition: detect.h:591
CleanupRuleAnalyzer
void CleanupRuleAnalyzer(void)
Definition: detect-engine-analyzer.c:416
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:531
DETECT_PERCENT_ENCODING_REGEX
#define DETECT_PERCENT_ENCODING_REGEX
DetectEngineBufferTypeGetById
const DetectBufferType * DetectEngineBufferTypeGetById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1099
SIG_FLAG_APPLAYER
#define SIG_FLAG_APPLAYER
Definition: detect.h:207
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1079
analyzer_item_map
int16_t analyzer_item_map[256]
Definition: detect-engine-analyzer.c:122
Signature_::gid
uint32_t gid
Definition: detect.h:575
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:396
ATTR_FMT_PRINTF
#define ATTR_FMT_PRINTF(x, y)
Definition: suricata-common.h:399
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:58
DetectEngineAnalyzerItems::export_item_seen
bool export_item_seen
Definition: detect-engine-analyzer.c:63
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
RequiresFeature
bool RequiresFeature(const char *feature_name)
Definition: feature.c:126
feature.h
DETECT_CONTENT_ENDS_WITH
#define DETECT_CONTENT_ENDS_WITH
Definition: detect-content.h:42
DETECT_CONTENT_DISTANCE
#define DETECT_CONTENT_DISTANCE
Definition: detect-content.h:30
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEnginePktInspectionEngine::sm_list
uint16_t sm_list
Definition: detect.h:446
SIG_FLAG_INIT_BIDIREC
#define SIG_FLAG_INIT_BIDIREC
Definition: detect.h:251
DETECT_FLOWINT
@ DETECT_FLOWINT
Definition: detect-engine-register.h:87
SIG_MASK_REQUIRE_ENGINE_EVENT
#define SIG_MASK_REQUIRE_ENGINE_EVENT
Definition: detect.h:269
SignatureInitData_::mpm_sm
SigMatch * mpm_sm
Definition: detect.h:512
DetectEngineBufferTypeGetDescriptionById
const char * DetectEngineBufferTypeGetDescriptionById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1187
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
SIG_FLAG_FLUSH
#define SIG_FLAG_FLUSH
Definition: detect.h:220
SignatureInitData_::mpm_sm_list
int mpm_sm_list
Definition: detect.h:510
SIG_MASK_REQUIRE_FLOW
#define SIG_MASK_REQUIRE_FLOW
Definition: detect.h:263
SIG_FLAG_BYPASS
#define SIG_FLAG_BYPASS
Definition: detect.h:233
DETECT_CONTENT_DEPTH
#define DETECT_CONTENT_DEPTH
Definition: detect-content.h:33
RuleAnalyzer
Definition: detect-engine-analyzer.c:561
Signature_::pkt_inspect
DetectEnginePktInspectionEngine * pkt_inspect
Definition: detect.h:590
DetectEngineAnalyzerItems
struct DetectEngineAnalyzerItems DetectEngineAnalyzerItems
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
SIG_MASK_REQUIRE_FLAGS_INITDEINIT
#define SIG_MASK_REQUIRE_FLAGS_INITDEINIT
Definition: detect.h:264
DetectEngineFrameInspectionEngine::sm_list
uint16_t sm_list
Definition: detect.h:473
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:317
DetectEngineAnalyzerItems::item_seen
bool item_seen
Definition: detect-engine-analyzer.c:62
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
util-time.h
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:78
EngineAnalysisRules
void EngineAnalysisRules(const DetectEngineCtx *de_ctx, const Signature *s, const char *line)
Prints analysis of loaded rules.
Definition: detect-engine-analyzer.c:1113
Signature_::app_inspect
DetectEngineAppInspectionEngine * app_inspect
Definition: detect.h:589
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:316
SIG_FLAG_REQUIRE_FLOWVAR
#define SIG_FLAG_REQUIRE_FLOWVAR
Definition: detect.h:224
FpPatternStats_::cnt
uint32_t cnt
Definition: detect-engine-analyzer.c:55
SCReturn
#define SCReturn
Definition: util-debug.h:273
Signature_::flags
uint32_t flags
Definition: detect.h:541
DetectContentData_::depth
uint16_t depth
Definition: detect-content.h:99
SIG_FLAG_IPONLY
#define SIG_FLAG_IPONLY
Definition: detect.h:208
SCLocalTime
struct tm * SCLocalTime(time_t timep, struct tm *result)
Definition: util-time.c:267
conf.h
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:97
CHECK
#define CHECK(pat)
Definition: detect-engine-analyzer.c:598
DetectEngineFrameInspectionEngine
Definition: detect.h:468
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:611
SIG_FLAG_SRC_IS_TARGET
#define SIG_FLAG_SRC_IS_TARGET
Definition: detect.h:241
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:534
DetectContentPatternPrettyPrint
void DetectContentPatternPrettyPrint(const DetectContentData *cd, char *str, size_t str_len)
Definition: detect-content.c:719
SIG_MASK_REQUIRE_PAYLOAD
#define SIG_MASK_REQUIRE_PAYLOAD
Definition: detect.h:262
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
DETECT_PCRE
@ DETECT_PCRE
Definition: detect-engine-register.h:64
ExposedItemSeen::bufname
const char * bufname
Definition: detect-engine-analyzer.c:71
DetectSigmatchListEnumToString
const char * DetectSigmatchListEnumToString(enum DetectSigmatchListEnum type)
Definition: detect-engine.c:4618
DetectEngineAppInspectionEngine_::alproto
AppProto alproto
Definition: detect.h:391
SIG_FLAG_MPM_NEG
#define SIG_FLAG_MPM_NEG
Definition: detect.h:218
detect-engine-analyzer.h
SIG_FLAG_INIT_STATE_MATCH
#define SIG_FLAG_INIT_STATE_MATCH
Definition: detect.h:254
DetectEngineAnalyzerItems::item_id
int16_t item_id
Definition: detect-engine-analyzer.c:61
DetectPatternTracker::sm_list
int sm_list
Definition: detect.h:671
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:407
EngineAnalysisRules2
void EngineAnalysisRules2(const DetectEngineCtx *de_ctx, const Signature *s)
Definition: detect-engine-analyzer.c:689
ARRAY_SIZE
#define ARRAY_SIZE(arr)
Definition: suricata-common.h:535
DETECT_CONTENT_STARTS_WITH
#define DETECT_CONTENT_STARTS_WITH
Definition: detect-content.h:59
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:38
DETECT_PROTO_IPV4
#define DETECT_PROTO_IPV4
Definition: detect-engine-proto.h:33
util-conf.h
SignatureHasStreamContent
int SignatureHasStreamContent(const Signature *s)
check if a signature has patterns that are to be inspected against the stream payload (as opposed to ...
Definition: detect-engine-mpm.c:808
RuleAnalyzer::js_notes
JsonBuilder * js_notes
Definition: detect-engine-analyzer.c:565
EngineAnalysisFP
void EngineAnalysisFP(const DetectEngineCtx *de_ctx, const Signature *s, char *line)
Definition: detect-engine-analyzer.c:154
FpPatternStats_::min
uint16_t min
Definition: detect-engine-analyzer.c:53
Signature_::proto
DetectProto proto
Definition: detect.h:558
SigMatchData_::is_last
uint8_t is_last
Definition: detect.h:324
suricata-common.h
SIG_MASK_REQUIRE_NO_PAYLOAD
#define SIG_MASK_REQUIRE_NO_PAYLOAD
Definition: detect.h:266
SIG_FLAG_SP_ANY
#define SIG_FLAG_SP_ANY
Definition: detect.h:202
ExposedItemSeen
Definition: detect-engine-analyzer.c:70
SigMatch_::type
uint16_t type
Definition: detect.h:314
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:90
DetectEngineAnalyzerItems::check_encoding_match
bool check_encoding_match
Definition: detect-engine-analyzer.c:64
DetectContentData_::distance
int32_t distance
Definition: detect-content.h:101
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
DetectEngineFrameInspectionEngine::next
struct DetectEngineFrameInspectionEngine * next
Definition: detect.h:481
DetectEnginePktInspectionEngine::next
struct DetectEnginePktInspectionEngine * next
Definition: detect.h:454
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:87
Signature_::rev
uint32_t rev
Definition: detect.h:576
SetupFPAnalyzer
int SetupFPAnalyzer(void)
Sets up the fast pattern analyzer according to the config.
Definition: detect-engine-analyzer.c:287
FatalError
#define FatalError(...)
Definition: util-debug.h:502
SIG_FLAG_NOALERT
#define SIG_FLAG_NOALERT
Definition: detect.h:205
SIG_MASK_REQUIRE_FLAGS_UNUSUAL
#define SIG_MASK_REQUIRE_FLAGS_UNUSUAL
Definition: detect.h:265
EngineAnalysisRulesFailure
void EngineAnalysisRulesFailure(char *line, char *file, int lineno)
Definition: detect-engine-analyzer.c:551
DETECT_FLOWBITS
@ DETECT_FLOWBITS
Definition: detect-engine-register.h:90
DetectEngineAppInspectionEngine_::progress
int16_t progress
Definition: detect.h:398
util-validate.h
detect-flow.h
PrintRawUriFp
void PrintRawUriFp(FILE *fp, uint8_t *buf, uint32_t buflen)
Definition: util-print.c:95
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
ConfigGetLogDirectory
const char * ConfigGetLogDirectory(void)
Definition: util-conf.c:37
FpPatternStats
struct FpPatternStats_ FpPatternStats
str
#define str(s)
Definition: suricata-common.h:280
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SigMatchListSMBelongsTo
int SigMatchListSMBelongsTo(const Signature *s, const SigMatch *key_sm)
Definition: detect-parse.c:631
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
Signature_::id
uint32_t id
Definition: detect.h:574
DETECT_CONTENT_OFFSET
#define DETECT_CONTENT_OFFSET
Definition: detect-content.h:32
HashListTableBucket_
Definition: util-hashlist.h:28
DETECT_CONTENT_FAST_PATTERN_ONLY
#define DETECT_CONTENT_FAST_PATTERN_ONLY
Definition: detect-content.h:35
DETECT_CONTENT_MPM
#define DETECT_CONTENT_MPM
Definition: detect-content.h:61
DetectBufferType_::transforms
DetectEngineTransforms transforms
Definition: detect.h:423
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
SigMatch_
a single match condition for a signature
Definition: detect.h:313
DETECT_SM_LIST_MAX
@ DETECT_SM_LIST_MAX
Definition: detect.h:95
FpPatternStats_::tot
uint64_t tot
Definition: detect-engine-analyzer.c:56
DetectFlagsData_
Definition: detect-tcp-flags.h:38
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
exposed_item_seen_list
struct ExposedItemSeen exposed_item_seen_list[]
Definition: detect-engine-analyzer.c:128
DetectEngineTransforms::cnt
int cnt
Definition: detect.h:374
suricata.h
SetupRuleAnalyzer
int SetupRuleAnalyzer(void)
Sets up the rule analyzer according to the config.
Definition: detect-engine-analyzer.c:336
DetectEngineAppInspectionEngine_::dir
uint8_t dir
Definition: detect.h:392
DetectEngineAnalyzerItems::item_name
const char * item_name
Definition: detect-engine-analyzer.c:65
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:88
ExposedItemSeen::item_seen_ptr
bool * item_seen_ptr
Definition: detect-engine-analyzer.c:72
DetectEngineCtx_::buffer_type_id
uint32_t buffer_type_id
Definition: detect.h:947
DetectEnginePktInspectionEngine::mpm
bool mpm
Definition: detect.h:445
PerCentEncodingSetup
int PerCentEncodingSetup(void)
Compiles regex for rule analysis.
Definition: detect-engine-analyzer.c:434
RuleAnalyzer
struct RuleAnalyzer RuleAnalyzer
SIG_FLAG_TLSSTORE
#define SIG_FLAG_TLSSTORE
Definition: detect.h:231
Signature_::msg
char * msg
Definition: detect.h:600
DETECT_CONTENT_FAST_PATTERN
#define DETECT_CONTENT_FAST_PATTERN
Definition: detect-content.h:34
DETECT_FLAGS
@ DETECT_FLAGS
Definition: detect-engine-register.h:42
FpPatternStats_
Definition: detect-engine-analyzer.c:52
analyzer_items
DetectEngineAnalyzerItems analyzer_items[]
Definition: detect-engine-analyzer.c:75
SCMutex
#define SCMutex
Definition: threads-debug.h:114
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
SIG_FLAG_PREFILTER
#define SIG_FLAG_PREFILTER
Definition: detect.h:235
SIG_MASK_REQUIRE_DCERPC
#define SIG_MASK_REQUIRE_DCERPC
Definition: detect.h:267
SIG_FLAG_FILESTORE
#define SIG_FLAG_FILESTORE
Definition: detect.h:226
DETECT_CONTENT_WITHIN
#define DETECT_CONTENT_WITHIN
Definition: detect-content.h:31
SIG_FLAG_DP_ANY
#define SIG_FLAG_DP_ANY
Definition: detect.h:203
SIG_FLAG_DSIZE
#define SIG_FLAG_DSIZE
Definition: detect.h:206
Signature_::mask
SignatureMask mask
Definition: detect.h:550
SignatureInitData_::smlists_array_size
uint32_t smlists_array_size
Definition: detect.h:532
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:135
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:215
DetectEngineFrameInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:480
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:808