suricata
detect-pcre.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  *
23  * Implements the pcre keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 #include "detect.h"
29 
30 #include "pkt-var.h"
31 #include "flow-var.h"
32 #include "flow-util.h"
33 
34 #include "detect-pcre.h"
35 #include "detect-flowvar.h"
36 
37 #include "detect-parse.h"
38 #include "detect-content.h"
39 #include "detect-engine.h"
40 #include "detect-engine-sigorder.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-helper.h"
47 #include "util-debug.h"
48 #include "util-unittest.h"
49 #include "util-print.h"
50 #include "util-pool.h"
51 
52 #include "conf.h"
53 #include "app-layer.h"
54 #include "app-layer-htp.h"
55 #include "stream.h"
56 #include "stream-tcp.h"
57 #include "stream-tcp-private.h"
58 #include "stream-tcp-reassemble.h"
59 #include "app-layer-protos.h"
60 #include "app-layer-parser.h"
61 #include "util-pages.h"
62 
63 /* pcre named substring capture supports only 32byte names, A-z0-9 plus _
64  * and needs to start with non-numeric. */
65 #define PARSE_CAPTURE_REGEX "\\(\\?P\\<([A-z]+)\\_([A-z0-9_]+)\\>"
66 #define PARSE_REGEX "(?<!\\\\)/(.*(?<!(?<!\\\\)\\\\))/([^\"]*)"
67 
68 static int pcre_match_limit = 0;
69 static int pcre_match_limit_recursion = 0;
70 
71 static DetectParseRegex *parse_regex;
72 static DetectParseRegex *parse_capture_regex;
73 
74 #ifdef PCRE2_HAVE_JIT
75 static int pcre2_use_jit = 1;
76 #endif
77 
78 // TODOpcre2 pcre2_jit_stack_create ?
79 
80 /* \brief Helper function for using pcre2_match with/without JIT
81  */
82 static inline int DetectPcreExec(DetectEngineThreadCtx *det_ctx, DetectPcreData *pd,
83  const char *str, const size_t strlen, int start_offset, int options,
84  pcre2_match_data *match)
85 {
86  return pcre2_match(pd->parse_regex.regex, (PCRE2_SPTR8)str, strlen, start_offset, options,
88 }
89 
90 static int DetectPcreSetup (DetectEngineCtx *, Signature *, const char *);
91 static void DetectPcreFree(DetectEngineCtx *, void *);
92 #ifdef UNITTESTS
93 static void DetectPcreRegisterTests(void);
94 #endif
95 
96 void DetectPcreRegister (void)
97 {
99  sigmatch_table[DETECT_PCRE].desc = "match on regular expression";
100  sigmatch_table[DETECT_PCRE].url = "/rules/payload-keywords.html#pcre-perl-compatible-regular-expressions";
102  sigmatch_table[DETECT_PCRE].Setup = DetectPcreSetup;
103  sigmatch_table[DETECT_PCRE].Free = DetectPcreFree;
104 #ifdef UNITTESTS
105  sigmatch_table[DETECT_PCRE].RegisterTests = DetectPcreRegisterTests;
106 #endif
108 
109  intmax_t val = 0;
110 
111  if (!ConfGetInt("pcre.match-limit", &val)) {
112  pcre_match_limit = SC_MATCH_LIMIT_DEFAULT;
113  SCLogDebug("Using PCRE match-limit setting of: %i", pcre_match_limit);
114  }
115  else {
116  pcre_match_limit = val;
117  if (pcre_match_limit != SC_MATCH_LIMIT_DEFAULT) {
118  SCLogInfo("Using PCRE match-limit setting of: %i", pcre_match_limit);
119  } else {
120  SCLogDebug("Using PCRE match-limit setting of: %i", pcre_match_limit);
121  }
122  }
123 
124  val = 0;
125 
126  if (!ConfGetInt("pcre.match-limit-recursion", &val)) {
127  pcre_match_limit_recursion = SC_MATCH_LIMIT_RECURSION_DEFAULT;
128  SCLogDebug("Using PCRE match-limit-recursion setting of: %i", pcre_match_limit_recursion);
129  }
130  else {
131  pcre_match_limit_recursion = val;
132  if (pcre_match_limit_recursion != SC_MATCH_LIMIT_RECURSION_DEFAULT) {
133  SCLogInfo("Using PCRE match-limit-recursion setting of: %i", pcre_match_limit_recursion);
134  } else {
135  SCLogDebug("Using PCRE match-limit-recursion setting of: %i", pcre_match_limit_recursion);
136  }
137  }
138 
139  parse_regex = DetectSetupPCRE2(PARSE_REGEX, 0);
140  if (parse_regex == NULL) {
141  FatalError("pcre2 compile failed for parse_regex");
142  }
143 
144  /* setup the capture regex, as it needs PCRE2_UNGREEDY we do it manually */
145  /* pkt_http_ua should be pkt, http_ua, for this reason the UNGREEDY */
146  parse_capture_regex = DetectSetupPCRE2(PARSE_CAPTURE_REGEX, PCRE2_UNGREEDY);
147  if (parse_capture_regex == NULL) {
148  FatalError("pcre2 compile failed for parse_capture_regex");
149  }
150 
151 #ifdef PCRE2_HAVE_JIT
152  if (PageSupportsRWX() == 0) {
153  SCLogConfig("PCRE2 won't use JIT as OS doesn't allow RWX pages");
154  pcre2_use_jit = 0;
155  }
156 #endif
157 
158  return;
159 }
160 
161 /**
162  * \brief Match a regex on a single payload.
163  *
164  * \param det_ctx Thread detection ctx.
165  * \param s Signature.
166  * \param sm Sig match to match against.
167  * \param p Packet to set PktVars if any.
168  * \param f Flow to set FlowVars if any.
169  * \param payload Payload to inspect.
170  * \param payload_len Length of the payload.
171  *
172  * \retval 1 Match.
173  * \retval 0 No match.
174  */
176  const SigMatchData *smd, Packet *p, Flow *f,
177  const uint8_t *payload, uint32_t payload_len)
178 {
179  SCEnter();
180  int ret = 0;
181  const uint8_t *ptr = NULL;
182  uint32_t len = 0;
183  PCRE2_SIZE capture_len = 0;
184 
185  DetectPcreData *pe = (DetectPcreData *)smd->ctx;
186 
187  if (pe->flags & DETECT_PCRE_RELATIVE) {
188  ptr = payload + det_ctx->buffer_offset;
189  len = payload_len - det_ctx->buffer_offset;
190  } else {
191  ptr = payload;
192  len = payload_len;
193  }
194 
195  int start_offset = 0;
196  if (det_ctx->pcre_match_start_offset != 0) {
197  start_offset = (payload + det_ctx->pcre_match_start_offset - ptr);
198  }
199 
200  /* run the actual pcre detection */
201  pcre2_match_data *match =
202  (pcre2_match_data *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, pe->thread_ctx_id);
203 
204  ret = DetectPcreExec(det_ctx, pe, (char *)ptr, len, start_offset, 0, match);
205  SCLogDebug("ret %d (negating %s)", ret, (pe->flags & DETECT_PCRE_NEGATE) ? "set" : "not set");
206 
207  if (ret == PCRE2_ERROR_NOMATCH) {
208  if (pe->flags & DETECT_PCRE_NEGATE) {
209  /* regex didn't match with negate option means we
210  * consider it a match */
211  ret = 1;
212  } else {
213  ret = 0;
214  }
215  } else if (ret >= 0) {
216  if (pe->flags & DETECT_PCRE_NEGATE) {
217  /* regex matched but we're negated, so not
218  * considering it a match */
219  ret = 0;
220  } else {
221  /* regex matched and we're not negated,
222  * considering it a match */
223 
224  SCLogDebug("ret %d pe->idx %u", ret, pe->idx);
225 
226  /* see if we need to do substring capturing. */
227  if (ret > 1 && pe->idx != 0) {
228  uint8_t x;
229  for (x = 0; x < pe->idx; x++) {
230  SCLogDebug("capturing %u", x);
231  const char *pcre2_str_ptr = NULL;
232  ret = pcre2_substring_get_bynumber(
233  match, x + 1, (PCRE2_UCHAR8 **)&pcre2_str_ptr, &capture_len);
234  if (unlikely(ret != 0)) {
235  pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
236  continue;
237  }
238  /* store max 64k. Errors are ignored */
239  capture_len = (capture_len < 0xffff) ? (uint16_t)capture_len : 0xffff;
240  uint8_t *str_ptr = SCMalloc(capture_len);
241  if (unlikely(str_ptr == NULL)) {
242  pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
243  continue;
244  }
245  memcpy(str_ptr, pcre2_str_ptr, capture_len);
246  pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
247 
248  SCLogDebug("data %p/%u, type %u id %u p %p",
249  str_ptr, ret, pe->captypes[x], pe->capids[x], p);
250 
251  if (pe->captypes[x] == VAR_TYPE_PKT_VAR_KV) {
252  /* get the value, as first capture is the key */
253  const char *pcre2_str_ptr2 = NULL;
254  /* key length is limited to 256 chars */
255  uint16_t key_len = (capture_len < 0xff) ? (uint16_t)capture_len : 0xff;
256  int ret2 = pcre2_substring_get_bynumber(
257  match, x + 2, (PCRE2_UCHAR8 **)&pcre2_str_ptr2, &capture_len);
258 
259  if (unlikely(ret2 != 0)) {
260  SCFree(str_ptr);
261  pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
262  break;
263  }
264  capture_len = (capture_len < 0xffff) ? (uint16_t)capture_len : 0xffff;
265  uint8_t *str_ptr2 = SCMalloc(capture_len);
266  if (unlikely(str_ptr2 == NULL)) {
267  SCFree(str_ptr);
268  pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
269  break;
270  }
271  memcpy(str_ptr2, pcre2_str_ptr2, capture_len);
272  pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
273 
274  (void)DetectVarStoreMatchKeyValue(det_ctx, (uint8_t *)str_ptr, key_len,
275  (uint8_t *)str_ptr2, (uint16_t)capture_len,
277 
278  } else if (pe->captypes[x] == VAR_TYPE_PKT_VAR) {
279  (void)DetectVarStoreMatch(det_ctx, pe->capids[x], (uint8_t *)str_ptr,
280  (uint16_t)capture_len, DETECT_VAR_TYPE_PKT_POSTMATCH);
281 
282  } else if (pe->captypes[x] == VAR_TYPE_FLOW_VAR && f != NULL) {
283  (void)DetectVarStoreMatch(det_ctx, pe->capids[x], (uint8_t *)str_ptr,
284  (uint16_t)capture_len, DETECT_VAR_TYPE_FLOW_POSTMATCH);
285  } else {
286  BUG_ON(1); // Impossible captype
287  SCFree(str_ptr);
288  }
289  }
290  }
291 
292  PCRE2_SIZE *ov = pcre2_get_ovector_pointer(match);
293  /* update offset for pcre RELATIVE */
294  det_ctx->buffer_offset = (ptr + ov[1]) - payload;
295  det_ctx->pcre_match_start_offset = (ptr + ov[0] + 1) - payload;
296 
297  ret = 1;
298  }
299 
300  } else {
301  SCLogDebug("pcre had matching error");
302  ret = 0;
303  }
304  SCReturnInt(ret);
305 }
306 
307 static int DetectPcreSetList(int list, int set)
308 {
309  if (list != DETECT_SM_LIST_NOTSET) {
310  SCLogError("only one pcre option to specify a buffer type is allowed");
311  return -1;
312  }
313  return set;
314 }
315 
316 static int DetectPcreHasUpperCase(const char *re)
317 {
318  size_t len = strlen(re);
319  bool is_meta = false;
320  bool is_meta_hex = false;
321  int meta_hex_cnt = 0;
322 
323  for (size_t i = 0; i < len; i++) {
324  if (is_meta_hex) {
325  meta_hex_cnt++;
326 
327  if (meta_hex_cnt == 2) {
328  is_meta_hex = false;
329  meta_hex_cnt = 0;
330  }
331  } else if (is_meta) {
332  if (re[i] == 'x') {
333  is_meta_hex = true;
334  } else {
335  is_meta = false;
336  }
337  }
338  else if (re[i] == '\\') {
339  is_meta = true;
340  }
341  else if (isupper((unsigned char)re[i])) {
342  return 1;
343  }
344  }
345 
346  return 0;
347 }
348 
349 static DetectPcreData *DetectPcreParse (DetectEngineCtx *de_ctx,
350  const char *regexstr, int *sm_list, char *capture_names,
351  size_t capture_names_size, bool negate, AppProto *alproto)
352 {
353  pcre2_match_data *match = NULL;
354  int en;
355  PCRE2_SIZE eo2;
356  int opts = 0;
357  DetectPcreData *pd = NULL;
358  char *op = NULL;
359  int ret = 0, res = 0;
360  int check_host_header = 0;
361  char op_str[64] = "";
362 
363  int cut_capture = 0;
364  char *fcap = strstr(regexstr, "flow:");
365  char *pcap = strstr(regexstr, "pkt:");
366  /* take the size of the whole input as buffer size for the regex we will
367  * extract below. Add 1 to please Coverity's alloc_strlen test. */
368  size_t slen = strlen(regexstr) + 1;
369  if (fcap || pcap) {
370  SCLogDebug("regexstr %s", regexstr);
371 
372  if (fcap && !pcap)
373  cut_capture = fcap - regexstr;
374  else if (pcap && !fcap)
375  cut_capture = pcap - regexstr;
376  else {
377  BUG_ON(pcap == NULL); // added to assist cppcheck
378  BUG_ON(fcap == NULL);
379  cut_capture = MIN((pcap - regexstr), (fcap - regexstr));
380  }
381 
382  SCLogDebug("cut_capture %d", cut_capture);
383 
384  if (cut_capture > 1) {
385  int offset = cut_capture - 1;
386  while (offset) {
387  SCLogDebug("regexstr[offset] %c", regexstr[offset]);
388  if (regexstr[offset] == ',' || regexstr[offset] == ' ') {
389  offset--;
390  }
391  else
392  break;
393  }
394 
395  if (cut_capture == (offset + 1)) {
396  SCLogDebug("missing separators, assume it's part of the regex");
397  } else {
398  slen = offset + 1;
399  strlcpy(capture_names, regexstr+cut_capture, capture_names_size);
400  if (capture_names[strlen(capture_names)-1] == '"')
401  capture_names[strlen(capture_names)-1] = '\0';
402  }
403  }
404  }
405 
406  char re[slen];
407 
408  match = pcre2_match_data_create_from_pattern(parse_regex->regex, NULL);
409  if (!match) {
410  goto error;
411  }
412 
413  ret = pcre2_match(parse_regex->regex, (PCRE2_SPTR8)regexstr, slen, 0, 0, match, NULL);
414  if (ret <= 0) {
415  SCLogError("pcre parse error: %s", regexstr);
416  goto error;
417  }
418 
419  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)re, &slen);
420  if (res < 0) {
421  SCLogError("pcre2_substring_copy_bynumber failed");
422  pcre2_match_data_free(match);
423  return NULL;
424  }
425 
426  if (ret > 2) {
427  size_t copylen = sizeof(op_str);
428  res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)op_str, &copylen);
429  if (res < 0) {
430  SCLogError("pcre2_substring_copy_bynumber failed");
431  pcre2_match_data_free(match);
432  return NULL;
433  }
434  op = op_str;
435  }
436  //printf("ret %" PRId32 " re \'%s\', op \'%s\'\n", ret, re, op);
437 
438  pd = SCCalloc(1, sizeof(DetectPcreData));
439  if (unlikely(pd == NULL))
440  goto error;
441 
442  if (negate)
443  pd->flags |= DETECT_PCRE_NEGATE;
444 
445  if (op != NULL) {
446  while (*op) {
447  SCLogDebug("regex option %c", *op);
448 
449  switch (*op) {
450  case 'A':
451  opts |= PCRE2_ANCHORED;
452  break;
453  case 'E':
454  opts |= PCRE2_DOLLAR_ENDONLY;
455  break;
456  case 'G':
457  opts |= PCRE2_UNGREEDY;
458  break;
459 
460  case 'i':
461  opts |= PCRE2_CASELESS;
463  break;
464  case 'm':
465  opts |= PCRE2_MULTILINE;
466  break;
467  case 's':
468  opts |= PCRE2_DOTALL;
469  break;
470  case 'x':
471  opts |= PCRE2_EXTENDED;
472  break;
473 
474  case 'O':
476  break;
477 
478  case 'B': /* snort's option */
479  if (*sm_list != DETECT_SM_LIST_NOTSET) {
480  SCLogError("regex modifier 'B' inconsistent with chosen buffer");
481  goto error;
482  }
484  break;
485  case 'R': /* snort's option */
487  break;
488 
489  /* buffer selection */
490 
491  case 'U': { /* snort's option */
492  if (pd->flags & DETECT_PCRE_RAWBYTES) {
493  SCLogError("regex modifier 'U' inconsistent with 'B'");
494  goto error;
495  }
496  int list = DetectBufferTypeGetByName("http_uri");
497  *sm_list = DetectPcreSetList(*sm_list, list);
498  *alproto = ALPROTO_HTTP1;
499  break;
500  }
501  case 'V': {
502  if (pd->flags & DETECT_PCRE_RAWBYTES) {
503  SCLogError("regex modifier 'V' inconsistent with 'B'");
504  goto error;
505  }
506  int list = DetectBufferTypeGetByName("http_user_agent");
507  *sm_list = DetectPcreSetList(*sm_list, list);
508  *alproto = ALPROTO_HTTP1;
509  break;
510  }
511  case 'W': {
512  if (pd->flags & DETECT_PCRE_RAWBYTES) {
513  SCLogError("regex modifier 'W' inconsistent with 'B'");
514  goto error;
515  }
516  int list = DetectBufferTypeGetByName("http_host");
517  *sm_list = DetectPcreSetList(*sm_list, list);
518  *alproto = ALPROTO_HTTP1;
519  check_host_header = 1;
520  break;
521  }
522  case 'Z': {
523  if (pd->flags & DETECT_PCRE_RAWBYTES) {
524  SCLogError("regex modifier 'Z' inconsistent with 'B'");
525  goto error;
526  }
527  int list = DetectBufferTypeGetByName("http_raw_host");
528  *sm_list = DetectPcreSetList(*sm_list, list);
529  *alproto = ALPROTO_HTTP1;
530  break;
531  }
532  case 'H': { /* snort's option */
533  if (pd->flags & DETECT_PCRE_RAWBYTES) {
534  SCLogError("regex modifier 'H' inconsistent with 'B'");
535  goto error;
536  }
537  int list = DetectBufferTypeGetByName("http_header");
538  *sm_list = DetectPcreSetList(*sm_list, list);
539  *alproto = ALPROTO_HTTP1;
540  break;
541  } case 'I': { /* snort's option */
542  if (pd->flags & DETECT_PCRE_RAWBYTES) {
543  SCLogError("regex modifier 'I' inconsistent with 'B'");
544  goto error;
545  }
546  int list = DetectBufferTypeGetByName("http_raw_uri");
547  *sm_list = DetectPcreSetList(*sm_list, list);
548  *alproto = ALPROTO_HTTP1;
549  break;
550  }
551  case 'D': { /* snort's option */
552  int list = DetectBufferTypeGetByName("http_raw_header");
553  *sm_list = DetectPcreSetList(*sm_list, list);
554  *alproto = ALPROTO_HTTP1;
555  break;
556  }
557  case 'M': { /* snort's option */
558  if (pd->flags & DETECT_PCRE_RAWBYTES) {
559  SCLogError("regex modifier 'M' inconsistent with 'B'");
560  goto error;
561  }
562  int list = DetectBufferTypeGetByName("http_method");
563  *sm_list = DetectPcreSetList(*sm_list, list);
564  *alproto = ALPROTO_HTTP1;
565  break;
566  }
567  case 'C': { /* snort's option */
568  if (pd->flags & DETECT_PCRE_RAWBYTES) {
569  SCLogError("regex modifier 'C' inconsistent with 'B'");
570  goto error;
571  }
572  int list = DetectBufferTypeGetByName("http_cookie");
573  *sm_list = DetectPcreSetList(*sm_list, list);
574  *alproto = ALPROTO_HTTP1;
575  break;
576  }
577  case 'P': {
578  /* snort's option (http request body inspection) */
579  int list = DetectBufferTypeGetByName("http_client_body");
580  *sm_list = DetectPcreSetList(*sm_list, list);
581  *alproto = ALPROTO_HTTP1;
582  break;
583  }
584  case 'Q': {
585  int list = DetectBufferTypeGetByName("file_data");
586  /* suricata extension (http response body inspection) */
587  *sm_list = DetectPcreSetList(*sm_list, list);
588  *alproto = ALPROTO_HTTP1;
589  break;
590  }
591  case 'Y': {
592  /* snort's option */
593  int list = DetectBufferTypeGetByName("http_stat_msg");
594  *sm_list = DetectPcreSetList(*sm_list, list);
595  *alproto = ALPROTO_HTTP1;
596  break;
597  }
598  case 'S': {
599  /* snort's option */
600  int list = DetectBufferTypeGetByName("http_stat_code");
601  *sm_list = DetectPcreSetList(*sm_list, list);
602  *alproto = ALPROTO_HTTP1;
603  break;
604  }
605  default:
606  SCLogError("unknown regex modifier '%c'", *op);
607  goto error;
608  }
609  op++;
610  }
611  }
612  if (*sm_list == -1)
613  goto error;
614 
615  SCLogDebug("DetectPcreParse: \"%s\"", re);
616 
617  /* host header */
618  if (check_host_header) {
619  if (pd->flags & DETECT_PCRE_CASELESS) {
620  SCLogWarning("http host pcre(\"W\") "
621  "specified along with \"i(caseless)\" modifier. "
622  "Since the hostname buffer we match against "
623  "is actually lowercase, having a "
624  "nocase is redundant.");
625  }
626  else if (DetectPcreHasUpperCase(re)) {
627  SCLogError("pcre host(\"W\") "
628  "specified has an uppercase char. "
629  "Since the hostname buffer we match against "
630  "is actually lowercase, please specify an "
631  "all lowercase based pcre.");
632  goto error;
633  }
634  }
635 
636  /* Try to compile as if all (...) groups had been meant as (?:...),
637  * which is the common case in most rules.
638  * If we fail because a capture group is later referenced (e.g., \1),
639  * PCRE will let us know.
640  */
641  if (capture_names == NULL || strlen(capture_names) == 0)
642  opts |= PCRE2_NO_AUTO_CAPTURE;
643 
644  pd->parse_regex.regex =
645  pcre2_compile((PCRE2_SPTR8)re, PCRE2_ZERO_TERMINATED, opts, &en, &eo2, NULL);
646  if (pd->parse_regex.regex == NULL && en == 115) { // reference to nonexistent subpattern
647  opts &= ~PCRE2_NO_AUTO_CAPTURE;
648  pd->parse_regex.regex =
649  pcre2_compile((PCRE2_SPTR8)re, PCRE2_ZERO_TERMINATED, opts, &en, &eo2, NULL);
650  }
651  if (pd->parse_regex.regex == NULL) {
652  PCRE2_UCHAR errbuffer[256];
653  pcre2_get_error_message(en, errbuffer, sizeof(errbuffer));
654  SCLogError("pcre2 compile of \"%s\" failed at "
655  "offset %d: %s",
656  regexstr, (int)eo2, errbuffer);
657  goto error;
658  }
659 
660 #ifdef PCRE2_HAVE_JIT
661  if (pcre2_use_jit) {
662  ret = pcre2_jit_compile(pd->parse_regex.regex, PCRE2_JIT_COMPLETE);
663  if (ret != 0) {
664  /* warning, so we won't print the sig after this. Adding
665  * file and line to the message so the admin can figure
666  * out what sig this is about */
667  SCLogDebug("PCRE2 JIT compiler does not support: %s. "
668  "Falling back to regular PCRE2 handling (%s:%d)",
669  regexstr, de_ctx->rule_file, de_ctx->rule_line);
670  }
671  }
672 #endif /*PCRE2_HAVE_JIT*/
673 
674  pd->parse_regex.context = pcre2_match_context_create(NULL);
675  if (pd->parse_regex.context == NULL) {
676  SCLogError("pcre2 could not create match context");
677  goto error;
678  }
679  pd->parse_regex.match = pcre2_match_data_create_from_pattern(pd->parse_regex.regex, NULL);
680 
681  if (pd->flags & DETECT_PCRE_MATCH_LIMIT) {
682  if (pcre_match_limit >= -1) {
683  pcre2_set_match_limit(pd->parse_regex.context, pcre_match_limit);
684  }
685  if (pcre_match_limit_recursion >= -1) {
686  // pcre2_set_depth_limit unsupported on ubuntu 16.04
687  pcre2_set_recursion_limit(pd->parse_regex.context, pcre_match_limit_recursion);
688  }
689  } else {
690  pcre2_set_match_limit(pd->parse_regex.context, SC_MATCH_LIMIT_DEFAULT);
691  pcre2_set_recursion_limit(pd->parse_regex.context, SC_MATCH_LIMIT_RECURSION_DEFAULT);
692  }
693 
694  pcre2_match_data_free(match);
695  return pd;
696 
697 error:
698  pcre2_match_data_free(match);
699  DetectPcreFree(de_ctx, pd);
700  return NULL;
701 }
702 
703 /** \internal
704  * \brief check if we need to extract capture settings and set them up if needed
705  */
706 static int DetectPcreParseCapture(const char *regexstr, DetectEngineCtx *de_ctx, DetectPcreData *pd,
707  char *capture_names)
708 {
709  int ret = 0, res = 0;
710  char type_str[16] = "";
711  const char *orig_right_edge = regexstr + strlen(regexstr);
712  char *name_array[DETECT_PCRE_CAPTURE_MAX] = { NULL };
713  int name_idx = 0;
714  int capture_cnt = 0;
715  int key = 0;
716  size_t copylen;
717  pcre2_match_data *match = NULL;
718 
719  SCLogDebug("regexstr %s, pd %p", regexstr, pd);
720 
721  ret = pcre2_pattern_info(pd->parse_regex.regex, PCRE2_INFO_CAPTURECOUNT, &capture_cnt);
722  SCLogDebug("ret %d capture_cnt %d", ret, capture_cnt);
723  if (ret == 0 && capture_cnt && strlen(capture_names) > 0)
724  {
725  char *ptr = NULL;
726  while ((name_array[name_idx] = strtok_r(name_idx == 0 ? capture_names : NULL, " ,", &ptr))){
727  if (name_idx > (capture_cnt - 1)) {
728  SCLogError("more pkt/flow "
729  "var capture names than capturing substrings");
730  return -1;
731  }
732  SCLogDebug("name '%s'", name_array[name_idx]);
733 
734  if (strcmp(name_array[name_idx], "pkt:key") == 0) {
735  key = 1;
736  SCLogDebug("key-value/key");
737 
738  pd->captypes[pd->idx] = VAR_TYPE_PKT_VAR_KV;
739  SCLogDebug("id %u type %u", pd->capids[pd->idx], pd->captypes[pd->idx]);
740  pd->idx++;
741 
742  } else if (key == 1 && strcmp(name_array[name_idx], "pkt:value") == 0) {
743  SCLogDebug("key-value/value");
744  key = 0;
745 
746  /* kv error conditions */
747  } else if (key == 0 && strcmp(name_array[name_idx], "pkt:value") == 0) {
748  return -1;
749  } else if (key == 1) {
750  return -1;
751 
752  } else if (strncmp(name_array[name_idx], "flow:", 5) == 0) {
753  pd->capids[pd->idx] =
754  VarNameStoreRegister(name_array[name_idx] + 5, VAR_TYPE_FLOW_VAR);
755  pd->captypes[pd->idx] = VAR_TYPE_FLOW_VAR;
756  pd->idx++;
757 
758  } else if (strncmp(name_array[name_idx], "pkt:", 4) == 0) {
759  pd->capids[pd->idx] =
760  VarNameStoreRegister(name_array[name_idx] + 4, VAR_TYPE_PKT_VAR);
761  pd->captypes[pd->idx] = VAR_TYPE_PKT_VAR;
762  SCLogDebug("id %u type %u", pd->capids[pd->idx], pd->captypes[pd->idx]);
763  pd->idx++;
764 
765  } else {
766  SCLogError(" pkt/flow "
767  "var capture names must start with 'pkt:' or 'flow:'");
768  return -1;
769  }
770 
771  name_idx++;
772  if (name_idx >= DETECT_PCRE_CAPTURE_MAX)
773  break;
774  }
775  }
776 
777  /* take the size of the whole input as buffer size for the string we will
778  * extract below. Add 1 to please Coverity's alloc_strlen test. */
779  size_t cap_buffer_len = strlen(regexstr) + 1;
780  char capture_str[cap_buffer_len];
781  memset(capture_str, 0x00, cap_buffer_len);
782 
783  if (de_ctx == NULL)
784  goto error;
785 
786  while (1) {
787  SCLogDebug("\'%s\'", regexstr);
788 
789  ret = DetectParsePcreExec(parse_capture_regex, &match, regexstr, 0, 0);
790  if (ret < 3) {
791  pcre2_match_data_free(match);
792  return 0;
793  }
794  copylen = sizeof(type_str);
795  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)type_str, &copylen);
796  if (res != 0) {
797  SCLogError("pcre2_substring_copy_bynumber failed");
798  goto error;
799  }
800  cap_buffer_len = strlen(regexstr) + 1;
801  res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)capture_str, &cap_buffer_len);
802  if (res != 0) {
803  SCLogError("pcre2_substring_copy_bynumber failed");
804  goto error;
805  }
806  if (strlen(capture_str) == 0 || strlen(type_str) == 0) {
807  goto error;
808  }
809 
810  SCLogDebug("type \'%s\'", type_str);
811  SCLogDebug("capture \'%s\'", capture_str);
812 
813  if (pd->idx >= DETECT_PCRE_CAPTURE_MAX) {
814  SCLogError("rule can have maximally %d pkt/flow "
815  "var captures",
817  pcre2_match_data_free(match);
818  return -1;
819  }
820 
821  if (strcmp(type_str, "pkt") == 0) {
822  pd->capids[pd->idx] = VarNameStoreRegister((char *)capture_str, VAR_TYPE_PKT_VAR);
823  pd->captypes[pd->idx] = VAR_TYPE_PKT_VAR;
824  SCLogDebug("id %u type %u", pd->capids[pd->idx], pd->captypes[pd->idx]);
825  pd->idx++;
826  } else if (strcmp(type_str, "flow") == 0) {
827  pd->capids[pd->idx] = VarNameStoreRegister((char *)capture_str, VAR_TYPE_FLOW_VAR);
828  pd->captypes[pd->idx] = VAR_TYPE_FLOW_VAR;
829  pd->idx++;
830  }
831 
832  //SCLogNotice("pd->capname %s", pd->capname);
833  PCRE2_SIZE *ov = pcre2_get_ovector_pointer(match);
834  regexstr += ov[1];
835 
836  pcre2_match_data_free(match);
837  match = NULL;
838 
839  if (regexstr >= orig_right_edge)
840  break;
841  }
842  return 0;
843 
844 error:
845  pcre2_match_data_free(match);
846  return -1;
847 }
848 
849 static void *DetectPcreThreadInit(void *data)
850 {
851  DetectPcreData *pd = (DetectPcreData *)data;
852  pcre2_match_data *match = pcre2_match_data_create_from_pattern(pd->parse_regex.regex, NULL);
853  return match;
854 }
855 
856 static void DetectPcreThreadFree(void *ctx)
857 {
858  if (ctx != NULL) {
859  pcre2_match_data *match = (pcre2_match_data *)ctx;
860  pcre2_match_data_free(match);
861  }
862 }
863 
864 static int DetectPcreSetup (DetectEngineCtx *de_ctx, Signature *s, const char *regexstr)
865 {
866  SCEnter();
867  DetectPcreData *pd = NULL;
868  SigMatch *sm = NULL;
869  int parsed_sm_list = DETECT_SM_LIST_NOTSET;
870  char capture_names[1024] = "";
871  AppProto alproto = ALPROTO_UNKNOWN;
872 
873  pd = DetectPcreParse(de_ctx, regexstr, &parsed_sm_list,
874  capture_names, sizeof(capture_names), s->init_data->negated,
875  &alproto);
876  if (pd == NULL)
877  goto error;
878  if (DetectPcreParseCapture(regexstr, de_ctx, pd, capture_names) < 0)
879  goto error;
880 
882  de_ctx, "pcre", DetectPcreThreadInit, (void *)pd, DetectPcreThreadFree, 0);
883  if (pd->thread_ctx_id == -1)
884  goto error;
885 
886  int sm_list = -1;
887  if (s->init_data->list != DETECT_SM_LIST_NOTSET) {
888  if (parsed_sm_list != DETECT_SM_LIST_NOTSET && parsed_sm_list != s->init_data->list) {
889  SCLogError("Expression seen with a sticky buffer still set; either (1) reset sticky "
890  "buffer with pkt_data or (2) use a sticky buffer providing \"%s\".",
892  goto error;
893  }
894  if (DetectBufferGetActiveList(de_ctx, s) == -1)
895  goto error;
896 
897  sm_list = s->init_data->list;
898  } else {
899  switch (parsed_sm_list) {
901  sm_list = DETECT_SM_LIST_PMATCH;
902  break;
903  default: {
904  if (alproto != ALPROTO_UNKNOWN) {
905  /* see if the proto doesn't conflict
906  * with what we already have. */
907  if (s->alproto != ALPROTO_UNKNOWN && !AppProtoEquals(s->alproto, alproto)) {
908  goto error;
909  }
910  if (DetectSignatureSetAppProto(s, alproto) < 0)
911  goto error;
912  }
913  sm_list = parsed_sm_list;
914  break;
915  }
916  }
917  }
918  if (sm_list == -1)
919  goto error;
920 
921  sm = SigMatchAlloc();
922  if (sm == NULL)
923  goto error;
924  sm->type = DETECT_PCRE;
925  sm->ctx = (void *)pd;
926  SigMatchAppendSMToList(s, sm, sm_list);
927 
928  for (uint8_t x = 0; x < pd->idx; x++) {
929  if (DetectFlowvarPostMatchSetup(de_ctx, s, pd->capids[x]) < 0)
930  goto error_nofree;
931  }
932 
933  if (!(pd->flags & DETECT_PCRE_RELATIVE))
934  goto okay;
935 
936  /* errors below shouldn't free pd */
937 
938  SigMatch *prev_pm = DetectGetLastSMByListPtr(s, sm->prev,
940  if (s->init_data->list == DETECT_SM_LIST_NOTSET && prev_pm == NULL) {
941  SCLogError("pcre with /R (relative) needs "
942  "preceding match in the same buffer");
943  goto error_nofree;
944  /* null is allowed when we use a sticky buffer */
945  } else if (prev_pm == NULL) {
946  goto okay;
947  }
948  if (prev_pm->type == DETECT_CONTENT) {
949  DetectContentData *cd = (DetectContentData *)prev_pm->ctx;
951  } else if (prev_pm->type == DETECT_PCRE) {
952  DetectPcreData *tmp = (DetectPcreData *)prev_pm->ctx;
954  }
955 
956  okay:
957  SCReturnInt(0);
958  error:
959  DetectPcreFree(de_ctx, pd);
960  error_nofree:
961  SCReturnInt(-1);
962 }
963 
964 static void DetectPcreFree(DetectEngineCtx *de_ctx, void *ptr)
965 {
966  if (ptr == NULL)
967  return;
968 
969  DetectPcreData *pd = (DetectPcreData *)ptr;
972 
973  for (uint8_t i = 0; i < pd->idx; i++) {
974  VarNameStoreUnregister(pd->capids[i], pd->captypes[i]);
975  }
976  SCFree(pd);
977 
978  return;
979 }
980 
981 #ifdef UNITTESTS /* UNITTESTS */
982 #include "detect-engine-alert.h"
983 static int g_file_data_buffer_id = 0;
984 static int g_http_header_buffer_id = 0;
985 static int g_dce_stub_data_buffer_id = 0;
986 
987 /**
988  * \test DetectPcreParseTest01 make sure we don't allow invalid opts 7.
989  */
990 static int DetectPcreParseTest01 (void)
991 {
992  int result = 1;
993  DetectPcreData *pd = NULL;
994  const char *teststring = "/blah/7";
995  int list = DETECT_SM_LIST_NOTSET;
998  AppProto alproto = ALPROTO_UNKNOWN;
999 
1000  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1001  FAIL_IF_NOT_NULL(pd);
1002 
1004  return result;
1005 }
1006 
1007 /**
1008  * \test DetectPcreParseTest02 make sure we don't allow invalid opts Ui$.
1009  */
1010 static int DetectPcreParseTest02 (void)
1011 {
1012  int result = 1;
1013  DetectPcreData *pd = NULL;
1014  const char *teststring = "/blah/Ui$";
1015  int list = DETECT_SM_LIST_NOTSET;
1018  AppProto alproto = ALPROTO_UNKNOWN;
1019 
1020  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1021  FAIL_IF_NOT_NULL(pd);
1022  FAIL_IF_NOT(alproto == ALPROTO_HTTP1);
1023 
1025  return result;
1026 }
1027 
1028 /**
1029  * \test DetectPcreParseTest03 make sure we don't allow invalid opts UZi.
1030  */
1031 static int DetectPcreParseTest03 (void)
1032 {
1033  int result = 1;
1034  DetectPcreData *pd = NULL;
1035  const char *teststring = "/blah/UNi";
1036  int list = DETECT_SM_LIST_NOTSET;
1039  AppProto alproto = ALPROTO_UNKNOWN;
1040 
1041  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1042  FAIL_IF_NOT_NULL(pd);
1043 
1045  return result;
1046 }
1047 
1048 /**
1049  * \test DetectPcreParseTest04 make sure we allow escaped "
1050  */
1051 static int DetectPcreParseTest04 (void)
1052 {
1053  int result = 1;
1054  DetectPcreData *pd = NULL;
1055  const char *teststring = "/b\\\"lah/i";
1056  int list = DETECT_SM_LIST_NOTSET;
1059  AppProto alproto = ALPROTO_UNKNOWN;
1060 
1061  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1062  FAIL_IF_NULL(pd);
1063  FAIL_IF_NOT(alproto == ALPROTO_UNKNOWN);
1064 
1065  DetectPcreFree(de_ctx, pd);
1067  return result;
1068 }
1069 
1070 /**
1071  * \test DetectPcreParseTest05 make sure we parse pcre with no opts
1072  */
1073 static int DetectPcreParseTest05 (void)
1074 {
1075  int result = 1;
1076  DetectPcreData *pd = NULL;
1077  const char *teststring = "/b(l|a)h/";
1078  int list = DETECT_SM_LIST_NOTSET;
1081  AppProto alproto = ALPROTO_UNKNOWN;
1082 
1083  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1084  FAIL_IF_NULL(pd);
1085  FAIL_IF_NOT(alproto == ALPROTO_UNKNOWN);
1086 
1087  DetectPcreFree(de_ctx, pd);
1089  return result;
1090 }
1091 
1092 /**
1093  * \test DetectPcreParseTest06 make sure we parse pcre with smi opts
1094  */
1095 static int DetectPcreParseTest06 (void)
1096 {
1097  int result = 1;
1098  DetectPcreData *pd = NULL;
1099  const char *teststring = "/b(l|a)h/smi";
1100  int list = DETECT_SM_LIST_NOTSET;
1103  AppProto alproto = ALPROTO_UNKNOWN;
1104 
1105  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1106  FAIL_IF_NULL(pd);
1107  FAIL_IF_NOT(alproto == ALPROTO_UNKNOWN);
1108 
1109  DetectPcreFree(de_ctx, pd);
1111  return result;
1112 }
1113 
1114 /**
1115  * \test DetectPcreParseTest07 make sure we parse pcre with /Ui opts
1116  */
1117 static int DetectPcreParseTest07 (void)
1118 {
1119  int result = 1;
1120  DetectPcreData *pd = NULL;
1121  const char *teststring = "/blah/Ui";
1122  int list = DETECT_SM_LIST_NOTSET;
1125  AppProto alproto = ALPROTO_UNKNOWN;
1126 
1127  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1128  FAIL_IF_NULL(pd);
1129  FAIL_IF_NOT(alproto == ALPROTO_HTTP1);
1130 
1131  DetectPcreFree(de_ctx, pd);
1133  return result;
1134 }
1135 
1136 /**
1137  * \test DetectPcreParseTest08 make sure we parse pcre with O opts
1138  */
1139 static int DetectPcreParseTest08 (void)
1140 {
1141  int result = 1;
1142  DetectPcreData *pd = NULL;
1143  const char *teststring = "/b(l|a)h/O";
1144  int list = DETECT_SM_LIST_NOTSET;
1147  AppProto alproto = ALPROTO_UNKNOWN;
1148 
1149  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1150  FAIL_IF_NULL(pd);
1151  FAIL_IF_NOT(alproto == ALPROTO_UNKNOWN);
1152 
1153  DetectPcreFree(de_ctx, pd);
1155  return result;
1156 }
1157 
1158 /**
1159  * \test DetectPcreParseTest09 make sure we parse pcre with a content
1160  * that has slashes
1161  */
1162 static int DetectPcreParseTest09 (void)
1163 {
1164  DetectPcreData *pd = NULL;
1165  const char *teststring = "/lala\\\\/";
1166  int list = DETECT_SM_LIST_NOTSET;
1169  AppProto alproto = ALPROTO_UNKNOWN;
1170 
1171  pd = DetectPcreParse(de_ctx, teststring, &list, NULL, 0, false, &alproto);
1172  FAIL_IF_NULL(pd);
1173 
1174  DetectPcreFree(de_ctx, pd);
1176  PASS;
1177 }
1178 
1179 /**
1180  * \test Test pcre option for dce sig(yeah I'm bored of writing test titles).
1181  */
1182 static int DetectPcreParseTest10(void)
1183 {
1184  Signature *s = SigAlloc();
1187 
1189 
1190  FAIL_IF_NOT(DetectPcreSetup(de_ctx, s, "/bamboo/") == 0);
1191  FAIL_IF_NOT(DetectBufferGetFirstSigMatch(s, g_dce_stub_data_buffer_id) == NULL);
1193 
1194  SigFree(de_ctx, s);
1195 
1196  s = SigAlloc();
1197  FAIL_IF_NULL(s);
1198 
1199  /* failure since we have no preceding content/pcre/bytejump */
1200  FAIL_IF_NOT(DetectPcreSetup(de_ctx, s, "/bamboo/") == 0);
1201  FAIL_IF_NOT(DetectBufferGetFirstSigMatch(s, g_dce_stub_data_buffer_id) == NULL);
1203 
1204  SigFree(de_ctx, s);
1206 
1207  PASS;
1208 }
1209 
1210 /** \test Check a signature with pcre relative method */
1211 static int DetectPcreParseTest15(void)
1212 {
1213  DetectEngineCtx *de_ctx = NULL;
1214 
1215  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1216 
1217  de_ctx->flags |= DE_QUIET;
1219  "alert tcp any any -> any any "
1220  "(msg:\"Testing pcre relative http_method\"; "
1221  "content:\"GET\"; "
1222  "http_method; pcre:\"/abc/RM\"; sid:1;)");
1224 
1225  if (de_ctx != NULL)
1227  if (de_ctx != NULL)
1229  PASS;
1230 }
1231 
1232 
1233 /** \test Check a signature with pcre relative cookie */
1234 static int DetectPcreParseTest16(void)
1235 {
1236  DetectEngineCtx *de_ctx = NULL;
1237 
1238  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1239 
1240  de_ctx->flags |= DE_QUIET;
1242  "alert tcp any any -> any any "
1243  "(msg:\"Testing pcre relative http_cookie\"; "
1244  "content:\"test\"; "
1245  "http_cookie; pcre:\"/abc/RC\"; sid:1;)");
1247 
1248  if (de_ctx != NULL)
1250  if (de_ctx != NULL)
1252  PASS;
1253 }
1254 
1255 /** \test Check a signature with pcre relative raw header */
1256 static int DetectPcreParseTest17(void)
1257 {
1258  DetectEngineCtx *de_ctx = NULL;
1259 
1260  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1261 
1262  de_ctx->flags |= DE_QUIET;
1264  "alert tcp any any -> any any "
1265  "(msg:\"Testing pcre relative http_raw_header\"; "
1266  "flow:to_server; content:\"test\"; "
1267  "http_raw_header; pcre:\"/abc/RD\"; sid:1;)");
1269 
1270  if (de_ctx != NULL)
1272  if (de_ctx != NULL)
1274  PASS;
1275 }
1276 
1277 /** \test Check a signature with pcre relative header */
1278 static int DetectPcreParseTest18(void)
1279 {
1280  DetectEngineCtx *de_ctx = NULL;
1281 
1282  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1283 
1284  de_ctx->flags |= DE_QUIET;
1286  "alert tcp any any -> any any "
1287  "(msg:\"Testing pcre relative http_header\"; "
1288  "content:\"test\"; "
1289  "http_header; pcre:\"/abc/RH\"; sid:1;)");
1291 
1292  if (de_ctx != NULL)
1294  if (de_ctx != NULL)
1296  PASS;
1297 }
1298 
1299 /** \test Check a signature with pcre relative client-body */
1300 static int DetectPcreParseTest19(void)
1301 {
1302  DetectEngineCtx *de_ctx = NULL;
1303 
1304  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1305 
1306  de_ctx->flags |= DE_QUIET;
1308  "alert tcp any any -> any any "
1309  "(msg:\"Testing pcre relative http_client_body\"; "
1310  "content:\"test\"; "
1311  "http_client_body; pcre:\"/abc/RP\"; sid:1;)");
1313 
1314  if (de_ctx != NULL)
1316  if (de_ctx != NULL)
1318  PASS;
1319 }
1320 
1321 /** \test Check a signature with pcre relative raw uri */
1322 static int DetectPcreParseTest20(void)
1323 {
1324  DetectEngineCtx *de_ctx = NULL;
1325 
1326  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1327 
1328  de_ctx->flags |= DE_QUIET;
1330  "alert tcp any any -> any any "
1331  "(msg:\"Testing http_raw_uri\"; "
1332  "content:\"test\"; "
1333  "http_raw_uri; pcre:\"/abc/RI\"; sid:1;)");
1335 
1336  if (de_ctx != NULL)
1338  if (de_ctx != NULL)
1340  PASS;
1341 }
1342 
1343 /** \test Check a signature with pcre relative uricontent */
1344 static int DetectPcreParseTest21(void)
1345 {
1346  DetectEngineCtx *de_ctx = NULL;
1347 
1348  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1349 
1350  de_ctx->flags |= DE_QUIET;
1352  "alert tcp any any -> any any "
1353  "(msg:\"Testing pcre relative uricontent\"; "
1354  "uricontent:\"test\"; "
1355  "pcre:\"/abc/RU\"; sid:1;)");
1357 
1358  if (de_ctx != NULL)
1360  if (de_ctx != NULL)
1362  PASS;
1363 }
1364 
1365 /** \test Check a signature with pcre relative http_uri */
1366 static int DetectPcreParseTest22(void)
1367 {
1368  DetectEngineCtx *de_ctx = NULL;
1369 
1370  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1371 
1372  de_ctx->flags |= DE_QUIET;
1374  "alert tcp any any -> any any "
1375  "(msg:\"Testing pcre relative http_uri\"; "
1376  "content:\"test\"; "
1377  "http_uri; pcre:\"/abc/RU\"; sid:1;)");
1379 
1380  if (de_ctx != NULL)
1382  if (de_ctx != NULL)
1384  PASS;
1385 }
1386 
1387 /** \test Check a signature with inconsistent pcre relative */
1388 static int DetectPcreParseTest23(void)
1389 {
1390  DetectEngineCtx *de_ctx = NULL;
1391 
1392  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1393 
1394  de_ctx->flags |= DE_QUIET;
1396  "alert tcp any any -> any any "
1397  "(msg:\"Testing inconsistent pcre relative\"; "
1398  "content:\"GET\"; "
1399  "http_cookie; pcre:\"/abc/RM\"; sid:1;)");
1401 
1402  if (de_ctx != NULL)
1404  if (de_ctx != NULL)
1406  PASS;
1407 }
1408 
1409 /** \test Check a signature with inconsistent pcre modifiers */
1410 static int DetectPcreParseTest24(void)
1411 {
1412  DetectEngineCtx *de_ctx = NULL;
1413 
1414  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1415 
1416  de_ctx->flags |= DE_QUIET;
1418  "alert tcp any any -> any any "
1419  "(msg:\"Testing inconsistent pcre modifiers\"; "
1420  "pcre:\"/abc/UI\"; sid:1;)");
1422 
1423  if (de_ctx != NULL)
1425  if (de_ctx != NULL)
1427  PASS;
1428 }
1429 
1430 /** \test Check a signature with inconsistent pcre modifiers */
1431 static int DetectPcreParseTest25(void)
1432 {
1433  DetectEngineCtx *de_ctx = NULL;
1434 
1435  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1436 
1437  de_ctx->flags |= DE_QUIET;
1439  "alert tcp any any -> any any "
1440  "(msg:\"Testing inconsistent pcre modifiers\"; "
1441  "pcre:\"/abc/DH\"; sid:1;)");
1443 
1444  if (de_ctx != NULL)
1446  if (de_ctx != NULL)
1448  PASS;
1449 }
1450 
1451 /** \test Check a signature with inconsistent pcre modifiers */
1452 static int DetectPcreParseTest26(void)
1453 {
1454  DetectEngineCtx *de_ctx = NULL;
1455 
1456  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1457 
1458  de_ctx->flags |= DE_QUIET;
1460  "alert http any any -> any any "
1461  "(msg:\"Testing inconsistent pcre modifiers\"; "
1462  "pcre:\"/abc/F\"; sid:1;)");
1464 
1465  if (de_ctx != NULL)
1467  if (de_ctx != NULL)
1469  PASS;
1470 }
1471 
1472 /** \test Bug 1098 */
1473 static int DetectPcreParseTest27(void)
1474 {
1475  DetectEngineCtx *de_ctx = NULL;
1476 
1477  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1478 
1479  de_ctx->flags |= DE_QUIET;
1480  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any 80 "
1481  "(content:\"baduricontent\"; http_raw_uri; "
1482  "pcre:\"/^[a-z]{5}\\.html/R\"; sid:2; rev:2;)");
1483  FAIL_IF_NOT(de_ctx->sig_list == NULL);
1484 
1485  if (de_ctx != NULL)
1487  if (de_ctx != NULL)
1489  PASS;
1490 }
1491 
1492 /** \test Bug 1957 */
1493 static int DetectPcreParseTest28(void)
1494 {
1495  DetectEngineCtx *de_ctx = NULL;
1496 
1497  FAIL_IF( (de_ctx = DetectEngineCtxInit()) == NULL);
1498 
1499  de_ctx->flags |= DE_QUIET;
1500  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any 80 "
1501  "(content:\"|2E|suricata\"; http_host; pcre:\"/\\x2Esuricata$/W\"; "
1502  "sid:2; rev:2;)");
1504 
1506  PASS;
1507 }
1508 
1509 static int DetectPcreTestSig01(void)
1510 {
1511  uint8_t *buf = (uint8_t *)"lalala lalala\\ lala\n";
1512  uint16_t buflen = strlen((char *)buf);
1513  Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1514  int result = 0;
1515 
1516  char sig[] = "alert tcp any any -> any any (msg:\"pcre with an ending slash\"; pcre:\"/ "
1517  "lalala\\\\/\"; sid:1;)";
1518  if (UTHPacketMatchSig(p, sig) == 0) {
1519  result = 0;
1520  goto end;
1521  }
1522  result = 1;
1523 end:
1524  if (p != NULL)
1525  UTHFreePacket(p);
1526  return result;
1527 }
1528 
1529 /** \test anchored pcre */
1530 static int DetectPcreTestSig02(void)
1531 {
1532  uint8_t *buf = (uint8_t *)"lalala\n";
1533  uint16_t buflen = strlen((char *)buf);
1534  Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1535 
1536  char sig[] = "alert tcp any any -> any any (msg:\"pcre with an ending slash\"; "
1537  "pcre:\"/^(la)+$/\"; sid:1;)";
1538  FAIL_IF(UTHPacketMatchSig(p, sig) == 0);
1539 
1540  if (p != NULL)
1541  UTHFreePacket(p);
1542  PASS;
1543 }
1544 
1545 /** \test anchored pcre */
1546 static int DetectPcreTestSig03(void)
1547 {
1548  /* test it also without ending in a newline "\n" */
1549  uint8_t *buf = (uint8_t *)"lalala";
1550  uint16_t buflen = strlen((char *)buf);
1551  Packet *p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
1552 
1553  char sig[] = "alert tcp any any -> any any (msg:\"pcre with an ending slash\"; "
1554  "pcre:\"/^(la)+$/\"; sid:1;)";
1555  FAIL_IF(UTHPacketMatchSig(p, sig) == 0);
1556 
1557  if (p != NULL)
1558  UTHFreePacket(p);
1559  PASS;
1560 }
1561 
1562 /** \test Test tracking of body chunks per transactions (on requests)
1563  */
1564 static int DetectPcreTxBodyChunksTest01(void)
1565 {
1566  Flow f;
1567  TcpSession ssn;
1568  Packet *p = NULL;
1569  uint8_t httpbuf1[] = "GET / HTTP/1.1\r\n";
1570  uint8_t httpbuf2[] = "User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1571  uint8_t httpbuf3[] = "Cookie: dummy\r\n\r\n";
1572  uint8_t httpbuf4[] = "Body one!!";
1573  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1574  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1575  uint32_t httplen3 = sizeof(httpbuf3) - 1; /* minus the \0 */
1576  uint32_t httplen4 = sizeof(httpbuf4) - 1; /* minus the \0 */
1577  uint8_t httpbuf5[] = "GET /?var=val HTTP/1.1\r\n";
1578  uint8_t httpbuf6[] = "User-Agent: Firefox/1.0\r\n";
1579  uint8_t httpbuf7[] = "Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1580  uint32_t httplen5 = sizeof(httpbuf5) - 1; /* minus the \0 */
1581  uint32_t httplen6 = sizeof(httpbuf6) - 1; /* minus the \0 */
1582  uint32_t httplen7 = sizeof(httpbuf7) - 1; /* minus the \0 */
1584 
1585  memset(&f, 0, sizeof(f));
1586  memset(&ssn, 0, sizeof(ssn));
1587 
1588  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1589 
1590  FLOW_INITIALIZE(&f);
1591  f.protoctx = (void *)&ssn;
1592  f.proto = IPPROTO_TCP;
1593  f.flags |= FLOW_IPV4;
1594 
1595  p->flow = &f;
1599  f.alproto = ALPROTO_HTTP1;
1600 
1601  StreamTcpInitConfig(true);
1602 
1604 
1605  int r = AppLayerParserParse(
1606  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER | STREAM_START, httpbuf1, httplen1);
1607  FAIL_IF(r != 0);
1608 
1609  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
1610  FAIL_IF(r != 0);
1611 
1612  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf3, httplen3);
1613  FAIL_IF(r != 0);
1614 
1615  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf4, httplen4);
1616  FAIL_IF(r != 0);
1617 
1618  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf5, httplen5);
1619  FAIL_IF(r != 0);
1620 
1621  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf6, httplen6);
1622  FAIL_IF(r != 0);
1623 
1624  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf7, httplen7);
1625  FAIL_IF(r != 0);
1626 
1627  /* Now we should have 2 transactions, each with it's own list
1628  * of request body chunks (let's test it) */
1629 
1630  HtpState *htp_state = f.alstate;
1631  FAIL_IF(htp_state == NULL);
1632 
1633  /* hardcoded check of the transactions and it's client body chunks */
1634  FAIL_IF(AppLayerParserGetTxCnt(&f, htp_state) != 2);
1635 
1636  htp_tx_t *t1 = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, htp_state, 0);
1637  htp_tx_t *t2 = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, htp_state, 1);
1638 
1639  HtpTxUserData *htud = (HtpTxUserData *) htp_tx_get_user_data(t1);
1640  FAIL_IF(htud == NULL);
1641 
1642  HtpBodyChunk *cur = htud->request_body.first;
1643  FAIL_IF(htud->request_body.first == NULL);
1644 
1645  FAIL_IF(StreamingBufferSegmentCompareRawData(htud->request_body.sb, &cur->sbseg, (uint8_t *)"Body one!!", 10) != 1);
1646 
1647  htud = (HtpTxUserData *) htp_tx_get_user_data(t2);
1648 
1649  cur = htud->request_body.first;
1650  FAIL_IF(htud->request_body.first == NULL);
1651 
1652  FAIL_IF(StreamingBufferSegmentCompareRawData(htud->request_body.sb, &cur->sbseg, (uint8_t *)"Body two!!", 10) != 1);
1653 
1654  if (alp_tctx != NULL)
1656  StreamTcpFreeConfig(true);
1657  FLOW_DESTROY(&f);
1658  UTHFreePacket(p);
1659  PASS;
1660 }
1661 
1662 /** \test test pcre P modifier with multiple pipelined http transactions */
1663 static int DetectPcreTxBodyChunksTest02(void)
1664 {
1665  Signature *s = NULL;
1666  DetectEngineThreadCtx *det_ctx = NULL;
1667  ThreadVars th_v;
1668  Flow f;
1669  TcpSession ssn;
1670  Packet *p = NULL;
1671  uint8_t httpbuf1[] = "POST / HTTP/1.1\r\n";
1672  uint8_t httpbuf2[] = "User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1673  uint8_t httpbuf3[] = "Cookie: dummy\r\n\r\n";
1674  uint8_t httpbuf4[] = "Body one!!";
1675  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1676  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1677  uint32_t httplen3 = sizeof(httpbuf3) - 1; /* minus the \0 */
1678  uint32_t httplen4 = sizeof(httpbuf4) - 1; /* minus the \0 */
1679  uint8_t httpbuf5[] = "GET /?var=val HTTP/1.1\r\n";
1680  uint8_t httpbuf6[] = "User-Agent: Firefox/1.0\r\n";
1681  uint8_t httpbuf7[] = "Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1682  uint32_t httplen5 = sizeof(httpbuf5) - 1; /* minus the \0 */
1683  uint32_t httplen6 = sizeof(httpbuf6) - 1; /* minus the \0 */
1684  uint32_t httplen7 = sizeof(httpbuf7) - 1; /* minus the \0 */
1686 
1687  memset(&th_v, 0, sizeof(th_v));
1688  memset(&f, 0, sizeof(f));
1689  memset(&ssn, 0, sizeof(ssn));
1690 
1691  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1692 
1693  FLOW_INITIALIZE(&f);
1694  f.protoctx = (void *)&ssn;
1695  f.proto = IPPROTO_TCP;
1696  f.flags |= FLOW_IPV4;
1697 
1698  p->flow = &f;
1702  f.alproto = ALPROTO_HTTP1;
1703 
1704  StreamTcpInitConfig(true);
1705 
1707  FAIL_IF(de_ctx == NULL);
1708 
1709  de_ctx->flags |= DE_QUIET;
1710 
1711  s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (content:\"POST\"; http_method; content:\"Mozilla\"; http_header; content:\"dummy\"; http_cookie; pcre:\"/one/P\"; sid:1; rev:1;)");
1712  FAIL_IF(s == NULL);
1713  s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (content:\"GET\"; http_method; content:\"Firefox\"; http_header; content:\"dummy2\"; http_cookie; pcre:\"/two/P\"; sid:2; rev:1;)");
1714  FAIL_IF(s == NULL);
1715 
1717  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1718 
1719  int r = AppLayerParserParse(
1720  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
1721  FAIL_IF(r != 0);
1722 
1723  /* do detect */
1724  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1725  FAIL_IF(PacketAlertCheck(p, 1));
1726  p->alerts.cnt = 0;
1727 
1728  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
1729  FAIL_IF(r != 0);
1730 
1731  /* do detect */
1732  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1733  FAIL_IF(PacketAlertCheck(p, 1));
1734  p->alerts.cnt = 0;
1735 
1736  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf3, httplen3);
1737  FAIL_IF(r != 0);
1738 
1739  /* do detect */
1740  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1741  FAIL_IF(PacketAlertCheck(p, 1));
1742  p->alerts.cnt = 0;
1743 
1744  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf4, httplen4);
1745  FAIL_IF(r != 0);
1746 
1747  /* do detect */
1748  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1749  FAIL_IF(!(PacketAlertCheck(p, 1)));
1750  p->alerts.cnt = 0;
1751 
1752  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf5, httplen5);
1753  FAIL_IF(r != 0);
1754 
1755  /* do detect */
1756  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1757  FAIL_IF(PacketAlertCheck(p, 1));
1758  p->alerts.cnt = 0;
1759 
1760  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf6, httplen6);
1761  FAIL_IF(r != 0);
1762 
1763  /* do detect */
1764  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1765  FAIL_IF((PacketAlertCheck(p, 1)) || (PacketAlertCheck(p, 2)));
1766  p->alerts.cnt = 0;
1767 
1768  SCLogDebug("sending data chunk 7");
1769 
1770  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf7, httplen7);
1771  FAIL_IF(r != 0);
1772 
1773  /* do detect */
1774  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1775  FAIL_IF(!(PacketAlertCheck(p, 2)));
1776  p->alerts.cnt = 0;
1777 
1778  HtpState *htp_state = f.alstate;
1779  FAIL_IF(htp_state == NULL);
1780 
1781  /* hardcoded check of the transactions and it's client body chunks */
1782  FAIL_IF(AppLayerParserGetTxCnt(&f, htp_state) != 2);
1783 
1784  htp_tx_t *t1 = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, htp_state, 0);
1785  htp_tx_t *t2 = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, htp_state, 1);
1786 
1787  HtpTxUserData *htud = (HtpTxUserData *) htp_tx_get_user_data(t1);
1788 
1789  HtpBodyChunk *cur = htud->request_body.first;
1790  FAIL_IF(htud->request_body.first == NULL);
1791 
1792  FAIL_IF(StreamingBufferSegmentCompareRawData(htud->request_body.sb, &cur->sbseg, (uint8_t *)"Body one!!", 10) != 1);
1793 
1794  htud = (HtpTxUserData *) htp_tx_get_user_data(t2);
1795 
1796  cur = htud->request_body.first;
1797  FAIL_IF(htud->request_body.first == NULL);
1798 
1799  FAIL_IF(StreamingBufferSegmentCompareRawData(htud->request_body.sb, &cur->sbseg, (uint8_t *)"Body two!!", 10) != 1);
1800 
1801  if (alp_tctx != NULL)
1803  if (det_ctx != NULL) {
1804  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1805  }
1806  if (de_ctx != NULL) {
1809  }
1810 
1811  StreamTcpFreeConfig(true);
1812  FLOW_DESTROY(&f);
1813  UTHFreePacket(p);
1814  PASS;
1815 }
1816 
1817 /** \test multiple http transactions and body chunks of request handling */
1818 static int DetectPcreTxBodyChunksTest03(void)
1819 {
1820  Signature *s = NULL;
1821  DetectEngineThreadCtx *det_ctx = NULL;
1822  ThreadVars th_v;
1823  Flow f;
1824  TcpSession ssn;
1825  Packet *p = NULL;
1826  uint8_t httpbuf1[] = "POST / HTTP/1.1\r\n";
1827  uint8_t httpbuf2[] = "User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1828  uint8_t httpbuf3[] = "Cookie: dummy\r\n\r\n";
1829  uint8_t httpbuf4[] = "Body one!!";
1830  uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
1831  uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */
1832  uint32_t httplen3 = sizeof(httpbuf3) - 1; /* minus the \0 */
1833  uint32_t httplen4 = sizeof(httpbuf4) - 1; /* minus the \0 */
1834  uint8_t httpbuf5[] = "GET /?var=val HTTP/1.1\r\n";
1835  uint8_t httpbuf6[] = "User-Agent: Firefox/1.0\r\n";
1836  uint8_t httpbuf7[] = "Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1837  uint32_t httplen5 = sizeof(httpbuf5) - 1; /* minus the \0 */
1838  uint32_t httplen6 = sizeof(httpbuf6) - 1; /* minus the \0 */
1839  uint32_t httplen7 = sizeof(httpbuf7) - 1; /* minus the \0 */
1841 
1842  memset(&th_v, 0, sizeof(th_v));
1843  memset(&f, 0, sizeof(f));
1844  memset(&ssn, 0, sizeof(ssn));
1845 
1846  p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
1847 
1848  FLOW_INITIALIZE(&f);
1849  f.protoctx = (void *)&ssn;
1850  f.proto = IPPROTO_TCP;
1851  f.flags |= FLOW_IPV4;
1852 
1853  p->flow = &f;
1857  f.alproto = ALPROTO_HTTP1;
1858 
1859  StreamTcpInitConfig(true);
1860 
1862  FAIL_IF(de_ctx == NULL);
1863 
1864  de_ctx->flags |= DE_QUIET;
1865 
1866  s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (content:\"POST\"; http_method; content:\"Mozilla\"; http_header; content:\"dummy\"; http_cookie; pcre:\"/one/P\"; sid:1; rev:1;)");
1867  FAIL_IF(s == NULL);
1868  s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (content:\"GET\"; http_method; content:\"Firefox\"; http_header; content:\"dummy2\"; http_cookie; pcre:\"/two/P\"; sid:2; rev:1;)");
1869  FAIL_IF(s == NULL);
1870 
1872  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1873 
1874  int r = AppLayerParserParse(
1875  NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
1876  FAIL_IF(r != 0);
1877 
1878  /* do detect */
1879  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1880  FAIL_IF(PacketAlertCheck(p, 1));
1881  p->alerts.cnt = 0;
1882 
1883  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf2, httplen2);
1884  FAIL_IF(r != 0);
1885 
1886  /* do detect */
1887  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1888  FAIL_IF(PacketAlertCheck(p, 1));
1889  p->alerts.cnt = 0;
1890 
1891  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf3, httplen3);
1892  FAIL_IF(r != 0);
1893 
1894  /* do detect */
1895  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1896  FAIL_IF(PacketAlertCheck(p, 1));
1897  p->alerts.cnt = 0;
1898 
1899  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf4, httplen4);
1900  FAIL_IF(r != 0);
1901 
1902  /* do detect */
1903  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1904  FAIL_IF(!(PacketAlertCheck(p, 1)));
1905  p->alerts.cnt = 0;
1906 
1907  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf5, httplen5);
1908  FAIL_IF(r != 0);
1909 
1910  /* do detect */
1911  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1912  FAIL_IF(PacketAlertCheck(p, 1));
1913  p->alerts.cnt = 0;
1914 
1915  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf6, httplen6);
1916  FAIL_IF(r != 0);
1917 
1918  /* do detect */
1919  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1920  FAIL_IF((PacketAlertCheck(p, 1)) || (PacketAlertCheck(p, 2)));
1921  p->alerts.cnt = 0;
1922 
1923  SCLogDebug("sending data chunk 7");
1924 
1925  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf7, httplen7);
1926  FAIL_IF(r != 0);
1927 
1928  /* do detect */
1929  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1930  FAIL_IF(!(PacketAlertCheck(p, 2)));
1931  p->alerts.cnt = 0;
1932 
1933  HtpState *htp_state = f.alstate;
1934  FAIL_IF(htp_state == NULL);
1935 
1936  FAIL_IF(AppLayerParserGetTxCnt(&f, htp_state) != 2);
1937 
1938  if (alp_tctx != NULL)
1940  if (det_ctx != NULL) {
1941  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1942  }
1943  if (de_ctx != NULL) {
1946  }
1947 
1948  StreamTcpFreeConfig(true);
1949  FLOW_DESTROY(&f);
1950  UTHFreePacket(p);
1951  PASS;
1952 }
1953 
1954 /**
1955  * \brief Test parsing of pcre's with the W modifier set.
1956  */
1957 static int DetectPcreParseHttpHost(void)
1958 {
1959  AppProto alproto = ALPROTO_UNKNOWN;
1960  int list = DETECT_SM_LIST_NOTSET;
1962 
1963  FAIL_IF(de_ctx == NULL);
1964 
1965  DetectPcreData *pd = DetectPcreParse(de_ctx, "/domain\\.com/W", &list, NULL, 0, false, &alproto);
1966  FAIL_IF(pd == NULL);
1967  DetectPcreFree(de_ctx, pd);
1968 
1969  list = DETECT_SM_LIST_NOTSET;
1970  pd = DetectPcreParse(de_ctx, "/dOmain\\.com/W", &list, NULL, 0, false, &alproto);
1971  FAIL_IF(pd != NULL);
1972 
1973  /* Uppercase meta characters are valid. */
1974  list = DETECT_SM_LIST_NOTSET;
1975  pd = DetectPcreParse(de_ctx, "/domain\\D+\\.com/W", &list, NULL, 0, false, &alproto);
1976  FAIL_IF(pd == NULL);
1977  DetectPcreFree(de_ctx, pd);
1978 
1979  /* This should not parse as the first \ escapes the second \, then
1980  * we have a D. */
1981  list = DETECT_SM_LIST_NOTSET;
1982  pd = DetectPcreParse(de_ctx, "/\\\\Ddomain\\.com/W", &list, NULL, 0, false, &alproto);
1983  FAIL_IF(pd != NULL);
1984 
1986  PASS;
1987 }
1988 
1989 /**
1990  * \brief Test parsing of capture extension
1991  */
1992 static int DetectPcreParseCaptureTest(void)
1993 {
1995  FAIL_IF(de_ctx == NULL);
1996 
1997  Signature *s = DetectEngineAppendSig(de_ctx, "alert http any any -> any any "
1998  "(content:\"Server: \"; http_header; pcre:\"/(.*)\\r\\n/HR, flow:somecapture\"; content:\"xyz\"; http_header; sid:1;)");
1999  FAIL_IF(s == NULL);
2000  s = DetectEngineAppendSig(de_ctx, "alert http any any -> any any "
2001  "(content:\"Server: \"; http_header; pcre:\"/(flow:.*)\\r\\n/HR\"; content:\"xyz\"; http_header; sid:2;)");
2002  FAIL_IF(s == NULL);
2003  s = DetectEngineAppendSig(de_ctx, "alert http any any -> any any "
2004  "(content:\"Server: \"; http_header; pcre:\"/([a-z]+)([0-9]+)\\r\\n/HR, flow:somecapture, pkt:anothercap\"; content:\"xyz\"; http_header; sid:3;)");
2005  FAIL_IF(s == NULL);
2007  "alert http any any -> any any "
2008  "(content:\"Server: \"; http_header; pcre:\"/([a-z]+)\\r\\n/HR, flow:somecapture, "
2009  "pkt:anothercap\"; content:\"xyz\"; http_header; sid:3;)");
2010  FAIL_IF_NOT_NULL(s);
2011 
2013 
2014  uint32_t capid1 = VarNameStoreLookupByName("somecapture", VAR_TYPE_FLOW_VAR);
2015  FAIL_IF(capid1 == 0);
2016  uint32_t capid2 = VarNameStoreLookupByName("anothercap", VAR_TYPE_PKT_VAR);
2017  FAIL_IF(capid2 == 0);
2018  FAIL_IF(capid1 == capid2);
2019 
2021  PASS;
2022 }
2023 
2024 /**
2025  * \brief this function registers unit tests for DetectPcre
2026  */
2027 static void DetectPcreRegisterTests(void)
2028 {
2029  g_file_data_buffer_id = DetectBufferTypeGetByName("file_data");
2030  g_http_header_buffer_id = DetectBufferTypeGetByName("http_header");
2031  g_dce_stub_data_buffer_id = DetectBufferTypeGetByName("dce_stub_data");
2032 
2033  UtRegisterTest("DetectPcreParseTest01", DetectPcreParseTest01);
2034  UtRegisterTest("DetectPcreParseTest02", DetectPcreParseTest02);
2035  UtRegisterTest("DetectPcreParseTest03", DetectPcreParseTest03);
2036  UtRegisterTest("DetectPcreParseTest04", DetectPcreParseTest04);
2037  UtRegisterTest("DetectPcreParseTest05", DetectPcreParseTest05);
2038  UtRegisterTest("DetectPcreParseTest06", DetectPcreParseTest06);
2039  UtRegisterTest("DetectPcreParseTest07", DetectPcreParseTest07);
2040  UtRegisterTest("DetectPcreParseTest08", DetectPcreParseTest08);
2041  UtRegisterTest("DetectPcreParseTest09", DetectPcreParseTest09);
2042  UtRegisterTest("DetectPcreParseTest10", DetectPcreParseTest10);
2043  UtRegisterTest("DetectPcreParseTest15", DetectPcreParseTest15);
2044  UtRegisterTest("DetectPcreParseTest16", DetectPcreParseTest16);
2045  UtRegisterTest("DetectPcreParseTest17", DetectPcreParseTest17);
2046  UtRegisterTest("DetectPcreParseTest18", DetectPcreParseTest18);
2047  UtRegisterTest("DetectPcreParseTest19", DetectPcreParseTest19);
2048  UtRegisterTest("DetectPcreParseTest20", DetectPcreParseTest20);
2049  UtRegisterTest("DetectPcreParseTest21", DetectPcreParseTest21);
2050  UtRegisterTest("DetectPcreParseTest22", DetectPcreParseTest22);
2051  UtRegisterTest("DetectPcreParseTest23", DetectPcreParseTest23);
2052  UtRegisterTest("DetectPcreParseTest24", DetectPcreParseTest24);
2053  UtRegisterTest("DetectPcreParseTest25", DetectPcreParseTest25);
2054  UtRegisterTest("DetectPcreParseTest26", DetectPcreParseTest26);
2055  UtRegisterTest("DetectPcreParseTest27", DetectPcreParseTest27);
2056  UtRegisterTest("DetectPcreParseTest28", DetectPcreParseTest28);
2057 
2058  UtRegisterTest("DetectPcreTestSig01", DetectPcreTestSig01);
2059  UtRegisterTest("DetectPcreTestSig02 -- anchored pcre", DetectPcreTestSig02);
2060  UtRegisterTest("DetectPcreTestSig03 -- anchored pcre", DetectPcreTestSig03);
2061 
2062  UtRegisterTest("DetectPcreTxBodyChunksTest01",
2063  DetectPcreTxBodyChunksTest01);
2064  UtRegisterTest("DetectPcreTxBodyChunksTest02 -- modifier P, body chunks per tx",
2065  DetectPcreTxBodyChunksTest02);
2066  UtRegisterTest("DetectPcreTxBodyChunksTest03 -- modifier P, body chunks per tx",
2067  DetectPcreTxBodyChunksTest03);
2068 
2069  UtRegisterTest("DetectPcreParseHttpHost", DetectPcreParseHttpHost);
2070  UtRegisterTest("DetectPcreParseCaptureTest", DetectPcreParseCaptureTest);
2071 
2072 }
2073 #endif /* UNITTESTS */
DETECT_PCRE_CASELESS
#define DETECT_PCRE_CASELESS
Definition: detect-pcre.h:32
ConfGetInt
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:399
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:65
SigTableElmt_::url
const char * url
Definition: detect.h:1288
SC_MATCH_LIMIT_DEFAULT
#define SC_MATCH_LIMIT_DEFAULT
Definition: detect-pcre.h:40
DETECT_CONTENT_RELATIVE_NEXT
#define DETECT_CONTENT_RELATIVE_NEXT
Definition: detect-content.h:66
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1704
SigMatch_::prev
struct SigMatch_ * prev
Definition: detect.h:346
detect-content.h
DetectPcreData_::idx
uint8_t idx
Definition: detect-pcre.h:49
len
uint8_t len
Definition: app-layer-dnp3.h:2
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1103
DetectFlowvarPostMatchSetup
int DetectFlowvarPostMatchSetup(DetectEngineCtx *de_ctx, Signature *s, uint32_t idx)
Setup a post-match for flowvar storage We're piggyback riding the DetectFlowvarData struct.
Definition: detect-flowvar.c:266
detect-engine.h
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:110
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:437
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:567
SigTableElmt_::desc
const char * desc
Definition: detect.h:1287
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1000
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
ALPROTO_DCERPC
@ ALPROTO_DCERPC
Definition: app-layer-protos.h:38
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1275
flow-util.h
DetectPcrePayloadMatch
int DetectPcrePayloadMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *payload, uint32_t payload_len)
Match a regex on a single payload.
Definition: detect-pcre.c:175
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1285
stream-tcp.h
HtpBody_::sb
StreamingBuffer * sb
Definition: app-layer-htp.h:189
SigFree
void SigFree(DetectEngineCtx *, Signature *)
Definition: detect-parse.c:1595
DetectEngineCtx_::rule_file
char * rule_file
Definition: detect.h:918
DetectThreadCtxGetKeywordThreadCtx
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3656
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectVarStoreMatch
int DetectVarStoreMatch(DetectEngineThreadCtx *det_ctx, uint32_t idx, uint8_t *buffer, uint16_t len, int type)
Store flowvar in det_ctx so we can exec it post-match.
Definition: detect-flowvar.c:230
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:62
DetectParseRegex::context
pcre2_match_context * context
Definition: detect-parse.h:64
Signature_::alproto
AppProto alproto
Definition: detect.h:587
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
SIGMATCH_QUOTES_OPTIONAL
#define SIGMATCH_QUOTES_OPTIONAL
Definition: detect.h:1479
Flow_::proto
uint8_t proto
Definition: flow.h:369
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:80
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:289
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:141
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:353
Packet_::flags
uint32_t flags
Definition: decode.h:467
Flow_
Flow data structure.
Definition: flow.h:347
DetectSetupPCRE2
DetectParseRegex * DetectSetupPCRE2(const char *parse_str, int opts)
Definition: detect-parse.c:2702
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-pcre.c:66
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1279
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:827
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2592
HtpTxUserData_::request_body
HtpBody request_body
Definition: app-layer-htp.h:221
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:314
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:221
util-var-name.h
MIN
#define MIN(x, y)
Definition: suricata-common.h:386
DetectParseRegex::regex
pcre2_code * regex
Definition: detect-parse.h:63
DetectPcreData_::parse_regex
DetectParseRegex parse_regex
Definition: detect-pcre.h:45
DE_QUIET
#define DE_QUIET
Definition: detect.h:315
UTHPacketMatchSig
int UTHPacketMatchSig(Packet *p, const char *sig)
Definition: util-unittest-helper.c:843
DetectGetLastSMByListPtr
SigMatch * DetectGetLastSMByListPtr(const Signature *s, SigMatch *sm_list,...)
Returns the sm with the largest index (added last) from the list passed to us as a pointer.
Definition: detect-parse.c:649
stream-tcp-reassemble.h
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:337
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1824
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2629
DetectContentData_
Definition: detect-content.h:93
DetectPcreData_::flags
uint16_t flags
Definition: detect-pcre.h:48
VarNameStoreRegister
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
Definition: util-var-name.c:155
SigCleanSignatures
void SigCleanSignatures(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:46
DETECT_PCRE_CAPTURE_MAX
#define DETECT_PCRE_CAPTURE_MAX
Definition: detect-pcre.h:38
DETECT_VAR_TYPE_PKT_POSTMATCH
#define DETECT_VAR_TYPE_PKT_POSTMATCH
Definition: detect.h:726
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2575
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:461
DetectBufferGetActiveList
int DetectBufferGetActiveList(DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine.c:1453
StreamingBufferSegmentCompareRawData
int StreamingBufferSegmentCompareRawData(const StreamingBuffer *sb, const StreamingBufferSegment *seg, const uint8_t *rawdata, uint32_t rawdata_len)
Definition: util-streaming-buffer.c:1735
Flow_::protoctx
void * protoctx
Definition: flow.h:437
SigMatchData_
Data needed for Match()
Definition: detect.h:350
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1270
detect-pcre.h
PARSE_CAPTURE_REGEX
#define PARSE_CAPTURE_REGEX
Definition: detect-pcre.c:65
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:96
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:592
util-unittest.h
HtpBody_::first
HtpBodyChunk * first
Definition: app-layer-htp.h:186
HtpState_
Definition: app-layer-htp.h:243
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1124
VAR_TYPE_PKT_VAR_KV
@ VAR_TYPE_PKT_VAR_KV
Definition: util-var.h:33
detect-flowvar.h
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
DetectPcreData_::capids
uint32_t capids[DETECT_PCRE_CAPTURE_MAX]
Definition: detect-pcre.h:51
StreamTcpInitConfig
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition: stream-tcp.c:359
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:38
app-layer-htp.h
VarNameStoreLookupByName
uint32_t VarNameStoreLookupByName(const char *name, const enum VarTypes type)
find name for id+type at packet time.
Definition: util-var-name.c:322
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
DETECT_PCRE_RAWBYTES
#define DETECT_PCRE_RAWBYTES
Definition: detect-pcre.h:31
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1075
DetectEngineBufferTypeGetDescriptionById
const char * DetectEngineBufferTypeGetDescriptionById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1232
AppLayerHtpEnableRequestBodyCallback
void AppLayerHtpEnableRequestBodyCallback(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request body...
Definition: app-layer-htp.c:470
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:22
SignatureInitData_::list
int list
Definition: detect.h:551
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
DetectPcreData_::thread_ctx_id
int thread_ctx_id
Definition: detect-pcre.h:52
pkt-var.h
VarNameStoreUnregister
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:201
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:2270
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:344
SC_MATCH_LIMIT_RECURSION_DEFAULT
#define SC_MATCH_LIMIT_RECURSION_DEFAULT
Definition: detect-pcre.h:41
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:295
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2040
stream.h
Packet_
Definition: decode.h:430
detect-engine-build.h
stream-tcp-private.h
detect-engine-alert.h
conf.h
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
SIGMATCH_HANDLE_NEGATION
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1487
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:654
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SignatureInitData_::negated
bool negated
Definition: detect.h:529
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1253
PageSupportsRWX
#define PageSupportsRWX()
Definition: util-pages.h:37
util-pages.h
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:322
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
DETECT_PCRE
@ DETECT_PCRE
Definition: detect-engine-register.h:64
DETECT_VAR_TYPE_FLOW_POSTMATCH
#define DETECT_VAR_TYPE_FLOW_POSTMATCH
Definition: detect.h:725
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1127
DetectPcreData_::captypes
uint8_t captypes[DETECT_PCRE_CAPTURE_MAX]
Definition: detect-pcre.h:50
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1971
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:293
DETECT_SM_LIST_NOTSET
#define DETECT_SM_LIST_NOTSET
Definition: detect.h:135
Packet_::flow
struct Flow_ * flow
Definition: decode.h:469
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3308
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
StreamTcpFreeConfig
void StreamTcpFreeConfig(bool quiet)
Definition: stream-tcp.c:690
DetectRegisterThreadCtxFuncs
int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *), int mode)
Register Thread keyword context Funcs.
Definition: detect-engine.c:3586
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1307
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3522
SigMatch_::type
uint16_t type
Definition: detect.h:342
HtpBodyChunk_
Definition: app-layer-htp.h:177
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
DetectBufferGetFirstSigMatch
SigMatch * DetectBufferGetFirstSigMatch(const Signature *s, const uint32_t buf_id)
Definition: detect-engine.c:1356
FatalError
#define FatalError(...)
Definition: util-debug.h:502
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:835
HtpTxUserData_
Definition: app-layer-htp.h:206
detect-engine-sigorder.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
DetectEngineThreadCtx_::pcre_match_start_offset
uint32_t pcre_match_start_offset
Definition: detect.h:1105
str
#define str(s)
Definition: suricata-common.h:286
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:485
Flow_::alstate
void * alstate
Definition: flow.h:472
Flow_::flags
uint32_t flags
Definition: flow.h:417
detect-parse.h
Signature_
Signature container.
Definition: detect.h:582
SigMatch_
a single match condition for a signature
Definition: detect.h:341
payload_len
uint16_t payload_len
Definition: stream-tcp-private.h:1
VAR_TYPE_FLOW_VAR
@ VAR_TYPE_FLOW_VAR
Definition: util-var.h:37
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:223
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2553
DETECT_PCRE_RELATIVE_NEXT
#define DETECT_PCRE_RELATIVE_NEXT
Definition: detect-pcre.h:35
app-layer-protos.h
DetectVarStoreMatchKeyValue
int DetectVarStoreMatchKeyValue(DetectEngineThreadCtx *det_ctx, uint8_t *key, uint16_t key_len, uint8_t *buffer, uint16_t len, int type)
Store flowvar in det_ctx so we can exec it post-match.
Definition: detect-flowvar.c:210
DetectPcreData_
Definition: detect-pcre.h:43
DetectParseFreeRegex
void DetectParseFreeRegex(DetectParseRegex *r)
Definition: detect-parse.c:2639
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:829
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:66
DETECT_PCRE_RELATIVE
#define DETECT_PCRE_RELATIVE
Definition: detect-pcre.h:29
SigAlloc
Signature * SigAlloc(void)
Definition: detect-parse.c:1480
DetectUnregisterThreadCtxFuncs
int DetectUnregisterThreadCtxFuncs(DetectEngineCtx *de_ctx, void *data, const char *name)
Remove Thread keyword context registration.
Definition: detect-engine.c:3638
DetectEngineCtx_::rule_line
int rule_line
Definition: detect.h:917
TcpSession_
Definition: stream-tcp-private.h:283
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:446
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
util-pool.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DETECT_PCRE_NEGATE
#define DETECT_PCRE_NEGATE
Definition: detect-pcre.h:36
flow-var.h
AppLayerParserGetTxCnt
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
Definition: app-layer-parser.c:1120
HtpBodyChunk_::sbseg
StreamingBufferSegment sbseg
Definition: app-layer-htp.h:180
DetectPcreRegister
void DetectPcreRegister(void)
Definition: detect-pcre.c:96
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:127
VAR_TYPE_PKT_VAR
@ VAR_TYPE_PKT_VAR
Definition: util-var.h:32
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:997
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1277
app-layer.h
DETECT_PCRE_MATCH_LIMIT
#define DETECT_PCRE_MATCH_LIMIT
Definition: detect-pcre.h:34