65 #define PARSE_CAPTURE_REGEX "\\(\\?P\\<([A-z]+)\\_([A-z0-9_]+)\\>"
66 #define PARSE_REGEX "(?<!\\\\)/(.*(?<!(?<!\\\\)\\\\))/([^\"]*)"
68 static int pcre_match_limit = 0;
69 static int pcre_match_limit_recursion = 0;
75 static int pcre2_use_jit = 1;
83 const char *
str,
const size_t strlen,
int start_offset,
int options,
84 pcre2_match_data *match)
93 static void DetectPcreRegisterTests(
void);
114 SCLogDebug(
"Using PCRE match-limit setting of: %i", pcre_match_limit);
116 pcre_match_limit = (int)val;
118 SCLogInfo(
"Using PCRE match-limit setting of: %i", pcre_match_limit);
120 SCLogDebug(
"Using PCRE match-limit setting of: %i", pcre_match_limit);
126 if (!
SCConfGetInt(
"pcre.match-limit-recursion", &val)) {
128 SCLogDebug(
"Using PCRE match-limit-recursion setting of: %i", pcre_match_limit_recursion);
130 pcre_match_limit_recursion = (int)val;
132 SCLogInfo(
"Using PCRE match-limit-recursion setting of: %i", pcre_match_limit_recursion);
134 SCLogDebug(
"Using PCRE match-limit-recursion setting of: %i", pcre_match_limit_recursion);
139 if (parse_regex == NULL) {
140 FatalError(
"pcre2 compile failed for parse_regex");
146 if (parse_capture_regex == NULL) {
147 FatalError(
"pcre2 compile failed for parse_capture_regex");
150 #ifdef PCRE2_HAVE_JIT
152 SCLogConfig(
"PCRE2 won't use JIT as OS doesn't allow RWX pages");
159 uint8_t *str_ptr, uint16_t capture_len)
164 if (json_key == NULL) {
181 SCJbSetStringFromBytes(js, json_key, str_ptr, capture_len);
182 uint32_t js_len = (uint32_t)SCJbLen(js);
184 SCLogDebug(
"Captured length is too long for JSON.");
190 SCLogDebug(
"Captured length is zero for JSON.");
228 const uint8_t *ptr = NULL;
230 PCRE2_SIZE capture_len = 0;
242 int start_offset = 0;
248 pcre2_match_data *match =
251 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
252 det_ctx->fuzzed_pcre_len +=
len;
253 if (det_ctx->fuzzed_pcre_len > 0x20000) {
257 ret = DetectPcreExec(det_ctx, pe, (
char *)ptr,
len, start_offset, 0, match);
260 if (ret == PCRE2_ERROR_NOMATCH) {
268 }
else if (ret >= 0) {
280 if (ret > 1 && pe->
idx != 0) {
282 for (x = 0; x < pe->
idx; x++) {
288 const char *pcre2_str_ptr = NULL;
289 ret = pcre2_substring_get_bynumber(
290 match, x + 1, (PCRE2_UCHAR8 **)&pcre2_str_ptr, &capture_len);
292 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
296 capture_len = (capture_len < 0xffff) ? (uint16_t)capture_len : 0xffff;
297 uint8_t *str_ptr =
SCMalloc(capture_len);
299 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
302 memcpy(str_ptr, pcre2_str_ptr, capture_len);
303 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
310 const char *pcre2_str_ptr2 = NULL;
312 uint16_t key_len = (capture_len < 0xff) ? (uint16_t)capture_len : 0xff;
313 int ret2 = pcre2_substring_get_bynumber(
314 match, x + 2, (PCRE2_UCHAR8 **)&pcre2_str_ptr2, &capture_len);
318 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
321 capture_len = (capture_len < 0xffff) ? (uint16_t)capture_len : 0xffff;
322 uint8_t *str_ptr2 =
SCMalloc(capture_len);
325 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
328 memcpy(str_ptr2, pcre2_str_ptr2, capture_len);
329 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
332 (uint8_t *)str_ptr2, (uint16_t)capture_len,
344 (void)DetectAlertStoreMatch(det_ctx, s, pe->
capids[x], (uint8_t *)str_ptr,
345 (uint16_t)capture_len);
354 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(match);
356 det_ctx->
buffer_offset = (uint32_t)((ptr + ov[1]) - payload);
369 static int DetectPcreSetList(
int list,
int set)
372 SCLogError(
"only one pcre option to specify a buffer type is allowed");
378 static bool DetectPcreHasUnicodeCluster(
const char *re)
380 bool escaping =
false;
381 for (
const char *
p = re; *
p != 0;
p++) {
387 }
else if (*
p ==
'\\') {
394 static int DetectPcreHasUpperCase(
const char *re)
396 size_t len = strlen(re);
397 bool is_meta =
false;
398 bool is_meta_hex =
false;
399 int meta_hex_cnt = 0;
401 for (
size_t i = 0; i <
len; i++) {
405 if (meta_hex_cnt == 2) {
409 }
else if (is_meta) {
416 else if (re[i] ==
'\\') {
419 else if (isupper((
unsigned char)re[i])) {
428 const char *regexstr,
int *sm_list,
char *capture_names,
429 size_t capture_names_size,
bool negate,
AppProto *alproto)
431 pcre2_match_data *match = NULL;
437 int ret = 0, res = 0;
438 int check_host_header = 0;
439 char op_str[64] =
"";
441 bool apply_match_limit =
false;
444 const char *fcap = strstr(regexstr,
"flow:");
445 const char *pcap = strstr(regexstr,
"pkt:");
446 const char *acap = strstr(regexstr,
"alert:");
449 size_t slen = strlen(regexstr) + 1;
450 if (fcap || pcap || acap) {
457 cut_capture = (int)(fcap - regexstr);
461 cut_capture = (int)
MIN(cut_capture, (pcap - regexstr));
463 cut_capture = (int)(pcap - regexstr);
469 cut_capture =
MIN(cut_capture, (
int)(acap - regexstr));
471 cut_capture = (int)(acap - regexstr);
476 if (cut_capture > 1) {
477 int offset = cut_capture - 1;
480 if (regexstr[
offset] ==
',' || regexstr[
offset] ==
' ') {
487 if (cut_capture == (
offset + 1)) {
488 SCLogDebug(
"missing separators, assume it's part of the regex");
491 strlcpy(capture_names, regexstr+cut_capture, capture_names_size);
492 if (capture_names[strlen(capture_names)-1] ==
'"')
493 capture_names[strlen(capture_names)-1] =
'\0';
501 match = pcre2_match_data_create_from_pattern(parse_regex->
regex, NULL);
506 ret = pcre2_match(parse_regex->
regex, (PCRE2_SPTR8)regexstr, slen, 0, 0, match, NULL);
512 res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)re, &slen);
514 SCLogError(
"pcre2_substring_copy_bynumber failed");
515 pcre2_match_data_free(match);
520 size_t copylen =
sizeof(op_str);
521 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)op_str, ©len);
523 SCLogError(
"pcre2_substring_copy_bynumber failed");
524 pcre2_match_data_free(match);
544 opts |= PCRE2_ANCHORED;
547 opts |= PCRE2_DOLLAR_ENDONLY;
550 opts |= PCRE2_UNGREEDY;
554 opts |= PCRE2_CASELESS;
558 opts |= PCRE2_MULTILINE;
561 opts |= PCRE2_DOTALL;
564 opts |= PCRE2_EXTENDED;
568 apply_match_limit =
true;
573 SCLogError(
"regex modifier 'B' inconsistent with chosen buffer");
586 SCLogError(
"regex modifier 'U' inconsistent with 'B'");
590 *sm_list = DetectPcreSetList(*sm_list, list);
596 SCLogError(
"regex modifier 'V' inconsistent with 'B'");
600 *sm_list = DetectPcreSetList(*sm_list, list);
606 SCLogError(
"regex modifier 'W' inconsistent with 'B'");
610 *sm_list = DetectPcreSetList(*sm_list, list);
612 check_host_header = 1;
617 SCLogError(
"regex modifier 'Z' inconsistent with 'B'");
621 *sm_list = DetectPcreSetList(*sm_list, list);
627 SCLogError(
"regex modifier 'H' inconsistent with 'B'");
631 *sm_list = DetectPcreSetList(*sm_list, list);
636 SCLogError(
"regex modifier 'I' inconsistent with 'B'");
640 *sm_list = DetectPcreSetList(*sm_list, list);
646 *sm_list = DetectPcreSetList(*sm_list, list);
652 SCLogError(
"regex modifier 'M' inconsistent with 'B'");
656 *sm_list = DetectPcreSetList(*sm_list, list);
662 SCLogError(
"regex modifier 'C' inconsistent with 'B'");
666 *sm_list = DetectPcreSetList(*sm_list, list);
673 *sm_list = DetectPcreSetList(*sm_list, list);
680 *sm_list = DetectPcreSetList(*sm_list, list);
687 *sm_list = DetectPcreSetList(*sm_list, list);
694 *sm_list = DetectPcreSetList(*sm_list, list);
699 SCLogError(
"unknown regex modifier '%c'", *op);
711 if (check_host_header) {
714 "specified along with \"i(caseless)\" modifier. "
715 "Since the hostname buffer we match against "
716 "is actually lowercase, having a "
717 "nocase is redundant.");
719 else if (DetectPcreHasUpperCase(re)) {
721 "specified has an uppercase char. "
722 "Since the hostname buffer we match against "
723 "is actually lowercase, please specify an "
724 "all lowercase based pcre.");
734 if (capture_names == NULL || strlen(capture_names) == 0)
735 opts |= PCRE2_NO_AUTO_CAPTURE;
738 if (DetectPcreHasUnicodeCluster(re)) {
739 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
747 pcre2_compile((PCRE2_SPTR8)re, PCRE2_ZERO_TERMINATED, opts, &en, &eo2, NULL);
749 opts &= ~PCRE2_NO_AUTO_CAPTURE;
751 pcre2_compile((PCRE2_SPTR8)re, PCRE2_ZERO_TERMINATED, opts, &en, &eo2, NULL);
754 PCRE2_UCHAR errbuffer[256];
755 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
756 SCLogError(
"pcre2 compile of \"%s\" failed at "
758 regexstr, (
int)eo2, errbuffer);
762 #ifdef PCRE2_HAVE_JIT
769 SCLogDebug(
"PCRE2 JIT compiler does not support: %s. "
770 "Falling back to regular PCRE2 handling (%s:%d)",
778 SCLogError(
"pcre2 could not create match context");
782 if (apply_match_limit) {
783 if (pcre_match_limit >= -1) {
786 if (pcre_match_limit_recursion >= -1) {
795 pcre2_match_data_free(match);
799 pcre2_match_data_free(match);
800 DetectPcreFree(
de_ctx, pd);
810 int ret = 0, res = 0;
811 char type_str[16] =
"";
812 const char *orig_right_edge = regexstr + strlen(regexstr);
818 pcre2_match_data *match = NULL;
820 SCLogDebug(
"regexstr %s, pd %p", regexstr, pd);
822 ret = pcre2_pattern_info(pd->
parse_regex.
regex, PCRE2_INFO_CAPTURECOUNT, &capture_cnt);
823 SCLogDebug(
"ret %d capture_cnt %d", ret, capture_cnt);
824 if (ret == 0 && capture_cnt && strlen(capture_names) > 0)
827 while ((name_array[name_idx] = strtok_r(name_idx == 0 ? capture_names : NULL,
" ,", &ptr))){
828 if (name_idx > (capture_cnt - 1)) {
830 "var capture names than capturing substrings");
833 SCLogDebug(
"name '%s'", name_array[name_idx]);
835 if (strcmp(name_array[name_idx],
"pkt:key") == 0) {
843 }
else if (key == 1 && strcmp(name_array[name_idx],
"pkt:value") == 0) {
848 }
else if (key == 0 && strcmp(name_array[name_idx],
"pkt:value") == 0) {
850 }
else if (key == 1) {
853 }
else if (strncmp(name_array[name_idx],
"flow:", 5) == 0) {
854 uint32_t varname_id =
862 }
else if (strncmp(name_array[name_idx],
"pkt:", 4) == 0) {
863 uint32_t varname_id =
872 }
else if (strncmp(name_array[name_idx],
"alert:", 6) == 0) {
873 uint32_t varname_id =
883 "var capture names must start with 'pkt:' or 'flow:'");
895 size_t cap_buffer_len = strlen(regexstr) + 1;
897 char capture_str[cap_buffer_len];
898 memset(capture_str, 0x00, cap_buffer_len);
908 pcre2_match_data_free(match);
911 copylen =
sizeof(type_str);
912 res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)type_str, ©len);
914 SCLogError(
"pcre2_substring_copy_bynumber failed");
917 cap_buffer_len = strlen(regexstr) + 1;
918 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)capture_str, &cap_buffer_len);
920 SCLogError(
"pcre2_substring_copy_bynumber failed");
923 if (strlen(capture_str) == 0 || strlen(type_str) == 0) {
931 SCLogError(
"rule can have maximally %d pkt/flow "
934 pcre2_match_data_free(match);
938 if (strcmp(type_str,
"pkt") == 0) {
946 }
else if (strcmp(type_str,
"flow") == 0) {
953 }
else if (strcmp(type_str,
"alert") == 0) {
963 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(match);
966 pcre2_match_data_free(match);
969 if (regexstr >= orig_right_edge)
975 pcre2_match_data_free(match);
979 static void *DetectPcreThreadInit(
void *data)
982 pcre2_match_data *match = pcre2_match_data_create_from_pattern(pd->
parse_regex.
regex, NULL);
986 static void DetectPcreThreadFree(
void *
ctx)
989 pcre2_match_data *match = (pcre2_match_data *)
ctx;
990 pcre2_match_data_free(match);
999 char capture_names[1024] =
"";
1002 pd = DetectPcreParse(
de_ctx, regexstr, &parsed_sm_list,
1007 if (DetectPcreParseCapture(regexstr,
de_ctx, pd, capture_names) < 0)
1011 de_ctx,
"pcre", DetectPcreThreadInit, (
void *)pd, DetectPcreThreadFree, 0);
1018 SCLogError(
"Expression seen with a sticky buffer still set; either (1) reset sticky "
1019 "buffer with pkt_data or (2) use a sticky buffer providing \"%s\".",
1028 switch (parsed_sm_list) {
1042 sm_list = parsed_sm_list;
1055 for (uint8_t x = 0; x < pd->
idx; x++) {
1072 "preceding match in the same buffer");
1075 }
else if (prev_pm == NULL) {
1089 DetectPcreFree(
de_ctx, pd);
1103 for (uint8_t i = 0; i < pd->
idx; i++) {
1111 static int g_file_data_buffer_id = 0;
1112 static int g_http_header_buffer_id = 0;
1113 static int g_dce_stub_data_buffer_id = 0;
1118 static int DetectPcreParseTest01 (
void)
1121 const char *teststring =
"/blah/7";
1127 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1137 static int DetectPcreParseTest02 (
void)
1140 const char *teststring =
"/blah/Ui$";
1146 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1157 static int DetectPcreParseTest03 (
void)
1160 const char *teststring =
"/blah/UNi";
1166 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1176 static int DetectPcreParseTest04 (
void)
1179 const char *teststring =
"/b\\\"lah/i";
1185 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1189 DetectPcreFree(
de_ctx, pd);
1197 static int DetectPcreParseTest05 (
void)
1200 const char *teststring =
"/b(l|a)h/";
1206 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1210 DetectPcreFree(
de_ctx, pd);
1218 static int DetectPcreParseTest06 (
void)
1221 const char *teststring =
"/b(l|a)h/smi";
1227 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1231 DetectPcreFree(
de_ctx, pd);
1239 static int DetectPcreParseTest07 (
void)
1242 const char *teststring =
"/blah/Ui";
1248 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1252 DetectPcreFree(
de_ctx, pd);
1260 static int DetectPcreParseTest08 (
void)
1263 const char *teststring =
"/b(l|a)h/O";
1269 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1273 DetectPcreFree(
de_ctx, pd);
1282 static int DetectPcreParseTest09 (
void)
1285 const char *teststring =
"/lala\\\\/";
1291 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1294 DetectPcreFree(
de_ctx, pd);
1302 static int DetectPcreParseTest10(
void)
1332 static int DetectPcreParseTest15(
void)
1339 "alert tcp any any -> any any "
1340 "(msg:\"Testing pcre relative http_method\"; "
1342 "http_method; pcre:\"/abc/RM\"; sid:1;)");
1352 static int DetectPcreParseTest16(
void)
1359 "alert tcp any any -> any any "
1360 "(msg:\"Testing pcre relative http_cookie\"; "
1361 "content:\"test\"; "
1362 "http_cookie; pcre:\"/abc/RC\"; sid:1;)");
1371 static int DetectPcreParseTest17(
void)
1378 "alert tcp any any -> any any "
1379 "(msg:\"Testing pcre relative http_raw_header\"; "
1380 "flow:to_server; content:\"test\"; "
1381 "http_raw_header; pcre:\"/abc/RD\"; sid:1;)");
1390 static int DetectPcreParseTest18(
void)
1397 "alert tcp any any -> any any "
1398 "(msg:\"Testing pcre relative http_header\"; "
1399 "content:\"test\"; "
1400 "http_header; pcre:\"/abc/RH\"; sid:1;)");
1409 static int DetectPcreParseTest19(
void)
1416 "alert tcp any any -> any any "
1417 "(msg:\"Testing pcre relative http_client_body\"; "
1418 "content:\"test\"; "
1419 "http_client_body; pcre:\"/abc/RP\"; sid:1;)");
1428 static int DetectPcreParseTest20(
void)
1436 "alert tcp any any -> any any "
1437 "(msg:\"Testing http_raw_uri\"; "
1438 "content:\"test\"; "
1439 "http_raw_uri; pcre:\"/abc/RI\"; sid:1;)");
1448 static int DetectPcreParseTest21(
void)
1456 "alert tcp any any -> any any "
1457 "(msg:\"Testing pcre relative uricontent\"; "
1458 "uricontent:\"test\"; "
1459 "pcre:\"/abc/RU\"; sid:1;)");
1468 static int DetectPcreParseTest22(
void)
1476 "alert tcp any any -> any any "
1477 "(msg:\"Testing pcre relative http_uri\"; "
1478 "content:\"test\"; "
1479 "http_uri; pcre:\"/abc/RU\"; sid:1;)");
1488 static int DetectPcreParseTest23(
void)
1496 "alert tcp any any -> any any "
1497 "(msg:\"Testing inconsistent pcre relative\"; "
1499 "http_cookie; pcre:\"/abc/RM\"; sid:1;)");
1508 static int DetectPcreParseTest24(
void)
1516 "alert tcp any any -> any any "
1517 "(msg:\"Testing inconsistent pcre modifiers\"; "
1518 "pcre:\"/abc/UI\"; sid:1;)");
1527 static int DetectPcreParseTest25(
void)
1535 "alert tcp any any -> any any "
1536 "(msg:\"Testing inconsistent pcre modifiers\"; "
1537 "pcre:\"/abc/DH\"; sid:1;)");
1546 static int DetectPcreParseTest26(
void)
1554 "alert http any any -> any any "
1555 "(msg:\"Testing inconsistent pcre modifiers\"; "
1556 "pcre:\"/abc/F\"; sid:1;)");
1565 static int DetectPcreParseTest27(
void)
1573 "(content:\"baduricontent\"; http_raw_uri; "
1574 "pcre:\"/^[a-z]{5}\\.html/R\"; sid:2; rev:2;)");
1583 static int DetectPcreParseTest28(
void)
1591 "(content:\"|2E|suricata\"; http_host; pcre:\"/\\x2Esuricata$/W\"; "
1601 static int DetectPcreParseTest29(
void)
1610 SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"byte_jump\"; byte_jump:4,0; "
1611 "pcre:\"/abc/R\"; sid:1; rev:1;)");
1616 "byte_jump:4,0,relative; pcre:\"/abc/R\"; sid:1; rev:1;)");
1621 "byte_extract:4,0,len; pcre:\"/abc/R\"; sid:1; rev:1;)");
1626 SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"byte_math\"; "
1627 "byte_math: bytes 4, offset 0, oper +, rvalue 0, result len; "
1628 "pcre:\"/abc/R\"; sid:1; rev:1;)");
1635 SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"byte_test\"; byte_test:4,=,0,0; "
1636 "pcre:\"/abc/R\"; sid:1; rev:1;)");
1641 SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"isdataat\"; isdataat:10; "
1642 "pcre:\"/abc/R\"; sid:1; rev:1;)");
1647 "alert tcp any any -> any any (msg:\"no match\"; pcre:\"/abc/R\"; sid:1; rev:1;)");
1654 static int DetectPcreTestSig01(
void)
1656 uint8_t *buf = (uint8_t *)
"lalala lalala\\ lala\n";
1657 uint16_t buflen = strlen((
char *)buf);
1660 char sig[] =
"alert tcp any any -> any any (msg:\"pcre with an ending slash\"; pcre:\"/ "
1661 "lalala\\\\/\"; sid:1;)";
1669 static int DetectPcreTestSig02(
void)
1671 uint8_t *buf = (uint8_t *)
"lalala\n";
1672 uint16_t buflen = strlen((
char *)buf);
1675 char sig[] =
"alert tcp any any -> any any (msg:\"pcre with an ending slash\"; "
1676 "pcre:\"/^(la)+$/\"; sid:1;)";
1684 static int DetectPcreTestSig03(
void)
1687 uint8_t *buf = (uint8_t *)
"lalala";
1688 uint16_t buflen = strlen((
char *)buf);
1691 char sig[] =
"alert tcp any any -> any any (msg:\"pcre with an ending slash\"; "
1692 "pcre:\"/^(la)+$/\"; sid:1;)";
1701 static int DetectPcreTxBodyChunksTest01(
void)
1706 uint8_t httpbuf1[] =
"GET / HTTP/1.1\r\n";
1707 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1708 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
1709 uint8_t httpbuf4[] =
"Body one!!";
1710 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1711 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1712 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1713 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1714 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
1715 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
1716 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1717 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
1718 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
1719 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
1722 memset(&
f, 0,
sizeof(
f));
1723 memset(&
ssn, 0,
sizeof(
ssn));
1799 static int DetectPcreTxBodyChunksTest02(
void)
1807 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n";
1808 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1809 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
1810 uint8_t httpbuf4[] =
"Body one!!";
1811 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1812 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1813 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1814 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1815 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
1816 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
1817 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1818 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
1819 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
1820 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
1825 memset(&
f, 0,
sizeof(
f));
1826 memset(&
ssn, 0,
sizeof(
ssn));
1848 s =
DetectEngineAppendSig(
de_ctx,
"alert tcp any any -> any any (content:\"POST\"; http_method; content:\"Mozilla\"; http_header; content:\"dummy\"; http_cookie; pcre:\"/one/P\"; sid:1; rev:1;)");
1850 s =
DetectEngineAppendSig(
de_ctx,
"alert tcp any any -> any any (content:\"GET\"; http_method; content:\"Firefox\"; http_header; content:\"dummy2\"; http_cookie; pcre:\"/two/P\"; sid:2; rev:1;)");
1949 static int DetectPcreTxBodyChunksTest03(
void)
1957 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n";
1958 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1959 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
1960 uint8_t httpbuf4[] =
"Body one!!";
1961 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1962 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1963 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1964 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1965 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
1966 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
1967 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1968 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
1969 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
1970 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
1975 memset(&
f, 0,
sizeof(
f));
1976 memset(&
ssn, 0,
sizeof(
ssn));
1998 s =
DetectEngineAppendSig(
de_ctx,
"alert tcp any any -> any any (content:\"POST\"; http_method; content:\"Mozilla\"; http_header; content:\"dummy\"; http_cookie; pcre:\"/one/P\"; sid:1; rev:1;)");
2000 s =
DetectEngineAppendSig(
de_ctx,
"alert tcp any any -> any any (content:\"GET\"; http_method; content:\"Firefox\"; http_header; content:\"dummy2\"; http_cookie; pcre:\"/two/P\"; sid:2; rev:1;)");
2084 static int DetectPcreParseHttpHost(
void)
2092 DetectPcreData *pd = DetectPcreParse(
de_ctx,
"/domain\\.com/W", &list, NULL, 0,
false, &alproto);
2094 DetectPcreFree(
de_ctx, pd);
2097 pd = DetectPcreParse(
de_ctx,
"/dOmain\\.com/W", &list, NULL, 0,
false, &alproto);
2102 pd = DetectPcreParse(
de_ctx,
"/domain\\D+\\.com/W", &list, NULL, 0,
false, &alproto);
2104 DetectPcreFree(
de_ctx, pd);
2109 pd = DetectPcreParse(
de_ctx,
"/\\\\Ddomain\\.com/W", &list, NULL, 0,
false, &alproto);
2119 static int DetectPcreParseCaptureTest(
void)
2125 "(content:\"Server: \"; http_header; pcre:\"/(.*)\\r\\n/HR, flow:somecapture\"; content:\"xyz\"; http_header; sid:1;)");
2128 "(content:\"Server: \"; http_header; pcre:\"/(flow:.*)\\r\\n/HR\"; content:\"xyz\"; http_header; sid:2;)");
2131 "(content:\"Server: \"; http_header; pcre:\"/([a-z]+)([0-9]+)\\r\\n/HR, flow:somecapture, pkt:anothercap\"; content:\"xyz\"; http_header; sid:3;)");
2134 "alert http any any -> any any "
2135 "(content:\"Server: \"; http_header; pcre:\"/([a-z]+)\\r\\n/HR, flow:somecapture, "
2136 "pkt:anothercap\"; content:\"xyz\"; http_header; sid:3;)");
2154 static void DetectPcreRegisterTests(
void)
2187 UtRegisterTest(
"DetectPcreTestSig02 -- anchored pcre", DetectPcreTestSig02);
2188 UtRegisterTest(
"DetectPcreTestSig03 -- anchored pcre", DetectPcreTestSig03);
2191 DetectPcreTxBodyChunksTest01);
2192 UtRegisterTest(
"DetectPcreTxBodyChunksTest02 -- modifier P, body chunks per tx",
2193 DetectPcreTxBodyChunksTest02);
2194 UtRegisterTest(
"DetectPcreTxBodyChunksTest03 -- modifier P, body chunks per tx",
2195 DetectPcreTxBodyChunksTest03);
2197 UtRegisterTest(
"DetectPcreParseHttpHost", DetectPcreParseHttpHost);
2198 UtRegisterTest(
"DetectPcreParseCaptureTest", DetectPcreParseCaptureTest);