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