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 ret = DetectPcreExec(det_ctx, pe, (
char *)ptr,
len, start_offset, 0, match);
254 if (ret == PCRE2_ERROR_NOMATCH) {
262 }
else if (ret >= 0) {
274 if (ret > 1 && pe->
idx != 0) {
276 for (x = 0; x < pe->
idx; x++) {
282 const char *pcre2_str_ptr = NULL;
283 ret = pcre2_substring_get_bynumber(
284 match, x + 1, (PCRE2_UCHAR8 **)&pcre2_str_ptr, &capture_len);
286 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
290 capture_len = (capture_len < 0xffff) ? (uint16_t)capture_len : 0xffff;
291 uint8_t *str_ptr =
SCMalloc(capture_len);
293 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
296 memcpy(str_ptr, pcre2_str_ptr, capture_len);
297 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr);
304 const char *pcre2_str_ptr2 = NULL;
306 uint16_t key_len = (capture_len < 0xff) ? (uint16_t)capture_len : 0xff;
307 int ret2 = pcre2_substring_get_bynumber(
308 match, x + 2, (PCRE2_UCHAR8 **)&pcre2_str_ptr2, &capture_len);
312 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
315 capture_len = (capture_len < 0xffff) ? (uint16_t)capture_len : 0xffff;
316 uint8_t *str_ptr2 =
SCMalloc(capture_len);
319 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
322 memcpy(str_ptr2, pcre2_str_ptr2, capture_len);
323 pcre2_substring_free((PCRE2_UCHAR8 *)pcre2_str_ptr2);
326 (uint8_t *)str_ptr2, (uint16_t)capture_len,
338 (void)DetectAlertStoreMatch(det_ctx, s, pe->
capids[x], (uint8_t *)str_ptr,
339 (uint16_t)capture_len);
348 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(match);
350 det_ctx->
buffer_offset = (uint32_t)((ptr + ov[1]) - payload);
363 static int DetectPcreSetList(
int list,
int set)
366 SCLogError(
"only one pcre option to specify a buffer type is allowed");
372 static bool DetectPcreHasUnicodeCluster(
const char *re)
374 return strstr(re,
"\\X") != NULL;
377 static int DetectPcreHasUpperCase(
const char *re)
379 size_t len = strlen(re);
380 bool is_meta =
false;
381 bool is_meta_hex =
false;
382 int meta_hex_cnt = 0;
384 for (
size_t i = 0; i <
len; i++) {
388 if (meta_hex_cnt == 2) {
392 }
else if (is_meta) {
399 else if (re[i] ==
'\\') {
402 else if (isupper((
unsigned char)re[i])) {
411 const char *regexstr,
int *sm_list,
char *capture_names,
412 size_t capture_names_size,
bool negate,
AppProto *alproto)
414 pcre2_match_data *match = NULL;
420 int ret = 0, res = 0;
421 int check_host_header = 0;
422 char op_str[64] =
"";
424 bool apply_match_limit =
false;
427 const char *fcap = strstr(regexstr,
"flow:");
428 const char *pcap = strstr(regexstr,
"pkt:");
429 const char *acap = strstr(regexstr,
"alert:");
432 size_t slen = strlen(regexstr) + 1;
433 if (fcap || pcap || acap) {
440 cut_capture = (int)(fcap - regexstr);
444 cut_capture = (int)
MIN(cut_capture, (pcap - regexstr));
446 cut_capture = (int)(pcap - regexstr);
452 cut_capture =
MIN(cut_capture, (
int)(acap - regexstr));
454 cut_capture = (int)(acap - regexstr);
459 if (cut_capture > 1) {
460 int offset = cut_capture - 1;
463 if (regexstr[
offset] ==
',' || regexstr[
offset] ==
' ') {
470 if (cut_capture == (
offset + 1)) {
471 SCLogDebug(
"missing separators, assume it's part of the regex");
474 strlcpy(capture_names, regexstr+cut_capture, capture_names_size);
475 if (capture_names[strlen(capture_names)-1] ==
'"')
476 capture_names[strlen(capture_names)-1] =
'\0';
484 match = pcre2_match_data_create_from_pattern(parse_regex->
regex, NULL);
489 ret = pcre2_match(parse_regex->
regex, (PCRE2_SPTR8)regexstr, slen, 0, 0, match, NULL);
495 res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)re, &slen);
497 SCLogError(
"pcre2_substring_copy_bynumber failed");
498 pcre2_match_data_free(match);
503 size_t copylen =
sizeof(op_str);
504 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)op_str, ©len);
506 SCLogError(
"pcre2_substring_copy_bynumber failed");
507 pcre2_match_data_free(match);
527 opts |= PCRE2_ANCHORED;
530 opts |= PCRE2_DOLLAR_ENDONLY;
533 opts |= PCRE2_UNGREEDY;
537 opts |= PCRE2_CASELESS;
541 opts |= PCRE2_MULTILINE;
544 opts |= PCRE2_DOTALL;
547 opts |= PCRE2_EXTENDED;
551 apply_match_limit =
true;
556 SCLogError(
"regex modifier 'B' inconsistent with chosen buffer");
569 SCLogError(
"regex modifier 'U' inconsistent with 'B'");
573 *sm_list = DetectPcreSetList(*sm_list, list);
579 SCLogError(
"regex modifier 'V' inconsistent with 'B'");
583 *sm_list = DetectPcreSetList(*sm_list, list);
589 SCLogError(
"regex modifier 'W' inconsistent with 'B'");
593 *sm_list = DetectPcreSetList(*sm_list, list);
595 check_host_header = 1;
600 SCLogError(
"regex modifier 'Z' inconsistent with 'B'");
604 *sm_list = DetectPcreSetList(*sm_list, list);
610 SCLogError(
"regex modifier 'H' inconsistent with 'B'");
614 *sm_list = DetectPcreSetList(*sm_list, list);
619 SCLogError(
"regex modifier 'I' inconsistent with 'B'");
623 *sm_list = DetectPcreSetList(*sm_list, list);
629 *sm_list = DetectPcreSetList(*sm_list, list);
635 SCLogError(
"regex modifier 'M' inconsistent with 'B'");
639 *sm_list = DetectPcreSetList(*sm_list, list);
645 SCLogError(
"regex modifier 'C' inconsistent with 'B'");
649 *sm_list = DetectPcreSetList(*sm_list, list);
656 *sm_list = DetectPcreSetList(*sm_list, list);
663 *sm_list = DetectPcreSetList(*sm_list, list);
670 *sm_list = DetectPcreSetList(*sm_list, list);
677 *sm_list = DetectPcreSetList(*sm_list, list);
682 SCLogError(
"unknown regex modifier '%c'", *op);
694 if (check_host_header) {
697 "specified along with \"i(caseless)\" modifier. "
698 "Since the hostname buffer we match against "
699 "is actually lowercase, having a "
700 "nocase is redundant.");
702 else if (DetectPcreHasUpperCase(re)) {
704 "specified has an uppercase char. "
705 "Since the hostname buffer we match against "
706 "is actually lowercase, please specify an "
707 "all lowercase based pcre.");
717 if (capture_names == NULL || strlen(capture_names) == 0)
718 opts |= PCRE2_NO_AUTO_CAPTURE;
721 if (DetectPcreHasUnicodeCluster(re)) {
722 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
730 pcre2_compile((PCRE2_SPTR8)re, PCRE2_ZERO_TERMINATED, opts, &en, &eo2, NULL);
732 opts &= ~PCRE2_NO_AUTO_CAPTURE;
734 pcre2_compile((PCRE2_SPTR8)re, PCRE2_ZERO_TERMINATED, opts, &en, &eo2, NULL);
737 PCRE2_UCHAR errbuffer[256];
738 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
739 SCLogError(
"pcre2 compile of \"%s\" failed at "
741 regexstr, (
int)eo2, errbuffer);
745 #ifdef PCRE2_HAVE_JIT
752 SCLogDebug(
"PCRE2 JIT compiler does not support: %s. "
753 "Falling back to regular PCRE2 handling (%s:%d)",
761 SCLogError(
"pcre2 could not create match context");
765 if (apply_match_limit) {
766 if (pcre_match_limit >= -1) {
769 if (pcre_match_limit_recursion >= -1) {
778 pcre2_match_data_free(match);
782 pcre2_match_data_free(match);
783 DetectPcreFree(
de_ctx, pd);
793 int ret = 0, res = 0;
794 char type_str[16] =
"";
795 const char *orig_right_edge = regexstr + strlen(regexstr);
801 pcre2_match_data *match = NULL;
803 SCLogDebug(
"regexstr %s, pd %p", regexstr, pd);
805 ret = pcre2_pattern_info(pd->
parse_regex.
regex, PCRE2_INFO_CAPTURECOUNT, &capture_cnt);
806 SCLogDebug(
"ret %d capture_cnt %d", ret, capture_cnt);
807 if (ret == 0 && capture_cnt && strlen(capture_names) > 0)
810 while ((name_array[name_idx] = strtok_r(name_idx == 0 ? capture_names : NULL,
" ,", &ptr))){
811 if (name_idx > (capture_cnt - 1)) {
813 "var capture names than capturing substrings");
816 SCLogDebug(
"name '%s'", name_array[name_idx]);
818 if (strcmp(name_array[name_idx],
"pkt:key") == 0) {
826 }
else if (key == 1 && strcmp(name_array[name_idx],
"pkt:value") == 0) {
831 }
else if (key == 0 && strcmp(name_array[name_idx],
"pkt:value") == 0) {
833 }
else if (key == 1) {
836 }
else if (strncmp(name_array[name_idx],
"flow:", 5) == 0) {
837 uint32_t varname_id =
845 }
else if (strncmp(name_array[name_idx],
"pkt:", 4) == 0) {
846 uint32_t varname_id =
855 }
else if (strncmp(name_array[name_idx],
"alert:", 6) == 0) {
856 uint32_t varname_id =
866 "var capture names must start with 'pkt:' or 'flow:'");
878 size_t cap_buffer_len = strlen(regexstr) + 1;
880 char capture_str[cap_buffer_len];
881 memset(capture_str, 0x00, cap_buffer_len);
891 pcre2_match_data_free(match);
894 copylen =
sizeof(type_str);
895 res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)type_str, ©len);
897 SCLogError(
"pcre2_substring_copy_bynumber failed");
900 cap_buffer_len = strlen(regexstr) + 1;
901 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)capture_str, &cap_buffer_len);
903 SCLogError(
"pcre2_substring_copy_bynumber failed");
906 if (strlen(capture_str) == 0 || strlen(type_str) == 0) {
914 SCLogError(
"rule can have maximally %d pkt/flow "
917 pcre2_match_data_free(match);
921 if (strcmp(type_str,
"pkt") == 0) {
929 }
else if (strcmp(type_str,
"flow") == 0) {
936 }
else if (strcmp(type_str,
"alert") == 0) {
946 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(match);
949 pcre2_match_data_free(match);
952 if (regexstr >= orig_right_edge)
958 pcre2_match_data_free(match);
962 static void *DetectPcreThreadInit(
void *data)
965 pcre2_match_data *match = pcre2_match_data_create_from_pattern(pd->
parse_regex.
regex, NULL);
969 static void DetectPcreThreadFree(
void *
ctx)
972 pcre2_match_data *match = (pcre2_match_data *)
ctx;
973 pcre2_match_data_free(match);
982 char capture_names[1024] =
"";
985 pd = DetectPcreParse(
de_ctx, regexstr, &parsed_sm_list,
990 if (DetectPcreParseCapture(regexstr,
de_ctx, pd, capture_names) < 0)
994 de_ctx,
"pcre", DetectPcreThreadInit, (
void *)pd, DetectPcreThreadFree, 0);
1001 SCLogError(
"Expression seen with a sticky buffer still set; either (1) reset sticky "
1002 "buffer with pkt_data or (2) use a sticky buffer providing \"%s\".",
1011 switch (parsed_sm_list) {
1025 sm_list = parsed_sm_list;
1038 for (uint8_t x = 0; x < pd->
idx; x++) {
1055 "preceding match in the same buffer");
1058 }
else if (prev_pm == NULL) {
1072 DetectPcreFree(
de_ctx, pd);
1086 for (uint8_t i = 0; i < pd->
idx; i++) {
1094 static int g_file_data_buffer_id = 0;
1095 static int g_http_header_buffer_id = 0;
1096 static int g_dce_stub_data_buffer_id = 0;
1101 static int DetectPcreParseTest01 (
void)
1104 const char *teststring =
"/blah/7";
1110 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1120 static int DetectPcreParseTest02 (
void)
1123 const char *teststring =
"/blah/Ui$";
1129 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1140 static int DetectPcreParseTest03 (
void)
1143 const char *teststring =
"/blah/UNi";
1149 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1159 static int DetectPcreParseTest04 (
void)
1162 const char *teststring =
"/b\\\"lah/i";
1168 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1172 DetectPcreFree(
de_ctx, pd);
1180 static int DetectPcreParseTest05 (
void)
1183 const char *teststring =
"/b(l|a)h/";
1189 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1193 DetectPcreFree(
de_ctx, pd);
1201 static int DetectPcreParseTest06 (
void)
1204 const char *teststring =
"/b(l|a)h/smi";
1210 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1214 DetectPcreFree(
de_ctx, pd);
1222 static int DetectPcreParseTest07 (
void)
1225 const char *teststring =
"/blah/Ui";
1231 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1235 DetectPcreFree(
de_ctx, pd);
1243 static int DetectPcreParseTest08 (
void)
1246 const char *teststring =
"/b(l|a)h/O";
1252 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1256 DetectPcreFree(
de_ctx, pd);
1265 static int DetectPcreParseTest09 (
void)
1268 const char *teststring =
"/lala\\\\/";
1274 pd = DetectPcreParse(
de_ctx, teststring, &list, NULL, 0,
false, &alproto);
1277 DetectPcreFree(
de_ctx, pd);
1285 static int DetectPcreParseTest10(
void)
1315 static int DetectPcreParseTest15(
void)
1322 "alert tcp any any -> any any "
1323 "(msg:\"Testing pcre relative http_method\"; "
1325 "http_method; pcre:\"/abc/RM\"; sid:1;)");
1335 static int DetectPcreParseTest16(
void)
1342 "alert tcp any any -> any any "
1343 "(msg:\"Testing pcre relative http_cookie\"; "
1344 "content:\"test\"; "
1345 "http_cookie; pcre:\"/abc/RC\"; sid:1;)");
1354 static int DetectPcreParseTest17(
void)
1361 "alert tcp any any -> any any "
1362 "(msg:\"Testing pcre relative http_raw_header\"; "
1363 "flow:to_server; content:\"test\"; "
1364 "http_raw_header; pcre:\"/abc/RD\"; sid:1;)");
1373 static int DetectPcreParseTest18(
void)
1380 "alert tcp any any -> any any "
1381 "(msg:\"Testing pcre relative http_header\"; "
1382 "content:\"test\"; "
1383 "http_header; pcre:\"/abc/RH\"; sid:1;)");
1392 static int DetectPcreParseTest19(
void)
1399 "alert tcp any any -> any any "
1400 "(msg:\"Testing pcre relative http_client_body\"; "
1401 "content:\"test\"; "
1402 "http_client_body; pcre:\"/abc/RP\"; sid:1;)");
1411 static int DetectPcreParseTest20(
void)
1419 "alert tcp any any -> any any "
1420 "(msg:\"Testing http_raw_uri\"; "
1421 "content:\"test\"; "
1422 "http_raw_uri; pcre:\"/abc/RI\"; sid:1;)");
1431 static int DetectPcreParseTest21(
void)
1439 "alert tcp any any -> any any "
1440 "(msg:\"Testing pcre relative uricontent\"; "
1441 "uricontent:\"test\"; "
1442 "pcre:\"/abc/RU\"; sid:1;)");
1451 static int DetectPcreParseTest22(
void)
1459 "alert tcp any any -> any any "
1460 "(msg:\"Testing pcre relative http_uri\"; "
1461 "content:\"test\"; "
1462 "http_uri; pcre:\"/abc/RU\"; sid:1;)");
1471 static int DetectPcreParseTest23(
void)
1479 "alert tcp any any -> any any "
1480 "(msg:\"Testing inconsistent pcre relative\"; "
1482 "http_cookie; pcre:\"/abc/RM\"; sid:1;)");
1491 static int DetectPcreParseTest24(
void)
1499 "alert tcp any any -> any any "
1500 "(msg:\"Testing inconsistent pcre modifiers\"; "
1501 "pcre:\"/abc/UI\"; sid:1;)");
1510 static int DetectPcreParseTest25(
void)
1518 "alert tcp any any -> any any "
1519 "(msg:\"Testing inconsistent pcre modifiers\"; "
1520 "pcre:\"/abc/DH\"; sid:1;)");
1529 static int DetectPcreParseTest26(
void)
1537 "alert http any any -> any any "
1538 "(msg:\"Testing inconsistent pcre modifiers\"; "
1539 "pcre:\"/abc/F\"; sid:1;)");
1548 static int DetectPcreParseTest27(
void)
1556 "(content:\"baduricontent\"; http_raw_uri; "
1557 "pcre:\"/^[a-z]{5}\\.html/R\"; sid:2; rev:2;)");
1566 static int DetectPcreParseTest28(
void)
1574 "(content:\"|2E|suricata\"; http_host; pcre:\"/\\x2Esuricata$/W\"; "
1584 static int DetectPcreParseTest29(
void)
1593 SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"byte_jump\"; byte_jump:4,0; "
1594 "pcre:\"/abc/R\"; sid:1; rev:1;)");
1599 "byte_jump:4,0,relative; pcre:\"/abc/R\"; sid:1; rev:1;)");
1604 "byte_extract:4,0,len; pcre:\"/abc/R\"; sid:1; rev:1;)");
1609 SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"byte_math\"; "
1610 "byte_math: bytes 4, offset 0, oper +, rvalue 0, result len; "
1611 "pcre:\"/abc/R\"; sid:1; rev:1;)");
1618 SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"byte_test\"; byte_test:4,=,0,0; "
1619 "pcre:\"/abc/R\"; sid:1; rev:1;)");
1624 SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"isdataat\"; isdataat:10; "
1625 "pcre:\"/abc/R\"; sid:1; rev:1;)");
1630 "alert tcp any any -> any any (msg:\"no match\"; pcre:\"/abc/R\"; sid:1; rev:1;)");
1637 static int DetectPcreTestSig01(
void)
1639 uint8_t *buf = (uint8_t *)
"lalala lalala\\ lala\n";
1640 uint16_t buflen = strlen((
char *)buf);
1643 char sig[] =
"alert tcp any any -> any any (msg:\"pcre with an ending slash\"; pcre:\"/ "
1644 "lalala\\\\/\"; sid:1;)";
1652 static int DetectPcreTestSig02(
void)
1654 uint8_t *buf = (uint8_t *)
"lalala\n";
1655 uint16_t buflen = strlen((
char *)buf);
1658 char sig[] =
"alert tcp any any -> any any (msg:\"pcre with an ending slash\"; "
1659 "pcre:\"/^(la)+$/\"; sid:1;)";
1667 static int DetectPcreTestSig03(
void)
1670 uint8_t *buf = (uint8_t *)
"lalala";
1671 uint16_t buflen = strlen((
char *)buf);
1674 char sig[] =
"alert tcp any any -> any any (msg:\"pcre with an ending slash\"; "
1675 "pcre:\"/^(la)+$/\"; sid:1;)";
1684 static int DetectPcreTxBodyChunksTest01(
void)
1689 uint8_t httpbuf1[] =
"GET / HTTP/1.1\r\n";
1690 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1691 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
1692 uint8_t httpbuf4[] =
"Body one!!";
1693 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1694 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1695 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1696 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1697 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
1698 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
1699 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1700 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
1701 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
1702 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
1705 memset(&
f, 0,
sizeof(
f));
1706 memset(&
ssn, 0,
sizeof(
ssn));
1782 static int DetectPcreTxBodyChunksTest02(
void)
1790 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n";
1791 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1792 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
1793 uint8_t httpbuf4[] =
"Body one!!";
1794 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1795 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1796 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1797 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1798 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
1799 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
1800 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1801 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
1802 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
1803 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
1808 memset(&
f, 0,
sizeof(
f));
1809 memset(&
ssn, 0,
sizeof(
ssn));
1831 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;)");
1833 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;)");
1932 static int DetectPcreTxBodyChunksTest03(
void)
1940 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n";
1941 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
1942 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
1943 uint8_t httpbuf4[] =
"Body one!!";
1944 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1945 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1946 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1947 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1948 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
1949 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
1950 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nBody two!!";
1951 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
1952 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
1953 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
1958 memset(&
f, 0,
sizeof(
f));
1959 memset(&
ssn, 0,
sizeof(
ssn));
1981 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;)");
1983 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;)");
2067 static int DetectPcreParseHttpHost(
void)
2075 DetectPcreData *pd = DetectPcreParse(
de_ctx,
"/domain\\.com/W", &list, NULL, 0,
false, &alproto);
2077 DetectPcreFree(
de_ctx, pd);
2080 pd = DetectPcreParse(
de_ctx,
"/dOmain\\.com/W", &list, NULL, 0,
false, &alproto);
2085 pd = DetectPcreParse(
de_ctx,
"/domain\\D+\\.com/W", &list, NULL, 0,
false, &alproto);
2087 DetectPcreFree(
de_ctx, pd);
2092 pd = DetectPcreParse(
de_ctx,
"/\\\\Ddomain\\.com/W", &list, NULL, 0,
false, &alproto);
2102 static int DetectPcreParseCaptureTest(
void)
2108 "(content:\"Server: \"; http_header; pcre:\"/(.*)\\r\\n/HR, flow:somecapture\"; content:\"xyz\"; http_header; sid:1;)");
2111 "(content:\"Server: \"; http_header; pcre:\"/(flow:.*)\\r\\n/HR\"; content:\"xyz\"; http_header; sid:2;)");
2114 "(content:\"Server: \"; http_header; pcre:\"/([a-z]+)([0-9]+)\\r\\n/HR, flow:somecapture, pkt:anothercap\"; content:\"xyz\"; http_header; sid:3;)");
2117 "alert http any any -> any any "
2118 "(content:\"Server: \"; http_header; pcre:\"/([a-z]+)\\r\\n/HR, flow:somecapture, "
2119 "pkt:anothercap\"; content:\"xyz\"; http_header; sid:3;)");
2137 static void DetectPcreRegisterTests(
void)
2170 UtRegisterTest(
"DetectPcreTestSig02 -- anchored pcre", DetectPcreTestSig02);
2171 UtRegisterTest(
"DetectPcreTestSig03 -- anchored pcre", DetectPcreTestSig03);
2174 DetectPcreTxBodyChunksTest01);
2175 UtRegisterTest(
"DetectPcreTxBodyChunksTest02 -- modifier P, body chunks per tx",
2176 DetectPcreTxBodyChunksTest02);
2177 UtRegisterTest(
"DetectPcreTxBodyChunksTest03 -- modifier P, body chunks per tx",
2178 DetectPcreTxBodyChunksTest03);
2180 UtRegisterTest(
"DetectPcreParseHttpHost", DetectPcreParseHttpHost);
2181 UtRegisterTest(
"DetectPcreParseCaptureTest", DetectPcreParseCaptureTest);