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);
93 static inline int FlowMatch(
const uint32_t pflags,
const uint8_t pflowflags,
const uint16_t dflags,
94 const uint16_t match_cnt)
120 return (match_cnt ==
cnt) ? 1 : 0;
154 SCLogDebug(
"returning %" PRId32
" fd->match_cnt %" PRId32
" fd->flags 0x%02X p->flowflags 0x%02X",
173 char *args[3] = {NULL,NULL,NULL};
176 char str1[16] =
"", str2[16] =
"", str3[16] =
"";
177 pcre2_match_data *match = NULL;
180 if (ret < 1 || ret > 4) {
181 SCLogError(
"parse error, ret %" PRId32
", string %s", ret, flowstr);
186 pcre2len =
sizeof(str1);
189 SCLogError(
"pcre2_substring_copy_bynumber failed");
192 args[0] = (
char *)str1;
195 pcre2len =
sizeof(str2);
196 res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)str2, &pcre2len);
198 SCLogError(
"pcre2_substring_copy_bynumber failed");
201 args[1] = (
char *)str2;
204 pcre2len =
sizeof(str3);
205 res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)str3, &pcre2len);
207 SCLogError(
"pcre2_substring_copy_bynumber failed");
210 args[2] = (
char *)str3;
220 for (
int i = 0; i < (ret - 1); i++) {
223 if (strcasecmp(args[i],
"established") == 0) {
225 SCLogError(
"DETECT_FLOW_FLAG_ESTABLISHED flag is already set");
228 SCLogError(
"cannot set DETECT_FLOW_FLAG_ESTABLISHED, "
229 "DETECT_FLOW_FLAG_NOT_ESTABLISHED already set");
232 SCLogError(
"DETECT_FLOW_FLAG_STATELESS already set");
237 }
else if (strcasecmp(args[i],
"not_established") == 0) {
239 SCLogError(
"DETECT_FLOW_FLAG_NOT_ESTABLISHED flag is already set");
242 SCLogError(
"cannot set DETECT_FLOW_FLAG_NOT_ESTABLISHED, "
243 "DETECT_FLOW_FLAG_ESTABLISHED already set");
248 }
else if (strcasecmp(args[i],
"stateless") == 0) {
250 SCLogError(
"DETECT_FLOW_FLAG_STATELESS flag is already set");
253 SCLogError(
"cannot set DETECT_FLOW_FLAG_STATELESS, "
254 "DETECT_FLOW_FLAG_ESTABLISHED already set");
259 }
else if (strcasecmp(args[i],
"to_client") == 0 || strcasecmp(args[i],
"from_server") == 0) {
261 SCLogError(
"cannot set DETECT_FLOW_FLAG_TOCLIENT flag is already set");
264 SCLogError(
"cannot set to_client, DETECT_FLOW_FLAG_TOSERVER already set");
269 }
else if (strcasecmp(args[i],
"to_server") == 0 || strcasecmp(args[i],
"from_client") == 0){
271 SCLogError(
"cannot set DETECT_FLOW_FLAG_TOSERVER flag is already set");
274 SCLogError(
"cannot set to_server, DETECT_FLOW_FLAG_TO_CLIENT flag already set");
279 }
else if (strcasecmp(args[i],
"no_frag") == 0) {
281 SCLogError(
"cannot set no_frag flag is already set");
284 SCLogError(
"cannot set no_frag flag, only_frag already set");
289 }
else if (strcasecmp(args[i],
"only_frag") == 0) {
291 SCLogError(
"cannot set only_frag flag is already set");
294 SCLogError(
"cannot set only_frag flag, no_frag already set");
302 }
else if (strcasecmp(args[i],
"only_stream") == 0) {
304 SCLogError(
"cannot set only_stream flag is already set");
308 "cannot set only_stream flag, DETECT_FLOW_FLAG_NOSTREAM already set");
312 }
else if (strcasecmp(args[i],
"no_stream") == 0) {
314 SCLogError(
"cannot set no_stream flag is already set");
318 "cannot set no_stream flag, DETECT_FLOW_FLAG_ONLYSTREAM already set");
323 SCLogError(
"invalid flow option \"%s\"", args[i]);
328 pcre2_match_data_free(match);
333 pcre2_match_data_free(match);
343 #define SIG_FLAG_BOTH (SIG_FLAG_TOSERVER|SIG_FLAG_TOCLIENT)
384 uint16_t parse_flags = 0;
388 SCLogError(
"A signature may have only one flow option.");
396 bool appendsm =
true;
401 "rule %u means to use both directions, cannot specify a flow direction", s->
id);
405 SCLogError(
"rule %u has flow to_server but a hook to_client", s->
id);
412 "rule %u means to use both directions, cannot specify a flow direction", s->
id);
416 SCLogError(
"rule %u has flow to_client but a hook to_server", s->
id);
478 if (!PrefilterPacketHeaderExtraMatch(
ctx, p))
483 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
506 PrefilterPacketFlowCompare, PrefilterPacketFlowMatch);
509 static bool PrefilterFlowIsPrefilterable(
const Signature *s)
511 return PrefilterIsPrefilterableById(s,
DETECT_FLOW);
521 static int DetectFlowTestParse01 (
void)
523 uint16_t parsed_flags = 0;
524 DetectFlowData *fd = DetectFlowParse(NULL,
"established", &parsed_flags);
534 static int DetectFlowTestParse02 (
void)
536 uint16_t parsed_flags = 0;
537 DetectFlowData *fd = DetectFlowParse(NULL,
"established", &parsed_flags);
548 static int DetectFlowTestParse03 (
void)
550 uint16_t parsed_flags = 0;
551 DetectFlowData *fd = DetectFlowParse(NULL,
"stateless", &parsed_flags);
561 static int DetectFlowTestParse04 (
void)
563 uint16_t parsed_flags = 0;
564 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client", &parsed_flags);
574 static int DetectFlowTestParse05 (
void)
576 uint16_t parsed_flags = 0;
577 DetectFlowData *fd = DetectFlowParse(NULL,
"to_server", &parsed_flags);
587 static int DetectFlowTestParse06 (
void)
589 uint16_t parsed_flags = 0;
590 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server", &parsed_flags);
600 static int DetectFlowTestParse07 (
void)
602 uint16_t parsed_flags = 0;
603 DetectFlowData *fd = DetectFlowParse(NULL,
"from_client", &parsed_flags);
613 static int DetectFlowTestParse08 (
void)
615 uint16_t parsed_flags = 0;
616 DetectFlowData *fd = DetectFlowParse(NULL,
"established,to_client", &parsed_flags);
626 static int DetectFlowTestParse09 (
void)
628 uint16_t parsed_flags = 0;
629 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,stateless", &parsed_flags);
641 static int DetectFlowTestParse10 (
void)
643 uint16_t parsed_flags = 0;
644 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,stateless", &parsed_flags);
656 static int DetectFlowTestParse11 (
void)
658 uint16_t parsed_flags = 0;
659 DetectFlowData *fd = DetectFlowParse(NULL,
" from_server , stateless ", &parsed_flags);
672 static int DetectFlowTestParseNocase01 (
void)
674 uint16_t parsed_flags = 0;
675 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED", &parsed_flags);
684 static int DetectFlowTestParseNocase02 (
void)
686 uint16_t parsed_flags = 0;
687 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED", &parsed_flags);
698 static int DetectFlowTestParseNocase03 (
void)
700 uint16_t parsed_flags = 0;
701 DetectFlowData *fd = DetectFlowParse(NULL,
"STATELESS", &parsed_flags);
711 static int DetectFlowTestParseNocase04 (
void)
713 uint16_t parsed_flags = 0;
714 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_CLIENT", &parsed_flags);
724 static int DetectFlowTestParseNocase05 (
void)
726 uint16_t parsed_flags = 0;
727 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_SERVER", &parsed_flags);
737 static int DetectFlowTestParseNocase06 (
void)
739 uint16_t parsed_flags = 0;
740 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER", &parsed_flags);
750 static int DetectFlowTestParseNocase07 (
void)
752 uint16_t parsed_flags = 0;
753 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_CLIENT", &parsed_flags);
763 static int DetectFlowTestParseNocase08 (
void)
765 uint16_t parsed_flags = 0;
766 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED,TO_CLIENT", &parsed_flags);
778 static int DetectFlowTestParseNocase09 (
void)
780 uint16_t parsed_flags = 0;
781 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_CLIENT,STATELESS", &parsed_flags);
793 static int DetectFlowTestParseNocase10 (
void)
795 uint16_t parsed_flags = 0;
796 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER,STATELESS", &parsed_flags);
808 static int DetectFlowTestParseNocase11 (
void)
810 uint16_t parsed_flags = 0;
811 DetectFlowData *fd = DetectFlowParse(NULL,
" FROM_SERVER , STATELESS ", &parsed_flags);
823 static int DetectFlowTestParse12 (
void)
825 uint16_t parsed_flags = 0;
826 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server:stateless", &parsed_flags);
834 static int DetectFlowTestParse13 (
void)
836 uint16_t parsed_flags = 0;
837 DetectFlowData *fd = DetectFlowParse(NULL,
"invalidoptiontest", &parsed_flags);
845 static int DetectFlowTestParse14 (
void)
847 uint16_t parsed_flags = 0;
856 static int DetectFlowTestParse15 (
void)
858 uint16_t parsed_flags = 0;
859 DetectFlowData *fd = DetectFlowParse(NULL,
"established,stateless", &parsed_flags);
867 static int DetectFlowTestParse16 (
void)
869 uint16_t parsed_flags = 0;
870 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,to_server", &parsed_flags);
879 static int DetectFlowTestParse17 (
void)
881 uint16_t parsed_flags = 0;
882 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,from_server", &parsed_flags);
890 static int DetectFlowTestParse18 (
void)
892 uint16_t parsed_flags = 0;
894 DetectFlowParse(NULL,
"from_server,established,only_stream", &parsed_flags);
906 static int DetectFlowTestParseNocase18 (
void)
908 uint16_t parsed_flags = 0;
910 DetectFlowParse(NULL,
"FROM_SERVER,ESTABLISHED,ONLY_STREAM", &parsed_flags);
923 static int DetectFlowTestParse19 (
void)
925 uint16_t parsed_flags = 0;
927 DetectFlowParse(NULL,
"from_server,established,only_stream,a", &parsed_flags);
935 static int DetectFlowTestParse20 (
void)
937 uint16_t parsed_flags = 0;
938 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,established,no_stream", &parsed_flags);
950 static int DetectFlowTestParseNocase20 (
void)
952 uint16_t parsed_flags = 0;
953 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER,ESTABLISHED,NO_STREAM", &parsed_flags);
965 static int DetectFlowTestParse21 (
void)
967 uint16_t parsed_flags = 0;
968 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,a,no_stream", &parsed_flags);
976 static int DetectFlowTestParse22(
void)
978 uint16_t parsed_flags = 0;
979 DetectFlowData *fd = DetectFlowParse(NULL,
"established,not_established", &parsed_flags);
981 fd = DetectFlowParse(NULL,
"not_established,established", &parsed_flags);
986 static int DetectFlowSigTest01(
void)
988 uint8_t *buf = (uint8_t *)
"supernovaduper";
989 uint16_t buflen = strlen((
char *)buf);
993 memset(&th_v, 0,
sizeof(th_v));
999 const char *sig1 =
"alert tcp any any -> any any (msg:\"dummy\"; "
1000 "content:\"nova\"; flow:no_stream; sid:1;)";
1028 static int DetectFlowTestParseNotEstablished(
void)
1030 uint16_t parsed_flags = 0;
1031 DetectFlowData *fd = DetectFlowParse(NULL,
"not_established", &parsed_flags);
1041 static int DetectFlowTestParseNoFrag(
void)
1043 uint16_t parsed_flags = 0;
1044 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag", &parsed_flags);
1054 static int DetectFlowTestParseOnlyFrag(
void)
1056 uint16_t parsed_flags = 0;
1057 DetectFlowData *fd = DetectFlowParse(NULL,
"only_frag", &parsed_flags);
1067 static int DetectFlowTestParseNoFragOnlyFrag(
void)
1069 uint16_t parsed_flags = 0;
1070 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag,only_frag", &parsed_flags);
1078 static int DetectFlowTestNoFragMatch(
void)
1080 uint16_t parsed_flags = 0;
1081 uint32_t pflags = 0;
1082 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag", &parsed_flags);
1096 static int DetectFlowTestOnlyFragMatch(
void)
1098 uint16_t parsed_flags = 0;
1099 uint32_t pflags = 0;
1100 DetectFlowData *fd = DetectFlowParse(NULL,
"only_frag", &parsed_flags);
1114 static void DetectFlowRegisterTests(
void)
1127 UtRegisterTest(
"DetectFlowTestParseNocase01", DetectFlowTestParseNocase01);
1128 UtRegisterTest(
"DetectFlowTestParseNocase02", DetectFlowTestParseNocase02);
1129 UtRegisterTest(
"DetectFlowTestParseNocase03", DetectFlowTestParseNocase03);
1130 UtRegisterTest(
"DetectFlowTestParseNocase04", DetectFlowTestParseNocase04);
1131 UtRegisterTest(
"DetectFlowTestParseNocase05", DetectFlowTestParseNocase05);
1132 UtRegisterTest(
"DetectFlowTestParseNocase06", DetectFlowTestParseNocase06);
1133 UtRegisterTest(
"DetectFlowTestParseNocase07", DetectFlowTestParseNocase07);
1134 UtRegisterTest(
"DetectFlowTestParseNocase08", DetectFlowTestParseNocase08);
1135 UtRegisterTest(
"DetectFlowTestParseNocase09", DetectFlowTestParseNocase09);
1136 UtRegisterTest(
"DetectFlowTestParseNocase10", DetectFlowTestParseNocase10);
1137 UtRegisterTest(
"DetectFlowTestParseNocase11", DetectFlowTestParseNocase11);
1145 UtRegisterTest(
"DetectFlowTestParseNocase18", DetectFlowTestParseNocase18);
1148 UtRegisterTest(
"DetectFlowTestParseNocase20", DetectFlowTestParseNocase20);
1152 DetectFlowTestParseNotEstablished);
1153 UtRegisterTest(
"DetectFlowTestParseNoFrag", DetectFlowTestParseNoFrag);
1155 DetectFlowTestParseOnlyFrag);
1157 DetectFlowTestParseNoFragOnlyFrag);
1158 UtRegisterTest(
"DetectFlowTestNoFragMatch", DetectFlowTestNoFragMatch);
1159 UtRegisterTest(
"DetectFlowTestOnlyFragMatch", DetectFlowTestOnlyFragMatch);