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) {
318 if (prev_pm == NULL) {
332 }
else if (data->flags & DETECT_BYTEMATH_FLAG_RELATIVE) {
337 if (prev_pm == NULL) {
349 if (data->endian == EndianDCE) {
353 if ((data->flags & DETECT_BYTEMATH_FLAG_STRING) || (data->base == BaseDec) ||
354 (data->base == BaseHex) || (data->base == BaseOct)) {
356 "A bytemath keyword with dce holds other invalid modifiers.");
361 if (nbytes != NULL) {
364 SCLogError(
"unknown byte_ keyword var seen in byte_math - %s", nbytes);
367 data->nbytes = index;
368 data->flags |= DETECT_BYTEMATH_FLAG_NBYTES_VAR;
373 if (rvalue != NULL) {
376 SCLogError(
"unknown byte_ keyword var seen in byte_math - %s", rvalue);
379 data->rvalue = index;
380 data->flags |= DETECT_BYTEMATH_FLAG_RVALUE_VAR;
387 if (prev_bmd_sm == NULL) {
390 data->local_id = ((DetectByteMathData *)prev_bmd_sm->
ctx)->local_id + 1;
401 if (!(data->flags & DETECT_BYTEMATH_FLAG_RELATIVE))
423 DetectByteMathFree(
de_ctx, data);
451 const DetectByteMathData *bmd = (
const DetectByteMathData *)sm->
ctx;
452 if (strcmp(bmd->result, arg) == 0) {
466 const DetectByteMathData *bmd = (
const DetectByteMathData *)sm->
ctx;
467 if (strcmp(bmd->result, arg) == 0) {
483 static int DetectByteMathParseTest01(
void)
486 DetectByteMathData *bmd = DetectByteMathParse(NULL,
487 "bytes 4, offset 2, oper +,"
488 "rvalue 10, result bar",
500 DetectByteMathFree(NULL, bmd);
505 static int DetectByteMathParseTest02(
void)
508 DetectByteMathData *bmd = DetectByteMathParse(NULL,
509 "bytes 257, offset 2, oper +, "
510 "rvalue 39, result bar",
518 static int DetectByteMathParseTest03(
void)
521 DetectByteMathData *bmd = DetectByteMathParse(NULL,
522 "bytes 11, offset 2, oper +, "
523 "rvalue 39, result bar",
530 static int DetectByteMathParseTest04(
void)
533 DetectByteMathData *bmd = DetectByteMathParse(NULL,
534 "bytes 4, offset 70000, oper +,"
535 " rvalue 39, result bar",
543 static int DetectByteMathParseTest05(
void)
546 DetectByteMathData *bmd = DetectByteMathParse(NULL,
547 "bytes 11, offset 16, oper &,"
548 "rvalue 39, result bar",
555 static int DetectByteMathParseTest06(
void)
557 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE;
560 DetectByteMathData *bmd = DetectByteMathParse(NULL,
561 "bytes 4, offset 0, oper +,"
562 "rvalue 248, result var, relative",
575 DetectByteMathFree(NULL, bmd);
580 static int DetectByteMathParseTest07(
void)
584 DetectByteMathData *bmd = DetectByteMathParse(NULL,
585 "bytes 4, offset 2, oper +,"
586 "rvalue foo, result bar",
597 DetectByteMathFree(NULL, bmd);
604 static int DetectByteMathParseTest08(
void)
607 DetectByteMathData *bmd = DetectByteMathParse(NULL,
608 "bytes 4, offset 2, oper +,"
609 "rvalue foo, result bar",
616 static int DetectByteMathParseTest09(
void)
618 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE;
620 DetectByteMathData *bmd = DetectByteMathParse(NULL,
621 "bytes 4, offset 2, oper +,"
622 "rvalue 39, result bar, relative",
635 DetectByteMathFree(NULL, bmd);
640 static int DetectByteMathParseTest10(
void)
642 uint8_t
flags = DETECT_BYTEMATH_FLAG_ENDIAN;
644 DetectByteMathData *bmd = DetectByteMathParse(NULL,
645 "bytes 4, offset 2, oper +,"
646 "rvalue 39, result bar, endian"
660 DetectByteMathFree(NULL, bmd);
665 static int DetectByteMathParseTest11(
void)
667 uint8_t
flags = DETECT_BYTEMATH_FLAG_ENDIAN;
669 DetectByteMathData *bmd = DetectByteMathParse(NULL,
670 "bytes 4, offset 2, oper +, "
671 "rvalue 39, result bar, dce",
684 DetectByteMathFree(NULL, bmd);
689 static int DetectByteMathParseTest12(
void)
691 uint8_t
flags = DETECT_BYTEMATH_FLAG_RELATIVE | DETECT_BYTEMATH_FLAG_STRING;
693 DetectByteMathData *bmd = DetectByteMathParse(NULL,
694 "bytes 4, offset 2, oper +,"
695 "rvalue 39, result bar, "
696 "relative, string dec",
709 DetectByteMathFree(NULL, bmd);
714 static int DetectByteMathParseTest13(
void)
716 uint8_t
flags = DETECT_BYTEMATH_FLAG_STRING |
717 DETECT_BYTEMATH_FLAG_RELATIVE |
718 DETECT_BYTEMATH_FLAG_BITMASK;
720 DetectByteMathData *bmd = DetectByteMathParse(NULL,
721 "bytes 4, offset 2, oper +, "
722 "rvalue 39, result bar, "
723 "relative, string dec, bitmask "
739 DetectByteMathFree(NULL, bmd);
745 static int DetectByteMathParseTest14(
void)
748 DetectByteMathData *bmd = DetectByteMathParse(NULL,
749 "bytes 4, offset 2, oper +,"
758 static int DetectByteMathParseTest15(
void)
762 DetectByteMathData *bmd = DetectByteMathParse(NULL,
763 "bytes 4, offset 2, oper +, "
772 static int DetectByteMathParseTest16(
void)
774 uint8_t
flags = DETECT_BYTEMATH_FLAG_STRING | DETECT_BYTEMATH_FLAG_RELATIVE |
775 DETECT_BYTEMATH_FLAG_BITMASK;
777 DetectByteMathData *bmd = DetectByteMathParse(NULL,
778 "bytes 4, offset -2, oper +, "
779 "rvalue 39, result bar, "
780 "relative, string dec, bitmask "
796 DetectByteMathFree(NULL, bmd);
801 static int DetectByteMathPacket01(
void)
803 uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01,
804 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
805 0x00, 0x00, 0x6d, 0x00, 0x01, 0x00 };
807 void *dns_state = NULL;
815 memset(&f, 0,
sizeof(
Flow));
818 "192.168.1.5",
"192.168.1.1",
824 f.
proto = IPPROTO_UDP;
846 "(byte_extract: 1, 0, extracted_val, relative;"
847 "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
848 "byte_test: 2, =, var, 13;"
849 "msg:\"Byte extract and byte math with byte test verification\";"
855 "(byte_extract: 1, 0, extracted_val, relative;"
856 "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
857 "byte_test: 2, !=, var, 13;"
858 "msg:\"Byte extract and byte math with byte test verification\";"
867 "(byte_extract: 1, 0, extracted_val, relative;"
868 "byte_math: bytes 1, offset 1, oper +, rvalue extracted_val, result var;"
869 "byte_test: 2, <, var, 15;"
870 "msg:\"Byte extract and byte math with byte test verification\";"
879 STREAM_TOSERVER, buf,
sizeof(buf));
903 static int DetectByteMathPacket02(
void)
905 uint8_t buf[] = { 0x38, 0x35, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
906 0x00, 0x70, 0x00, 0x01, 0x00 };
908 void *dns_state = NULL;
916 memset(&f, 0,
sizeof(
Flow));
918 p =
UTHBuildPacketReal(buf,
sizeof(buf), IPPROTO_UDP,
"192.168.1.5",
"192.168.1.1", 41424, 53);
923 f.
proto = IPPROTO_UDP;
945 "alert udp any any -> any any "
946 "(byte_extract: 1, 0, extracted_val, relative;"
947 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
948 "byte_test: 2, =, var, 13;"
949 "msg:\"Byte extract and byte math with byte test verification\";"
955 "alert udp any any -> any any "
956 "(byte_extract: 1, 0, extracted_val, relative;"
957 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
958 "byte_test: 2, !=, var, 13;"
959 "msg:\"Byte extract and byte math with byte test verification\";"
968 "alert udp any any -> any any "
969 "(byte_extract: 1, 0, extracted_val, relative;"
970 "byte_math: bytes 1, offset -1, oper +, rvalue extracted_val, result var, relative;"
971 "byte_test: 2, <, var, 15;"
972 "msg:\"Byte extract and byte math with byte test verification\";"
1004 static int DetectByteMathContext01(
void)
1010 DetectByteMathData *bmd = NULL;
1017 "(msg:\"Testing bytemath_body\"; "
1018 "content:\"|00 04 93 F3|\"; "
1019 "content:\"|00 00 00 07|\"; distance:4; within:4;"
1020 "byte_math:bytes 4, offset 0, oper +, rvalue "
1021 "248, result var, relative; sid:1;)");
1041 bmd = (DetectByteMathData *)sm->
ctx;
1046 FAIL_IF_NOT(bmd->flags == DETECT_BYTEMATH_FLAG_RELATIVE);
1056 static void DetectByteMathRegisterTests(
void)
1058 UtRegisterTest(
"DetectByteMathParseTest01", DetectByteMathParseTest01);
1059 UtRegisterTest(
"DetectByteMathParseTest02", DetectByteMathParseTest02);
1060 UtRegisterTest(
"DetectByteMathParseTest03", DetectByteMathParseTest03);
1061 UtRegisterTest(
"DetectByteMathParseTest04", DetectByteMathParseTest04);
1062 UtRegisterTest(
"DetectByteMathParseTest05", DetectByteMathParseTest05);
1063 UtRegisterTest(
"DetectByteMathParseTest06", DetectByteMathParseTest06);
1064 UtRegisterTest(
"DetectByteMathParseTest07", DetectByteMathParseTest07);
1065 UtRegisterTest(
"DetectByteMathParseTest08", DetectByteMathParseTest08);
1066 UtRegisterTest(
"DetectByteMathParseTest09", DetectByteMathParseTest09);
1067 UtRegisterTest(
"DetectByteMathParseTest10", DetectByteMathParseTest10);
1068 UtRegisterTest(
"DetectByteMathParseTest11", DetectByteMathParseTest11);
1069 UtRegisterTest(
"DetectByteMathParseTest12", DetectByteMathParseTest12);
1070 UtRegisterTest(
"DetectByteMathParseTest13", DetectByteMathParseTest13);
1071 UtRegisterTest(
"DetectByteMathParseTest14", DetectByteMathParseTest14);
1072 UtRegisterTest(
"DetectByteMathParseTest15", DetectByteMathParseTest15);
1073 UtRegisterTest(
"DetectByteMathParseTest16", DetectByteMathParseTest16);
1074 UtRegisterTest(
"DetectByteMathPacket01", DetectByteMathPacket01);
1075 UtRegisterTest(
"DetectByteMathPacket02", DetectByteMathPacket02);
1076 UtRegisterTest(
"DetectByteMathContext01", DetectByteMathContext01);