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

File data/v3 #9237

Merged
merged 9 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 0 additions & 2 deletions rust/src/filecontainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ use crate::core::*;
extern {
pub fn FileFlowFlagsToFlags(flow_file_flags: u16, flags: u8) -> u16;
}
pub const FILE_USE_DETECT: u16 = BIT_U16!(13);


#[repr(C)]
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions rust/src/http2/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ impl HTTP2Transaction {
}

pub fn update_file_flags(&mut self, flow_file_flags: u16) {
self.ft_ts.file_flags = unsafe { FileFlowFlagsToFlags(flow_file_flags, STREAM_TOSERVER) | FILE_USE_DETECT };
self.ft_tc.file_flags = unsafe { FileFlowFlagsToFlags(flow_file_flags, STREAM_TOCLIENT) | FILE_USE_DETECT };
self.ft_ts.file_flags = unsafe { FileFlowFlagsToFlags(flow_file_flags, STREAM_TOSERVER) };
self.ft_tc.file_flags = unsafe { FileFlowFlagsToFlags(flow_file_flags, STREAM_TOCLIENT) };
}

fn decompress<'a>(
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nfs/nfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl NFSTransactionFile {
}
pub fn update_file_flags(&mut self, flow_file_flags: u16) {
let dir_flag = if self.direction == Direction::ToServer { STREAM_TOSERVER } else { STREAM_TOCLIENT };
self.file_tracker.file_flags = unsafe { FileFlowFlagsToFlags(flow_file_flags, dir_flag) | FILE_USE_DETECT };
self.file_tracker.file_flags = unsafe { FileFlowFlagsToFlags(flow_file_flags, dir_flag) };
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust/src/smb/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl SMBTransactionFile {

pub fn update_file_flags(&mut self, flow_file_flags: u16) {
let dir_flag = if self.direction == Direction::ToServer { STREAM_TOSERVER } else { STREAM_TOCLIENT };
self.file_tracker.file_flags = unsafe { FileFlowFlagsToFlags(flow_file_flags, dir_flag) | FILE_USE_DETECT };
self.file_tracker.file_flags = unsafe { FileFlowFlagsToFlags(flow_file_flags, dir_flag) };
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/app-layer-ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,7 @@ static AppLayerResult FTPDataParse(Flow *f, FtpDataState *ftpdata_state,
ftpdata_state->tx_data.file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);

/* we depend on detection engine for file pruning */
const uint16_t flags =
FileFlowFlagsToFlags(ftpdata_state->tx_data.file_flags, direction) | FILE_USE_DETECT;
const uint16_t flags = FileFlowFlagsToFlags(ftpdata_state->tx_data.file_flags, direction);
int ret = 0;

SCLogDebug("FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len, flags, direction,
Expand Down
11 changes: 11 additions & 0 deletions src/app-layer-htp-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ int HTPFileOpen(HtpState *s, HtpTxUserData *tx, const uint8_t *filename, uint16_
if (FileOpenFileWithId(files, &htp_sbcfg, s->file_track_id++, filename, filename_len, data,
data_len, flags) != 0) {
retval = -1;
} else {
const HTPCfgDir *cfg;
if (direction & STREAM_TOCLIENT) {
cfg = &s->cfg->response;
} else {
cfg = &s->cfg->request;
}
FileSetInspectSizes(files->tail, cfg->inspect_window, cfg->inspect_min_size);
}

tx->tx_data.files_opened++;
Expand Down Expand Up @@ -160,6 +168,9 @@ int HTPFileOpenWithRange(HtpState *s, HtpTxUserData *txud, const uint8_t *filena
if (FileOpenFileWithId(files, &htp_sbcfg, s->file_track_id++, filename, filename_len, data,
data_len, flags) != 0) {
SCReturnInt(-1);
} else {
const HTPCfgDir *cfg = &s->cfg->response;
FileSetInspectSizes(files->tail, cfg->inspect_window, cfg->inspect_min_size);
}
txud->tx_data.files_opened++;

Expand Down
4 changes: 1 addition & 3 deletions src/app-layer-smtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,6 @@ int SMTPProcessDataChunk(const uint8_t *chunk, uint32_t len,
DEBUG_VALIDATE_BUG_ON(tx == NULL);

uint16_t flags = FileFlowToFlags(flow, STREAM_TOSERVER);
/* we depend on detection engine for file pruning */
flags |= FILE_USE_DETECT;

/* Find file */
if (entity->ctnt_flags & CTNT_IS_ATTACHMENT) {
Expand Down Expand Up @@ -1214,7 +1212,7 @@ static int SMTPProcessRequest(SMTPState *state, Flow *f, AppLayerParserState *ps
}
if (FileOpenFileWithId(&tx->files_ts, &smtp_config.sbcfg, state->file_track_id++,
(uint8_t *)rawmsgname, strlen(rawmsgname), NULL, 0,
FILE_NOMD5 | FILE_NOMAGIC | FILE_USE_DETECT) == 0) {
FILE_NOMD5 | FILE_NOMAGIC) == 0) {
SMTPNewFile(tx, tx->files_ts.tail);
}
} else if (smtp_config.decode_mime) {
Expand Down
24 changes: 0 additions & 24 deletions src/detect-engine-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,6 @@
#include "util-profiling.h"
#include "util-validate.h"

FileAppProto file_protos_ts_static[] = {
{ ALPROTO_HTTP1, HTP_REQUEST_BODY },
{ ALPROTO_SMTP, 0 },
{ ALPROTO_FTP, 0 },
{ ALPROTO_FTPDATA, 0 },
{ ALPROTO_SMB, 0 },
{ ALPROTO_NFS, 0 },
{ ALPROTO_HTTP2, HTTP2StateDataClient },
{ ALPROTO_UNKNOWN, 0 },
};

FileAppProto file_protos_tc_static[] = {
{ ALPROTO_HTTP1, HTP_RESPONSE_BODY },
{ ALPROTO_FTP, 0 },
{ ALPROTO_FTPDATA, 0 },
{ ALPROTO_SMB, 0 },
{ ALPROTO_NFS, 0 },
{ ALPROTO_HTTP2, HTTP2StateDataServer },
{ ALPROTO_UNKNOWN, 0 },
};

FileAppProto *file_protos_ts = file_protos_ts_static;
FileAppProto *file_protos_tc = file_protos_tc_static;

/**
* \brief Inspect the file inspecting keywords.
*
Expand Down
8 changes: 0 additions & 8 deletions src/detect-engine-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,4 @@ uint8_t DetectFileInspectGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx
const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
uint8_t flags, void *_alstate, void *tx, uint64_t tx_id);

typedef struct FileAppProto {
AppProto alproto;
int progress;
} FileAppProto;

extern FileAppProto *file_protos_ts;
extern FileAppProto *file_protos_tc;

#endif /* __DETECT_ENGINE_FILE_H__ */
17 changes: 3 additions & 14 deletions src/detect-engine-mpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ static int g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_SIZE] = { 0, 0, 0 };
* \note to be used at start up / registration only. Errors are fatal.
*/
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority,
int (*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
const DetectBufferMpmRegistry *mpm_reg, int list_id),
InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData,
AppProto alproto, int tx_min_progress)
{
SCLogDebug("registering %s/%d/%d/%p/%p/%u/%d", name, direction, priority,
PrefilterRegister, GetData, alproto, tx_min_progress);
Expand Down Expand Up @@ -1571,19 +1570,9 @@ static void MpmStoreSetup(const DetectEngineCtx *de_ctx, MpmStore *ms)
for (sig = 0; sig < (ms->sid_array_size * 8); sig++) {
if (ms->sid_array[sig / 8] & (1 << (sig % 8))) {
s = de_ctx->sig_array[sig];
DEBUG_VALIDATE_BUG_ON(s == NULL);
if (s == NULL)
continue;
if ((s->flags & ms->direction) == 0) {
SCLogDebug("s->flags %x ms->direction %x", s->flags, ms->direction);
continue;
}
if (s->init_data->mpm_sm == NULL)
continue;
int list = s->init_data->mpm_sm_list;
if (list < 0)
continue;
if (list != ms->sm_list)
continue;

SCLogDebug("%p: direction %d adding %u", ms, ms->direction, s->id);

Expand Down
12 changes: 6 additions & 6 deletions src/detect-engine-mpm.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ MpmStore *MpmStorePrepareBuffer(DetectEngineCtx *de_ctx, SigGroupHead *sgh, enum
*/
int DetectSetFastPatternAndItsId(DetectEngineCtx *de_ctx);

typedef int (*PrefilterRegisterFunc)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
const DetectBufferMpmRegistry *mpm_reg, int list_id);

/** \brief register an app layer keyword for mpm
* \param name buffer name
* \param direction SIG_FLAG_TOSERVER or SIG_FLAG_TOCLIENT
Expand All @@ -88,17 +91,14 @@ int DetectSetFastPatternAndItsId(DetectEngineCtx *de_ctx);
* If both are needed, register the keyword twice.
*/
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority,
int (*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
const DetectBufferMpmRegistry *mpm_reg, int list_id),
InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress);
PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData,
AppProto alproto, int tx_min_progress);
void DetectAppLayerMpmRegisterByParentId(
DetectEngineCtx *de_ctx,
const int id, const int parent_id,
DetectEngineTransforms *transforms);

void DetectPktMpmRegister(const char *name, int priority,
int (*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
const DetectBufferMpmRegistry *mpm_reg, int list_id),
void DetectPktMpmRegister(const char *name, int priority, PrefilterRegisterFunc PrefilterRegister,
InspectionBufferGetPktDataPtr GetData);
void DetectPktMpmRegisterByParentId(DetectEngineCtx *de_ctx,
const int id, const int parent_id,
Expand Down
10 changes: 10 additions & 0 deletions src/detect-engine-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ int SigTableList(const char *keyword)
return TM_ECODE_DONE;
}

static void DetectFileHandlerRegister(void)
{
for (int i = 0; i < DETECT_TBLSIZE; i++) {
if (filehandler_table[i].name)
DetectFileRegisterFileProtocols(&filehandler_table[i]);
}
}

void SigTableSetup(void)
{
memset(sigmatch_table, 0, sizeof(sigmatch_table));
Expand Down Expand Up @@ -689,6 +697,8 @@ void SigTableSetup(void)
DetectTransformUrlDecodeRegister();
DetectTransformXorRegister();

DetectFileHandlerRegister();

/* close keyword registration */
DetectBufferTypeCloseRegistration();
}
Expand Down
6 changes: 3 additions & 3 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ static void AppendAppInspectEngine(DetectEngineCtx *de_ctx,
} else {
new_engine->id = DE_STATE_FLAG_BASE; /* id is used as flag in stateful detect */
SCLogDebug("sid %u: engine %p/%u %s", s->id, new_engine, new_engine->id,
DetectEngineBufferTypeGetNameById(de_ctx, t->sm_list));
DetectEngineBufferTypeGetNameById(de_ctx, new_engine->sm_list));
}

/* prepend engine if forced or if our engine has a lower progress. */
Expand All @@ -721,7 +721,7 @@ static void AppendAppInspectEngine(DetectEngineCtx *de_ctx,
} else {
new_engine->id = ++(*last_id);
SCLogDebug("sid %u: engine %p/%u %s", s->id, new_engine, new_engine->id,
DetectEngineBufferTypeGetNameById(de_ctx, t->sm_list));
DetectEngineBufferTypeGetNameById(de_ctx, new_engine->sm_list));
}

} else {
Expand All @@ -741,7 +741,7 @@ static void AppendAppInspectEngine(DetectEngineCtx *de_ctx,
} else {
new_engine->id = ++(*last_id);
SCLogDebug("sid %u: engine %p/%u %s", s->id, new_engine, new_engine->id,
DetectEngineBufferTypeGetNameById(de_ctx, t->sm_list));
DetectEngineBufferTypeGetNameById(de_ctx, new_engine->sm_list));
}
}

Expand Down
Loading
Loading