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) {
452 if (!PrefilterPacketHeaderExtraMatch(
ctx, p))
457 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
483 PrefilterPacketFlowCompare, PrefilterPacketFlowMatch);
486 static bool PrefilterFlowIsPrefilterable(
const Signature *s)
505 static int DetectFlowTestParse01 (
void)
507 uint16_t parsed_flags = 0;
508 DetectFlowData *fd = DetectFlowParse(NULL,
"established", &parsed_flags);
518 static int DetectFlowTestParse02 (
void)
520 uint16_t parsed_flags = 0;
521 DetectFlowData *fd = DetectFlowParse(NULL,
"established", &parsed_flags);
531 static int DetectFlowTestParse03 (
void)
533 uint16_t parsed_flags = 0;
534 DetectFlowData *fd = DetectFlowParse(NULL,
"stateless", &parsed_flags);
544 static int DetectFlowTestParse04 (
void)
546 uint16_t parsed_flags = 0;
547 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client", &parsed_flags);
557 static int DetectFlowTestParse05 (
void)
559 uint16_t parsed_flags = 0;
560 DetectFlowData *fd = DetectFlowParse(NULL,
"to_server", &parsed_flags);
570 static int DetectFlowTestParse06 (
void)
572 uint16_t parsed_flags = 0;
573 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server", &parsed_flags);
583 static int DetectFlowTestParse07 (
void)
585 uint16_t parsed_flags = 0;
586 DetectFlowData *fd = DetectFlowParse(NULL,
"from_client", &parsed_flags);
596 static int DetectFlowTestParse08 (
void)
598 uint16_t parsed_flags = 0;
599 DetectFlowData *fd = DetectFlowParse(NULL,
"established,to_client", &parsed_flags);
609 static int DetectFlowTestParse09 (
void)
611 uint16_t parsed_flags = 0;
612 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,stateless", &parsed_flags);
624 static int DetectFlowTestParse10 (
void)
626 uint16_t parsed_flags = 0;
627 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,stateless", &parsed_flags);
639 static int DetectFlowTestParse11 (
void)
641 uint16_t parsed_flags = 0;
642 DetectFlowData *fd = DetectFlowParse(NULL,
" from_server , stateless ", &parsed_flags);
655 static int DetectFlowTestParseNocase01 (
void)
657 uint16_t parsed_flags = 0;
658 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED", &parsed_flags);
667 static int DetectFlowTestParseNocase02 (
void)
669 uint16_t parsed_flags = 0;
670 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED", &parsed_flags);
681 static int DetectFlowTestParseNocase03 (
void)
683 uint16_t parsed_flags = 0;
684 DetectFlowData *fd = DetectFlowParse(NULL,
"STATELESS", &parsed_flags);
694 static int DetectFlowTestParseNocase04 (
void)
696 uint16_t parsed_flags = 0;
697 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_CLIENT", &parsed_flags);
707 static int DetectFlowTestParseNocase05 (
void)
709 uint16_t parsed_flags = 0;
710 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_SERVER", &parsed_flags);
720 static int DetectFlowTestParseNocase06 (
void)
722 uint16_t parsed_flags = 0;
723 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER", &parsed_flags);
733 static int DetectFlowTestParseNocase07 (
void)
735 uint16_t parsed_flags = 0;
736 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_CLIENT", &parsed_flags);
746 static int DetectFlowTestParseNocase08 (
void)
748 uint16_t parsed_flags = 0;
749 DetectFlowData *fd = DetectFlowParse(NULL,
"ESTABLISHED,TO_CLIENT", &parsed_flags);
761 static int DetectFlowTestParseNocase09 (
void)
763 uint16_t parsed_flags = 0;
764 DetectFlowData *fd = DetectFlowParse(NULL,
"TO_CLIENT,STATELESS", &parsed_flags);
776 static int DetectFlowTestParseNocase10 (
void)
778 uint16_t parsed_flags = 0;
779 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER,STATELESS", &parsed_flags);
791 static int DetectFlowTestParseNocase11 (
void)
793 uint16_t parsed_flags = 0;
794 DetectFlowData *fd = DetectFlowParse(NULL,
" FROM_SERVER , STATELESS ", &parsed_flags);
806 static int DetectFlowTestParse12 (
void)
808 uint16_t parsed_flags = 0;
809 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server:stateless", &parsed_flags);
817 static int DetectFlowTestParse13 (
void)
819 uint16_t parsed_flags = 0;
820 DetectFlowData *fd = DetectFlowParse(NULL,
"invalidoptiontest", &parsed_flags);
828 static int DetectFlowTestParse14 (
void)
830 uint16_t parsed_flags = 0;
839 static int DetectFlowTestParse15 (
void)
841 uint16_t parsed_flags = 0;
842 DetectFlowData *fd = DetectFlowParse(NULL,
"established,stateless", &parsed_flags);
850 static int DetectFlowTestParse16 (
void)
852 uint16_t parsed_flags = 0;
853 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,to_server", &parsed_flags);
862 static int DetectFlowTestParse17 (
void)
864 uint16_t parsed_flags = 0;
865 DetectFlowData *fd = DetectFlowParse(NULL,
"to_client,from_server", &parsed_flags);
873 static int DetectFlowTestParse18 (
void)
875 uint16_t parsed_flags = 0;
877 DetectFlowParse(NULL,
"from_server,established,only_stream", &parsed_flags);
889 static int DetectFlowTestParseNocase18 (
void)
891 uint16_t parsed_flags = 0;
893 DetectFlowParse(NULL,
"FROM_SERVER,ESTABLISHED,ONLY_STREAM", &parsed_flags);
906 static int DetectFlowTestParse19 (
void)
908 uint16_t parsed_flags = 0;
910 DetectFlowParse(NULL,
"from_server,established,only_stream,a", &parsed_flags);
918 static int DetectFlowTestParse20 (
void)
920 uint16_t parsed_flags = 0;
921 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,established,no_stream", &parsed_flags);
933 static int DetectFlowTestParseNocase20 (
void)
935 uint16_t parsed_flags = 0;
936 DetectFlowData *fd = DetectFlowParse(NULL,
"FROM_SERVER,ESTABLISHED,NO_STREAM", &parsed_flags);
948 static int DetectFlowTestParse21 (
void)
950 uint16_t parsed_flags = 0;
951 DetectFlowData *fd = DetectFlowParse(NULL,
"from_server,a,no_stream", &parsed_flags);
959 static int DetectFlowTestParse22(
void)
961 uint16_t parsed_flags = 0;
962 DetectFlowData *fd = DetectFlowParse(NULL,
"established,not_established", &parsed_flags);
964 fd = DetectFlowParse(NULL,
"not_established,established", &parsed_flags);
969 static int DetectFlowSigTest01(
void)
971 uint8_t *buf = (uint8_t *)
"supernovaduper";
972 uint16_t buflen = strlen((
char *)buf);
976 memset(&th_v, 0,
sizeof(th_v));
981 const char *sig1 =
"alert tcp any any -> any any (msg:\"dummy\"; "
982 "content:\"nova\"; flow:no_stream; sid:1;)";
1009 static int DetectFlowTestParseNotEstablished(
void)
1011 uint16_t parsed_flags = 0;
1012 DetectFlowData *fd = DetectFlowParse(NULL,
"not_established", &parsed_flags);
1022 static int DetectFlowTestParseNoFrag(
void)
1024 uint16_t parsed_flags = 0;
1025 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag", &parsed_flags);
1035 static int DetectFlowTestParseOnlyFrag(
void)
1037 uint16_t parsed_flags = 0;
1038 DetectFlowData *fd = DetectFlowParse(NULL,
"only_frag", &parsed_flags);
1048 static int DetectFlowTestParseNoFragOnlyFrag(
void)
1050 uint16_t parsed_flags = 0;
1051 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag,only_frag", &parsed_flags);
1059 static int DetectFlowTestNoFragMatch(
void)
1061 uint16_t parsed_flags = 0;
1062 uint32_t pflags = 0;
1063 DetectFlowData *fd = DetectFlowParse(NULL,
"no_frag", &parsed_flags);
1076 static int DetectFlowTestOnlyFragMatch(
void)
1078 uint16_t parsed_flags = 0;
1079 uint32_t pflags = 0;
1080 DetectFlowData *fd = DetectFlowParse(NULL,
"only_frag", &parsed_flags);
1093 static void DetectFlowRegisterTests(
void)
1106 UtRegisterTest(
"DetectFlowTestParseNocase01", DetectFlowTestParseNocase01);
1107 UtRegisterTest(
"DetectFlowTestParseNocase02", DetectFlowTestParseNocase02);
1108 UtRegisterTest(
"DetectFlowTestParseNocase03", DetectFlowTestParseNocase03);
1109 UtRegisterTest(
"DetectFlowTestParseNocase04", DetectFlowTestParseNocase04);
1110 UtRegisterTest(
"DetectFlowTestParseNocase05", DetectFlowTestParseNocase05);
1111 UtRegisterTest(
"DetectFlowTestParseNocase06", DetectFlowTestParseNocase06);
1112 UtRegisterTest(
"DetectFlowTestParseNocase07", DetectFlowTestParseNocase07);
1113 UtRegisterTest(
"DetectFlowTestParseNocase08", DetectFlowTestParseNocase08);
1114 UtRegisterTest(
"DetectFlowTestParseNocase09", DetectFlowTestParseNocase09);
1115 UtRegisterTest(
"DetectFlowTestParseNocase10", DetectFlowTestParseNocase10);
1116 UtRegisterTest(
"DetectFlowTestParseNocase11", DetectFlowTestParseNocase11);
1124 UtRegisterTest(
"DetectFlowTestParseNocase18", DetectFlowTestParseNocase18);
1127 UtRegisterTest(
"DetectFlowTestParseNocase20", DetectFlowTestParseNocase20);
1131 DetectFlowTestParseNotEstablished);
1132 UtRegisterTest(
"DetectFlowTestParseNoFrag", DetectFlowTestParseNoFrag);
1134 DetectFlowTestParseOnlyFrag);
1136 DetectFlowTestParseNoFragOnlyFrag);
1137 UtRegisterTest(
"DetectFlowTestNoFragMatch", DetectFlowTestNoFragMatch);
1138 UtRegisterTest(
"DetectFlowTestOnlyFragMatch", DetectFlowTestOnlyFragMatch);