47 #define PARSE_REGEX "^\\s*([A-z_]+)\\s*(?:,\\s*([A-z_]+))?\\s*(?:,\\s*([A-z_]+))?\\s*$"
55 static void DetectFlowRegisterTests(
void);
60 static bool PrefilterFlowIsPrefilterable(
const Signature *s);
88 static inline int FlowMatch(
const uint32_t pflags,
const uint8_t pflowflags,
const uint16_t dflags,
89 const uint16_t match_cnt)
115 return (match_cnt ==
cnt) ? 1 : 0;
149 SCLogDebug(
"returning %" PRId32
" fd->match_cnt %" PRId32
" fd->flags 0x%02X p->flowflags 0x%02X",
168 char *args[3] = {NULL,NULL,NULL};
171 char str1[16] =
"", str2[16] =
"", str3[16] =
"";
172 pcre2_match_data *match = NULL;
175 if (ret < 1 || ret > 4) {
176 SCLogError(
"parse error, ret %" PRId32
", string %s", ret, flowstr);
181 pcre2len =
sizeof(str1);
184 SCLogError(
"pcre2_substring_copy_bynumber failed");
187 args[0] = (
char *)str1;
190 pcre2len =
sizeof(str2);
191 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)str2, &pcre2len);
193 SCLogError(
"pcre2_substring_copy_bynumber failed");
196 args[1] = (
char *)str2;
199 pcre2len =
sizeof(str3);
200 res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)str3, &pcre2len);
202 SCLogError(
"pcre2_substring_copy_bynumber failed");
205 args[2] = (
char *)str3;
215 for (
int i = 0; i < (ret - 1); i++) {
218 if (strcasecmp(args[i],
"established") == 0) {
220 SCLogError(
"DETECT_FLOW_FLAG_ESTABLISHED flag is already set");
223 SCLogError(
"cannot set DETECT_FLOW_FLAG_ESTABLISHED, "
224 "DETECT_FLOW_FLAG_NOT_ESTABLISHED already set");
227 SCLogError(
"DETECT_FLOW_FLAG_STATELESS already set");
232 }
else if (strcasecmp(args[i],
"not_established") == 0) {
234 SCLogError(
"DETECT_FLOW_FLAG_NOT_ESTABLISHED flag is already set");
237 SCLogError(
"cannot set DETECT_FLOW_FLAG_NOT_ESTABLISHED, "
238 "DETECT_FLOW_FLAG_ESTABLISHED already set");
243 }
else if (strcasecmp(args[i],
"stateless") == 0) {
245 SCLogError(
"DETECT_FLOW_FLAG_STATELESS flag is already set");
248 SCLogError(
"cannot set DETECT_FLOW_FLAG_STATELESS, "
249 "DETECT_FLOW_FLAG_ESTABLISHED already set");
254 }
else if (strcasecmp(args[i],
"to_client") == 0 || strcasecmp(args[i],
"from_server") == 0) {
256 SCLogError(
"cannot set DETECT_FLOW_FLAG_TOCLIENT flag is already set");
259 SCLogError(
"cannot set to_client, DETECT_FLOW_FLAG_TOSERVER already set");
264 }
else if (strcasecmp(args[i],
"to_server") == 0 || strcasecmp(args[i],
"from_client") == 0){
266 SCLogError(
"cannot set DETECT_FLOW_FLAG_TOSERVER flag is already set");
269 SCLogError(
"cannot set to_server, DETECT_FLOW_FLAG_TO_CLIENT flag already set");
274 }
else if (strcasecmp(args[i],
"no_frag") == 0) {
276 SCLogError(
"cannot set no_frag flag is already set");
279 SCLogError(
"cannot set no_frag flag, only_frag already set");
284 }
else if (strcasecmp(args[i],
"only_frag") == 0) {
286 SCLogError(
"cannot set only_frag flag is already set");
289 SCLogError(
"cannot set only_frag flag, no_frag already set");
297 }
else if (strcasecmp(args[i],
"only_stream") == 0) {
299 SCLogError(
"cannot set only_stream flag is already set");
303 "cannot set only_stream flag, DETECT_FLOW_FLAG_NOSTREAM already set");
307 }
else if (strcasecmp(args[i],
"no_stream") == 0) {
309 SCLogError(
"cannot set no_stream flag is already set");
313 "cannot set no_stream flag, DETECT_FLOW_FLAG_ONLYSTREAM already set");
318 SCLogError(
"invalid flow option \"%s\"", args[i]);
323 pcre2_match_data_free(match);
328 pcre2_match_data_free(match);
338 #define SIG_FLAG_BOTH (SIG_FLAG_TOSERVER|SIG_FLAG_TOCLIENT)
379 uint16_t parse_flags = 0;
383 SCLogError(
"A signature may have only one flow option.");
391 bool appendsm =
true;
415 }
else if (fd != NULL) {
450 if (!PrefilterPacketHeaderExtraMatch(
ctx, p))
454 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
479 PrefilterPacketFlowCompare, PrefilterPacketFlowMatch);
482 static bool PrefilterFlowIsPrefilterable(
const Signature *s)
501 static int DetectFlowTestParse01 (
void)
503 uint16_t parsed_flags = 0;
504 DetectFlowData *fd = DetectFlowParse(NULL,
"established", &parsed_flags);
514 static int DetectFlowTestParse02 (
void)
516 uint16_t parsed_flags = 0;
517 DetectFlowData *fd = DetectFlowParse(NULL,
"established", &parsed_flags);
527 static int DetectFlowTestParse03 (
void)
529 uint16_t parsed_flags = 0;
530 DetectFlowData *fd = DetectFlowParse(NULL,
"stateless", &parsed_flags);
540 static int DetectFlowTestParse04 (
void)
542 uint16_t parsed_flags = 0;
543 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client", &parsed_flags);
553 static int DetectFlowTestParse05 (
void)
555 uint16_t parsed_flags = 0;
556 DetectFlowData *fd = DetectFlowParse(NULL,
"to_server", &parsed_flags);
566 static int DetectFlowTestParse06 (
void)
568 uint16_t parsed_flags = 0;
569 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server", &parsed_flags);
579 static int DetectFlowTestParse07 (
void)
581 uint16_t parsed_flags = 0;
582 DetectFlowData *fd = DetectFlowParse(NULL,
"from_client", &parsed_flags);
592 static int DetectFlowTestParse08 (
void)
594 uint16_t parsed_flags = 0;
595 DetectFlowData *fd = DetectFlowParse(NULL,
"established,to_client", &parsed_flags);
605 static int DetectFlowTestParse09 (
void)
607 uint16_t parsed_flags = 0;
608 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,stateless", &parsed_flags);
620 static int DetectFlowTestParse10 (
void)
622 uint16_t parsed_flags = 0;
623 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,stateless", &parsed_flags);
635 static int DetectFlowTestParse11 (
void)
637 uint16_t parsed_flags = 0;
638 DetectFlowData *fd = DetectFlowParse(NULL,
" from_server , stateless ", &parsed_flags);
651 static int DetectFlowTestParseNocase01 (
void)
653 uint16_t parsed_flags = 0;
654 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED", &parsed_flags);
663 static int DetectFlowTestParseNocase02 (
void)
665 uint16_t parsed_flags = 0;
666 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED", &parsed_flags);
677 static int DetectFlowTestParseNocase03 (
void)
679 uint16_t parsed_flags = 0;
680 DetectFlowData *fd = DetectFlowParse(NULL,
"STATELESS", &parsed_flags);
690 static int DetectFlowTestParseNocase04 (
void)
692 uint16_t parsed_flags = 0;
693 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_CLIENT", &parsed_flags);
703 static int DetectFlowTestParseNocase05 (
void)
705 uint16_t parsed_flags = 0;
706 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_SERVER", &parsed_flags);
716 static int DetectFlowTestParseNocase06 (
void)
718 uint16_t parsed_flags = 0;
719 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER", &parsed_flags);
729 static int DetectFlowTestParseNocase07 (
void)
731 uint16_t parsed_flags = 0;
732 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_CLIENT", &parsed_flags);
742 static int DetectFlowTestParseNocase08 (
void)
744 uint16_t parsed_flags = 0;
745 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED,TO_CLIENT", &parsed_flags);
757 static int DetectFlowTestParseNocase09 (
void)
759 uint16_t parsed_flags = 0;
760 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_CLIENT,STATELESS", &parsed_flags);
772 static int DetectFlowTestParseNocase10 (
void)
774 uint16_t parsed_flags = 0;
775 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER,STATELESS", &parsed_flags);
787 static int DetectFlowTestParseNocase11 (
void)
789 uint16_t parsed_flags = 0;
790 DetectFlowData *fd = DetectFlowParse(NULL,
" FROM_SERVER , STATELESS ", &parsed_flags);
802 static int DetectFlowTestParse12 (
void)
804 uint16_t parsed_flags = 0;
805 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server:stateless", &parsed_flags);
813 static int DetectFlowTestParse13 (
void)
815 uint16_t parsed_flags = 0;
816 DetectFlowData *fd = DetectFlowParse(NULL,
"invalidoptiontest", &parsed_flags);
824 static int DetectFlowTestParse14 (
void)
826 uint16_t parsed_flags = 0;
835 static int DetectFlowTestParse15 (
void)
837 uint16_t parsed_flags = 0;
838 DetectFlowData *fd = DetectFlowParse(NULL,
"established,stateless", &parsed_flags);
846 static int DetectFlowTestParse16 (
void)
848 uint16_t parsed_flags = 0;
849 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,to_server", &parsed_flags);
858 static int DetectFlowTestParse17 (
void)
860 uint16_t parsed_flags = 0;
861 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,from_server", &parsed_flags);
869 static int DetectFlowTestParse18 (
void)
871 uint16_t parsed_flags = 0;
873 DetectFlowParse(NULL,
"from_server,established,only_stream", &parsed_flags);
885 static int DetectFlowTestParseNocase18 (
void)
887 uint16_t parsed_flags = 0;
889 DetectFlowParse(NULL,
"FROM_SERVER,ESTABLISHED,ONLY_STREAM", &parsed_flags);
902 static int DetectFlowTestParse19 (
void)
904 uint16_t parsed_flags = 0;
906 DetectFlowParse(NULL,
"from_server,established,only_stream,a", &parsed_flags);
914 static int DetectFlowTestParse20 (
void)
916 uint16_t parsed_flags = 0;
917 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,established,no_stream", &parsed_flags);
929 static int DetectFlowTestParseNocase20 (
void)
931 uint16_t parsed_flags = 0;
932 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER,ESTABLISHED,NO_STREAM", &parsed_flags);
944 static int DetectFlowTestParse21 (
void)
946 uint16_t parsed_flags = 0;
947 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,a,no_stream", &parsed_flags);
955 static int DetectFlowTestParse22(
void)
957 uint16_t parsed_flags = 0;
958 DetectFlowData *fd = DetectFlowParse(NULL,
"established,not_established", &parsed_flags);
960 fd = DetectFlowParse(NULL,
"not_established,established", &parsed_flags);
965 static int DetectFlowSigTest01(
void)
967 uint8_t *buf = (uint8_t *)
"supernovaduper";
968 uint16_t buflen = strlen((
char *)buf);
972 memset(&th_v, 0,
sizeof(th_v));
977 const char *sig1 =
"alert tcp any any -> any any (msg:\"dummy\"; "
978 "content:\"nova\"; flow:no_stream; sid:1;)";
1005 static int DetectFlowTestParseNotEstablished(
void)
1007 uint16_t parsed_flags = 0;
1008 DetectFlowData *fd = DetectFlowParse(NULL,
"not_established", &parsed_flags);
1018 static int DetectFlowTestParseNoFrag(
void)
1020 uint16_t parsed_flags = 0;
1021 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag", &parsed_flags);
1031 static int DetectFlowTestParseOnlyFrag(
void)
1033 uint16_t parsed_flags = 0;
1034 DetectFlowData *fd = DetectFlowParse(NULL,
"only_frag", &parsed_flags);
1044 static int DetectFlowTestParseNoFragOnlyFrag(
void)
1046 uint16_t parsed_flags = 0;
1047 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag,only_frag", &parsed_flags);
1055 static int DetectFlowTestNoFragMatch(
void)
1057 uint16_t parsed_flags = 0;
1058 uint32_t pflags = 0;
1059 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag", &parsed_flags);
1072 static int DetectFlowTestOnlyFragMatch(
void)
1074 uint16_t parsed_flags = 0;
1075 uint32_t pflags = 0;
1076 DetectFlowData *fd = DetectFlowParse(NULL,
"only_frag", &parsed_flags);
1089 static void DetectFlowRegisterTests(
void)
1102 UtRegisterTest(
"DetectFlowTestParseNocase01", DetectFlowTestParseNocase01);
1103 UtRegisterTest(
"DetectFlowTestParseNocase02", DetectFlowTestParseNocase02);
1104 UtRegisterTest(
"DetectFlowTestParseNocase03", DetectFlowTestParseNocase03);
1105 UtRegisterTest(
"DetectFlowTestParseNocase04", DetectFlowTestParseNocase04);
1106 UtRegisterTest(
"DetectFlowTestParseNocase05", DetectFlowTestParseNocase05);
1107 UtRegisterTest(
"DetectFlowTestParseNocase06", DetectFlowTestParseNocase06);
1108 UtRegisterTest(
"DetectFlowTestParseNocase07", DetectFlowTestParseNocase07);
1109 UtRegisterTest(
"DetectFlowTestParseNocase08", DetectFlowTestParseNocase08);
1110 UtRegisterTest(
"DetectFlowTestParseNocase09", DetectFlowTestParseNocase09);
1111 UtRegisterTest(
"DetectFlowTestParseNocase10", DetectFlowTestParseNocase10);
1112 UtRegisterTest(
"DetectFlowTestParseNocase11", DetectFlowTestParseNocase11);
1120 UtRegisterTest(
"DetectFlowTestParseNocase18", DetectFlowTestParseNocase18);
1123 UtRegisterTest(
"DetectFlowTestParseNocase20", DetectFlowTestParseNocase20);
1127 DetectFlowTestParseNotEstablished);
1128 UtRegisterTest(
"DetectFlowTestParseNoFrag", DetectFlowTestParseNoFrag);
1130 DetectFlowTestParseOnlyFrag);
1132 DetectFlowTestParseNoFragOnlyFrag);
1133 UtRegisterTest(
"DetectFlowTestNoFragMatch", DetectFlowTestNoFragMatch);
1134 UtRegisterTest(
"DetectFlowTestOnlyFragMatch", DetectFlowTestOnlyFragMatch);