61 #define DETECT_BYTEMATH_ENDIAN_DEFAULT (uint8_t) BigEndian
62 #define DETECT_BYTEMATH_BASE_DEFAULT (uint8_t) BaseDec
64 static void DetectByteMathRegisterTests(
void);
84 static inline bool DetectByteMathValidateNbytesOnly(
const DetectByteMathData *data, int32_t nbytes)
87 (((data->flags & DETECT_BYTEMATH_FLAG_STRING) && nbytes <= 10) || (nbytes <= 4));
92 uint64_t rvalue, uint64_t *value, uint8_t endian)
98 if (!DetectByteMathValidateNbytesOnly(data, nbytes)) {
110 if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
111 SCLogDebug(
"relative, working with det_ctx->buffer_offset %" PRIu32
", "
112 "data->offset %" PRIi32
"",
126 SCLogDebug(
"absolute, data->offset %" PRIi32
"", data->offset);
128 ptr = payload + data->offset;
133 if (ptr < payload || nbytes >
len) {
134 SCLogDebug(
"Data not within payload pkt=%p, ptr=%p, len=%" PRIu32
", nbytes=%d", payload,
140 if (data->flags & DETECT_BYTEMATH_FLAG_STRING) {
147 SCLogDebug(
"error extracting %d bytes of string data: %d", nbytes, extbytes);
152 ByteEndian bme = endian;
155 if (extbytes != nbytes) {
156 SCLogDebug(
"error extracting %d bytes of numeric data: %d", nbytes, extbytes);
165 switch (data->oper) {
198 if (data->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
199 val &= data->bitmask_val;
200 if (val && data->bitmask_shift_count) {
201 val = val >> data->bitmask_shift_count;
220 static DetectByteMathData *DetectByteMathParse(
223 DetectByteMathData *bmd;
224 if ((bmd = SCByteMathParse(arg)) == NULL) {
229 if (bmd->nbytes_str) {
230 if (nbytes == NULL) {
232 "var name for nbytes. \"nbytes\" argument supplied to "
233 "this function must be non-NULL");
236 *nbytes =
SCStrdup(bmd->nbytes_str);
237 if (*nbytes == NULL) {
242 if (bmd->rvalue_str) {
243 if (rvalue == NULL) {
245 "var name for rvalue. \"rvalue\" argument supplied to "
246 "this function must be non-NULL");
249 *rvalue =
SCStrdup(bmd->rvalue_str);
250 if (*rvalue == NULL) {
255 if (bmd->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
256 if (bmd->bitmask_val) {
257 uint32_t bmask = bmd->bitmask_val;
258 while (!(bmask & 0x1)){
260 bmd->bitmask_shift_count++;
269 DetectByteMathFree(
de_ctx, bmd);
287 DetectByteMathData *data;
292 data = DetectByteMathParse(
de_ctx, arg, &nbytes, &rvalue);
303 if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
307 "previous pattern match");
311 }
else if (data->endian == EndianDCE) {
312 if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
315 if (prev_pm == NULL) {
329 }
else if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
332 if (prev_pm == NULL) {
344 if (data->endian == EndianDCE) {
348 if ((data->flags & DETECT_BYTEMATH_FLAG_STRING) || (data->base == BaseDec) ||
349 (data->base == BaseHex) || (data->base == BaseOct)) {
351 "A bytemath keyword with dce holds other invalid modifiers.");
356 if (nbytes != NULL) {
359 SCLogError(
"unknown byte_ keyword var seen in byte_math - %s", nbytes);
362 data->nbytes = index;
363 data->flags |= DETECT_BYTEMATH_FLAG_NBYTES_VAR;
368 if (rvalue != NULL) {
371 SCLogError(
"unknown byte_ keyword var seen in byte_math - %s", rvalue);
374 data->rvalue = index;
375 data->flags |= DETECT_BYTEMATH_FLAG_RVALUE_VAR;
382 if (prev_bmd_sm == NULL) {
385 data->local_id = ((DetectByteMathData *)prev_bmd_sm->
ctx)->local_id + 1;
396 if (!(data->flags & DETECT_BYTEMATH_FLAG_RELATIVE))
418 DetectByteMathFree(
de_ctx, data);
446 const DetectByteMathData *bmd = (
const DetectByteMathData *)sm->
ctx;
447 if (strcmp(bmd->result, arg) == 0) {
461 const DetectByteMathData *bmd = (
const DetectByteMathData *)sm->
ctx;
462 if (strcmp(bmd->result, arg) == 0) {
478 static int DetectByteMathParseTest01(
void)
481 DetectByteMathData *bmd = DetectByteMathParse(NULL,
482 "bytes 4, offset 2, oper +,"
483 "rvalue 10, result bar",
495 DetectByteMathFree(NULL, bmd);
500 static int DetectByteMathParseTest02(
void)
503 DetectByteMathData *bmd = DetectByteMathParse(NULL,
504 "bytes 257, offset 2, oper +, "
505 "rvalue 39, result bar",
513 static int DetectByteMathParseTest03(
void)
516 DetectByteMathData *bmd = DetectByteMathParse(NULL,
517 "bytes 11, offset 2, oper +, "
518 "rvalue 39, result bar",
525 static int DetectByteMathParseTest04(
void)
528 DetectByteMathData *bmd = DetectByteMathParse(NULL,
529 "bytes 4, offset 70000, oper +,"
530 " rvalue 39, result bar",
538 static int DetectByteMathParseTest05(
void)
541 DetectByteMathData *bmd = DetectByteMathParse(NULL,
542 "bytes 11, offset 16, oper &,"
543 "rvalue 39, result bar",
550 static int DetectByteMathParseTest06(
void)
552 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE;
555 DetectByteMathData *bmd = DetectByteMathParse(NULL,
556 "bytes 4, offset 0, oper +,"
557 "rvalue 248, result var, relative",
570 DetectByteMathFree(NULL, bmd);
575 static int DetectByteMathParseTest07(
void)
579 DetectByteMathData *bmd = DetectByteMathParse(NULL,
580 "bytes 4, offset 2, oper +,"
581 "rvalue foo, result bar",
592 DetectByteMathFree(NULL, bmd);
599 static int DetectByteMathParseTest08(
void)
602 DetectByteMathData *bmd = DetectByteMathParse(NULL,
603 "bytes 4, offset 2, oper +,"
604 "rvalue foo, result bar",
611 static int DetectByteMathParseTest09(
void)
613 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE;
615 DetectByteMathData *bmd = DetectByteMathParse(NULL,
616 "bytes 4, offset 2, oper +,"
617 "rvalue 39, result bar, relative",
630 DetectByteMathFree(NULL, bmd);
635 static int DetectByteMathParseTest10(
void)
637 uint8_t
flags = DETECT_BYTEMATH_FLAG_ENDIAN;
639 DetectByteMathData *bmd = DetectByteMathParse(NULL,
640 "bytes 4, offset 2, oper +,"
641 "rvalue 39, result bar, endian"
655 DetectByteMathFree(NULL, bmd);
660 static int DetectByteMathParseTest11(
void)
662 uint8_t
flags = DETECT_BYTEMATH_FLAG_ENDIAN;
664 DetectByteMathData *bmd = DetectByteMathParse(NULL,
665 "bytes 4, offset 2, oper +, "
666 "rvalue 39, result bar, dce",
679 DetectByteMathFree(NULL, bmd);
684 static int DetectByteMathParseTest12(
void)
686 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE | DETECT_BYTEMATH_FLAG_STRING;
688 DetectByteMathData *bmd = DetectByteMathParse(NULL,
689 "bytes 4, offset 2, oper +,"
690 "rvalue 39, result bar, "
691 "relative, string dec",
704 DetectByteMathFree(NULL, bmd);
709 static int DetectByteMathParseTest13(
void)
711 uint8_t
flags = DETECT_BYTEMATH_FLAG_STRING |
712 DETECT_BYTEMATH_FLAG_RELATIVE |
713 DETECT_BYTEMATH_FLAG_BITMASK;
715 DetectByteMathData *bmd = DetectByteMathParse(NULL,
716 "bytes 4, offset 2, oper +, "
717 "rvalue 39, result bar, "
718 "relative, string dec, bitmask "
734 DetectByteMathFree(NULL, bmd);
740 static int DetectByteMathParseTest14(
void)
743 DetectByteMathData *bmd = DetectByteMathParse(NULL,
744 "bytes 4, offset 2, oper +,"
753 static int DetectByteMathParseTest15(
void)
757 DetectByteMathData *bmd = DetectByteMathParse(NULL,
758 "bytes 4, offset 2, oper +, "
767 static int DetectByteMathParseTest16(
void)
769 uint8_t
flags = DETECT_BYTEMATH_FLAG_STRING | DETECT_BYTEMATH_FLAG_RELATIVE |
770 DETECT_BYTEMATH_FLAG_BITMASK;
772 DetectByteMathData *bmd = DetectByteMathParse(NULL,
773 "bytes 4, offset -2, oper +, "
774 "rvalue 39, result bar, "
775 "relative, string dec, bitmask "
791 DetectByteMathFree(NULL, bmd);
796 static int DetectByteMathPacket01(
void)
798 uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01,
799 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
800 0x00, 0x00, 0x6d, 0x00, 0x01, 0x00 };
802 void *dns_state = NULL;
811 memset(&f, 0,
sizeof(
Flow));
814 "192.168.1.5",
"192.168.1.1",
820 f.
proto = IPPROTO_UDP;
842 "(byte_extract: 1, 0, extracted_val, relative;"
843 "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
844 "byte_test: 2, =, var, 13;"
845 "msg:\"Byte extract and byte math with byte test verification\";"
851 "(byte_extract: 1, 0, extracted_val, relative;"
852 "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
853 "byte_test: 2, !=, var, 13;"
854 "msg:\"Byte extract and byte math with byte test verification\";"
863 "(byte_extract: 1, 0, extracted_val, relative;"
864 "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
865 "byte_test: 2, <, var, 15;"
866 "msg:\"Byte extract and byte math with byte test verification\";"
875 STREAM_TOSERVER, buf,
sizeof(buf));
899 static int DetectByteMathPacket02(
void)
901 uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
902 0x00, 0x70, 0x00, 0x01, 0x00 };
904 void *dns_state = NULL;
913 memset(&f, 0,
sizeof(
Flow));
915 p =
UTHBuildPacketReal(buf,
sizeof(buf), IPPROTO_UDP,
"192.168.1.5",
"192.168.1.1", 41424, 53);
920 f.
proto = IPPROTO_UDP;
942 "alert udp any any -> any any "
943 "(byte_extract: 1, 0, extracted_val, relative;"
944 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
945 "byte_test: 2, =, var, 13;"
946 "msg:\"Byte extract and byte math with byte test verification\";"
952 "alert udp any any -> any any "
953 "(byte_extract: 1, 0, extracted_val, relative;"
954 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
955 "byte_test: 2, !=, var, 13;"
956 "msg:\"Byte extract and byte math with byte test verification\";"
965 "alert udp any any -> any any "
966 "(byte_extract: 1, 0, extracted_val, relative;"
967 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
968 "byte_test: 2, <, var, 15;"
969 "msg:\"Byte extract and byte math with byte test verification\";"
1002 static int DetectByteMathContext01(
void)
1008 DetectByteMathData *bmd = NULL;
1015 "(msg:\"Testing bytemath_body\"; "
1016 "content:\"|00 04 93 F3|\"; "
1017 "content:\"|00 00 00 07|\"; distance:4; within:4;"
1018 "byte_math:bytes 4, offset 0, oper +, rvalue "
1019 "248, result var, relative; sid:1;)");
1039 bmd = (DetectByteMathData *)sm->
ctx;
1044 FAIL_IF_NOT(bmd->flags == DETECT_BYTEMATH_FLAG_RELATIVE);
1054 static void DetectByteMathRegisterTests(
void)
1056 UtRegisterTest(
"DetectByteMathParseTest01", DetectByteMathParseTest01);
1057 UtRegisterTest(
"DetectByteMathParseTest02", DetectByteMathParseTest02);
1058 UtRegisterTest(
"DetectByteMathParseTest03", DetectByteMathParseTest03);
1059 UtRegisterTest(
"DetectByteMathParseTest04", DetectByteMathParseTest04);
1060 UtRegisterTest(
"DetectByteMathParseTest05", DetectByteMathParseTest05);
1061 UtRegisterTest(
"DetectByteMathParseTest06", DetectByteMathParseTest06);
1062 UtRegisterTest(
"DetectByteMathParseTest07", DetectByteMathParseTest07);
1063 UtRegisterTest(
"DetectByteMathParseTest08", DetectByteMathParseTest08);
1064 UtRegisterTest(
"DetectByteMathParseTest09", DetectByteMathParseTest09);
1065 UtRegisterTest(
"DetectByteMathParseTest10", DetectByteMathParseTest10);
1066 UtRegisterTest(
"DetectByteMathParseTest11", DetectByteMathParseTest11);
1067 UtRegisterTest(
"DetectByteMathParseTest12", DetectByteMathParseTest12);
1068 UtRegisterTest(
"DetectByteMathParseTest13", DetectByteMathParseTest13);
1069 UtRegisterTest(
"DetectByteMathParseTest14", DetectByteMathParseTest14);
1070 UtRegisterTest(
"DetectByteMathParseTest15", DetectByteMathParseTest15);
1071 UtRegisterTest(
"DetectByteMathParseTest16", DetectByteMathParseTest16);
1072 UtRegisterTest(
"DetectByteMathPacket01", DetectByteMathPacket01);
1073 UtRegisterTest(
"DetectByteMathPacket02", DetectByteMathPacket02);
1074 UtRegisterTest(
"DetectByteMathContext01", DetectByteMathContext01);