Skip to content

Commit

Permalink
detect/byte_extract: Use Rust keyword parser
Browse files Browse the repository at this point in the history
Issue: 6831

Modifications to use the Rust keyword parser for byte_extract.
  • Loading branch information
jlucovsky committed Mar 13, 2024
1 parent 37ff972 commit 8a92ee4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 44 deletions.
55 changes: 14 additions & 41 deletions src/detect-byte-extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
*/

#include "suricata-common.h"
#include "threads.h"
#include "decode.h"

#include "detect.h"
#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-mpm.h"
#include "detect-engine-state.h"
#include "detect-content.h"
#include "detect-pcre.h"
#include "detect-bytejump.h"
Expand All @@ -42,25 +38,20 @@

#include "app-layer-protos.h"

#include "flow.h"
#include "flow-var.h"
#include "flow-util.h"

#include "util-byte.h"
#include "util-debug.h"
#include "util-unittest.h"
#include "util-unittest-helper.h"
#include "util-spm.h"

/* the default value of endianness to be used, if none's specified */
#define DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT DETECT_BYTE_EXTRACT_ENDIAN_BIG
#define DETECT_BYTE_EXTRACT_ENDIAN_DEFAULT (uint8_t) BigEndian

/* the base to be used if string mode is specified. These options would be
* specified in DetectByteParseData->base */
#define DETECT_BYTE_EXTRACT_BASE_NONE 0
#define DETECT_BYTE_EXTRACT_BASE_HEX 16
#define DETECT_BYTE_EXTRACT_BASE_DEC 10
#define DETECT_BYTE_EXTRACT_BASE_OCT 8
#define DETECT_BYTE_EXTRACT_BASE_HEX (uint8_t) BaseHex
#define DETECT_BYTE_EXTRACT_BASE_DEC (uint8_t) BaseDec
#define DETECT_BYTE_EXTRACT_BASE_OCT (uint8_t) BaseOct

/* the default value for multiplier. Either ways we always store a
* multiplier, 1 or otherwise, so that we can always multiply the extracted
Expand All @@ -78,19 +69,6 @@
/* the max no of bytes that can be extracted in non-string mode */
#define NO_STRING_MAX_BYTES_TO_EXTRACT 8

#define PARSE_REGEX "^" \
"\\s*([0-9]+)\\s*" \
",\\s*(-?[0-9]+)\\s*" \
",\\s*([^\\s,]+)\\s*" \
"(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
"(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
"(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
"(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
"(?:(?:,\\s*([^\\s,]+)\\s*)|(?:,\\s*([^\\s,]+)\\s+([^\\s,]+)\\s*))?" \
"$"

static DetectParseRegex parse_regex;

static int DetectByteExtractSetup(DetectEngineCtx *, Signature *, const char *);
#ifdef UNITTESTS
static void DetectByteExtractRegisterTests(void);
Expand All @@ -111,26 +89,22 @@ void DetectByteExtractRegister(void)
#ifdef UNITTESTS
sigmatch_table[DETECT_BYTE_EXTRACT].RegisterTests = DetectByteExtractRegisterTests;
#endif
DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
}

int DetectByteExtractDoMatch(DetectEngineThreadCtx *det_ctx, const SigMatchData *smd,
const Signature *s, const uint8_t *payload, uint32_t payload_len, uint64_t *value,
uint8_t endian)
{
DetectByteExtractData *data = (DetectByteExtractData *)smd->ctx;
const uint8_t *ptr = NULL;
int32_t len = 0;
uint64_t val = 0;
int extbytes;

if (payload_len == 0) {
return 0;
}

/* Calculate the ptr value for the bytetest and length remaining in
* the packet from that point.
*/
const uint8_t *ptr;
int32_t len;
DetectByteExtractData *data = (DetectByteExtractData *)smd->ctx;
if (data->flags & DETECT_BYTE_EXTRACT_FLAG_RELATIVE) {
SCLogDebug("relative, working with det_ctx->buffer_offset %"PRIu32", "
"data->offset %"PRIu32"", det_ctx->buffer_offset, data->offset);
Expand Down Expand Up @@ -161,6 +135,8 @@ int DetectByteExtractDoMatch(DetectEngineThreadCtx *det_ctx, const SigMatchData
}

/* Extract the byte data */
uint64_t val = 0;
int extbytes;
if (data->flags & DETECT_BYTE_EXTRACT_FLAG_STRING) {
extbytes = ByteExtractStringUint64(&val, data->base,
data->nbytes, (const char *)ptr);
Expand Down Expand Up @@ -221,6 +197,10 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
goto error;
}

if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_SLICE) {
SCLogError("byte_extract slice not yet supported");
goto error;
}
if (bed->flags & DETECT_BYTE_EXTRACT_FLAG_STRING) {
if (bed->base == DETECT_BYTE_EXTRACT_BASE_OCT) {
/* if are dealing with octal nos, the max no that can fit in a 8
Expand Down Expand Up @@ -393,14 +373,7 @@ static int DetectByteExtractSetup(DetectEngineCtx *de_ctx, Signature *s, const c
*/
static void DetectByteExtractFree(DetectEngineCtx *de_ctx, void *ptr)
{
if (ptr != NULL) {
DetectByteExtractData *bed = ptr;
if (bed->name != NULL)
SCFree((void *)bed->name);
SCFree(bed);
}

return;
ScByteExtractFree(ptr);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/detect-byte-extract.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

/* endian value to be used. Would be stored in DetectByteParseData->endian */
#define DETECT_BYTE_EXTRACT_ENDIAN_NONE 0
#define DETECT_BYTE_EXTRACT_ENDIAN_BIG 1
#define DETECT_BYTE_EXTRACT_ENDIAN_LITTLE 2
#define DETECT_BYTE_EXTRACT_ENDIAN_DCE 3
#define DETECT_BYTE_EXTRACT_ENDIAN_BIG (uint8_t) BigEndian
#define DETECT_BYTE_EXTRACT_ENDIAN_LITTLE (uint8_t) LittleEndian
#define DETECT_BYTE_EXTRACT_ENDIAN_DCE (uint8_t) EndianDCE

void DetectByteExtractRegister(void);

Expand Down

0 comments on commit 8a92ee4

Please sign in to comment.