Go to the documentation of this file.
71 #define MODULE_NAME "JsonAlertLog"
73 #define LOG_JSON_PAYLOAD BIT_U16(0)
74 #define LOG_JSON_PACKET BIT_U16(1)
75 #define LOG_JSON_PAYLOAD_BASE64 BIT_U16(2)
76 #define LOG_JSON_TAGGED_PACKETS BIT_U16(3)
77 #define LOG_JSON_APP_LAYER BIT_U16(4)
78 #define LOG_JSON_FLOW BIT_U16(5)
79 #define LOG_JSON_HTTP_BODY BIT_U16(6)
80 #define LOG_JSON_HTTP_BODY_BASE64 BIT_U16(7)
81 #define LOG_JSON_RULE_METADATA BIT_U16(8)
82 #define LOG_JSON_RULE BIT_U16(9)
83 #define LOG_JSON_VERDICT BIT_U16(10)
84 #define LOG_JSON_WEBSOCKET_PAYLOAD BIT_U16(11)
85 #define LOG_JSON_WEBSOCKET_PAYLOAD_BASE64 BIT_U16(12)
86 #define LOG_JSON_PAYLOAD_LENGTH BIT_U16(13)
87 #define LOG_JSON_REFERENCE BIT_U16(14)
89 #define METADATA_DEFAULTS ( LOG_JSON_FLOW | \
90 LOG_JSON_APP_LAYER | \
91 LOG_JSON_RULE_METADATA)
93 #define JSON_BODY_LOGGING \
94 (LOG_JSON_HTTP_BODY | LOG_JSON_HTTP_BODY_BASE64 | LOG_JSON_WEBSOCKET_PAYLOAD | \
95 LOG_JSON_WEBSOCKET_PAYLOAD_BASE64)
97 #define JSON_STREAM_BUFFER_SIZE 4096
117 jb_open_object(js,
"source");
119 jb_set_string(js,
"ip", addr->
src_ip);
127 jb_set_uint(js,
"port", addr->
sp);
131 jb_set_string(js,
"ip", addr->
dst_ip);
139 jb_set_uint(js,
"port", addr->
dp);
145 jb_open_object(js,
"target");
147 jb_set_string(js,
"ip", addr->
dst_ip);
155 jb_set_uint(js,
"port", addr->
dp);
159 jb_set_string(js,
"ip", addr->
src_ip);
167 jb_set_uint(js,
"port", addr->
sp);
174 static void AlertJsonReference(
const PacketAlert *pa, JsonBuilder *jb)
181 jb_open_array(jb,
"references");
187 char kv_store[size_needed];
188 snprintf(kv_store, size_needed,
"%s%s", kv->
key, kv->
reference);
189 jb_append_string(jb, kv_store);
195 static void AlertJsonMetadata(
const PacketAlert *pa, JsonBuilder *js)
205 const char *action =
"allowed";
222 jb_set_uint(js,
"tx_id", pa->
tx_id);
225 jb_open_object(js,
"alert");
227 jb_set_string(js,
"action", action);
228 jb_set_uint(js,
"gid", pa->
s->
gid);
229 jb_set_uint(js,
"signature_id", pa->
s->
id);
230 jb_set_uint(js,
"rev", pa->
s->
rev);
233 jb_set_string(js,
"signature", pa->
s->
msg ? pa->
s->
msg:
"");
235 jb_set_uint(js,
"severity", pa->
s->
prio);
238 jb_set_uint(js,
"tenant_id", p->
tenant_id);
242 AlertJsonSourceTarget(p, pa, js, addr);
246 AlertJsonReference(pa, js);
250 AlertJsonMetadata(pa, js);
254 jb_set_string(js,
"rule", pa->
s->
sig_str);
256 if (xff_buffer && xff_buffer[0]) {
257 jb_set_string(js,
"xff", xff_buffer);
263 static void AlertJsonTunnel(
const Packet *p, JsonBuilder *js)
265 if (p->
root == NULL) {
269 jb_open_object(js,
"tunnel");
278 jb_set_string(js,
"src_ip", addr.
src_ip);
279 jb_set_uint(js,
"src_port", addr.
sp);
280 jb_set_string(js,
"dest_ip", addr.
dst_ip);
281 jb_set_uint(js,
"dest_port", addr.
dp);
282 jb_set_string(js,
"proto", addr.
proto);
286 jb_set_uint(js,
"pcap_cnt", pcap_cnt);
308 jb_set_string(js,
"payload_printable", (
char *)printable_buf);
312 static void AlertAddAppLayer(
const Packet *p, JsonBuilder *jb,
313 const uint64_t tx_id,
const uint16_t option_flags)
317 JsonBuilderMark mark = { 0, 0, 0 };
318 if (al && al->
LogTx) {
319 void *state = FlowGetAppState(p->
flow);
323 jb_get_mark(jb, &mark);
331 if (!SCWebSocketLogDetails(tx, jb, pp, pb64)) {
332 jb_restore_mark(jb, &mark);
338 if (!al->
LogTx(tx, jb)) {
339 jb_restore_mark(jb, &mark);
348 jb_open_object(jb,
"http");
360 jb_get_mark(jb, &mark);
361 jb_open_object(jb,
"smtp");
365 jb_restore_mark(jb, &mark);
367 jb_get_mark(jb, &mark);
368 jb_open_object(jb,
"email");
372 jb_restore_mark(jb, &mark);
377 jb_get_mark(jb, &mark);
378 jb_open_object(jb,
"rpc");
382 jb_restore_mark(jb, &mark);
385 jb_get_mark(jb, &mark);
386 jb_open_object(jb,
"nfs");
390 jb_restore_mark(jb, &mark);
394 jb_get_mark(jb, &mark);
395 jb_open_object(jb,
"smb");
399 jb_restore_mark(jb, &mark);
403 jb_get_mark(jb, &mark);
405 jb_restore_mark(jb, &mark);
409 void *state = FlowGetAppState(p->
flow);
413 jb_get_mark(jb, &mark);
414 jb_open_object(jb,
"dcerpc");
415 if (p->
proto == IPPROTO_TCP) {
416 if (!rs_dcerpc_log_json_record_tcp(state, tx, jb)) {
417 jb_restore_mark(jb, &mark);
420 if (!rs_dcerpc_log_json_record_udp(state, tx, jb)) {
421 jb_restore_mark(jb, &mark);
434 static void AlertAddFiles(
const Packet *p, JsonBuilder *jb,
const uint64_t tx_id)
436 const uint8_t direction =
452 jb_open_array(jb,
"files");
465 static void AlertAddFrame(
466 const Packet *p,
const int64_t frame_id, JsonBuilder *jb,
MemBuffer *buffer)
472 if (frames_container == NULL)
477 if (p->
proto == IPPROTO_TCP) {
481 frames = &frames_container->
toserver;
484 frames = &frames_container->
toclient;
490 }
else if (p->
proto == IPPROTO_UDP) {
492 frames = &frames_container->
toserver;
494 frames = &frames_container->
toclient;
511 jb_open_object(jb,
"verdict");
528 jb_open_array(jb,
"reject");
533 jb_append_string(jb,
"icmp-prohib");
536 jb_append_string(jb,
"tcp-reset");
559 static int AlertJsonStreamDataCallback(
560 void *cb_data,
const uint8_t *input,
const uint32_t input_len,
const uint64_t input_offset)
563 if (input_offset > cbd->
last_re) {
565 cbd->
payload,
"[%" PRIu64
" bytes missing]", input_offset - cbd->
last_re);
570 if (written < input_len)
572 cbd->
last_re = input_offset + input_len;
606 jb_set_string(jb,
"payload_printable", (
char *)printable_buf);
620 for (
int i = 0; i < p->
alerts.
cnt; i++) {
672 if (PacketIsTunnel(p)) {
673 AlertJsonTunnel(p, jb);
676 if (p->
flow != NULL) {
679 AlertAddAppLayer(p, jb, pa->
tx_id, json_output_ctx->
flags);
683 AlertAddFiles(p, jb, pa->
tx_id);
690 jb_set_string(jb,
"direction",
"to_server");
692 jb_set_string(jb,
"direction",
"to_client");
696 jb_open_object(jb,
"flow");
699 jb_set_string(jb,
"src_ip", addr.
dst_ip);
700 jb_set_string(jb,
"dest_ip", addr.
src_ip);
702 jb_set_uint(jb,
"src_port", addr.
dp);
703 jb_set_uint(jb,
"dest_port", addr.
sp);
706 jb_set_string(jb,
"src_ip", addr.
src_ip);
707 jb_set_string(jb,
"dest_ip", addr.
dst_ip);
709 jb_set_uint(jb,
"src_port", addr.
sp);
710 jb_set_uint(jb,
"dest_port", addr.
dp);
718 if (json_output_ctx->
flags &
720 int stream = (p->
proto == IPPROTO_TCP) ?
727 if (stream && p->
flow != NULL) {
728 const bool stream_data_logged =
729 AlertJsonStreamData(json_output_ctx, aft, p->
flow, p, jb);
732 AlertAddPayload(json_output_ctx, jb, p);
736 AlertAddPayload(json_output_ctx, jb, p);
739 jb_set_uint(jb,
"stream", stream);
766 JsonBuilder *packetjs =
788 for (
int i = 0; i < p->
alerts.
cnt; i++) {
794 JsonBuilder *jb = jb_new_object();
800 jb_set_string(jb,
"timestamp", timebuf);
815 if (PacketIsIPv4(p) || PacketIsIPv6(p)) {
816 return AlertJson(
tv, aft, p);
818 return AlertJsonDecoderEvent(
tv, aft, p);
828 static TmEcode JsonAlertLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
834 if (initdata == NULL)
836 SCLogDebug(
"Error getting context for EveLogAlert. \"initdata\" argument NULL");
882 static void JsonAlertLogDeInitCtxSub(
OutputCtx *output_ctx)
884 SCLogDebug(
"cleaning up sub output_ctx %p", output_ctx);
888 if (json_output_ctx != NULL) {
890 if (xff_cfg != NULL) {
898 static void SetFlag(
const ConfNode *conf,
const char *name, uint16_t flag, uint16_t *out_flags)
902 if (setting != NULL) {
914 static bool warn_no_meta =
false;
921 if (metadata != NULL) {
950 static const char *deprecated_flags[] = {
"http",
"tls",
"ssh",
"smtp",
"dnp3",
"app-layer",
952 for (
int i = 0; deprecated_flags[i] != NULL; i++) {
954 SCLogWarning(
"Found deprecated eve-log.alert flag \"%s\", this flag has no effect",
955 deprecated_flags[i]);
961 if (payload_buffer_value != NULL) {
965 "payload-buffer-size - %s. Killing engine",
966 payload_buffer_value);
969 payload_buffer_size = value;
975 SCLogWarning(
"HTTP body logging has been configured, however, "
976 "metadata logging has not been enabled. HTTP body logging will be "
997 if (
likely(xff_cfg != NULL)) {
1020 if (
unlikely(json_output_ctx == NULL)) {
1025 json_output_ctx->
eve_ctx = ajt;
1027 JsonAlertLogSetupMetadata(json_output_ctx, conf);
1028 json_output_ctx->
xff_cfg = JsonAlertLogGetXffCfg(conf);
1029 if (json_output_ctx->
xff_cfg == NULL) {
1033 output_ctx->
data = json_output_ctx;
1034 output_ctx->
DeInit = JsonAlertLogDeInitCtxSub;
1036 result.
ctx = output_ctx;
1041 if (json_output_ctx != NULL) {
1044 if (output_ctx != NULL) {
1054 JsonAlertLogInitCtxSub, JsonAlertLogger, JsonAlertLogCondition, JsonAlertLogThreadInit,
1055 JsonAlertLogThreadDeinit);
bool PacketCheckAction(const Packet *p, const uint8_t a)
#define PACKET_ALERT_FLAG_STREAM_MATCH
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
const struct Signature_ * s
void EveHttpLogJSONBodyPrintable(JsonBuilder *js, Flow *f, uint64_t tx_id)
void EvePacket(const Packet *p, JsonBuilder *js, uint32_t max_length)
Jsonify a packet.
#define PACKET_ALERT_FLAG_TX
void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
void OutputRegisterPacketSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, PacketLogger PacketLogFunc, PacketLogCondition PacketConditionFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Register a packet output sub-module.
HttpXFFCfg * parent_xff_cfg
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
#define SIG_FLAG_DEST_IS_TARGET
EveJsonSimpleAppLayerLogger * SCEveJsonSimpleGetLogger(AppProto alproto)
bool EveNFSAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
const JsonAddrInfo json_addr_info_zero
#define FLOW_PKT_TOSERVER
#define ACTION_REJECT_ANY
void EveFileInfo(JsonBuilder *jb, const File *ff, const uint64_t tx_id, const uint16_t flags)
#define ACTION_DROP_REJECT
bool EveHttpAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
int ConfValIsTrue(const char *val)
Check if a value is true.
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)
void PrintStringsToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size, const uint8_t *src_buf, const uint32_t src_buf_len)
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *tx, const uint8_t direction)
size_t strlcpy(char *dst, const char *src, size_t siz)
#define JSON_BODY_LOGGING
#define ACTION_REJECT_DST
OutputJsonThreadCtx * ctx
#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)
bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
#define STREAM_BASE_OFFSET(stream)
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
DetectReference * references
Per thread variable structure.
const char * PktSrcToString(enum PktSrcEnum pkt_src)
struct JsonAlertLogThread_ JsonAlertLogThread
#define SCLogWarning(...)
Macro used to log WARNING messages.
void EveHttpLogJSONBodyBase64(JsonBuilder *js, Flow *f, uint64_t tx_id)
Signature reference list.
FramesContainer * AppLayerFramesGetContainer(Flow *f)
#define JSON_STREAM_BUFFER_SIZE
#define LOG_JSON_PAYLOAD_BASE64
#define SIG_FLAG_HAS_TARGET
void AlertJsonHeader(const Packet *p, const PacketAlert *pa, JsonBuilder *js, uint16_t flags, JsonAddrInfo *addr, char *xff_buffer)
#define LOG_JSON_PAYLOAD_LENGTH
#define SIG_FLAG_SRC_IS_TARGET
bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
uint32_t payload_buffer_size
bool ConfNodeHasChildren(const ConfNode *node)
Check if a node has any children.
#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)
char proto[JSON_PROTO_LEN]
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
#define ACTION_REJECT_BOTH
#define PACKET_ALERT_RATE_FILTER_MODIFIED
bool EveEmailAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
#define LOG_JSON_WEBSOCKET_PAYLOAD_BASE64
void(* DeInit)(struct OutputCtx_ *)
char pcap_filename[PATH_MAX]
EveJsonSimpleTxLogFunc LogTx
AlertJsonOutputCtx * json_output_ctx
void EveAddVerdict(JsonBuilder *jb, const Packet *p)
Build verdict object.
void MemBufferFree(MemBuffer *buffer)
int OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *js, OutputJsonThreadCtx *ctx)
void EveAddAppProto(Flow *f, JsonBuilder *js)
int ParseSizeStringU32(const char *size, uint32_t *res)
char src_ip[JSON_ADDR_LEN]
DetectMetadataHead * metadata
#define PACKET_ALERT_FLAG_FRAME
#define SCLogError(...)
Macro used to log ERROR messages.
struct AlertJsonOutputCtx_ AlertJsonOutputCtx
MemBuffer * payload_buffer
#define LOG_JSON_HTTP_BODY_BASE64
int ConfValIsFalse(const char *val)
Check if a value is false.
void FrameJsonLogOneFrame(const uint8_t ipproto, const Frame *frame, Flow *f, const TcpStream *stream, const Packet *p, JsonBuilder *jb, MemBuffer *buffer)
log a single frame
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,...)
void JsonAddrInfoInit(const Packet *p, enum OutputJsonLogDirection dir, JsonAddrInfo *addr)
void HttpXFFGetCfg(ConfNode *conf, HttpXFFCfg *result)
Function to return XFF configuration from a configuration node.
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)
bool EveSMBAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
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 PACKET_ALERT_FLAG_STATE_MATCH
#define DEBUG_VALIDATE_BUG_ON(exp)
#define LOG_JSON_REFERENCE
MemBuffer * MemBufferCreateNew(uint32_t size)
void JsonAlertLogRegister(void)
void EveAddFlow(Flow *f, JsonBuilder *js)
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.