51 "^\\s*([a-zA-Z][\\w\\d_./" \
52 "]+)\\s*,\\s*([+=-]{1}|==|!=|<|<=|>|>=|isset|notset|isnotset)\\s*,?\\s*([a-zA-Z][\\w\\d]+|[" \
63 static void DetectFlowintRegisterTests(
void);
232 char *varname = NULL;
238 unsigned long long value_long = 0;
240 pcre2_match_data *match = NULL;
243 if (ret < 3 || ret > 4) {
244 SCLogError(
"\"%s\" is not a valid setting for flowint(ret = %d).", rawstr, ret);
249 res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
250 if (res < 0 || str_ptr == NULL) {
251 SCLogError(
"pcre2_substring_get_bynumber failed");
254 varname = (
char *)str_ptr;
256 res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
257 if (res < 0 || str_ptr == NULL) {
258 SCLogError(
"pcre2_substring_get_bynumber failed");
261 modstr = (
char *)str_ptr;
264 if (strcmp(
"=", modstr) == 0)
266 if (strcmp(
"+", modstr) == 0)
268 if (strcmp(
"-", modstr) == 0)
271 if (strcmp(
"<", modstr) == 0)
273 if (strcmp(
"<=", modstr) == 0)
275 if (strcmp(
"!=", modstr) == 0)
277 if (strcmp(
"==", modstr) == 0)
279 if (strcmp(
">=", modstr) == 0)
281 if (strcmp(
">", modstr) == 0)
283 if (strcmp(
"isset", modstr) == 0)
285 if (strcmp(
"notset", modstr) == 0 || strcmp(
"isnotset", modstr) == 0)
302 res = pcre2_substring_get_bynumber(match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
303 varval = (
char *)str_ptr;
304 if (res < 0 || varval == NULL || strcmp(varval,
"") == 0) {
305 SCLogError(
"pcre2_substring_get_bynumber failed");
309 if (varval[0] >=
'0' && varval[0] <=
'9') {
311 value_long = atoll(varval);
312 if (value_long > UINT32_MAX) {
313 SCLogDebug(
"DetectFlowintParse: Cannot load this value."
314 " Values should be between 0 and %"PRIu32, UINT32_MAX);
339 sfd->
idx = varname_id;
343 pcre2_substring_free((PCRE2_UCHAR *)varname);
344 pcre2_substring_free((PCRE2_UCHAR *)modstr);
346 pcre2_substring_free((PCRE2_UCHAR *)varval);
347 pcre2_match_data_free(match);
351 pcre2_match_data_free(match);
354 pcre2_substring_free((PCRE2_UCHAR *)varname);
356 pcre2_substring_free((PCRE2_UCHAR *)varval);
358 pcre2_substring_free((PCRE2_UCHAR *)modstr);
377 sfd = DetectFlowintParse(
de_ctx, rawstr);
428 if (sfd->
name != NULL)
446 SCLogDebug(
"DetectFlowintPrintData: Error, DetectFlowintData == NULL!");
450 SCLogDebug(
"Varname: %s, modifier: %"PRIu8
", idx: %"PRIu32
" Target: ",
461 SCLogDebug(
"DetectFlowintPrintData: Error, Targettype not known!");
469 static int DetectFlowintTestParseVal01(
void)
479 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,35");
480 DetectFlowintPrintData(sfd);
481 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
496 static int DetectFlowintTestParseVar01(
void)
506 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,targetvar");
507 DetectFlowintPrintData(sfd);
508 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
526 static int DetectFlowintTestParseVal02(
void)
536 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,35");
537 DetectFlowintPrintData(sfd);
538 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
553 static int DetectFlowintTestParseVar02(
void)
563 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,targetvar");
564 DetectFlowintPrintData(sfd);
565 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
583 static int DetectFlowintTestParseVal03(
void)
593 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,35");
594 DetectFlowintPrintData(sfd);
595 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
610 static int DetectFlowintTestParseVar03(
void)
620 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,targetvar");
621 DetectFlowintPrintData(sfd);
622 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
641 static int DetectFlowintTestParseVal04(
void)
651 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,35");
652 DetectFlowintPrintData(sfd);
653 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
668 static int DetectFlowintTestParseVar04(
void)
678 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,targetvar");
679 DetectFlowintPrintData(sfd);
680 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
698 static int DetectFlowintTestParseVal05(
void)
708 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,35");
709 DetectFlowintPrintData(sfd);
710 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
725 static int DetectFlowintTestParseVar05(
void)
735 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,targetvar");
736 DetectFlowintPrintData(sfd);
737 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
755 static int DetectFlowintTestParseVal06(
void)
765 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,35");
766 DetectFlowintPrintData(sfd);
767 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
782 static int DetectFlowintTestParseVar06(
void)
792 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,targetvar");
793 DetectFlowintPrintData(sfd);
794 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
812 static int DetectFlowintTestParseVal07(
void)
822 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,35");
823 DetectFlowintPrintData(sfd);
824 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
839 static int DetectFlowintTestParseVar07(
void)
849 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,targetvar");
850 DetectFlowintPrintData(sfd);
851 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
869 static int DetectFlowintTestParseVal08(
void)
879 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,35");
880 DetectFlowintPrintData(sfd);
881 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
896 static int DetectFlowintTestParseVar08(
void)
906 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,targetvar");
907 DetectFlowintPrintData(sfd);
908 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
926 static int DetectFlowintTestParseVal09(
void)
936 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,35");
937 DetectFlowintPrintData(sfd);
938 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
953 static int DetectFlowintTestParseVar09(
void)
963 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,targetvar");
964 DetectFlowintPrintData(sfd);
965 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
983 static int DetectFlowintTestParseIsset10(
void)
993 sfd = DetectFlowintParse(
de_ctx,
"myvar, isset");
994 DetectFlowintPrintData(sfd);
995 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
1005 sfd = DetectFlowintParse(
de_ctx,
"myvar, notset");
1006 DetectFlowintPrintData(sfd);
1025 static int DetectFlowintTestParseInvalidSyntaxis01(
void)
1035 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,9999999999");
1037 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,9532458716234857");
1042 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,45targetvar");
1044 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,45targetvar ");
1049 sfd = DetectFlowintParse(
de_ctx,
"657myvar,=,targetvar");
1051 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at 657myvar,=,targetvar ");
1056 sfd = DetectFlowintParse(
de_ctx,
"myvar,=<,targetvar");
1058 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=<,targetvar ");
1063 sfd = DetectFlowintParse(
de_ctx,
"myvar,===,targetvar");
1065 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,===,targetvar ");
1070 sfd = DetectFlowintParse(
de_ctx,
"myvar,==");
1072 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,==");
1077 sfd = DetectFlowintParse(
de_ctx,
"myvar,");
1079 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,");
1084 sfd = DetectFlowintParse(
de_ctx,
"myvar");
1086 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar");
1111 static int DetectFlowintTestPacket01Real(
void)
1124 const char *sigs[5];
1125 sigs[0] =
"alert tcp any any -> any any (msg:\"Setting a flowint counter\"; content:\"GET\"; flowint:myvar,=,1; flowint:maxvar,=,6; sid:101;)";
1126 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint: myvar,+,2; sid:102;)";
1127 sigs[2] =
"alert tcp any any -> any any (msg:\"if the flowint counter is 3 create a new counter\"; content:\"Unauthorized\"; flowint: myvar,==,3; flowint: cntpackets, =, 0; sid:103;)";
1128 sigs[3] =
"alert tcp any any -> any any (msg:\"and count the rest of the packets received without generating alerts!!!\"; flowint: myvar,==,3; flowint: cntpackets, +, 1; noalert;sid:104;)";
1129 sigs[4] =
"alert tcp any any -> any any (msg:\" and fire this when it reach 6\"; flowint: cntpackets, ==, maxvar; sid:105;)";
1185 static int DetectFlowintTestPacket02Real(
void)
1198 const char *sigs[5];
1199 sigs[0] =
"alert tcp any any -> any any (msg:\"Setting a flowint counter\"; content:\"GET\"; "
1200 "flowint:myvar,notset; flowint:maxvar,isnotset; flowint: myvar,=,1; flowint: "
1201 "maxvar,=,6; sid:101;)";
1202 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: myvar,+,2; sid:102;)";
1203 sigs[2] =
"alert tcp any any -> any any (msg:\"if the flowint counter is 3 create a new counter\"; content:\"Unauthorized\"; flowint: myvar, isset; flowint: myvar,==,3; flowint:cntpackets,notset; flowint: cntpackets, =, 0; sid:103;)";
1204 sigs[3] =
"alert tcp any any -> any any (msg:\"and count the rest of the packets received without generating alerts!!!\"; flowint: cntpackets,isset; flowint: cntpackets, +, 1; noalert;sid:104;)";
1205 sigs[4] =
"alert tcp any any -> any any (msg:\" and fire this when it reach 6\"; flowint: cntpackets, isset; flowint: maxvar,isset; flowint: cntpackets, ==, maxvar; sid:105;)";
1262 static int DetectFlowintTestPacket03Real(
void)
1275 const char *sigs[3];
1276 sigs[0] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"GET\"; flowint: myvar, notset; flowint: myvar,=,0; flowint: other,=,10; sid:101;)";
1277 sigs[1] =
"alert tcp any any -> any any (msg:\"check isset\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: other,isset; sid:102;)";
1278 sigs[2] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"Unauthorized\"; flowint:lala,isset; sid:103;)";
1326 void DetectFlowintRegisterTests(
void)
1328 UtRegisterTest(
"DetectFlowintTestParseVal01", DetectFlowintTestParseVal01);
1329 UtRegisterTest(
"DetectFlowintTestParseVar01", DetectFlowintTestParseVar01);
1330 UtRegisterTest(
"DetectFlowintTestParseVal02", DetectFlowintTestParseVal02);
1331 UtRegisterTest(
"DetectFlowintTestParseVar02", DetectFlowintTestParseVar02);
1332 UtRegisterTest(
"DetectFlowintTestParseVal03", DetectFlowintTestParseVal03);
1333 UtRegisterTest(
"DetectFlowintTestParseVar03", DetectFlowintTestParseVar03);
1334 UtRegisterTest(
"DetectFlowintTestParseVal04", DetectFlowintTestParseVal04);
1335 UtRegisterTest(
"DetectFlowintTestParseVar04", DetectFlowintTestParseVar04);
1336 UtRegisterTest(
"DetectFlowintTestParseVal05", DetectFlowintTestParseVal05);
1337 UtRegisterTest(
"DetectFlowintTestParseVar05", DetectFlowintTestParseVar05);
1338 UtRegisterTest(
"DetectFlowintTestParseVal06", DetectFlowintTestParseVal06);
1339 UtRegisterTest(
"DetectFlowintTestParseVar06", DetectFlowintTestParseVar06);
1340 UtRegisterTest(
"DetectFlowintTestParseVal07", DetectFlowintTestParseVal07);
1341 UtRegisterTest(
"DetectFlowintTestParseVar07", DetectFlowintTestParseVar07);
1342 UtRegisterTest(
"DetectFlowintTestParseVal08", DetectFlowintTestParseVal08);
1343 UtRegisterTest(
"DetectFlowintTestParseVar08", DetectFlowintTestParseVar08);
1344 UtRegisterTest(
"DetectFlowintTestParseVal09", DetectFlowintTestParseVal09);
1345 UtRegisterTest(
"DetectFlowintTestParseVar09", DetectFlowintTestParseVar09);
1347 DetectFlowintTestParseIsset10);
1349 DetectFlowintTestParseInvalidSyntaxis01);
1351 DetectFlowintTestPacket01Real);
1353 DetectFlowintTestPacket02Real);
1355 DetectFlowintTestPacket03Real);