42 #include "rust-bindings.h"
61 #define DETECT_BYTEMATH_ENDIAN_DEFAULT (uint8_t) BigEndian
62 #define DETECT_BYTEMATH_BASE_DEFAULT (uint8_t) BaseDec
64 static void DetectByteMathRegisterTests(
void);
82 static inline bool DetectByteMathValidateNbytesOnly(
const DetectByteMathData *data, int32_t nbytes)
85 (((data->flags & DETECT_BYTEMATH_FLAG_STRING) && nbytes <= 10) || (nbytes <= 4));
90 uint64_t rvalue, uint64_t *value, uint8_t endian)
96 if (!DetectByteMathValidateNbytesOnly(data, nbytes)) {
108 if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
109 SCLogDebug(
"relative, working with det_ctx->buffer_offset %" PRIu32
", "
110 "data->offset %" PRIi32
"",
124 SCLogDebug(
"absolute, data->offset %" PRIi32
"", data->offset);
126 ptr = payload + data->offset;
131 if (ptr < payload || nbytes >
len) {
132 SCLogDebug(
"Data not within payload pkt=%p, ptr=%p, len=%" PRIu32
", nbytes=%d", payload,
138 if (data->flags & DETECT_BYTEMATH_FLAG_STRING) {
145 SCLogDebug(
"error extracting %d bytes of string data: %d", nbytes, extbytes);
150 ByteEndian bme = endian;
153 if (extbytes != nbytes) {
154 SCLogDebug(
"error extracting %d bytes of numeric data: %d", nbytes, extbytes);
163 switch (data->oper) {
196 if (data->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
197 val &= data->bitmask_val;
198 if (val && data->bitmask_shift_count) {
199 val = val >> data->bitmask_shift_count;
218 static DetectByteMathData *DetectByteMathParse(
221 DetectByteMathData *bmd;
222 if ((bmd = SCByteMathParse(arg)) == NULL) {
227 if (bmd->nbytes_str) {
228 if (nbytes == NULL) {
230 "var name for nbytes. \"nbytes\" argument supplied to "
231 "this function must be non-NULL");
234 *nbytes =
SCStrdup(bmd->nbytes_str);
235 if (*nbytes == NULL) {
240 if (bmd->rvalue_str) {
241 if (rvalue == NULL) {
243 "var name for rvalue. \"rvalue\" argument supplied to "
244 "this function must be non-NULL");
247 *rvalue =
SCStrdup(bmd->rvalue_str);
248 if (*rvalue == NULL) {
253 if (bmd->flags & DETECT_BYTEMATH_FLAG_BITMASK) {
254 if (bmd->bitmask_val) {
255 uint32_t bmask = bmd->bitmask_val;
256 while (!(bmask & 0x1)){
258 bmd->bitmask_shift_count++;
267 DetectByteMathFree(
de_ctx, bmd);
285 DetectByteMathData *data;
290 data = DetectByteMathParse(
de_ctx, arg, &nbytes, &rvalue);
301 if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
305 "previous pattern match");
309 }
else if (data->endian == EndianDCE) {
310 if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
316 if (prev_pm == NULL) {
330 }
else if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
335 if (prev_pm == NULL) {
347 if (data->endian == EndianDCE) {
351 if ((data->flags & DETECT_BYTEMATH_FLAG_STRING) || (data->base == BaseDec) ||
352 (data->base == BaseHex) || (data->base == BaseOct)) {
354 "A bytemath keyword with dce holds other invalid modifiers.");
359 if (nbytes != NULL) {
362 SCLogError(
"unknown byte_ keyword var seen in byte_math - %s", nbytes);
365 data->nbytes = index;
366 data->flags |= DETECT_BYTEMATH_FLAG_NBYTES_VAR;
371 if (rvalue != NULL) {
374 SCLogError(
"unknown byte_ keyword var seen in byte_math - %s", rvalue);
377 data->rvalue = index;
378 data->flags |= DETECT_BYTEMATH_FLAG_RVALUE_VAR;
385 if (prev_bmd_sm == NULL) {
388 data->local_id = ((DetectByteMathData *)prev_bmd_sm->
ctx)->local_id + 1;
398 if (!(data->flags & DETECT_BYTEMATH_FLAG_RELATIVE))
420 DetectByteMathFree(
de_ctx, data);
448 const DetectByteMathData *bmd = (
const DetectByteMathData *)sm->
ctx;
449 if (strcmp(bmd->result, arg) == 0) {
462 const DetectByteMathData *bmd = (
const DetectByteMathData *)sm->
ctx;
463 if (strcmp(bmd->result, arg) == 0) {
479 static int DetectByteMathParseTest01(
void)
482 DetectByteMathData *bmd = DetectByteMathParse(NULL,
483 "bytes 4, offset 2, oper +,"
484 "rvalue 10, result bar",
496 DetectByteMathFree(NULL, bmd);
501 static int DetectByteMathParseTest02(
void)
504 DetectByteMathData *bmd = DetectByteMathParse(NULL,
505 "bytes 257, offset 2, oper +, "
506 "rvalue 39, result bar",
514 static int DetectByteMathParseTest03(
void)
517 DetectByteMathData *bmd = DetectByteMathParse(NULL,
518 "bytes 11, offset 2, oper +, "
519 "rvalue 39, result bar",
526 static int DetectByteMathParseTest04(
void)
529 DetectByteMathData *bmd = DetectByteMathParse(NULL,
530 "bytes 4, offset 70000, oper +,"
531 " rvalue 39, result bar",
539 static int DetectByteMathParseTest05(
void)
542 DetectByteMathData *bmd = DetectByteMathParse(NULL,
543 "bytes 11, offset 16, oper &,"
544 "rvalue 39, result bar",
551 static int DetectByteMathParseTest06(
void)
553 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE;
556 DetectByteMathData *bmd = DetectByteMathParse(NULL,
557 "bytes 4, offset 0, oper +,"
558 "rvalue 248, result var, relative",
571 DetectByteMathFree(NULL, bmd);
576 static int DetectByteMathParseTest07(
void)
580 DetectByteMathData *bmd = DetectByteMathParse(NULL,
581 "bytes 4, offset 2, oper +,"
582 "rvalue foo, result bar",
593 DetectByteMathFree(NULL, bmd);
600 static int DetectByteMathParseTest08(
void)
603 DetectByteMathData *bmd = DetectByteMathParse(NULL,
604 "bytes 4, offset 2, oper +,"
605 "rvalue foo, result bar",
612 static int DetectByteMathParseTest09(
void)
614 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE;
616 DetectByteMathData *bmd = DetectByteMathParse(NULL,
617 "bytes 4, offset 2, oper +,"
618 "rvalue 39, result bar, relative",
631 DetectByteMathFree(NULL, bmd);
636 static int DetectByteMathParseTest10(
void)
638 uint8_t
flags = DETECT_BYTEMATH_FLAG_ENDIAN;
640 DetectByteMathData *bmd = DetectByteMathParse(NULL,
641 "bytes 4, offset 2, oper +,"
642 "rvalue 39, result bar, endian"
656 DetectByteMathFree(NULL, bmd);
661 static int DetectByteMathParseTest11(
void)
663 uint8_t
flags = DETECT_BYTEMATH_FLAG_ENDIAN;
665 DetectByteMathData *bmd = DetectByteMathParse(NULL,
666 "bytes 4, offset 2, oper +, "
667 "rvalue 39, result bar, dce",
680 DetectByteMathFree(NULL, bmd);
685 static int DetectByteMathParseTest12(
void)
687 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE | DETECT_BYTEMATH_FLAG_STRING;
689 DetectByteMathData *bmd = DetectByteMathParse(NULL,
690 "bytes 4, offset 2, oper +,"
691 "rvalue 39, result bar, "
692 "relative, string dec",
705 DetectByteMathFree(NULL, bmd);
710 static int DetectByteMathParseTest13(
void)
712 uint8_t
flags = DETECT_BYTEMATH_FLAG_STRING |
713 DETECT_BYTEMATH_FLAG_RELATIVE |
714 DETECT_BYTEMATH_FLAG_BITMASK;
716 DetectByteMathData *bmd = DetectByteMathParse(NULL,
717 "bytes 4, offset 2, oper +, "
718 "rvalue 39, result bar, "
719 "relative, string dec, bitmask "
735 DetectByteMathFree(NULL, bmd);
741 static int DetectByteMathParseTest14(
void)
744 DetectByteMathData *bmd = DetectByteMathParse(NULL,
745 "bytes 4, offset 2, oper +,"
754 static int DetectByteMathParseTest15(
void)
758 DetectByteMathData *bmd = DetectByteMathParse(NULL,
759 "bytes 4, offset 2, oper +, "
768 static int DetectByteMathParseTest16(
void)
770 uint8_t
flags = DETECT_BYTEMATH_FLAG_STRING | DETECT_BYTEMATH_FLAG_RELATIVE |
771 DETECT_BYTEMATH_FLAG_BITMASK;
773 DetectByteMathData *bmd = DetectByteMathParse(NULL,
774 "bytes 4, offset -2, oper +, "
775 "rvalue 39, result bar, "
776 "relative, string dec, bitmask "
792 DetectByteMathFree(NULL, bmd);
797 static int DetectByteMathPacket01(
void)
799 uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01,
800 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
801 0x00, 0x00, 0x6d, 0x00, 0x01, 0x00 };
803 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;
912 memset(&f, 0,
sizeof(
Flow));
914 p =
UTHBuildPacketReal(buf,
sizeof(buf), IPPROTO_UDP,
"192.168.1.5",
"192.168.1.1", 41424, 53);
919 f.
proto = IPPROTO_UDP;
941 "alert udp any any -> any any "
942 "(byte_extract: 1, 0, extracted_val, relative;"
943 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
944 "byte_test: 2, =, var, 13;"
945 "msg:\"Byte extract and byte math with byte test verification\";"
951 "alert udp any any -> any any "
952 "(byte_extract: 1, 0, extracted_val, relative;"
953 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
954 "byte_test: 2, !=, var, 13;"
955 "msg:\"Byte extract and byte math with byte test verification\";"
964 "alert udp any any -> any any "
965 "(byte_extract: 1, 0, extracted_val, relative;"
966 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
967 "byte_test: 2, <, var, 15;"
968 "msg:\"Byte extract and byte math with byte test verification\";"
1000 static int DetectByteMathContext01(
void)
1006 DetectByteMathData *bmd = NULL;
1013 "(msg:\"Testing bytemath_body\"; "
1014 "content:\"|00 04 93 F3|\"; "
1015 "content:\"|00 00 00 07|\"; distance:4; within:4;"
1016 "byte_math:bytes 4, offset 0, oper +, rvalue "
1017 "248, result var, relative; sid:1;)");
1037 bmd = (DetectByteMathData *)sm->
ctx;
1042 FAIL_IF_NOT(bmd->flags == DETECT_BYTEMATH_FLAG_RELATIVE);
1052 static void DetectByteMathRegisterTests(
void)
1054 UtRegisterTest(
"DetectByteMathParseTest01", DetectByteMathParseTest01);
1055 UtRegisterTest(
"DetectByteMathParseTest02", DetectByteMathParseTest02);
1056 UtRegisterTest(
"DetectByteMathParseTest03", DetectByteMathParseTest03);
1057 UtRegisterTest(
"DetectByteMathParseTest04", DetectByteMathParseTest04);
1058 UtRegisterTest(
"DetectByteMathParseTest05", DetectByteMathParseTest05);
1059 UtRegisterTest(
"DetectByteMathParseTest06", DetectByteMathParseTest06);
1060 UtRegisterTest(
"DetectByteMathParseTest07", DetectByteMathParseTest07);
1061 UtRegisterTest(
"DetectByteMathParseTest08", DetectByteMathParseTest08);
1062 UtRegisterTest(
"DetectByteMathParseTest09", DetectByteMathParseTest09);
1063 UtRegisterTest(
"DetectByteMathParseTest10", DetectByteMathParseTest10);
1064 UtRegisterTest(
"DetectByteMathParseTest11", DetectByteMathParseTest11);
1065 UtRegisterTest(
"DetectByteMathParseTest12", DetectByteMathParseTest12);
1066 UtRegisterTest(
"DetectByteMathParseTest13", DetectByteMathParseTest13);
1067 UtRegisterTest(
"DetectByteMathParseTest14", DetectByteMathParseTest14);
1068 UtRegisterTest(
"DetectByteMathParseTest15", DetectByteMathParseTest15);
1069 UtRegisterTest(
"DetectByteMathParseTest16", DetectByteMathParseTest16);
1070 UtRegisterTest(
"DetectByteMathPacket01", DetectByteMathPacket01);
1071 UtRegisterTest(
"DetectByteMathPacket02", DetectByteMathPacket02);
1072 UtRegisterTest(
"DetectByteMathContext01", DetectByteMathContext01);