56 #define VALID_KW "relative|big|little|string|oct|dec|hex|dce|bitmask"
57 #define PARSE_REGEX "^\\s*" \
58 "([^\\s,]+)\\s*,\\s*" \
59 "(\\!?\\s*[^\\s,]*)" \
60 "\\s*,\\s*([^\\s,]+\\s*,\\s*[^\\s,]+)" \
61 "(?:\\s*,\\s*((?:"VALID_KW")\\s+[^\\s,]+|["VALID_KW"]+))?" \
62 "(?:\\s*,\\s*((?:"VALID_KW")\\s+[^\\s,]+|["VALID_KW"]+))?" \
63 "(?:\\s*,\\s*((?:"VALID_KW")\\s+[^\\s,]+|["VALID_KW"]+))?" \
64 "(?:\\s*,\\s*((?:"VALID_KW")\\s+[^\\s,]+|["VALID_KW"]+))?" \
65 "(?:\\s*,\\s*((?:"VALID_KW")\\s+[^\\s,]+|["VALID_KW"]+))?" \
66 "(?:\\s*,\\s*((?:"VALID_KW")\\s+[^\\s,]+|["VALID_KW"]+))?" \
74 static void DetectBytetestRegisterTests(
void);
80 sigmatch_table[
DETECT_BYTETEST].
desc =
"extract <num of bytes> and perform an operation selected with <operator> against the value in <test value> at a particular <offset>";
99 static inline bool DetectBytetestValidateNbytesOnly(
const DetectBytetestData *data, int32_t nbytes)
104 static bool DetectBytetestValidateNbytes(
107 if (!DetectBytetestValidateNbytesOnly(data, nbytes)) {
117 SCLogError(
"Cannot test more than 23 bytes with \"string\": %s", optstr);
121 SCLogError(
"Cannot test more than 8 bytes without \"string\": %s", optstr);
124 SCLogError(
"Cannot use a base without \"string\": %s", optstr);
134 SCLogError(
"Cannot use a base without \"string\": %s", optstr);
156 int32_t
offset, int32_t nbytes, uint64_t value)
166 if (!DetectBytetestValidateNbytesOnly(data, nbytes)) {
167 SCLogDebug(
"Invalid byte_test nbytes seen in byte_test - %d", nbytes);
172 const uint8_t *ptr = NULL;
183 SCLogDebug(
"relative, working with det_ctx->buffer_offset %"PRIu32
", "
193 if (ptr == NULL ||
len <= 0) {
207 if (ptr < payload || nbytes >
len) {
208 SCLogDebug(
"Data not within payload pkt=%p, ptr=%p, len=%" PRIu32
", nbytes=%d", payload,
225 "bytes of string data: %d",
231 SCLogDebug(
"comparing base %d string 0x%" PRIx64
" %s%u 0x%" PRIx64,
232 data->
base, val, (neg ?
"!" :
""), data->
op, data->
value);
238 if (extbytes != nbytes) {
240 "of numeric data: %d",
245 SCLogDebug(
"comparing numeric 0x%" PRIx64
" %s%u 0x%" PRIx64,
246 val, (neg ?
"!" :
""), data->
op, data->
value);
303 if ((!neg && match) || (neg && !match)) {
304 SCLogDebug(
"MATCH [bt] extracted value is %"PRIu64, val);
314 const char *optstr,
char **value,
char **
offset,
char **nbytes_str)
318 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
321 char *test_value = NULL;
322 char *data_offset = NULL;
327 const char *str_ptr = NULL;
328 pcre2_match_data *match = NULL;
332 if (ret < 4 || ret > 9) {
333 SCLogError(
"parse error, ret %" PRId32
", string %s", ret, optstr);
338 for (i = 0; i < (ret - 1); i++) {
339 res = pcre2_substring_get_bynumber(match, i + 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
341 SCLogError(
"pcre2_substring_get_bynumber failed "
348 test_value = (
char *) str_ptr;
349 data_offset =
SCStrdup((
char *) str_ptr);
350 if (data_offset == NULL) {
354 args[i] = (
char *)str_ptr;
377 if (args[0][0] !=
'-' && isalpha((
unsigned char)args[0][0])) {
378 if (nbytes_str == NULL) {
380 "var name for nbytes. \"value\" argument supplied to "
381 "this function has to be non-NULL");
385 if (*nbytes_str == NULL)
390 SCLogError(
"Malformed number of bytes: %s", str_ptr);
397 if (args[1] != NULL) {
400 if (args[1][op_offset] ==
'!') {
402 op_ptr = &args[1][1];
403 while (isspace((
char)*op_ptr) || (*op_ptr ==
',')) op_ptr++;
404 op_offset = op_ptr - &args[1][0];
408 op_ptr = args[1] + op_offset;
409 if ((strcmp(
"=", op_ptr) == 0) || (data->
neg_op
410 && strcmp(
"", op_ptr) == 0)) {
412 }
else if (strcmp(
"<", op_ptr) == 0) {
414 }
else if (strcmp(
">", op_ptr) == 0) {
416 }
else if (strcmp(
"&", op_ptr) == 0) {
418 }
else if (strcmp(
"^", op_ptr) == 0) {
420 }
else if (strcmp(
">=", op_ptr) == 0) {
422 }
else if (strcmp(
"<=", op_ptr) == 0) {
436 char *end_ptr = test_value;
437 while (!(isspace((
unsigned char)*end_ptr) || (*end_ptr ==
','))) end_ptr++;
440 if (test_value[0] !=
'-' && isalpha((
unsigned char)test_value[0])) {
443 "var name for value. \"value\" argument supplied to "
444 "this function has to be non-NULL");
452 SCLogError(
"Malformed value: %s", test_value);
460 char *end_ptr = data_offset;
461 while (!(isspace((
unsigned char)*end_ptr) || (*end_ptr ==
','))) end_ptr++;
463 while (isspace((
unsigned char)*str_ptr) || (*str_ptr ==
',')) str_ptr++;
464 end_ptr = (
char *)str_ptr;
465 while (!(isspace((
unsigned char)*end_ptr) || (*end_ptr ==
',')) && (*end_ptr !=
'\0'))
467 memmove(data_offset, str_ptr, end_ptr - str_ptr);
468 data_offset[end_ptr-str_ptr] =
'\0';
469 if (data_offset[0] !=
'-' && isalpha((
unsigned char)data_offset[0])) {
470 if (data_offset == NULL) {
472 "var name for offset. \"offset\" argument supplied to "
473 "this function has to be non-NULL");
481 SCLogError(
"Malformed offset: %s", data_offset);
489 int bitmask_index = -1;
490 for (i = 3; i < (ret - 1); i++) {
491 if (args[i] != NULL) {
492 if (strcmp(
"relative", args[i]) == 0) {
494 }
else if (strcasecmp(
"string", args[i]) == 0) {
496 }
else if (strcasecmp(
"dec", args[i]) == 0) {
498 }
else if (strcasecmp(
"hex", args[i]) == 0) {
500 }
else if (strcasecmp(
"oct", args[i]) == 0) {
502 }
else if (strcasecmp(
"big", args[i]) == 0) {
507 }
else if (strcasecmp(
"little", args[i]) == 0) {
509 }
else if (strcasecmp(
"dce", args[i]) == 0) {
511 }
else if (strncasecmp(
"bitmask", args[i], strlen(
"bitmask")) == 0) {
522 if (!DetectBytetestValidateNbytes(data, nbytes, optstr)) {
527 data->
nbytes = (uint8_t)nbytes;
532 SCLogError(
"Malformed bitmask value: %s", args[bitmask_index] + strlen(
"bitmask"));
540 uint32_t bmask = data->
bitmask;
541 while (!(bmask & 0x1)){
548 for (i = 0; i < (ret - 1); i++){
550 pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
552 if (data_offset)
SCFree(data_offset);
554 pcre2_substring_free((PCRE2_UCHAR8 *)test_value);
555 pcre2_match_data_free(match);
559 for (i = 0; i < (ret - 1); i++){
561 pcre2_substring_free((PCRE2_UCHAR8 *)args[i]);
563 if (data_offset)
SCFree(data_offset);
565 pcre2_substring_free((PCRE2_UCHAR8 *)test_value);
568 pcre2_match_data_free(match);
602 if (prev_pm == NULL) {
621 if (prev_pm == NULL) {
641 "A byte_test keyword with dce holds other invalid modifiers.");
650 "seen in byte_test - %s",
664 "seen in byte_test - %s",
674 if (nbytes != NULL) {
678 "seen in byte_test - %s",
715 DetectBytetestFree(
de_ctx, data);
739 static int g_file_data_buffer_id = 0;
740 static int g_dce_stub_data_buffer_id = 0;
746 static int DetectBytetestTestParse01(
void)
749 data = DetectBytetestParse(
"4, =, 1 , 0", NULL, NULL, NULL);
751 DetectBytetestFree(NULL, data);
758 static int DetectBytetestTestParse02(
void)
761 data = DetectBytetestParse(
"4, !=, 1, 0", NULL, NULL, NULL);
770 DetectBytetestFree(NULL, data);
777 static int DetectBytetestTestParse03(
void)
780 data = DetectBytetestParse(
"4, !=, 1, 0, relative", NULL, NULL, NULL);
790 DetectBytetestFree(NULL, data);
797 static int DetectBytetestTestParse04(
void)
800 data = DetectBytetestParse(
"4, !=, 1, 0, string, oct", NULL, NULL, NULL);
809 DetectBytetestFree(NULL, data);
816 static int DetectBytetestTestParse05(
void)
819 data = DetectBytetestParse(
"4, =, 1, 0, string, dec", NULL, NULL, NULL);
827 DetectBytetestFree(NULL, data);
834 static int DetectBytetestTestParse06(
void)
837 data = DetectBytetestParse(
"4, >, 1, 0, string, hex", NULL, NULL, NULL);
845 DetectBytetestFree(NULL, data);
852 static int DetectBytetestTestParse07(
void)
855 data = DetectBytetestParse(
"4, <, 5, 0, big", NULL, NULL, NULL);
863 DetectBytetestFree(NULL, data);
870 static int DetectBytetestTestParse08(
void)
873 data = DetectBytetestParse(
"4, <, 5, 0, little", NULL, NULL, NULL);
882 DetectBytetestFree(NULL, data);
889 static int DetectBytetestTestParse09(
void)
892 data = DetectBytetestParse(
"4, !, 5, 0", NULL, NULL, NULL);
900 DetectBytetestFree(NULL, data);
907 static int DetectBytetestTestParse10(
void)
910 data = DetectBytetestParse(
" 4 , ! &, 5 , 0 , little ", NULL, NULL, NULL);
920 DetectBytetestFree(NULL, data);
927 static int DetectBytetestTestParse11(
void)
930 data = DetectBytetestParse(
"4,!^,5,0,little,string,relative,hex", NULL, NULL, NULL);
941 DetectBytetestFree(NULL, data);
948 static int DetectBytetestTestParse12(
void)
951 data = DetectBytetestParse(
"4, =, 1, 0, hex", NULL, NULL, NULL);
960 static int DetectBytetestTestParse13(
void)
963 data = DetectBytetestParse(
"9, =, 1, 0", NULL, NULL, NULL);
971 static int DetectBytetestTestParse14(
void)
974 data = DetectBytetestParse(
"23,=,0xffffffffffffffffULL,0,string,oct", NULL, NULL, NULL);
983 DetectBytetestFree(NULL, data);
990 static int DetectBytetestTestParse15(
void)
993 data = DetectBytetestParse(
"24, =, 0xffffffffffffffffULL, 0, string", NULL, NULL, NULL);
1002 static int DetectBytetestTestParse16(
void)
1005 data = DetectBytetestParse(
"4,=,0,0xffffffffffffffffULL", NULL, NULL, NULL);
1014 static int DetectBytetestTestParse17(
void)
1017 data = DetectBytetestParse(
"4, <, 5, 0, dce", NULL, NULL, NULL);
1025 DetectBytetestFree(NULL, data);
1032 static int DetectBytetestTestParse18(
void)
1035 data = DetectBytetestParse(
"4, <, 5, 0", NULL, NULL, NULL);
1043 DetectBytetestFree(NULL, data);
1050 static int DetectBytetestTestParse19(
void)
1057 FAIL_IF_NOT(DetectBytetestSetup(NULL, s,
"1,=,1,6,dce") == 0);
1058 FAIL_IF_NOT(DetectBytetestSetup(NULL, s,
"1,=,1,6,string,dce") == -1);
1059 FAIL_IF_NOT(DetectBytetestSetup(NULL, s,
"1,=,1,6,big,dce") == -1);
1060 FAIL_IF_NOT(DetectBytetestSetup(NULL, s,
"1,=,1,6,little,dce") == -1);
1061 FAIL_IF_NOT(DetectBytetestSetup(NULL, s,
"1,=,1,6,hex,dce") == -1);
1062 FAIL_IF_NOT(DetectBytetestSetup(NULL, s,
"1,=,1,6,oct,dce") == -1);
1063 FAIL_IF_NOT(DetectBytetestSetup(NULL, s,
"1,=,1,6,dec,dce") == -1);
1072 static int DetectBytetestTestParse20(
void)
1083 "(msg:\"Testing bytetest_body\"; "
1084 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1086 "content:\"one\"; distance:0; "
1087 "byte_test:1,=,1,6,relative,dce; sid:1;)");
1106 "(msg:\"Testing bytetest_body\"; "
1107 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1109 "content:\"one\"; distance:0; "
1110 "byte_test:1,=,1,6,relative,dce; sid:1;)");
1128 "(msg:\"Testing bytetest_body\"; "
1129 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1131 "content:\"one\"; distance:0; "
1132 "byte_test:1,=,1,6,relative; sid:1;)");
1158 static int DetectBytetestTestParse21(
void)
1168 "(msg:\"Testing bytetest_body\"; "
1169 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1170 "content:\"one\"; byte_test:1,=,1,6,string,dce; sid:1;)");
1174 "(msg:\"Testing bytetest_body\"; "
1175 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1176 "content:\"one\"; byte_test:1,=,1,6,big,dce; sid:1;)");
1180 "(msg:\"Testing bytetest_body\"; "
1181 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1182 "content:\"one\"; byte_test:1,=,1,6,little,dce; sid:1;)");
1186 "(msg:\"Testing bytetest_body\"; "
1187 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1188 "content:\"one\"; byte_test:1,=,1,6,hex,dce; sid:1;)");
1192 "(msg:\"Testing bytetest_body\"; "
1193 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1194 "content:\"one\"; byte_test:1,=,1,6,dec,dce; sid:1;)");
1198 "(msg:\"Testing bytetest_body\"; "
1199 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1200 "content:\"one\"; byte_test:1,=,1,6,oct,dce; sid:1;)");
1204 "(msg:\"Testing bytetest_body\"; "
1205 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1206 "content:\"one\"; byte_test:1,=,1,6,string,hex,dce; sid:1;)");
1210 "(msg:\"Testing bytetest_body\"; "
1211 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1212 "content:\"one\"; byte_test:1,=,1,6,big,string,hex,dce; sid:1;)");
1216 "(msg:\"Testing bytetest_body\"; "
1217 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1218 "content:\"one\"; byte_test:1,=,1,6,big,string,oct,dce; sid:1;)");
1222 "(msg:\"Testing bytetest_body\"; "
1223 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1224 "content:\"one\"; byte_test:1,=,1,6,little,string,hex,dce; sid:1;)");
1228 "(msg:\"Testing bytetest_body\"; "
1229 "dce_iface:3919286a-b10c-11d0-9ba8-00c04fd92ef5; "
1230 "content:\"one\"; byte_test:1,=,1,6,big,string,dec,dce; sid:1;)");
1243 static int DetectBytetestTestParse22(
void)
1254 "(file_data; byte_test:1,=,1,6,relative; sid:1;)");
1279 static int DetectBytetestTestParse23(
void)
1282 data = DetectBytetestParse(
"4, <, 5, 0, bitmask 0xf8", NULL, NULL, NULL);
1293 DetectBytetestFree(NULL, data);
1301 static int DetectBytetestTestParse24(
void)
1304 data = DetectBytetestParse(
1305 "4, !<, 5, 0, relative,string,hex, big, bitmask 0xf8", NULL, NULL, NULL);
1319 DetectBytetestFree(NULL, data);
1327 static void DetectBytetestRegisterTests(
void)
1332 UtRegisterTest(
"DetectBytetestTestParse01", DetectBytetestTestParse01);
1333 UtRegisterTest(
"DetectBytetestTestParse02", DetectBytetestTestParse02);
1334 UtRegisterTest(
"DetectBytetestTestParse03", DetectBytetestTestParse03);
1335 UtRegisterTest(
"DetectBytetestTestParse04", DetectBytetestTestParse04);
1336 UtRegisterTest(
"DetectBytetestTestParse05", DetectBytetestTestParse05);
1337 UtRegisterTest(
"DetectBytetestTestParse06", DetectBytetestTestParse06);
1338 UtRegisterTest(
"DetectBytetestTestParse07", DetectBytetestTestParse07);
1339 UtRegisterTest(
"DetectBytetestTestParse08", DetectBytetestTestParse08);
1340 UtRegisterTest(
"DetectBytetestTestParse09", DetectBytetestTestParse09);
1341 UtRegisterTest(
"DetectBytetestTestParse10", DetectBytetestTestParse10);
1342 UtRegisterTest(
"DetectBytetestTestParse11", DetectBytetestTestParse11);
1343 UtRegisterTest(
"DetectBytetestTestParse12", DetectBytetestTestParse12);
1344 UtRegisterTest(
"DetectBytetestTestParse13", DetectBytetestTestParse13);
1345 UtRegisterTest(
"DetectBytetestTestParse14", DetectBytetestTestParse14);
1346 UtRegisterTest(
"DetectBytetestTestParse15", DetectBytetestTestParse15);
1347 UtRegisterTest(
"DetectBytetestTestParse16", DetectBytetestTestParse16);
1348 UtRegisterTest(
"DetectBytetestTestParse17", DetectBytetestTestParse17);
1349 UtRegisterTest(
"DetectBytetestTestParse18", DetectBytetestTestParse18);
1350 UtRegisterTest(
"DetectBytetestTestParse19", DetectBytetestTestParse19);
1351 UtRegisterTest(
"DetectBytetestTestParse20", DetectBytetestTestParse20);
1352 UtRegisterTest(
"DetectBytetestTestParse21", DetectBytetestTestParse21);
1353 UtRegisterTest(
"DetectBytetestTestParse22", DetectBytetestTestParse22);
1354 UtRegisterTest(
"DetectBytetestTestParse23", DetectBytetestTestParse23);
1355 UtRegisterTest(
"DetectBytetestTestParse24", DetectBytetestTestParse24);