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