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