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 logging v2 #12080

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions doc/userguide/partials/eve-log.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ outputs:
# http-body-printable: yes # Requires metadata; enable dumping of HTTP body in printable format
# websocket-payload: yes # Requires metadata; enable dumping of WebSocket Payload in Base64
# websocket-payload-printable: yes # Requires metadata; enable dumping of WebSocket Payload in printable format
# file-data: yes # Requires metadata; enable dumping of the file data in base64 format

# Enable the logging of tagged packets for rules using the
# "tag" keyword.
Expand Down
6 changes: 6 additions & 0 deletions etc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
"items": {
"type": "integer"
}
},
"data": {
"type": "string"
},
"offset": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if these fields should be grouped together in a subobject (making the meaning of offset more clear)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a good idea. I can try implementing that to see if it works ok.

"type": "integer"
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/output-json-alert.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include "util-validate.h"

#include "action-globals.h"
#include <stdint.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this include added ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to fix my LSP which is adding these headers by itself...


#define MODULE_NAME "JsonAlertLog"

Expand All @@ -85,6 +86,7 @@
#define LOG_JSON_WEBSOCKET_PAYLOAD_BASE64 BIT_U16(12)
#define LOG_JSON_PAYLOAD_LENGTH BIT_U16(13)
#define LOG_JSON_REFERENCE BIT_U16(14)
#define LOG_JSON_FILE_DATA BIT_U16(15)

#define METADATA_DEFAULTS ( LOG_JSON_FLOW | \
LOG_JSON_APP_LAYER | \
Expand Down Expand Up @@ -431,7 +433,8 @@ static void AlertAddAppLayer(const Packet *p, JsonBuilder *jb,
}
}

static void AlertAddFiles(const Packet *p, JsonBuilder *jb, const uint64_t tx_id)
static void AlertAddFiles(
const Packet *p, JsonBuilder *jb, const uint64_t tx_id, int32_t file_dump_length)
{
const uint8_t direction =
(p->flowflags & FLOW_PKT_TOSERVER) ? STREAM_TOSERVER : STREAM_TOCLIENT;
Expand All @@ -452,7 +455,7 @@ static void AlertAddFiles(const Packet *p, JsonBuilder *jb, const uint64_t tx_id
jb_open_array(jb, "files");
}
jb_start_object(jb);
EveFileInfo(jb, file, tx_id, file->flags);
EveFileInfo(jb, file, tx_id, file->flags, file_dump_length);
jb_close(jb);
file = file->next;
}
Expand Down Expand Up @@ -680,7 +683,10 @@ static int AlertJson(ThreadVars *tv, JsonAlertLogThread *aft, const Packet *p)
}
/* including fileinfo data is configured by the metadata setting */
if (json_output_ctx->flags & LOG_JSON_RULE_METADATA) {
AlertAddFiles(p, jb, pa->tx_id);
AlertAddFiles(p, jb, pa->tx_id,
json_output_ctx->flags & LOG_JSON_FILE_DATA
? json_output_ctx->payload_buffer_size
: -1);
}
}

Expand Down Expand Up @@ -945,6 +951,7 @@ static void JsonAlertLogSetupMetadata(AlertJsonOutputCtx *json_output_ctx,
SetFlag(conf, "websocket-payload", LOG_JSON_WEBSOCKET_PAYLOAD_BASE64, &flags);
SetFlag(conf, "verdict", LOG_JSON_VERDICT, &flags);
SetFlag(conf, "payload-length", LOG_JSON_PAYLOAD_LENGTH, &flags);
SetFlag(conf, "file-data", LOG_JSON_FILE_DATA, &flags);

/* Check for obsolete flags and warn that they have no effect. */
static const char *deprecated_flags[] = { "http", "tls", "ssh", "smtp", "dnp3", "app-layer",
Expand Down
4 changes: 2 additions & 2 deletions src/output-json-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ JsonBuilder *JsonBuildFileInfoRecord(const Packet *p, const File *ff, void *tx,
if (stored) {
// the file has just been stored on disk cf OUTPUT_FILEDATA_FLAG_CLOSE
// but the flag is not set until the loggers have been called
EveFileInfo(js, ff, tx_id, ff->flags | FILE_STORED);
EveFileInfo(js, ff, tx_id, ff->flags | FILE_STORED, -1);
} else {
EveFileInfo(js, ff, tx_id, ff->flags);
EveFileInfo(js, ff, tx_id, ff->flags, -1);
}
jb_close(js);

Expand Down
25 changes: 24 additions & 1 deletion src/output-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "conf.h"

#include "util-debug.h"
#include "util-streaming-buffer.h"
#include "util-time.h"
#include "util-var-name.h"
#include "util-macset.h"
Expand Down Expand Up @@ -121,7 +122,8 @@ json_t *SCJsonString(const char *val)
/* Default Sensor ID value */
static int64_t sensor_id = -1; /* -1 = not defined */

void EveFileInfo(JsonBuilder *jb, const File *ff, const uint64_t tx_id, const uint16_t flags)
void EveFileInfo(JsonBuilder *jb, const File *ff, const uint64_t tx_id, const uint16_t flags,
int32_t dump_length)
{
jb_set_string_from_bytes(jb, "filename", ff->name, ff->name_len);

Expand Down Expand Up @@ -159,6 +161,8 @@ void EveFileInfo(JsonBuilder *jb, const File *ff, const uint64_t tx_id, const ui
break;
}

/* Log sha256 even if incomplete as it can be useful to correlate with
stored file */
if (ff->flags & FILE_SHA256) {
jb_set_hex(jb, "sha256", (uint8_t *)ff->sha256, (uint32_t)sizeof(ff->sha256));
}
Expand All @@ -179,6 +183,25 @@ void EveFileInfo(JsonBuilder *jb, const File *ff, const uint64_t tx_id, const ui
jb_set_uint(jb, "end", ff->end);
}
jb_set_uint(jb, "tx_id", tx_id);

if (dump_length >= 0) {
const uint8_t *data;
uint32_t data_len;
uint64_t data_offset;
StreamingBufferGetData(ff->sb, &data, &data_len, &data_offset);
if (data == NULL || data_len == 0)
return;
if (dump_length == 0) {
jb_set_base64(jb, "data", data, data_len);
} else {
if (data_len < (uint32_t)dump_length) {
jb_set_base64(jb, "data", data, data_len);
} else {
jb_set_base64(jb, "data", data, dump_length);
}
}
jb_set_uint(jb, "offset", data_offset);
}
}

static void EveAddPacketVars(const Packet *p, JsonBuilder *js_vars)
Expand Down
3 changes: 2 additions & 1 deletion src/output-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ typedef struct OutputJsonThreadCtx_ {
json_t *SCJsonString(const char *val);

void CreateEveFlowId(JsonBuilder *js, const Flow *f);
void EveFileInfo(JsonBuilder *js, const File *file, const uint64_t tx_id, const uint16_t flags);
void EveFileInfo(JsonBuilder *js, const File *file, const uint64_t tx_id, const uint16_t flags,
int32_t file_dump);
void EveTcpFlags(uint8_t flags, JsonBuilder *js);
void EvePacket(const Packet *p, JsonBuilder *js, uint32_t max_length);
JsonBuilder *CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir,
Expand Down
1 change: 1 addition & 0 deletions suricata.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ outputs:
# http-body-printable: yes # Requires metadata; enable dumping of HTTP body in printable format
# websocket-payload: yes # Requires metadata; enable dumping of WebSocket Payload in Base64
# websocket-payload-printable: yes # Requires metadata; enable dumping of WebSocket Payload in printable format
# file-data: yes # Requires metadata; enable dumping of the file data in base64 format

# Enable the logging of tagged packets for rules using the
# "tag" keyword.
Expand Down
Loading