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;
236 unsigned long long value_long = 0;
238 pcre2_match_data *match = NULL;
241 if (ret < 3 || ret > 4) {
242 SCLogError(
"\"%s\" is not a valid setting for flowint(ret = %d).", rawstr, ret);
247 res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
248 if (res < 0 || str_ptr == NULL) {
249 SCLogError(
"pcre2_substring_get_bynumber failed");
252 varname = (
char *)str_ptr;
254 res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
255 if (res < 0 || str_ptr == NULL) {
256 SCLogError(
"pcre2_substring_get_bynumber failed");
259 modstr = (
char *)str_ptr;
262 if (strcmp(
"=", modstr) == 0)
264 if (strcmp(
"+", modstr) == 0)
266 if (strcmp(
"-", modstr) == 0)
269 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(
"isset", modstr) == 0)
283 if (strcmp(
"notset", modstr) == 0)
300 res = pcre2_substring_get_bynumber(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);
342 pcre2_match_data_free(match);
346 pcre2_match_data_free(match);
349 pcre2_substring_free((PCRE2_UCHAR *)varname);
351 pcre2_substring_free((PCRE2_UCHAR *)varval);
353 pcre2_substring_free((PCRE2_UCHAR *)modstr);
373 sfd = DetectFlowintParse(
de_ctx, rawstr);
423 if (sfd->
name != NULL)
440 SCLogDebug(
"DetectFlowintPrintData: Error, DetectFlowintData == NULL!");
444 SCLogDebug(
"Varname: %s, modifier: %"PRIu8
", idx: %"PRIu32
" Target: ",
455 SCLogDebug(
"DetectFlowintPrintData: Error, Targettype not known!");
463 static int DetectFlowintTestParseVal01(
void)
473 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,35");
474 DetectFlowintPrintData(sfd);
475 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
490 static int DetectFlowintTestParseVar01(
void)
500 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,targetvar");
501 DetectFlowintPrintData(sfd);
502 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
520 static int DetectFlowintTestParseVal02(
void)
530 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,35");
531 DetectFlowintPrintData(sfd);
532 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
547 static int DetectFlowintTestParseVar02(
void)
557 sfd = DetectFlowintParse(
de_ctx,
"myvar,+,targetvar");
558 DetectFlowintPrintData(sfd);
559 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
577 static int DetectFlowintTestParseVal03(
void)
587 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,35");
588 DetectFlowintPrintData(sfd);
589 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
604 static int DetectFlowintTestParseVar03(
void)
614 sfd = DetectFlowintParse(
de_ctx,
"myvar,-,targetvar");
615 DetectFlowintPrintData(sfd);
616 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
635 static int DetectFlowintTestParseVal04(
void)
645 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,35");
646 DetectFlowintPrintData(sfd);
647 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
662 static int DetectFlowintTestParseVar04(
void)
672 sfd = DetectFlowintParse(
de_ctx,
"myvar,==,targetvar");
673 DetectFlowintPrintData(sfd);
674 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
692 static int DetectFlowintTestParseVal05(
void)
702 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,35");
703 DetectFlowintPrintData(sfd);
704 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
719 static int DetectFlowintTestParseVar05(
void)
729 sfd = DetectFlowintParse(
de_ctx,
"myvar,!=,targetvar");
730 DetectFlowintPrintData(sfd);
731 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
749 static int DetectFlowintTestParseVal06(
void)
759 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,35");
760 DetectFlowintPrintData(sfd);
761 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
776 static int DetectFlowintTestParseVar06(
void)
786 sfd = DetectFlowintParse(
de_ctx,
"myvar, >,targetvar");
787 DetectFlowintPrintData(sfd);
788 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
806 static int DetectFlowintTestParseVal07(
void)
816 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,35");
817 DetectFlowintPrintData(sfd);
818 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
833 static int DetectFlowintTestParseVar07(
void)
843 sfd = DetectFlowintParse(
de_ctx,
"myvar, >= ,targetvar");
844 DetectFlowintPrintData(sfd);
845 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
863 static int DetectFlowintTestParseVal08(
void)
873 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,35");
874 DetectFlowintPrintData(sfd);
875 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
890 static int DetectFlowintTestParseVar08(
void)
900 sfd = DetectFlowintParse(
de_ctx,
"myvar, <= ,targetvar");
901 DetectFlowintPrintData(sfd);
902 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
920 static int DetectFlowintTestParseVal09(
void)
930 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,35");
931 DetectFlowintPrintData(sfd);
932 if (sfd != NULL && sfd->
target.
value == 35 && !strcmp(sfd->
name,
"myvar")
947 static int DetectFlowintTestParseVar09(
void)
957 sfd = DetectFlowintParse(
de_ctx,
"myvar, < ,targetvar");
958 DetectFlowintPrintData(sfd);
959 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
977 static int DetectFlowintTestParseIsset10(
void)
987 sfd = DetectFlowintParse(
de_ctx,
"myvar, isset");
988 DetectFlowintPrintData(sfd);
989 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
999 sfd = DetectFlowintParse(
de_ctx,
"myvar, notset");
1000 DetectFlowintPrintData(sfd);
1001 if (sfd != NULL && !strcmp(sfd->
name,
"myvar")
1020 static int DetectFlowintTestParseInvalidSyntaxis01(
void)
1030 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,9999999999");
1032 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,9532458716234857");
1037 sfd = DetectFlowintParse(
de_ctx,
"myvar,=,45targetvar");
1039 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=,45targetvar ");
1044 sfd = DetectFlowintParse(
de_ctx,
"657myvar,=,targetvar");
1046 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at 657myvar,=,targetvar ");
1051 sfd = DetectFlowintParse(
de_ctx,
"myvar,=<,targetvar");
1053 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,=<,targetvar ");
1058 sfd = DetectFlowintParse(
de_ctx,
"myvar,===,targetvar");
1060 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,===,targetvar ");
1065 sfd = DetectFlowintParse(
de_ctx,
"myvar,==");
1067 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,==");
1072 sfd = DetectFlowintParse(
de_ctx,
"myvar,");
1074 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar,");
1079 sfd = DetectFlowintParse(
de_ctx,
"myvar");
1081 SCLogDebug(
"DetectFlowintTestParseInvalidSyntaxis01: ERROR: invalid option at myvar");
1106 static int DetectFlowintTestPacket01Real(
void)
1111 memset(&th_v, 0,
sizeof(th_v));
1118 const char *sigs[5];
1119 sigs[0] =
"alert tcp any any -> any any (msg:\"Setting a flowint counter\"; content:\"GET\"; flowint:myvar,=,1; flowint:maxvar,=,6; sid:101;)";
1120 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint: myvar,+,2; sid:102;)";
1121 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;)";
1122 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;)";
1123 sigs[4] =
"alert tcp any any -> any any (msg:\" and fire this when it reach 6\"; flowint: cntpackets, ==, maxvar; sid:105;)";
1135 f->
proto = IPPROTO_TCP;
1179 static int DetectFlowintTestPacket02Real(
void)
1184 memset(&th_v, 0,
sizeof(th_v));
1191 const char *sigs[5];
1192 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;)";
1193 sigs[1] =
"alert tcp any any -> any any (msg:\"Adding to flowint counter\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: myvar,+,2; sid:102;)";
1194 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;)";
1195 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;)";
1196 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;)";
1208 f->
proto = IPPROTO_TCP;
1252 static int DetectFlowintTestPacket03Real(
void)
1257 memset(&th_v, 0,
sizeof(th_v));
1264 const char *sigs[3];
1265 sigs[0] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"GET\"; flowint: myvar, notset; flowint: myvar,=,0; flowint: other,=,10; sid:101;)";
1266 sigs[1] =
"alert tcp any any -> any any (msg:\"check isset\"; content:\"Unauthorized\"; flowint:myvar,isset; flowint: other,isset; sid:102;)";
1267 sigs[2] =
"alert tcp any any -> any any (msg:\"check notset\"; content:\"Unauthorized\"; flowint:lala,isset; sid:103;)";
1279 f->
proto = IPPROTO_TCP;
1314 void DetectFlowintRegisterTests(
void)
1316 UtRegisterTest(
"DetectFlowintTestParseVal01", DetectFlowintTestParseVal01);
1317 UtRegisterTest(
"DetectFlowintTestParseVar01", DetectFlowintTestParseVar01);
1318 UtRegisterTest(
"DetectFlowintTestParseVal02", DetectFlowintTestParseVal02);
1319 UtRegisterTest(
"DetectFlowintTestParseVar02", DetectFlowintTestParseVar02);
1320 UtRegisterTest(
"DetectFlowintTestParseVal03", DetectFlowintTestParseVal03);
1321 UtRegisterTest(
"DetectFlowintTestParseVar03", DetectFlowintTestParseVar03);
1322 UtRegisterTest(
"DetectFlowintTestParseVal04", DetectFlowintTestParseVal04);
1323 UtRegisterTest(
"DetectFlowintTestParseVar04", DetectFlowintTestParseVar04);
1324 UtRegisterTest(
"DetectFlowintTestParseVal05", DetectFlowintTestParseVal05);
1325 UtRegisterTest(
"DetectFlowintTestParseVar05", DetectFlowintTestParseVar05);
1326 UtRegisterTest(
"DetectFlowintTestParseVal06", DetectFlowintTestParseVal06);
1327 UtRegisterTest(
"DetectFlowintTestParseVar06", DetectFlowintTestParseVar06);
1328 UtRegisterTest(
"DetectFlowintTestParseVal07", DetectFlowintTestParseVal07);
1329 UtRegisterTest(
"DetectFlowintTestParseVar07", DetectFlowintTestParseVar07);
1330 UtRegisterTest(
"DetectFlowintTestParseVal08", DetectFlowintTestParseVal08);
1331 UtRegisterTest(
"DetectFlowintTestParseVar08", DetectFlowintTestParseVar08);
1332 UtRegisterTest(
"DetectFlowintTestParseVal09", DetectFlowintTestParseVal09);
1333 UtRegisterTest(
"DetectFlowintTestParseVar09", DetectFlowintTestParseVar09);
1335 DetectFlowintTestParseIsset10);
1337 DetectFlowintTestParseInvalidSyntaxis01);
1339 DetectFlowintTestPacket01Real);
1341 DetectFlowintTestPacket02Real);
1343 DetectFlowintTestPacket03Real);