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);
341 pcre2_substring_free((PCRE2_UCHAR *)varname);
342 pcre2_substring_free((PCRE2_UCHAR *)modstr);
344 pcre2_substring_free((PCRE2_UCHAR *)varval);
345 pcre2_match_data_free(match);
349 pcre2_match_data_free(match);
352 pcre2_substring_free((PCRE2_UCHAR *)varname);
354 pcre2_substring_free((PCRE2_UCHAR *)varval);
356 pcre2_substring_free((PCRE2_UCHAR *)modstr);
376 sfd = DetectFlowintParse(
de_ctx, rawstr);
426 if (sfd->
name != NULL)
443 SCLogDebug(
"DetectFlowintPrintData: Error, DetectFlowintData == NULL!");
447 SCLogDebug(
"Varname: %s, modifier: %"PRIu8
", idx: %"PRIu32
" Target: ",
458 SCLogDebug(
"DetectFlowintPrintData: Error, Targettype not known!");
466 static int DetectFlowintTestParseVal01(
void)
476 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,35");
477 DetectFlowintPrintData(sfd);
478 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
493 static int DetectFlowintTestParseVar01(
void)
503 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,targetvar");
504 DetectFlowintPrintData(sfd);
505 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
523 static int DetectFlowintTestParseVal02(
void)
533 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,35");
534 DetectFlowintPrintData(sfd);
535 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
550 static int DetectFlowintTestParseVar02(
void)
560 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,targetvar");
561 DetectFlowintPrintData(sfd);
562 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
580 static int DetectFlowintTestParseVal03(
void)
590 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,35");
591 DetectFlowintPrintData(sfd);
592 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
607 static int DetectFlowintTestParseVar03(
void)
617 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,targetvar");
618 DetectFlowintPrintData(sfd);
619 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
638 static int DetectFlowintTestParseVal04(
void)
648 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,35");
649 DetectFlowintPrintData(sfd);
650 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
665 static int DetectFlowintTestParseVar04(
void)
675 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,targetvar");
676 DetectFlowintPrintData(sfd);
677 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
695 static int DetectFlowintTestParseVal05(
void)
705 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,35");
706 DetectFlowintPrintData(sfd);
707 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
722 static int DetectFlowintTestParseVar05(
void)
732 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,targetvar");
733 DetectFlowintPrintData(sfd);
734 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
752 static int DetectFlowintTestParseVal06(
void)
762 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,35");
763 DetectFlowintPrintData(sfd);
764 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
779 static int DetectFlowintTestParseVar06(
void)
789 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,targetvar");
790 DetectFlowintPrintData(sfd);
791 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
809 static int DetectFlowintTestParseVal07(
void)
819 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,35");
820 DetectFlowintPrintData(sfd);
821 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
836 static int DetectFlowintTestParseVar07(
void)
846 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,targetvar");
847 DetectFlowintPrintData(sfd);
848 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
866 static int DetectFlowintTestParseVal08(
void)
876 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,35");
877 DetectFlowintPrintData(sfd);
878 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
893 static int DetectFlowintTestParseVar08(
void)
903 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,targetvar");
904 DetectFlowintPrintData(sfd);
905 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
923 static int DetectFlowintTestParseVal09(
void)
933 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,35");
934 DetectFlowintPrintData(sfd);
935 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
950 static int DetectFlowintTestParseVar09(
void)
960 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,targetvar");
961 DetectFlowintPrintData(sfd);
962 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
980 static int DetectFlowintTestParseIsset10(
void)
990 sfd = DetectFlowintParse(
de_ctx,
"myvar, isset");
991 DetectFlowintPrintData(sfd);
992 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
1002 sfd = DetectFlowintParse(
de_ctx,
"myvar, notset");
1003 DetectFlowintPrintData(sfd);
1022 static int DetectFlowintTestParseInvalidSyntaxis01(
void)
1032 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,9999999999");
1034 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,9532458716234857");
1039 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,45targetvar");
1041 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,45targetvar ");
1046 sfd = DetectFlowintParse(
de_ctx,
"657myvar,=,targetvar");
1048 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at 657myvar,=,targetvar ");
1053 sfd = DetectFlowintParse(
de_ctx,
"myvar,=<,targetvar");
1055 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=<,targetvar ");
1060 sfd = DetectFlowintParse(
de_ctx,
"myvar,===,targetvar");
1062 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,===,targetvar ");
1067 sfd = DetectFlowintParse(
de_ctx,
"myvar,==");
1069 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,==");
1074 sfd = DetectFlowintParse(
de_ctx,
"myvar,");
1076 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,");
1081 sfd = DetectFlowintParse(
de_ctx,
"myvar");
1083 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar");
1108 static int DetectFlowintTestPacket01Real(
void)
1113 memset(&th_v, 0,
sizeof(th_v));
1120 const char *sigs[5];
1121 sigs[0] =
"alert tcp any any -> any any (msg:\"Setting a flowint counter\"; content:\"GET\"; flowint:myvar,=,1; flowint:maxvar,=,6; sid:101;)";
1122 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint: myvar,+,2; sid:102;)";
1123 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;)";
1124 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;)";
1125 sigs[4] =
"alert tcp any any -> any any (msg:\" and fire this when it reach 6\"; flowint: cntpackets, ==, maxvar; sid:105;)";
1137 f->
proto = IPPROTO_TCP;
1181 static int DetectFlowintTestPacket02Real(
void)
1186 memset(&th_v, 0,
sizeof(th_v));
1193 const char *sigs[5];
1194 sigs[0] =
"alert tcp any any -> any any (msg:\"Setting a flowint counter\"; content:\"GET\"; "
1195 "flowint:myvar,notset; flowint:maxvar,isnotset; flowint: myvar,=,1; flowint: "
1196 "maxvar,=,6; sid:101;)";
1197 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: myvar,+,2; sid:102;)";
1198 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;)";
1199 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;)";
1200 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;)";
1212 f->
proto = IPPROTO_TCP;
1256 static int DetectFlowintTestPacket03Real(
void)
1261 memset(&th_v, 0,
sizeof(th_v));
1268 const char *sigs[3];
1269 sigs[0] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"GET\"; flowint: myvar, notset; flowint: myvar,=,0; flowint: other,=,10; sid:101;)";
1270 sigs[1] =
"alert tcp any any -> any any (msg:\"check isset\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: other,isset; sid:102;)";
1271 sigs[2] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"Unauthorized\"; flowint:lala,isset; sid:103;)";
1283 f->
proto = IPPROTO_TCP;
1318 void DetectFlowintRegisterTests(
void)
1320 UtRegisterTest(
"DetectFlowintTestParseVal01", DetectFlowintTestParseVal01);
1321 UtRegisterTest(
"DetectFlowintTestParseVar01", DetectFlowintTestParseVar01);
1322 UtRegisterTest(
"DetectFlowintTestParseVal02", DetectFlowintTestParseVal02);
1323 UtRegisterTest(
"DetectFlowintTestParseVar02", DetectFlowintTestParseVar02);
1324 UtRegisterTest(
"DetectFlowintTestParseVal03", DetectFlowintTestParseVal03);
1325 UtRegisterTest(
"DetectFlowintTestParseVar03", DetectFlowintTestParseVar03);
1326 UtRegisterTest(
"DetectFlowintTestParseVal04", DetectFlowintTestParseVal04);
1327 UtRegisterTest(
"DetectFlowintTestParseVar04", DetectFlowintTestParseVar04);
1328 UtRegisterTest(
"DetectFlowintTestParseVal05", DetectFlowintTestParseVal05);
1329 UtRegisterTest(
"DetectFlowintTestParseVar05", DetectFlowintTestParseVar05);
1330 UtRegisterTest(
"DetectFlowintTestParseVal06", DetectFlowintTestParseVal06);
1331 UtRegisterTest(
"DetectFlowintTestParseVar06", DetectFlowintTestParseVar06);
1332 UtRegisterTest(
"DetectFlowintTestParseVal07", DetectFlowintTestParseVal07);
1333 UtRegisterTest(
"DetectFlowintTestParseVar07", DetectFlowintTestParseVar07);
1334 UtRegisterTest(
"DetectFlowintTestParseVal08", DetectFlowintTestParseVal08);
1335 UtRegisterTest(
"DetectFlowintTestParseVar08", DetectFlowintTestParseVar08);
1336 UtRegisterTest(
"DetectFlowintTestParseVal09", DetectFlowintTestParseVal09);
1337 UtRegisterTest(
"DetectFlowintTestParseVar09", DetectFlowintTestParseVar09);
1339 DetectFlowintTestParseIsset10);
1341 DetectFlowintTestParseInvalidSyntaxis01);
1343 DetectFlowintTestPacket01Real);
1345 DetectFlowintTestPacket02Real);
1347 DetectFlowintTestPacket03Real);