Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform: luaxform transform script #11817

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ noinst_HEADERS = \
detect-transform-compress-whitespace.h \
detect-transform-dotprefix.h \
detect-transform-header-lowercase.h \
detect-transform-luaxform.h \
detect-transform-md5.h \
detect-transform-pcrexform.h \
detect-transform-sha1.h \
Expand Down Expand Up @@ -885,6 +886,7 @@ libsuricata_c_a_SOURCES = \
detect-transform-compress-whitespace.c \
detect-transform-dotprefix.c \
detect-transform-header-lowercase.c \
detect-transform-luaxform.c \
detect-transform-md5.c \
detect-transform-pcrexform.c \
detect-transform-sha1.c \
Expand Down
4 changes: 2 additions & 2 deletions src/detect-dns-answer-name.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ static InspectionBuffer *GetBuffer(DetectEngineThreadCtx *det_ctx,
uint32_t data_len = 0;

if (!SCDnsTxGetAnswerName(txv, to_client, index, &data, &data_len)) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}
InspectionBufferSetupMulti(buffer, transforms, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;
return buffer;
}
Expand Down
4 changes: 2 additions & 2 deletions src/detect-dns-query-name.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ static InspectionBuffer *GetBuffer(DetectEngineThreadCtx *det_ctx,
uint32_t data_len = 0;

if (!SCDnsTxGetQueryName(txv, to_client, index, &data, &data_len)) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}
InspectionBufferSetupMulti(buffer, transforms, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;
return buffer;
}
Expand Down
4 changes: 2 additions & 2 deletions src/detect-dns-query.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ static InspectionBuffer *DnsQueryGetData(DetectEngineThreadCtx *det_ctx,
const uint8_t *data;
uint32_t data_len;
if (SCDnsTxGetQueryName(txv, false, local_id, &data, &data_len) == 0) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}
InspectionBufferSetupMulti(buffer, transforms, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;

SCReturnPtr(buffer, "InspectionBuffer");
Expand Down
18 changes: 9 additions & 9 deletions src/detect-engine-frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ static bool SetupStreamCallbackData(struct FrameStreamData *dst, const TcpSessio

static bool BufferSetup(struct FrameStreamData *fsd, InspectionBuffer *buffer, const uint8_t *input,
const uint32_t input_len, const uint64_t input_offset);
static void BufferSetupUdp(InspectionBuffer *buffer, const Frame *frame, const Packet *p,
const DetectEngineTransforms *transforms);
static void BufferSetupUdp(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer,
const Frame *frame, const Packet *p, const DetectEngineTransforms *transforms);

void DetectRunPrefilterFrame(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p,
const Frames *frames, const Frame *frame, const AppProto alproto)
Expand Down Expand Up @@ -159,7 +159,7 @@ static void PrefilterMpmFrame(DetectEngineThreadCtx *det_ctx, const void *pectx,
if (frame->offset >= p->payload_len)
return;

BufferSetupUdp(buffer, frame, p, ctx->transforms);
BufferSetupUdp(det_ctx, buffer, frame, p, ctx->transforms);
const uint32_t data_len = buffer->inspect_len;
const uint8_t *data = buffer->inspect;

Expand Down Expand Up @@ -251,8 +251,8 @@ bool DetectRunFrameInspectRule(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, c
return false;
}

static void BufferSetupUdp(InspectionBuffer *buffer, const Frame *frame, const Packet *p,
const DetectEngineTransforms *transforms)
static void BufferSetupUdp(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer,
const Frame *frame, const Packet *p, const DetectEngineTransforms *transforms)
{
uint8_t ci_flags = DETECT_CI_FLAGS_START;
uint32_t frame_len;
Expand All @@ -275,7 +275,7 @@ static void BufferSetupUdp(InspectionBuffer *buffer, const Frame *frame, const P
AppLayerParserGetFrameNameById(p->flow->proto, p->flow->alproto, frame->type),
frame->offset, frame->type, frame->len);

InspectionBufferSetupMulti(buffer, transforms, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
buffer->inspect_offset = 0;
buffer->flags = ci_flags;
}
Expand All @@ -301,7 +301,7 @@ static int DetectFrameInspectUdp(DetectEngineThreadCtx *det_ctx,
return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;

if (!buffer->initialized)
BufferSetupUdp(buffer, frame, p, transforms);
BufferSetupUdp(det_ctx, buffer, frame, p, transforms);
DEBUG_VALIDATE_BUG_ON(!buffer->initialized);
if (buffer->inspect == NULL)
return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
Expand Down Expand Up @@ -361,7 +361,7 @@ static bool BufferSetup(struct FrameStreamData *fsd, InspectionBuffer *buffer, c
if (fo_inspect_offset >= (uint64_t)frame->len) {
SCLogDebug("data entirely past frame (%" PRIu64 " > %" PRIi64 ")",
fo_inspect_offset, frame->len);
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(fsd->det_ctx, buffer);
return false;
}

Expand All @@ -387,7 +387,7 @@ static bool BufferSetup(struct FrameStreamData *fsd, InspectionBuffer *buffer, c
}
// PrintRawDataFp(stdout, data, data_len);
SCLogDebug("fsd->transforms %p", fsd->transforms);
InspectionBufferSetupMulti(buffer, fsd->transforms, data, data_len);
InspectionBufferSetupMulti(fsd->det_ctx, buffer, fsd->transforms, data, data_len);
SCLogDebug("inspect_offset %" PRIu64, fo_inspect_offset);
buffer->inspect_offset = fo_inspect_offset;
buffer->flags = ci_flags;
Expand Down
4 changes: 2 additions & 2 deletions src/detect-engine-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ InspectionBuffer *DetectHelperGetMultiData(struct DetectEngineThreadCtx_ *det_ct
uint32_t data_len = 0;

if (!GetBuf(txv, flow_flags, index, &data, &data_len)) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}
InspectionBufferSetupMulti(buffer, transforms, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;
return buffer;
}
2 changes: 2 additions & 0 deletions src/detect-engine-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
#include "detect-transform-casechange.h"
#include "detect-transform-header-lowercase.h"
#include "detect-transform-base64.h"
#include "detect-transform-luaxform.h"

#include "util-rule-vars.h"

Expand Down Expand Up @@ -685,6 +686,7 @@ void SigTableSetup(void)
DetectTransformToUpperRegister();
DetectTransformHeaderLowercaseRegister();
DetectTransformFromBase64DecodeRegister();
DetectTransformLuaxformRegister();

DetectFileHandlerRegister();

Expand Down
1 change: 1 addition & 0 deletions src/detect-engine-register.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ enum DetectKeywordId {
DETECT_TRANSFORM_TOUPPER,
DETECT_TRANSFORM_HEADER_LOWERCASE,
DETECT_TRANSFORM_FROM_BASE64,
DETECT_TRANSFORM_LUAXFORM,

DETECT_AL_IKE_EXCH_TYPE,
DETECT_AL_IKE_SPI_INITIATOR,
Expand Down
28 changes: 17 additions & 11 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -955,14 +955,15 @@ static char DetectBufferTypeCompareIdFunc(void *data1, uint16_t len1, void *data
return map1->id == map2->id;
}

static void DetectBufferTypeFreeFunc(void *data)
static void DetectBufferTypeFreeFunc(void *ctx, void *data)
{
DetectBufferType *map = (DetectBufferType *)data;

if (map == NULL) {
if (data == NULL) {
return;
}

DetectBufferType *map = (DetectBufferType *)data;
DetectEngineCtx *de_ctx = (DetectEngineCtx *)ctx;

/* Release transformation option memory, if any */
for (int i = 0; i < map->transforms.cnt; i++) {
if (map->transforms.transforms[i].options == NULL)
Expand All @@ -972,7 +973,8 @@ static void DetectBufferTypeFreeFunc(void *data)
sigmatch_table[map->transforms.transforms[i].transform].name);
continue;
}
sigmatch_table[map->transforms.transforms[i].transform].Free(NULL, map->transforms.transforms[i].options);
sigmatch_table[map->transforms.transforms[i].transform].Free(
de_ctx, map->transforms.transforms[i].options);
}

SCFree(map);
Expand All @@ -981,7 +983,7 @@ static void DetectBufferTypeFreeFunc(void *data)
static int DetectBufferTypeInit(void)
{
BUG_ON(g_buffer_type_hash);
g_buffer_type_hash = HashListTableInit(256, DetectBufferTypeHashNameFunc,
g_buffer_type_hash = HashListTableInitWithCtx(256, DetectBufferTypeHashNameFunc,
DetectBufferTypeCompareNameFunc, DetectBufferTypeFreeFunc);
if (g_buffer_type_hash == NULL)
return -1;
Expand Down Expand Up @@ -1562,7 +1564,7 @@ void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
}

/** \brief setup the buffer empty */
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
void InspectionBufferSetupMultiEmpty(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer)
{
#ifdef DEBUG_VALIDATION
DEBUG_VALIDATE_BUG_ON(buffer->initialized);
Expand All @@ -1572,11 +1574,12 @@ void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
buffer->inspect_len = 0;
buffer->len = 0;
buffer->initialized = true;
buffer->det_ctx = det_ctx;
}

/** \brief setup the buffer with our initial data */
void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTransforms *transforms,
const uint8_t *data, const uint32_t data_len)
void InspectionBufferSetupMulti(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer,
const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
{
#ifdef DEBUG_VALIDATION
DEBUG_VALIDATE_BUG_ON(!buffer->multi);
Expand All @@ -1585,6 +1588,7 @@ void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTran
buffer->inspect_len = buffer->orig_len = data_len;
buffer->len = 0;
buffer->initialized = true;
buffer->det_ctx = det_ctx;

InspectionBufferApplyTransforms(buffer, transforms);
}
Expand All @@ -1603,6 +1607,7 @@ void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id,
#endif
det_ctx->inspect.to_clear_queue[det_ctx->inspect.to_clear_idx++] = list_id;
}
buffer->det_ctx = det_ctx;
buffer->inspect = buffer->orig = data;
buffer->inspect_len = buffer->orig_len = data_len;
buffer->len = 0;
Expand Down Expand Up @@ -1714,7 +1719,7 @@ static void DetectBufferTypeSetupDetectEngine(DetectEngineCtx *de_ctx)
const int size = g_buffer_type_id;
BUG_ON(!(size > 0));

de_ctx->buffer_type_hash_name = HashListTableInit(256, DetectBufferTypeHashNameFunc,
de_ctx->buffer_type_hash_name = HashListTableInitWithCtx(256, DetectBufferTypeHashNameFunc,
DetectBufferTypeCompareNameFunc, DetectBufferTypeFreeFunc);
BUG_ON(de_ctx->buffer_type_hash_name == NULL);
de_ctx->buffer_type_hash_id =
Expand Down Expand Up @@ -1756,7 +1761,7 @@ static void DetectBufferTypeFreeDetectEngine(DetectEngineCtx *de_ctx)
{
if (de_ctx) {
if (de_ctx->buffer_type_hash_name)
HashListTableFree(de_ctx->buffer_type_hash_name);
HashListTableFreeWithCtx(de_ctx, de_ctx->buffer_type_hash_name);
if (de_ctx->buffer_type_hash_id)
HashListTableFree(de_ctx->buffer_type_hash_id);

Expand Down Expand Up @@ -2571,6 +2576,7 @@ DetectEngineCtx *DetectEngineCtxInitWithPrefix(const char *prefix, uint32_t tena
static void DetectEngineCtxFreeThreadKeywordData(DetectEngineCtx *de_ctx)
{
HashListTableFree(de_ctx->keyword_hash);
de_ctx->keyword_hash = NULL;
jlucovsky marked this conversation as resolved.
Show resolved Hide resolved
}

static void DetectEngineCtxFreeFailedSigs(DetectEngineCtx *de_ctx)
Expand Down
6 changes: 3 additions & 3 deletions src/detect-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ void InspectionBufferApplyTransforms(InspectionBuffer *buffer,
const DetectEngineTransforms *transforms);
void InspectionBufferClean(DetectEngineThreadCtx *det_ctx);
InspectionBuffer *InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id);
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer);
void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTransforms *transforms,
const uint8_t *data, const uint32_t data_len);
void InspectionBufferSetupMultiEmpty(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer);
void InspectionBufferSetupMulti(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer,
const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len);
InspectionBuffer *InspectionBufferMultipleForListGet(
DetectEngineThreadCtx *det_ctx, const int list_id, uint32_t local_id);

Expand Down
7 changes: 4 additions & 3 deletions src/detect-file-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ static inline InspectionBuffer *FiledataWithXformsGetDataCallback(DetectEngineTh
return buffer;
}

InspectionBufferSetupMulti(buffer, transforms, base_buffer->inspect, base_buffer->inspect_len);
InspectionBufferSetupMulti(
det_ctx, buffer, transforms, base_buffer->inspect, base_buffer->inspect_len);
buffer->inspect_offset = base_buffer->inspect_offset;
SCLogDebug("xformed buffer %p size %u", buffer, buffer->inspect_len);
SCReturnPtr(buffer, "InspectionBuffer");
Expand Down Expand Up @@ -351,7 +352,7 @@ static InspectionBuffer *FiledataGetDataCallback(DetectEngineThreadCtx *det_ctx,
SCLogDebug("content inspected: %" PRIu64, cur_file->content_inspected);
}

InspectionBufferSetupMulti(buffer, NULL, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, NULL, data, data_len);
SCLogDebug("[list %d] [before] buffer offset %" PRIu64 "; buffer len %" PRIu32
"; data_len %" PRIu32 "; file_size %" PRIu64,
list_id, buffer->inspect_offset, buffer->inspect_len, data_len, file_size);
Expand Down Expand Up @@ -385,7 +386,7 @@ static InspectionBuffer *FiledataGetDataCallback(DetectEngineThreadCtx *det_ctx,
SCReturnPtr(buffer, "InspectionBuffer");

empty_return:
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions src/detect-filemagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static InspectionBuffer *FilemagicGetDataCallback(DetectEngineThreadCtx *det_ctx
(DetectFilemagicThreadData *)DetectThreadCtxGetKeywordThreadCtx(
det_ctx, det_ctx->de_ctx->filemagic_thread_ctx_id);
if (tfilemagic == NULL) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}

Expand All @@ -291,7 +291,7 @@ static InspectionBuffer *FilemagicGetDataCallback(DetectEngineThreadCtx *det_ctx
const uint8_t *data = (const uint8_t *)cur_file->magic;
uint32_t data_len = (uint32_t)strlen(cur_file->magic);

InspectionBufferSetupMulti(buffer, transforms, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);

SCReturnPtr(buffer, "InspectionBuffer");
}
Expand Down
2 changes: 1 addition & 1 deletion src/detect-filename.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ static InspectionBuffer *FilenameGetDataCallback(DetectEngineThreadCtx *det_ctx,
const uint8_t *data = cur_file->name;
uint32_t data_len = cur_file->name_len;

InspectionBufferSetupMulti(buffer, transforms, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);

SCReturnPtr(buffer, "InspectionBuffer");
}
Expand Down
12 changes: 6 additions & 6 deletions src/detect-http-header.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,15 @@ static InspectionBuffer *GetHttp2HeaderData(DetectEngineThreadCtx *det_ctx,
const uint8_t *b = NULL;

if (rs_http2_tx_get_header(txv, flags, local_id, &b, &b_len) != 1) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}
if (b == NULL || b_len == 0) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}

InspectionBufferSetupMulti(buffer, transforms, b, b_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, b, b_len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;

SCReturnPtr(buffer, "InspectionBuffer");
Expand Down Expand Up @@ -605,12 +605,12 @@ static InspectionBuffer *GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx,
// hdr_td->len is the number of header buffers
if (local_id < hdr_td->len) {
// we have one valid header buffer
InspectionBufferSetupMulti(
buffer, transforms, hdr_td->items[local_id].buffer, hdr_td->items[local_id].len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, hdr_td->items[local_id].buffer,
hdr_td->items[local_id].len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;
SCReturnPtr(buffer, "InspectionBuffer");
} // else there are no more header buffer to get
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions src/detect-ike-vendor.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ static InspectionBuffer *IkeVendorGetData(DetectEngineThreadCtx *det_ctx,
const uint8_t *data;
uint32_t data_len;
if (rs_ike_tx_get_vendor(txv, local_id, &data, &data_len) == 0) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}

InspectionBufferSetupMulti(buffer, transforms, data, data_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;

SCReturnPtr(buffer, "InspectionBuffer");
Expand Down
6 changes: 3 additions & 3 deletions src/detect-krb5-cname.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ static InspectionBuffer *GetKrb5CNameData(DetectEngineThreadCtx *det_ctx,
const uint8_t *b = NULL;

if (rs_krb5_tx_get_cname(txv, local_id, &b, &b_len) != 1) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}
if (b == NULL || b_len == 0) {
InspectionBufferSetupMultiEmpty(buffer);
InspectionBufferSetupMultiEmpty(det_ctx, buffer);
return NULL;
}

InspectionBufferSetupMulti(buffer, transforms, b, b_len);
InspectionBufferSetupMulti(det_ctx, buffer, transforms, b, b_len);
buffer->flags = DETECT_CI_FLAGS_SINGLE;

SCReturnPtr(buffer, "InspectionBuffer");
Expand Down
Loading