51 #define PARSE_REGEX "^\\s*([a-zA-Z][\\w\\d_./]+)\\s*,\\s*([+=-]{1}|==|!=|<|<=|>|>=|isset|notset)\\s*,?\\s*([a-zA-Z][\\w\\d]+|[\\d]{1,10})?\\s*$"
61 static void DetectFlowintRegisterTests(
void);
230 char *varname = NULL;
233 int ret = 0, res = 0;
236 unsigned long long value_long = 0;
240 if (ret < 3 || ret > 4) {
241 SCLogError(
"\"%s\" is not a valid setting for flowint(ret = %d).", rawstr, ret);
246 res = pcre2_substring_get_bynumber(parse_regex.
match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
247 if (res < 0 || str_ptr == NULL) {
248 SCLogError(
"pcre2_substring_get_bynumber failed");
251 varname = (
char *)str_ptr;
253 res = pcre2_substring_get_bynumber(parse_regex.
match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
254 if (res < 0 || str_ptr == NULL) {
255 SCLogError(
"pcre2_substring_get_bynumber failed");
258 modstr = (
char *)str_ptr;
261 if (strcmp(
"=", modstr) == 0)
263 if (strcmp(
"+", modstr) == 0)
265 if (strcmp(
"-", modstr) == 0)
268 if (strcmp(
"<", modstr) == 0)
270 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(
"isset", modstr) == 0)
282 if (strcmp(
"notset", modstr) == 0)
299 res = pcre2_substring_get_bynumber(
300 parse_regex.
match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
301 varval = (
char *)str_ptr;
302 if (res < 0 || varval == NULL || strcmp(varval,
"") == 0) {
303 SCLogError(
"pcre2_substring_get_bynumber failed");
307 if (varval[0] >=
'0' && varval[0] <=
'9') {
309 value_long = atoll(varval);
310 if (value_long > UINT32_MAX) {
311 SCLogDebug(
"DetectFlowintParse: Cannot load this value."
312 " Values should be between 0 and %"PRIu32, UINT32_MAX);
338 pcre2_substring_free((PCRE2_UCHAR *)varname);
339 pcre2_substring_free((PCRE2_UCHAR *)modstr);
341 pcre2_substring_free((PCRE2_UCHAR *)varval);
345 pcre2_substring_free((PCRE2_UCHAR *)varname);
347 pcre2_substring_free((PCRE2_UCHAR *)varval);
349 pcre2_substring_free((PCRE2_UCHAR *)modstr);
370 sfd = DetectFlowintParse(
de_ctx, rawstr);
421 if (sfd->
name != NULL)
438 SCLogDebug(
"DetectFlowintPrintData: Error, DetectFlowintData == NULL!");
442 SCLogDebug(
"Varname: %s, modifier: %"PRIu8
", idx: %"PRIu32
" Target: ",
453 SCLogDebug(
"DetectFlowintPrintData: Error, Targettype not known!");
461 static int DetectFlowintTestParseVal01(
void)
471 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,35");
472 DetectFlowintPrintData(sfd);
473 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
488 static int DetectFlowintTestParseVar01(
void)
498 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,targetvar");
499 DetectFlowintPrintData(sfd);
500 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
518 static int DetectFlowintTestParseVal02(
void)
528 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,35");
529 DetectFlowintPrintData(sfd);
530 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
545 static int DetectFlowintTestParseVar02(
void)
555 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,targetvar");
556 DetectFlowintPrintData(sfd);
557 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
575 static int DetectFlowintTestParseVal03(
void)
585 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,35");
586 DetectFlowintPrintData(sfd);
587 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
602 static int DetectFlowintTestParseVar03(
void)
612 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,targetvar");
613 DetectFlowintPrintData(sfd);
614 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
633 static int DetectFlowintTestParseVal04(
void)
643 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,35");
644 DetectFlowintPrintData(sfd);
645 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
660 static int DetectFlowintTestParseVar04(
void)
670 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,targetvar");
671 DetectFlowintPrintData(sfd);
672 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
690 static int DetectFlowintTestParseVal05(
void)
700 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,35");
701 DetectFlowintPrintData(sfd);
702 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
717 static int DetectFlowintTestParseVar05(
void)
727 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,targetvar");
728 DetectFlowintPrintData(sfd);
729 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
747 static int DetectFlowintTestParseVal06(
void)
757 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,35");
758 DetectFlowintPrintData(sfd);
759 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
774 static int DetectFlowintTestParseVar06(
void)
784 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,targetvar");
785 DetectFlowintPrintData(sfd);
786 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
804 static int DetectFlowintTestParseVal07(
void)
814 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,35");
815 DetectFlowintPrintData(sfd);
816 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
831 static int DetectFlowintTestParseVar07(
void)
841 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,targetvar");
842 DetectFlowintPrintData(sfd);
843 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
861 static int DetectFlowintTestParseVal08(
void)
871 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,35");
872 DetectFlowintPrintData(sfd);
873 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
888 static int DetectFlowintTestParseVar08(
void)
898 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,targetvar");
899 DetectFlowintPrintData(sfd);
900 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
918 static int DetectFlowintTestParseVal09(
void)
928 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,35");
929 DetectFlowintPrintData(sfd);
930 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
945 static int DetectFlowintTestParseVar09(
void)
955 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,targetvar");
956 DetectFlowintPrintData(sfd);
957 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
975 static int DetectFlowintTestParseIsset10(
void)
985 sfd = DetectFlowintParse(
de_ctx,
"myvar, isset");
986 DetectFlowintPrintData(sfd);
987 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
997 sfd = DetectFlowintParse(
de_ctx,
"myvar, notset");
998 DetectFlowintPrintData(sfd);
999 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
1018 static int DetectFlowintTestParseInvalidSyntaxis01(
void)
1028 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,9999999999");
1030 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,9532458716234857");
1035 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,45targetvar");
1037 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,45targetvar ");
1042 sfd = DetectFlowintParse(
de_ctx,
"657myvar,=,targetvar");
1044 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at 657myvar,=,targetvar ");
1049 sfd = DetectFlowintParse(
de_ctx,
"myvar,=<,targetvar");
1051 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=<,targetvar ");
1056 sfd = DetectFlowintParse(
de_ctx,
"myvar,===,targetvar");
1058 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,===,targetvar ");
1063 sfd = DetectFlowintParse(
de_ctx,
"myvar,==");
1065 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,==");
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");
1104 static int DetectFlowintTestPacket01Real(
void)
1109 memset(&th_v, 0,
sizeof(th_v));
1116 const char *sigs[5];
1117 sigs[0] =
"alert tcp any any -> any any (msg:\"Setting a flowint counter\"; content:\"GET\"; flowint:myvar,=,1; flowint:maxvar,=,6; sid:101;)";
1118 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint: myvar,+,2; sid:102;)";
1119 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;)";
1120 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;)";
1121 sigs[4] =
"alert tcp any any -> any any (msg:\" and fire this when it reach 6\"; flowint: cntpackets, ==, maxvar; sid:105;)";
1133 f->
proto = IPPROTO_TCP;
1177 static int DetectFlowintTestPacket02Real(
void)
1182 memset(&th_v, 0,
sizeof(th_v));
1189 const char *sigs[5];
1190 sigs[0] =
"alert tcp any any -> any any (msg:\"Setting a flowint counter\"; content:\"GET\"; flowint: myvar, notset; flowint:maxvar,notset; flowint: myvar,=,1; flowint: maxvar,=,6; sid:101;)";
1191 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: myvar,+,2; sid:102;)";
1192 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;)";
1193 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;)";
1194 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;)";
1206 f->
proto = IPPROTO_TCP;
1250 static int DetectFlowintTestPacket03Real(
void)
1255 memset(&th_v, 0,
sizeof(th_v));
1262 const char *sigs[3];
1263 sigs[0] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"GET\"; flowint: myvar, notset; flowint: myvar,=,0; flowint: other,=,10; sid:101;)";
1264 sigs[1] =
"alert tcp any any -> any any (msg:\"check isset\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: other,isset; sid:102;)";
1265 sigs[2] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"Unauthorized\"; flowint:lala,isset; sid:103;)";
1277 f->
proto = IPPROTO_TCP;
1312 void DetectFlowintRegisterTests(
void)
1314 UtRegisterTest(
"DetectFlowintTestParseVal01", DetectFlowintTestParseVal01);
1315 UtRegisterTest(
"DetectFlowintTestParseVar01", DetectFlowintTestParseVar01);
1316 UtRegisterTest(
"DetectFlowintTestParseVal02", DetectFlowintTestParseVal02);
1317 UtRegisterTest(
"DetectFlowintTestParseVar02", DetectFlowintTestParseVar02);
1318 UtRegisterTest(
"DetectFlowintTestParseVal03", DetectFlowintTestParseVal03);
1319 UtRegisterTest(
"DetectFlowintTestParseVar03", DetectFlowintTestParseVar03);
1320 UtRegisterTest(
"DetectFlowintTestParseVal04", DetectFlowintTestParseVal04);
1321 UtRegisterTest(
"DetectFlowintTestParseVar04", DetectFlowintTestParseVar04);
1322 UtRegisterTest(
"DetectFlowintTestParseVal05", DetectFlowintTestParseVal05);
1323 UtRegisterTest(
"DetectFlowintTestParseVar05", DetectFlowintTestParseVar05);
1324 UtRegisterTest(
"DetectFlowintTestParseVal06", DetectFlowintTestParseVal06);
1325 UtRegisterTest(
"DetectFlowintTestParseVar06", DetectFlowintTestParseVar06);
1326 UtRegisterTest(
"DetectFlowintTestParseVal07", DetectFlowintTestParseVal07);
1327 UtRegisterTest(
"DetectFlowintTestParseVar07", DetectFlowintTestParseVar07);
1328 UtRegisterTest(
"DetectFlowintTestParseVal08", DetectFlowintTestParseVal08);
1329 UtRegisterTest(
"DetectFlowintTestParseVar08", DetectFlowintTestParseVar08);
1330 UtRegisterTest(
"DetectFlowintTestParseVal09", DetectFlowintTestParseVal09);
1331 UtRegisterTest(
"DetectFlowintTestParseVar09", DetectFlowintTestParseVar09);
1333 DetectFlowintTestParseIsset10);
1335 DetectFlowintTestParseInvalidSyntaxis01);
1337 DetectFlowintTestPacket01Real);
1339 DetectFlowintTestPacket02Real);
1341 DetectFlowintTestPacket03Real);