Go to the documentation of this file.
72 #define MODULE_NAME "JsonAlertLog"
74 #define LOG_JSON_PAYLOAD BIT_U16(0)
75 #define LOG_JSON_PACKET BIT_U16(1)
76 #define LOG_JSON_PAYLOAD_BASE64 BIT_U16(2)
77 #define LOG_JSON_TAGGED_PACKETS BIT_U16(3)
78 #define LOG_JSON_APP_LAYER BIT_U16(4)
79 #define LOG_JSON_FLOW BIT_U16(5)
80 #define LOG_JSON_HTTP_BODY BIT_U16(6)
81 #define LOG_JSON_HTTP_BODY_BASE64 BIT_U16(7)
82 #define LOG_JSON_RULE_METADATA BIT_U16(8)
83 #define LOG_JSON_RULE BIT_U16(9)
84 #define LOG_JSON_VERDICT BIT_U16(10)
85 #define LOG_JSON_WEBSOCKET_PAYLOAD BIT_U16(11)
86 #define LOG_JSON_WEBSOCKET_PAYLOAD_BASE64 BIT_U16(12)
87 #define LOG_JSON_PAYLOAD_LENGTH BIT_U16(13)
88 #define LOG_JSON_REFERENCE BIT_U16(14)
90 #define METADATA_DEFAULTS ( LOG_JSON_FLOW | \
91 LOG_JSON_APP_LAYER | \
92 LOG_JSON_RULE_METADATA)
94 #define JSON_BODY_LOGGING \
95 (LOG_JSON_HTTP_BODY | LOG_JSON_HTTP_BODY_BASE64 | LOG_JSON_WEBSOCKET_PAYLOAD | \
96 LOG_JSON_WEBSOCKET_PAYLOAD_BASE64)
98 #define JSON_STREAM_BUFFER_SIZE 4096
115 static void AlertJsonSourceTarget(
118 SCJbOpenObject(js,
"source");
120 SCJbSetString(js,
"ip", addr->
src_ip);
128 SCJbSetUint(js,
"port", addr->
sp);
132 SCJbSetString(js,
"ip", addr->
dst_ip);
140 SCJbSetUint(js,
"port", addr->
dp);
146 SCJbOpenObject(js,
"target");
148 SCJbSetString(js,
"ip", addr->
dst_ip);
156 SCJbSetUint(js,
"port", addr->
dp);
160 SCJbSetString(js,
"ip", addr->
src_ip);
168 SCJbSetUint(js,
"port", addr->
sp);
175 static void AlertJsonReference(
const PacketAlert *pa, SCJsonBuilder *jb)
182 SCJbOpenArray(jb,
"references");
190 char kv_store[size_needed];
191 snprintf(kv_store, size_needed,
"%s%s", kv->
key, kv->
reference);
192 SCJbAppendString(jb, kv_store);
198 static void AlertJsonMetadata(
const PacketAlert *pa, SCJsonBuilder *js)
208 const char *action =
"allowed";
225 SCJbSetUint(js,
"tx_id", pa->
tx_id);
228 SCJbSetBool(js,
"tx_guessed",
true);
231 SCJbOpenObject(js,
"alert");
233 SCJbSetString(js,
"action", action);
237 SCJbSetUint(js,
"gid", pa->
s->
gid);
238 SCJbSetUint(js,
"signature_id", pa->
s->
id);
239 SCJbSetUint(js,
"rev", pa->
s->
rev);
242 SCJbSetString(js,
"signature", pa->
s->
msg ? pa->
s->
msg :
"");
244 SCJbSetUint(js,
"severity", pa->
s->
prio);
247 SCJbSetUint(js,
"tenant_id", p->
tenant_id);
251 AlertJsonSourceTarget(p, pa, js, addr);
255 AlertJsonReference(pa, js);
259 AlertJsonMetadata(pa, js);
263 SCJbOpenObject(js,
"context");
270 json_info = json_info->
next;
275 SCJbSetString(js,
"rule", pa->
s->
sig_str);
277 if (xff_buffer && xff_buffer[0]) {
278 SCJbSetString(js,
"xff", xff_buffer);
286 if (p->
root == NULL) {
290 SCJbOpenObject(js,
"tunnel");
297 SCJbSetString(js,
"src_ip", addr.
src_ip);
298 SCJbSetUint(js,
"src_port", addr.
sp);
299 SCJbSetString(js,
"dest_ip", addr.
dst_ip);
300 SCJbSetUint(js,
"dest_port", addr.
dp);
301 SCJbSetString(js,
"proto", addr.
proto);
306 SCJbSetUint(js,
"pcap_cnt", pcap_cnt);
326 static void AlertAddAppLayer(
327 const Packet *p, SCJsonBuilder *jb,
const uint64_t tx_id,
const uint16_t option_flags)
331 SCJsonBuilderMark mark = { 0, 0, 0 };
332 if (al && al->
LogTx) {
333 void *state = FlowGetAppState(p->
flow);
341 SCJbSetString(jb,
"ts_progress",
343 SCJbSetString(jb,
"tc_progress",
345 SCJbGetMark(jb, &mark);
353 if (!SCWebSocketLogDetails(tx, jb, pp, pb64)) {
354 SCJbRestoreMark(jb, &mark);
360 if (!al->
LogTx(tx, jb)) {
361 SCJbRestoreMark(jb, &mark);
367 void *state = FlowGetAppState(p->
flow);
375 SCJbSetString(jb,
"ts_progress",
377 SCJbSetString(jb,
"tc_progress",
384 SCJbOpenObject(jb,
"http");
396 SCJbGetMark(jb, &mark);
397 SCJbOpenObject(jb,
"smtp");
401 SCJbRestoreMark(jb, &mark);
403 SCJbGetMark(jb, &mark);
404 SCJbOpenObject(jb,
"email");
408 SCJbRestoreMark(jb, &mark);
413 SCJbGetMark(jb, &mark);
414 SCJbOpenObject(jb,
"rpc");
418 SCJbRestoreMark(jb, &mark);
421 SCJbGetMark(jb, &mark);
422 SCJbOpenObject(jb,
"nfs");
426 SCJbRestoreMark(jb, &mark);
430 SCJbGetMark(jb, &mark);
431 SCJbOpenObject(jb,
"smb");
435 SCJbRestoreMark(jb, &mark);
439 SCJbGetMark(jb, &mark);
441 SCJbRestoreMark(jb, &mark);
448 SCJbGetMark(jb, &mark);
449 SCJbOpenObject(jb,
"dcerpc");
450 if (p->
proto == IPPROTO_TCP) {
451 if (!SCDcerpcLogJsonRecordTcp(state, tx, jb)) {
452 SCJbRestoreMark(jb, &mark);
455 if (!SCDcerpcLogJsonRecordUdp(state, tx, jb)) {
456 SCJbRestoreMark(jb, &mark);
469 static void AlertAddFiles(
const Packet *p, SCJsonBuilder *jb,
const uint64_t tx_id)
471 const uint8_t direction =
487 SCJbOpenArray(jb,
"files");
500 static void AlertAddFrame(
501 const Packet *p,
const int64_t frame_id, SCJsonBuilder *jb,
MemBuffer *buffer)
507 if (frames_container == NULL)
512 if (p->
proto == IPPROTO_TCP) {
516 frames = &frames_container->
toserver;
519 frames = &frames_container->
toclient;
525 }
else if (p->
proto == IPPROTO_UDP) {
527 frames = &frames_container->
toserver;
529 frames = &frames_container->
toclient;
546 SCJbOpenObject(jb,
"verdict");
550 packet_action, alert_action);
566 SCJbOpenArray(jb,
"reject");
571 SCJbAppendString(jb,
"icmp-prohib");
574 SCJbAppendString(jb,
"tcp-reset");
599 static int AlertJsonStreamDataCallback(
600 void *cb_data,
const uint8_t *input,
const uint32_t input_len,
const uint64_t input_offset)
603 if (input_offset > cbd->
last_re) {
605 cbd->
payload,
"[%" PRIu64
" bytes missing]", input_offset - cbd->
last_re);
610 if (written < input_len)
612 cbd->
last_re = input_offset + input_len;
642 SCJbSetPrintAsciiString(
658 for (
int i = 0; i < p->
alerts.
cnt; i++) {
710 if (PacketIsTunnel(p)) {
711 AlertJsonTunnel(p, jb, &json_output_ctx->
eve_ctx->
cfg);
714 if (p->
flow != NULL) {
717 AlertAddAppLayer(p, jb, pa->
tx_id, json_output_ctx->
flags);
721 AlertAddFiles(p, jb, pa->
tx_id);
728 SCJbSetString(jb,
"direction",
"to_server");
730 SCJbSetString(jb,
"direction",
"to_client");
734 SCJbOpenObject(jb,
"flow");
737 SCJbSetString(jb,
"src_ip", addr.
dst_ip);
738 SCJbSetString(jb,
"dest_ip", addr.
src_ip);
740 SCJbSetUint(jb,
"src_port", addr.
dp);
741 SCJbSetUint(jb,
"dest_port", addr.
sp);
744 SCJbSetString(jb,
"src_ip", addr.
src_ip);
745 SCJbSetString(jb,
"dest_ip", addr.
dst_ip);
747 SCJbSetUint(jb,
"src_port", addr.
sp);
748 SCJbSetUint(jb,
"dest_port", addr.
dp);
756 if (json_output_ctx->
flags &
758 int stream = (p->
proto == IPPROTO_TCP) ?
765 if (stream && p->
flow != NULL) {
766 const bool stream_data_logged =
767 AlertJsonStreamData(json_output_ctx, aft, p->
flow, p, jb);
770 AlertAddPayload(json_output_ctx, jb, p);
774 AlertAddPayload(json_output_ctx, jb, p);
777 SCJbSetUint(jb,
"stream", stream);
804 SCJsonBuilder *packetjs =
824 for (
int i = 0; i < p->
alerts.
cnt; i++) {
837 if (PacketIsTunnel(p)) {
838 AlertJsonTunnel(p, jb, &json_output_ctx->
eve_ctx->
cfg);
866 if (PacketIsIPv4(p) || PacketIsIPv6(p)) {
867 return AlertJson(
tv, aft, p);
869 return AlertJsonDecoderEvent(
tv, aft, p);
879 static TmEcode JsonAlertLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
885 if (initdata == NULL)
887 SCLogDebug(
"Error getting context for EveLogAlert. \"initdata\" argument NULL");
933 static void JsonAlertLogDeInitCtxSub(
OutputCtx *output_ctx)
935 SCLogDebug(
"cleaning up sub output_ctx %p", output_ctx);
939 if (json_output_ctx != NULL) {
941 if (xff_cfg != NULL) {
949 static void SetFlag(
const SCConfNode *conf,
const char *
name, uint16_t flag, uint16_t *out_flags)
953 if (setting != NULL) {
964 static bool warn_no_meta =
false;
971 if (metadata != NULL) {
1000 static const char *deprecated_flags[] = {
"http",
"tls",
"ssh",
"smtp",
"dnp3",
"app-layer",
1002 for (
int i = 0; deprecated_flags[i] != NULL; i++) {
1004 SCLogWarning(
"Found deprecated eve-log.alert flag \"%s\", this flag has no effect",
1005 deprecated_flags[i]);
1011 if (payload_buffer_value != NULL) {
1015 "payload-buffer-size - %s. Killing engine",
1016 payload_buffer_value);
1018 }
else if (value == 0) {
1020 SCLogError(
"Error payload-buffer-size should not be 0");
1023 payload_buffer_size = value;
1029 SCLogWarning(
"HTTP body logging has been configured, however, "
1030 "metadata logging has not been enabled. HTTP body logging will be "
1033 warn_no_meta =
true;
1051 if (
likely(xff_cfg != NULL)) {
1074 if (
unlikely(json_output_ctx == NULL)) {
1079 json_output_ctx->
eve_ctx = ajt;
1081 JsonAlertLogSetupMetadata(json_output_ctx, conf);
1082 json_output_ctx->
xff_cfg = JsonAlertLogGetXffCfg(conf);
1083 if (json_output_ctx->
xff_cfg == NULL) {
1087 output_ctx->
data = json_output_ctx;
1088 output_ctx->
DeInit = JsonAlertLogDeInitCtxSub;
1090 result.
ctx = output_ctx;
1095 if (json_output_ctx != NULL) {
1098 if (output_ctx != NULL) {
1109 .ConditionFunc = JsonAlertLogCondition,
1110 .ThreadInitFunc = JsonAlertLogThreadInit,
1111 .ThreadDeinitFunc = JsonAlertLogThreadDeinit,
1112 .ThreadExitPrintStatsFunc = NULL,
1116 JsonAlertLogInitCtxSub, &output_logger_functions);
bool PacketCheckAction(const Packet *p, const uint8_t a)
#define PACKET_ALERT_FLAG_TX_GUESSED
int SCConfValIsTrue(const char *val)
Check if a value is true.
const struct Signature_ * s
const char * AppLayerParserGetStateNameById(uint8_t ipproto, AppProto alproto, const int id, const uint8_t direction)
void EveAddFlow(Flow *f, SCJsonBuilder *js)
OutputJsonCommonSettings cfg
void HttpXFFGetCfg(SCConfNode *conf, HttpXFFCfg *result)
Function to return XFF configuration from a configuration node.
#define PACKET_ALERT_FLAG_STATE_MATCH
void EveHttpLogJSONBodyPrintable(SCJsonBuilder *js, Flow *f, uint64_t tx_id)
FramesContainer * AppLayerFramesGetContainer(const Flow *f)
uint64_t PcapPacketCntGet(const Packet *p)
HttpXFFCfg * parent_xff_cfg
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
void EveAddAppProto(Flow *f, SCJsonBuilder *js)
#define SIG_FLAG_DEST_IS_TARGET
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
AppProto SCFlowGetAppProtocol(const Flow *f)
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
void JsonAddrInfoInit(const Packet *p, enum SCOutputJsonLogDirection dir, JsonAddrInfo *addr, OutputJsonCommonSettings *cfg)
void AlertJsonHeader(const Packet *p, const PacketAlert *pa, SCJsonBuilder *js, uint16_t flags, JsonAddrInfo *addr, char *xff_buffer)
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
const JsonAddrInfo json_addr_info_zero
#define FLOW_PKT_TOSERVER
#define ACTION_REJECT_ANY
int SCConfValIsFalse(const char *val)
Check if a value is false.
#define ACTION_DROP_REJECT
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
bool EngineModeIsFirewall(void)
bool SCConfNodeHasChildren(const SCConfNode *node)
Check if a node has any children.
bool EveHttpAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
int HttpXFFGetIP(const Flow *f, HttpXFFCfg *xff_cfg, char *dstbuf, int dstbuflen)
Function to return XFF IP if any. The caller needs to lock the flow.
char * PcapLogGetFilename(void)
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *tx, const uint8_t direction)
bool EveSMBAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *jb)
size_t strlcpy(char *dst, const char *src, size_t siz)
#define JSON_BODY_LOGGING
#define SIG_FLAG_FIREWALL
#define ACTION_REJECT_DST
bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
OutputJsonThreadCtx * ctx
struct PacketContextData * json_info
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
#define JB_SET_STRING(jb, key, val)
#define PKT_IS_TOSERVER(p)
#define LOG_JSON_APP_LAYER
#define LOG_JSON_WEBSOCKET_PAYLOAD
Frame * FrameGetById(Frames *frames, const int64_t id)
#define STREAM_BASE_OFFSET(stream)
bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *jb)
DetectReference * references
Per thread variable structure.
bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, SCJsonBuilder *jb)
const char * PktSrcToString(enum PktSrcEnum pkt_src)
struct JsonAlertLogThread_ JsonAlertLogThread
#define SCLogWarning(...)
Macro used to log WARNING messages.
Signature reference list.
EveJsonSimpleAppLayerLogger * SCEveJsonSimpleGetLogger(AppProto alproto)
void EveFileInfo(SCJsonBuilder *jb, const File *ff, const uint64_t tx_id, const uint16_t flags)
#define JSON_STREAM_BUFFER_SIZE
#define LOG_JSON_PAYLOAD_BASE64
#define DETECT_MAX_RULE_SIZE
#define SIG_FLAG_HAS_TARGET
#define LOG_JSON_PAYLOAD_LENGTH
#define SIG_FLAG_SRC_IS_TARGET
uint32_t payload_buffer_size
void EvePacket(const Packet *p, SCJsonBuilder *js, uint32_t max_length)
Jsonify a packet.
#define FLOW_PKT_TOCLIENT
struct DetectReference_ * next
#define LOG_JSON_TAGGED_PACKETS
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
#define PACKET_ALERT_FLAG_STREAM_MATCH
#define PACKET_ALERT_FLAG_FRAME
char proto[JSON_PROTO_LEN]
#define ACTION_REJECT_BOTH
SCConfNode * SCConfNodeLookupChild(const SCConfNode *node, const char *name)
Lookup a child configuration node by name.
bool EveEmailAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
#define LOG_JSON_WEBSOCKET_PAYLOAD_BASE64
void(* DeInit)(struct OutputCtx_ *)
char pcap_filename[PATH_MAX]
EveJsonSimpleTxLogFunc LogTx
AlertJsonOutputCtx * json_output_ctx
void MemBufferFree(MemBuffer *buffer)
uint8_t PacketGetAction(const Packet *p)
struct PacketContextData * next
void OutputRegisterPacketSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, OutputPacketLoggerFunctions *output_logger_functions)
Register a packet output sub-module.
int ParseSizeStringU32(const char *size, uint32_t *res)
char src_ip[JSON_ADDR_LEN]
void EveAddVerdict(SCJsonBuilder *jb, const Packet *p, const uint8_t alert_action)
Build verdict object.
#define PACKET_ALERT_FLAG_RATE_FILTER_MODIFIED
DetectMetadataHead * metadata
#define SCLogError(...)
Macro used to log ERROR messages.
void EveHttpLogJSONBodyBase64(SCJsonBuilder *js, Flow *f, uint64_t tx_id)
struct AlertJsonOutputCtx_ AlertJsonOutputCtx
MemBuffer * payload_buffer
#define LOG_JSON_HTTP_BODY_BASE64
void FrameJsonLogOneFrame(const uint8_t ipproto, const Frame *frame, Flow *f, const TcpStream *stream, const Packet *p, SCJsonBuilder *jb, MemBuffer *buffer)
log a single frame
#define PACKET_ALERT_FLAG_TX
char dst_ip[JSON_ADDR_LEN]
#define LOG_JSON_HTTP_BODY
#define METADATA_DEFAULTS
int EngineModeIsIPS(void)
uint32_t MemBufferWriteRaw(MemBuffer *dst, const uint8_t *raw, const uint32_t raw_len)
Write a raw buffer to the MemBuffer dst.
void MemBufferWriteString(MemBuffer *dst, const char *fmt,...)
int HttpXFFGetIPFromTx(const Flow *f, uint64_t tx_id, HttpXFFCfg *xff_cfg, char *dstbuf, int dstbuflen)
Function to return XFF IP if any in the selected transaction. The caller needs to lock the flow.
void DetectEngineSetParseMetadata(void)
AppProto alproto
application level protocol
int StreamReassembleLog(const TcpSession *ssn, const TcpStream *stream, StreamReassembleRawFunc Callback, void *cb_data, const uint64_t progress_in, uint64_t *progress_out, const bool eof)
#define LOG_JSON_RULE_METADATA
#define DEBUG_VALIDATE_BUG_ON(exp)
#define LOG_JSON_REFERENCE
MemBuffer * MemBufferCreateNew(uint32_t size)
void JsonAlertLogRegister(void)