42 #define PARSE_REGEX "^(\\s*only\\s*)|\\s*([0-9]+)\\s*,\\s*([0-9]+)\\s*$"
48 static void DetectFastPatternRegisterTests(
void);
82 if (list_id == tmp->list_id) {
91 if (priority < tmp->priority)
102 new->list_id = list_id;
103 new->priority = priority;
113 new->list_id = list_id;
114 new->priority = priority;
134 Add(&g_fp_support_smlist_list, list_id, priority);
158 FatalError(
"out of memory: %s", strerror(errno));
218 char arg_substr[128] =
"";
220 pcre2_match_data *match = NULL;
224 if (pm1 == NULL && pm2 == NULL) {
226 "the rule, without a content context. Please use a "
227 "content based keyword before using fast_pattern");
237 }
else if (pm1 && !pm2) {
252 "used with negated content, along with relative modifiers");
256 if (arg == NULL|| strcmp(arg,
"") == 0) {
259 "options for the same content");
270 "can be used on only one content in a rule");
293 "used with negated content or with any of the relative "
294 "modifiers like distance, within, offset, depth");
300 }
else if (ret == 4) {
301 pcre2len =
sizeof(arg_substr);
302 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)arg_substr, &pcre2len);
304 SCLogError(
"pcre2_substring_copy_bynumber failed "
305 "for fast_pattern offset");
310 (
const char *)arg_substr) < 0) {
317 pcre2len =
sizeof(arg_substr);
318 res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)arg_substr, &pcre2len);
320 SCLogError(
"pcre2_substring_copy_bynumber failed "
321 "for fast_pattern offset");
326 (
const char *)arg_substr) < 0) {
334 if (
offset > (65535 - length)) {
336 "exceeds limit pattern length limit");
342 "offset (%u)) exceeds pattern length (%u)",
352 SCLogError(
"parse error, ret %" PRId32
", string %s", ret, arg);
358 pcre2_match_data_free(match);
363 pcre2_match_data_free(match);
381 static int DetectFastPatternStickySingle(
const char *sticky,
const int list)
386 snprintf(
string,
sizeof(
string),
387 "alert tcp any any -> any any "
388 "(%s%scontent:\"one\"; fast_pattern; sid:1;)",
389 sticky ? sticky :
"", sticky ?
"; " :
" ");
402 static int DetectFastPatternModifierSingle(
const char *sticky,
const int list)
407 snprintf(
string,
sizeof(
string),
408 "alert tcp any any -> any any "
409 "(content:\"one\"; %s%sfast_pattern; sid:1;)",
410 sticky ? sticky :
"", sticky ?
"; " :
" ");
423 static int DetectFastPatternStickySingleNoFP(
const char *sticky,
const int list)
428 snprintf(
string,
sizeof(
string),
429 "alert tcp any any -> any any "
430 "(%s%scontent:\"one\"; sid:1;)",
431 sticky ? sticky :
"", sticky ?
"; " :
" ");
444 static int DetectFastPatternModifierSingleNoFP(
const char *sticky,
const int list)
449 snprintf(
string,
sizeof(
string),
450 "alert tcp any any -> any any "
451 "(content:\"one\"; %s%ssid:1;)",
452 sticky ? sticky :
"", sticky ?
"; " :
" ");
465 static int DetectFastPatternStickySingleBadArg(
const char *sticky)
471 snprintf(
string,
sizeof(
string),
472 "alert tcp any any -> any any "
473 "(%s%scontent:\"one\"; fast_pattern:boo; sid:1;)",
474 sticky ? sticky :
"", sticky ?
"; " :
" ");
478 snprintf(
string,
sizeof(
string),
479 "alert tcp any any -> any any "
480 "(%s%scontent:\"one\"; fast_pattern:only; content:\"two\"; distance:10; sid:1;)",
481 sticky ? sticky :
"", sticky ?
"; " :
" ");
485 snprintf(
string,
sizeof(
string),
486 "alert tcp any any -> any any "
487 "(%s%scontent:\"one\"; content:\"two\"; fast_pattern:only; distance:10; sid:1;)",
488 sticky ? sticky :
"", sticky ?
"; " :
" ");
492 snprintf(
string,
sizeof(
string),
493 "alert tcp any any -> any any "
494 "(%s%scontent:\"one\"; content:\"two\"; distance:10; fast_pattern:only; sid:1;)",
495 sticky ? sticky :
"", sticky ?
"; " :
" ");
499 snprintf(
string,
sizeof(
string),
500 "alert tcp any any -> any any "
501 "(%s%scontent:\"one\"; fast_pattern:5,6; sid:1;)",
502 sticky ? sticky :
"", sticky ?
"; " :
" ");
509 static int DetectFastPatternModifierBadRules(
const char *sticky)
515 snprintf(
string,
sizeof(
string),
516 "alert tcp any any -> any any "
517 "(content:\"one\"; %s%sfast_pattern:boo; sid:1;)",
518 sticky ? sticky :
"", sticky ?
"; " :
" ");
522 snprintf(
string,
sizeof(
string),
523 "alert tcp any any -> any any "
524 "(content:\"one\"; %s%sfast_pattern:only; content:\"two\"; %s%sdistance:10; sid:1;)",
525 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
530 snprintf(
string,
sizeof(
string),
"alert tcp any any -> any any "
531 "(content:\"one\"; %s%s content:\"two\"; %s%sdistance:10; fast_pattern:only; sid:1;)",
532 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
537 snprintf(
string,
sizeof(
string),
538 "alert tcp any any -> any any "
539 "(content:\"one\"; %s%sfast_pattern:only; content:\"two\"; %s%swithin:10; sid:1;)",
540 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
544 snprintf(
string,
sizeof(
string),
545 "alert tcp any any -> any any "
546 "(content:\"one\"; %s%s content:\"two\"; %s%swithin:10; fast_pattern:only; sid:1;)",
547 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
551 snprintf(
string,
sizeof(
string),
552 "alert tcp any any -> any any "
553 "(content:\"one\"; %s%sfast_pattern:only; offset:10; sid:1;)",
554 sticky ? sticky :
"", sticky ?
"; " :
" ");
558 snprintf(
string,
sizeof(
string),
559 "alert tcp any any -> any any "
560 "(content:\"one\"; %s%s offset:10; fast_pattern:only; sid:1;)",
561 sticky ? sticky :
"", sticky ?
"; " :
" ");
565 snprintf(
string,
sizeof(
string),
566 "alert tcp any any -> any any "
567 "(content:\"one\"; %s%sfast_pattern:only; depth:10; sid:1;)",
568 sticky ? sticky :
"", sticky ?
"; " :
" ");
572 snprintf(
string,
sizeof(
string),
573 "alert tcp any any -> any any "
574 "(content:\"one\"; %s%s depth:10; fast_pattern:only; sid:1;)",
575 sticky ? sticky :
"", sticky ?
"; " :
" ");
579 snprintf(
string,
sizeof(
string),
580 "alert tcp any any -> any any "
581 "(content:\"one\"; %s%s content:!\"two\"; %s%sfast_pattern:only; sid:1;)",
582 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
586 snprintf(
string,
sizeof(
string),
587 "alert tcp any any -> any any "
588 "(content:\"one\"; %s%sfast_pattern:5,6; sid:1;)",
589 sticky ? sticky :
"", sticky ?
"; " :
" ");
593 snprintf(
string,
sizeof(
string),
594 "alert tcp any any -> any any "
595 "(content:\"one\"; %s%sfast_pattern:65977,2; sid:1;)",
596 sticky ? sticky :
"", sticky ?
"; " :
" ");
600 snprintf(
string,
sizeof(
string),
601 "alert tcp any any -> any any "
602 "(content:\"one\"; %s%sfast_pattern:2,65977; sid:1;)",
603 sticky ? sticky :
"", sticky ?
"; " :
" ");
607 snprintf(
string,
sizeof(
string),
608 "alert tcp any any -> any any "
609 "(content:\"one\"; %s%sfast_pattern:2,65534; sid:1;)",
610 sticky ? sticky :
"", sticky ?
"; " :
" ");
614 snprintf(
string,
sizeof(
string),
615 "alert tcp any any -> any any "
616 "(content:\"one\"; %s%sfast_pattern:65534,2; sid:1;)",
617 sticky ? sticky :
"", sticky ?
"; " :
" ");
621 snprintf(
string,
sizeof(
string),
622 "alert tcp any any -> any any "
623 "(content:\"one\"; %s%scontent:!\"two\"; fast_pattern:1,2; %s%sdistance:10; sid:1;)",
624 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
628 snprintf(
string,
sizeof(
string),
629 "alert tcp any any -> any any "
630 "(content:\"one\"; %s%scontent:!\"two\"; fast_pattern:1,2; %s%swithin:10; sid:1;)",
631 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
635 snprintf(
string,
sizeof(
string),
636 "alert tcp any any -> any any "
637 "(content:\"one\"; %s%scontent:!\"two\"; fast_pattern:1,2; %s%sdepth:10; sid:1;)",
638 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
642 snprintf(
string,
sizeof(
string),
643 "alert tcp any any -> any any "
644 "(content:\"one\"; %s%scontent:!\"two\"; fast_pattern:1,2; %s%soffset:10; sid:1;)",
645 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
652 static int DetectFastPatternStickySingleFPOnly(
const char *sticky,
const int list)
657 snprintf(
string,
sizeof(
string),
658 "alert tcp any any -> any any "
659 "(%s%scontent:\"one\"; fast_pattern:only; sid:1;)",
660 sticky ? sticky :
"", sticky ?
"; " :
" ");
674 static int DetectFastPatternModifierFPOnly(
const char *sticky,
const int list)
679 snprintf(
string,
sizeof(
string),
680 "alert tcp any any -> any any "
681 "(content:\"one\"; %s%sfast_pattern:only; sid:1;)",
682 sticky ? sticky :
"", sticky ?
"; " :
" ");
693 snprintf(
string,
sizeof(
string),
694 "alert tcp any any -> any any "
695 "(content:\"one\"; %s%scontent:\"two\"; %s%sfast_pattern:only; sid:2;)",
696 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
699 sm = GetMatches(s, list);
714 snprintf(
string,
sizeof(
string),
715 "alert tcp any any -> any any "
716 "(content:\"one\"; %s%scontent:\"two\"; distance:10; %s%scontent:\"three\"; "
717 "%s%sfast_pattern:only; sid:3;)",
718 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ",
719 sticky ? sticky :
"", sticky ?
"; " :
" ");
722 sm = GetMatches(s, list);
745 snprintf(
string,
sizeof(
string),
746 "alert tcp any any -> any any "
747 "(content:\"one\"; %s%scontent:\"two\"; within:10; %s%scontent:\"three\"; "
748 "%s%sfast_pattern:only; sid:4;)",
749 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ",
750 sticky ? sticky :
"", sticky ?
"; " :
" ");
753 sm = GetMatches(s, list);
776 snprintf(
string,
sizeof(
string),
777 "alert tcp any any -> any any "
778 "(content:\"one\"; %s%scontent:\"two\"; offset:10; %s%scontent:\"three\"; "
779 "%s%sfast_pattern:only; sid:5;)",
780 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ",
781 sticky ? sticky :
"", sticky ?
"; " :
" ");
784 sm = GetMatches(s, list);
807 snprintf(
string,
sizeof(
string),
808 "alert tcp any any -> any any "
809 "(content:\"one\"; %s%scontent:\"two\"; depth:10; %s%scontent:\"three\"; "
810 "%s%sfast_pattern:only; sid:6;)",
811 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ",
812 sticky ? sticky :
"", sticky ?
"; " :
" ");
815 sm = GetMatches(s, list);
838 snprintf(
string,
sizeof(
string),
839 "alert tcp any any -> any any "
840 "(content:!\"one\"; %s%sfast_pattern; content:\"two\"; depth:10; %s%ssid:7;)",
841 sticky ? sticky :
"", sticky ?
"; " :
" ", sticky ? sticky :
"", sticky ?
"; " :
" ");
844 sm = GetMatches(s, list);
865 static int DetectFastPatternStickyFPChop(
const char *sticky,
const int list)
870 snprintf(
string,
sizeof(
string),
871 "alert tcp any any -> any any "
872 "(%s%scontent:\"onetwothree\"; fast_pattern:3,4; sid:1;)",
873 sticky ? sticky :
"", sticky ?
"; " :
" ");
887 snprintf(
string,
sizeof(
string),
888 "alert tcp any any -> any any "
889 "(%s%scontent:\"onetwothree\"; fast_pattern:3,4; content:\"xyz\"; distance:10; sid:2;)",
890 sticky ? sticky :
"", sticky ?
"; " :
" ");
893 sm = GetMatches(s, list);
908 static int DetectFastPatternModifierFPChop(
const char *sticky,
const int list)
913 snprintf(
string,
sizeof(
string),
914 "alert tcp any any -> any any "
915 "(content:\"onetwothree\"; %s%sfast_pattern:3,4; sid:1;)",
916 sticky ? sticky :
"", sticky ?
"; " :
" ");
930 snprintf(
string,
sizeof(
string),
931 "alert tcp any any -> any any "
932 "(content:!\"onetwothree\"; %s%sfast_pattern:3,4; sid:2;)",
933 sticky ? sticky :
"", sticky ?
"; " :
" ");
936 sm = GetMatches(s, list);
956 static int DetectFastPatternTest01(
void)
962 FAIL_IF_NOT(DetectFastPatternStickySingleBadArg(NULL));
963 FAIL_IF_NOT(DetectFastPatternModifierBadRules(NULL));
970 const char *buffer_name;
972 const char *mod_name;
974 {
"file_data",
"file.data", NULL },
975 {
"http_uri",
"http.uri",
"http_uri" },
976 {
"http_raw_uri",
"http.uri.raw",
"http_raw_uri" },
977 {
"http_user_agent",
"http.user_agent",
"http_user_agent" },
978 {
"http_header",
"http.header",
"http_header" },
981 {
"http_method",
"http.method",
"http_method" },
982 {
"http_cookie",
"http.cookie",
"http_cookie" },
983 {
"http_host",
"http.host",
"http_host" },
984 {
"http_raw_host",
"http.host.raw",
"http_raw_host" },
985 {
"http_stat_code",
"http.stat_code",
"http_stat_code" },
986 {
"http_stat_msg",
"http.stat_msg",
"http_stat_msg" },
987 {
"http_client_body",
"http.request_body",
"http_client_body" },
988 { NULL, NULL, NULL },
991 for (
int i = 0; keywords[i].buffer_name != NULL; i++) {
995 const char *k = keywords[i].sb_name;
997 FAIL_IF_NOT(DetectFastPatternStickySingle(k, list_id));
998 FAIL_IF_NOT(DetectFastPatternStickySingleNoFP(k, list_id));
999 FAIL_IF_NOT(DetectFastPatternStickySingleBadArg(k));
1000 FAIL_IF_NOT(DetectFastPatternStickySingleFPOnly(k, list_id));
1001 FAIL_IF_NOT(DetectFastPatternStickyFPChop(k, list_id));
1003 k = keywords[i].mod_name;
1005 FAIL_IF_NOT(DetectFastPatternModifierSingle(k, list_id));
1006 FAIL_IF_NOT(DetectFastPatternModifierSingleNoFP(k, list_id));
1007 FAIL_IF_NOT(DetectFastPatternModifierBadRules(k));
1008 FAIL_IF_NOT(DetectFastPatternModifierFPOnly(k, list_id));
1009 FAIL_IF_NOT(DetectFastPatternModifierFPChop(k, list_id));
1021 static int DetectFastPatternTest14(
void)
1023 uint8_t *buf = (uint8_t *)
"Dummy is our name. Oh yes. From right here "
1024 "right now, all the way to hangover. right. strings5_imp now here "
1025 "comes our dark knight strings_string5. Yes here is our dark knight";
1026 uint16_t buflen = strlen((
char *)buf);
1030 memset(&th_v, 0,
sizeof(th_v));
1041 "alert tcp any any -> any any "
1042 "(msg:\"fast_pattern test\"; content:\"strings_string5\"; content:\"knight\"; "
1043 "fast_pattern; sid:1;)");
1047 "alert tcp any any -> any any "
1048 "(msg:\"test different content\"; content:\"Dummy is our name\"; sid:2;)");
1072 static int DetectFastPatternTest671(
void)
1080 de_ctx,
"alert tcp any any -> any any (content:\"onetwothreefour\"; sid:1;)");
1083 de_ctx,
"alert tcp any any -> any any (content:\"onetwothreefour\"; sid:2;)");
1086 de_ctx,
"alert tcp any any -> any any (content:\"uniquepattern\"; sid:3;)");
1089 "alert tcp any any -> any any (content:\"onetwothreefour\"; fast_pattern:3,5; sid:4;)");
1092 de_ctx,
"alert tcp any any -> any any (content:\"twoth\"; sid:5;)");
1095 "alert tcp any any -> any any (content:\"onetwothreefour\"; fast_pattern:0,15; "
1129 static int DetectFastPatternPrefilter(
void)
1133 const char *
string =
"alert tcp any any -> any any "
1134 "(content:\"one\"; prefilter; sid:1;)";
1147 static void DetectFastPatternRegisterTests(
void)
1149 UtRegisterTest(
"DetectFastPatternTest01", DetectFastPatternTest01);
1150 UtRegisterTest(
"DetectFastPatternTest14", DetectFastPatternTest14);
1157 UtRegisterTest(
"DetectFastPatternTest671", DetectFastPatternTest671);
1159 UtRegisterTest(
"DetectFastPatternPrefilter", DetectFastPatternPrefilter);