52 "^\\s*([a-zA-Z][\\w\\d_./" \
53 "]+)\\s*,\\s*([+=-]{1}|==|!=|<|<=|>|>=|isset|notset|isnotset)\\s*,?\\s*([a-zA-Z][\\w\\d]+|[" \
64 static void DetectFlowintRegisterTests(
void);
233 char *varname = NULL;
239 unsigned long long value_long = 0;
241 pcre2_match_data *match = NULL;
244 if (ret < 3 || ret > 4) {
245 SCLogError(
"\"%s\" is not a valid setting for flowint(ret = %d).", rawstr, ret);
250 res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
251 if (res < 0 || str_ptr == NULL) {
252 SCLogError(
"pcre2_substring_get_bynumber failed");
255 varname = (
char *)str_ptr;
257 res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
258 if (res < 0 || str_ptr == NULL) {
259 SCLogError(
"pcre2_substring_get_bynumber failed");
262 modstr = (
char *)str_ptr;
265 if (strcmp(
"=", modstr) == 0)
267 if (strcmp(
"+", modstr) == 0)
269 if (strcmp(
"-", modstr) == 0)
272 if (strcmp(
"<", modstr) == 0)
274 if (strcmp(
"<=", modstr) == 0)
276 if (strcmp(
"!=", modstr) == 0)
278 if (strcmp(
"==", modstr) == 0)
280 if (strcmp(
">=", modstr) == 0)
282 if (strcmp(
">", modstr) == 0)
284 if (strcmp(
"isset", modstr) == 0)
286 if (strcmp(
"notset", modstr) == 0 || strcmp(
"isnotset", modstr) == 0)
303 res = pcre2_substring_get_bynumber(match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
304 varval = (
char *)str_ptr;
305 if (res < 0 || varval == NULL || strcmp(varval,
"") == 0) {
306 SCLogError(
"pcre2_substring_get_bynumber failed");
310 if (varval[0] >=
'0' && varval[0] <=
'9') {
312 value_long = atoll(varval);
313 if (value_long > UINT32_MAX) {
314 SCLogDebug(
"DetectFlowintParse: Cannot load this value."
315 " Values should be between 0 and %"PRIu32, UINT32_MAX);
340 sfd->
idx = varname_id;
344 pcre2_substring_free((PCRE2_UCHAR *)varname);
345 pcre2_substring_free((PCRE2_UCHAR *)modstr);
347 pcre2_substring_free((PCRE2_UCHAR *)varval);
348 pcre2_match_data_free(match);
352 pcre2_match_data_free(match);
355 pcre2_substring_free((PCRE2_UCHAR *)varname);
357 pcre2_substring_free((PCRE2_UCHAR *)varval);
359 pcre2_substring_free((PCRE2_UCHAR *)modstr);
378 sfd = DetectFlowintParse(
de_ctx, rawstr);
429 if (sfd->
name != NULL)
447 SCLogDebug(
"DetectFlowintPrintData: Error, DetectFlowintData == NULL!");
451 SCLogDebug(
"Varname: %s, modifier: %"PRIu8
", idx: %"PRIu32
" Target: ",
462 SCLogDebug(
"DetectFlowintPrintData: Error, Targettype not known!");
470 static int DetectFlowintTestParseVal01(
void)
480 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,35");
481 DetectFlowintPrintData(sfd);
482 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
497 static int DetectFlowintTestParseVar01(
void)
507 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,targetvar");
508 DetectFlowintPrintData(sfd);
509 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
527 static int DetectFlowintTestParseVal02(
void)
537 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,35");
538 DetectFlowintPrintData(sfd);
539 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
554 static int DetectFlowintTestParseVar02(
void)
564 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,targetvar");
565 DetectFlowintPrintData(sfd);
566 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
584 static int DetectFlowintTestParseVal03(
void)
594 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,35");
595 DetectFlowintPrintData(sfd);
596 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
611 static int DetectFlowintTestParseVar03(
void)
621 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,targetvar");
622 DetectFlowintPrintData(sfd);
623 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
642 static int DetectFlowintTestParseVal04(
void)
652 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,35");
653 DetectFlowintPrintData(sfd);
654 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
669 static int DetectFlowintTestParseVar04(
void)
679 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,targetvar");
680 DetectFlowintPrintData(sfd);
681 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
699 static int DetectFlowintTestParseVal05(
void)
709 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,35");
710 DetectFlowintPrintData(sfd);
711 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
726 static int DetectFlowintTestParseVar05(
void)
736 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,targetvar");
737 DetectFlowintPrintData(sfd);
738 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
756 static int DetectFlowintTestParseVal06(
void)
766 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,35");
767 DetectFlowintPrintData(sfd);
768 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
783 static int DetectFlowintTestParseVar06(
void)
793 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,targetvar");
794 DetectFlowintPrintData(sfd);
795 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
813 static int DetectFlowintTestParseVal07(
void)
823 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,35");
824 DetectFlowintPrintData(sfd);
825 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
840 static int DetectFlowintTestParseVar07(
void)
850 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,targetvar");
851 DetectFlowintPrintData(sfd);
852 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
870 static int DetectFlowintTestParseVal08(
void)
880 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,35");
881 DetectFlowintPrintData(sfd);
882 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
897 static int DetectFlowintTestParseVar08(
void)
907 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,targetvar");
908 DetectFlowintPrintData(sfd);
909 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
927 static int DetectFlowintTestParseVal09(
void)
937 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,35");
938 DetectFlowintPrintData(sfd);
939 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
954 static int DetectFlowintTestParseVar09(
void)
964 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,targetvar");
965 DetectFlowintPrintData(sfd);
966 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
984 static int DetectFlowintTestParseIsset10(
void)
994 sfd = DetectFlowintParse(
de_ctx,
"myvar, isset");
995 DetectFlowintPrintData(sfd);
996 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
1006 sfd = DetectFlowintParse(
de_ctx,
"myvar, notset");
1007 DetectFlowintPrintData(sfd);
1026 static int DetectFlowintTestParseInvalidSyntaxis01(
void)
1036 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,9999999999");
1038 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,9532458716234857");
1043 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,45targetvar");
1045 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,45targetvar ");
1050 sfd = DetectFlowintParse(
de_ctx,
"657myvar,=,targetvar");
1052 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at 657myvar,=,targetvar ");
1057 sfd = DetectFlowintParse(
de_ctx,
"myvar,=<,targetvar");
1059 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=<,targetvar ");
1064 sfd = DetectFlowintParse(
de_ctx,
"myvar,===,targetvar");
1066 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,===,targetvar ");
1071 sfd = DetectFlowintParse(
de_ctx,
"myvar,==");
1073 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,==");
1078 sfd = DetectFlowintParse(
de_ctx,
"myvar,");
1080 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,");
1085 sfd = DetectFlowintParse(
de_ctx,
"myvar");
1087 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar");
1112 static int DetectFlowintTestPacket01Real(
void)
1117 memset(&th_v, 0,
sizeof(th_v));
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;)";
1141 f->
proto = IPPROTO_TCP;
1185 static int DetectFlowintTestPacket02Real(
void)
1190 memset(&th_v, 0,
sizeof(th_v));
1197 const char *sigs[5];
1198 sigs[0] =
"alert tcp any any -> any any (msg:\"Setting a flowint counter\"; content:\"GET\"; "
1199 "flowint:myvar,notset; flowint:maxvar,isnotset; flowint: myvar,=,1; flowint: "
1200 "maxvar,=,6; sid:101;)";
1201 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: myvar,+,2; sid:102;)";
1202 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;)";
1203 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;)";
1204 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;)";
1216 f->
proto = IPPROTO_TCP;
1261 static int DetectFlowintTestPacket03Real(
void)
1266 memset(&th_v, 0,
sizeof(th_v));
1273 const char *sigs[3];
1274 sigs[0] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"GET\"; flowint: myvar, notset; flowint: myvar,=,0; flowint: other,=,10; sid:101;)";
1275 sigs[1] =
"alert tcp any any -> any any (msg:\"check isset\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: other,isset; sid:102;)";
1276 sigs[2] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"Unauthorized\"; flowint:lala,isset; sid:103;)";
1288 f->
proto = IPPROTO_TCP;
1324 void DetectFlowintRegisterTests(
void)
1326 UtRegisterTest(
"DetectFlowintTestParseVal01", DetectFlowintTestParseVal01);
1327 UtRegisterTest(
"DetectFlowintTestParseVar01", DetectFlowintTestParseVar01);
1328 UtRegisterTest(
"DetectFlowintTestParseVal02", DetectFlowintTestParseVal02);
1329 UtRegisterTest(
"DetectFlowintTestParseVar02", DetectFlowintTestParseVar02);
1330 UtRegisterTest(
"DetectFlowintTestParseVal03", DetectFlowintTestParseVal03);
1331 UtRegisterTest(
"DetectFlowintTestParseVar03", DetectFlowintTestParseVar03);
1332 UtRegisterTest(
"DetectFlowintTestParseVal04", DetectFlowintTestParseVal04);
1333 UtRegisterTest(
"DetectFlowintTestParseVar04", DetectFlowintTestParseVar04);
1334 UtRegisterTest(
"DetectFlowintTestParseVal05", DetectFlowintTestParseVal05);
1335 UtRegisterTest(
"DetectFlowintTestParseVar05", DetectFlowintTestParseVar05);
1336 UtRegisterTest(
"DetectFlowintTestParseVal06", DetectFlowintTestParseVal06);
1337 UtRegisterTest(
"DetectFlowintTestParseVar06", DetectFlowintTestParseVar06);
1338 UtRegisterTest(
"DetectFlowintTestParseVal07", DetectFlowintTestParseVal07);
1339 UtRegisterTest(
"DetectFlowintTestParseVar07", DetectFlowintTestParseVar07);
1340 UtRegisterTest(
"DetectFlowintTestParseVal08", DetectFlowintTestParseVal08);
1341 UtRegisterTest(
"DetectFlowintTestParseVar08", DetectFlowintTestParseVar08);
1342 UtRegisterTest(
"DetectFlowintTestParseVal09", DetectFlowintTestParseVal09);
1343 UtRegisterTest(
"DetectFlowintTestParseVar09", DetectFlowintTestParseVar09);
1345 DetectFlowintTestParseIsset10);
1347 DetectFlowintTestParseInvalidSyntaxis01);
1349 DetectFlowintTestPacket01Real);
1351 DetectFlowintTestPacket02Real);
1353 DetectFlowintTestPacket03Real);