50 "^\\s*(track|count|seconds)\\s+(by_src|by_dst|by_flow|\\d+)\\s*,\\s*(track|count|seconds)\\s+" \
52 "by_dst|by_flow|\\d+)\\s*,\\s*(track|count|seconds)\\s+(by_src|by_dst|by_flow|\\d+)\\s*$"
56 static int DetectDetectionFilterMatch(
60 static void DetectDetectionFilterRegisterTests(
void);
71 "alert on every match after a threshold has been reached";
85 static int DetectDetectionFilterMatch(
106 const char *str_ptr = NULL;
107 char *args[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
108 char *copy_str = NULL, *df_opt = NULL;
109 int seconds_found = 0, count_found = 0, track_found = 0;
110 int seconds_pos = 0, count_pos = 0;
113 char *saveptr = NULL;
114 pcre2_match_data *match = NULL;
121 for (pos = 0, df_opt = strtok_r(copy_str,
",", &saveptr);
122 pos < strlen(copy_str) && df_opt != NULL;
123 pos++, df_opt = strtok_r(NULL,
",", &saveptr)) {
124 if (strstr(df_opt,
"count"))
126 if (strstr(df_opt,
"second"))
128 if (strstr(df_opt,
"track"))
134 if (count_found != 1 || seconds_found != 1 || track_found != 1)
139 SCLogError(
"pcre_exec parse error, ret %" PRId32
", string %s", ret, rawstr);
149 for (i = 0; i < (ret - 1); i++) {
150 res = pcre2_substring_get_bynumber(match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
152 SCLogError(
"pcre2_substring_get_bynumber failed");
156 args[i] = (
char *)str_ptr;
158 if (strncasecmp(args[i],
"by_dst", strlen(
"by_dst")) == 0)
160 if (strncasecmp(args[i],
"by_src", strlen(
"by_src")) == 0)
162 if (strncasecmp(args[i],
"by_flow", strlen(
"by_flow")) == 0)
164 if (strncasecmp(args[i],
"count", strlen(
"count")) == 0)
166 if (strncasecmp(args[i],
"seconds", strlen(
"seconds")) == 0)
170 if (args[count_pos] == NULL || args[seconds_pos] == NULL) {
187 for (i = 0; i < 6; i++) {
189 pcre2_substring_free((PCRE2_UCHAR *)args[i]);
192 pcre2_match_data_free(match);
196 for (i = 0; i < 6; i++) {
198 pcre2_substring_free((PCRE2_UCHAR *)args[i]);
203 pcre2_match_data_free(match);
229 SCLogError(
"\"detection_filter\" and \"threshold\" are not allowed in the same rule");
235 SCLogError(
"At most one \"detection_filter\" is allowed per rule");
239 df = DetectDetectionFilterParse(rawstr);
286 static int DetectDetectionFilterTestParse01(
void)
293 DetectDetectionFilterFree(NULL, df);
302 static int DetectDetectionFilterTestParse02(
void)
315 static int DetectDetectionFilterTestParse03(
void)
317 DetectThresholdData *df = DetectDetectionFilterParse(
"track by_dst, seconds 60, count 10");
322 DetectDetectionFilterFree(NULL, df);
332 static int DetectDetectionFilterTestParse04(
void)
335 DetectDetectionFilterParse(
"count 10, track by_dst, seconds 60, count 10");
346 static int DetectDetectionFilterTestParse05(
void)
348 DetectThresholdData *df = DetectDetectionFilterParse(
"count 10, track by_dst, seconds 60");
353 DetectDetectionFilterFree(NULL, df);
362 static int DetectDetectionFilterTestParse06(
void)
376 static int DetectDetectionFilterTestSig1(
void)
383 memset(&th_v, 0,
sizeof(th_v));
393 "alert tcp any any -> any 80 (msg:\"detection_filter Test\"; detection_filter: "
394 "track by_dst, count 4, seconds 60; sid:1;)");
433 static int DetectDetectionFilterTestSig2(
void)
440 memset(&th_v, 0,
sizeof(th_v));
451 "alert tcp any any -> any 80 (msg:\"detection_filter Test 2\"; "
452 "detection_filter: track by_dst, count 4, seconds 60; sid:10;)");
491 static int DetectDetectionFilterTestSig3(
void)
498 memset(&th_v, 0,
sizeof(th_v));
508 "drop tcp any any -> any 80 (msg:\"detection_filter Test 2\"; "
509 "detection_filter: track by_dst, count 2, seconds 60; sid:10;)");
564 static void DetectDetectionFilterRegisterTests(
void)
566 UtRegisterTest(
"DetectDetectionFilterTestParse01", DetectDetectionFilterTestParse01);
567 UtRegisterTest(
"DetectDetectionFilterTestParse02", DetectDetectionFilterTestParse02);
568 UtRegisterTest(
"DetectDetectionFilterTestParse03", DetectDetectionFilterTestParse03);
569 UtRegisterTest(
"DetectDetectionFilterTestParse04", DetectDetectionFilterTestParse04);
570 UtRegisterTest(
"DetectDetectionFilterTestParse05", DetectDetectionFilterTestParse05);
571 UtRegisterTest(
"DetectDetectionFilterTestParse06", DetectDetectionFilterTestParse06);
572 UtRegisterTest(
"DetectDetectionFilterTestSig1", DetectDetectionFilterTestSig1);
573 UtRegisterTest(
"DetectDetectionFilterTestSig2", DetectDetectionFilterTestSig2);
574 UtRegisterTest(
"DetectDetectionFilterTestSig3", DetectDetectionFilterTestSig3);