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_set_bool(js,
"tx_guessed",
true);
228 jb_open_object(js,
"alert");
230 jb_set_string(js,
"action", action);
231 jb_set_uint(js,
"gid", pa->
s->
gid);
232 jb_set_uint(js,
"signature_id", pa->
s->
id);
233 jb_set_uint(js,
"rev", pa->
s->
rev);
236 jb_set_string(js,
"signature", pa->
s->
msg ? pa->
s->
msg:
"");
238 jb_set_uint(js,
"severity", pa->
s->
prio);
241 jb_set_uint(js,
"tenant_id", p->
tenant_id);
245 AlertJsonSourceTarget(p, pa, js, addr);
249 AlertJsonReference(pa, js);
253 AlertJsonMetadata(pa, js);
257 jb_set_string(js,
"rule", pa->
s->
sig_str);
259 if (xff_buffer && xff_buffer[0]) {
260 jb_set_string(js,
"xff", xff_buffer);
266 static void AlertJsonTunnel(
const Packet *p, JsonBuilder *js)
268 if (p->
root == NULL) {
272 jb_open_object(js,
"tunnel");
281 jb_set_string(js,
"src_ip", addr.
src_ip);
282 jb_set_uint(js,
"src_port", addr.
sp);
283 jb_set_string(js,
"dest_ip", addr.
dst_ip);
284 jb_set_uint(js,
"dest_port", addr.
dp);
285 jb_set_string(js,
"proto", addr.
proto);
289 jb_set_uint(js,
"pcap_cnt", pcap_cnt);
311 jb_set_string(js,
"payload_printable", (
char *)printable_buf);
315 static void AlertAddAppLayer(
const Packet *p, JsonBuilder *jb,
316 const uint64_t tx_id,
const uint16_t option_flags)
320 JsonBuilderMark mark = { 0, 0, 0 };
321 if (al && al->
LogTx) {
322 void *state = FlowGetAppState(p->
flow);
326 jb_get_mark(jb, &mark);
334 if (!SCWebSocketLogDetails(tx, jb, pp, pb64)) {
335 jb_restore_mark(jb, &mark);
341 if (!al->
LogTx(tx, jb)) {
342 jb_restore_mark(jb, &mark);
351 jb_open_object(jb,
"http");
363 jb_get_mark(jb, &mark);
364 jb_open_object(jb,
"smtp");
368 jb_restore_mark(jb, &mark);
370 jb_get_mark(jb, &mark);
371 jb_open_object(jb,
"email");
375 jb_restore_mark(jb, &mark);
380 jb_get_mark(jb, &mark);
381 jb_open_object(jb,
"rpc");
385 jb_restore_mark(jb, &mark);
388 jb_get_mark(jb, &mark);
389 jb_open_object(jb,
"nfs");
393 jb_restore_mark(jb, &mark);
397 jb_get_mark(jb, &mark);
398 jb_open_object(jb,
"smb");
402 jb_restore_mark(jb, &mark);
406 jb_get_mark(jb, &mark);
408 jb_restore_mark(jb, &mark);
412 void *state = FlowGetAppState(p->
flow);
416 jb_get_mark(jb, &mark);
417 jb_open_object(jb,
"dcerpc");
418 if (p->
proto == IPPROTO_TCP) {
419 if (!rs_dcerpc_log_json_record_tcp(state, tx, jb)) {
420 jb_restore_mark(jb, &mark);
423 if (!rs_dcerpc_log_json_record_udp(state, tx, jb)) {
424 jb_restore_mark(jb, &mark);
437 static void AlertAddFiles(
const Packet *p, JsonBuilder *jb,
const uint64_t tx_id)
439 const uint8_t direction =
455 jb_open_array(jb,
"files");
468 static void AlertAddFrame(
469 const Packet *p,
const int64_t frame_id, JsonBuilder *jb,
MemBuffer *buffer)
475 if (frames_container == NULL)
480 if (p->
proto == IPPROTO_TCP) {
484 frames = &frames_container->
toserver;
487 frames = &frames_container->
toclient;
493 }
else if (p->
proto == IPPROTO_UDP) {
495 frames = &frames_container->
toserver;
497 frames = &frames_container->
toclient;
514 jb_open_object(jb,
"verdict");
531 jb_open_array(jb,
"reject");
536 jb_append_string(jb,
"icmp-prohib");
539 jb_append_string(jb,
"tcp-reset");
562 static int AlertJsonStreamDataCallback(
563 void *cb_data,
const uint8_t *input,
const uint32_t input_len,
const uint64_t input_offset)
566 if (input_offset > cbd->
last_re) {
568 cbd->
payload,
"[%" PRIu64
" bytes missing]", input_offset - cbd->
last_re);
573 if (written < input_len)
575 cbd->
last_re = input_offset + input_len;
609 jb_set_string(jb,
"payload_printable", (
char *)printable_buf);
623 for (
int i = 0; i < p->
alerts.
cnt; i++) {
675 if (PacketIsTunnel(p)) {
676 AlertJsonTunnel(p, jb);
679 if (p->
flow != NULL) {
682 AlertAddAppLayer(p, jb, pa->
tx_id, json_output_ctx->
flags);
686 AlertAddFiles(p, jb, pa->
tx_id);
693 jb_set_string(jb,
"direction",
"to_server");
695 jb_set_string(jb,
"direction",
"to_client");
699 jb_open_object(jb,
"flow");
702 jb_set_string(jb,
"src_ip", addr.
dst_ip);
703 jb_set_string(jb,
"dest_ip", addr.
src_ip);
705 jb_set_uint(jb,
"src_port", addr.
dp);
706 jb_set_uint(jb,
"dest_port", addr.
sp);
709 jb_set_string(jb,
"src_ip", addr.
src_ip);
710 jb_set_string(jb,
"dest_ip", addr.
dst_ip);
712 jb_set_uint(jb,
"src_port", addr.
sp);
713 jb_set_uint(jb,
"dest_port", addr.
dp);
721 if (json_output_ctx->
flags &
723 int stream = (p->
proto == IPPROTO_TCP) ?
730 if (stream && p->
flow != NULL) {
731 const bool stream_data_logged =
732 AlertJsonStreamData(json_output_ctx, aft, p->
flow, p, jb);
735 AlertAddPayload(json_output_ctx, jb, p);
739 AlertAddPayload(json_output_ctx, jb, p);
742 jb_set_uint(jb,
"stream", stream);
769 JsonBuilder *packetjs =
788 for (
int i = 0; i < p->
alerts.
cnt; i++) {
801 if (PacketIsTunnel(p)) {
802 AlertJsonTunnel(p, jb);
830 if (PacketIsIPv4(p) || PacketIsIPv6(p)) {
831 return AlertJson(
tv, aft, p);
833 return AlertJsonDecoderEvent(
tv, aft, p);
843 static TmEcode JsonAlertLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
849 if (initdata == NULL)
851 SCLogDebug(
"Error getting context for EveLogAlert. \"initdata\" argument NULL");
897 static void JsonAlertLogDeInitCtxSub(
OutputCtx *output_ctx)
899 SCLogDebug(
"cleaning up sub output_ctx %p", output_ctx);
903 if (json_output_ctx != NULL) {
905 if (xff_cfg != NULL) {
913 static void SetFlag(
const ConfNode *conf,
const char *name, uint16_t flag, uint16_t *out_flags)
917 if (setting != NULL) {
929 static bool warn_no_meta =
false;
936 if (metadata != NULL) {
965 static const char *deprecated_flags[] = {
"http",
"tls",
"ssh",
"smtp",
"dnp3",
"app-layer",
967 for (
int i = 0; deprecated_flags[i] != NULL; i++) {
969 SCLogWarning(
"Found deprecated eve-log.alert flag \"%s\", this flag has no effect",
970 deprecated_flags[i]);
976 if (payload_buffer_value != NULL) {
980 "payload-buffer-size - %s. Killing engine",
981 payload_buffer_value);
984 payload_buffer_size = value;
990 SCLogWarning(
"HTTP body logging has been configured, however, "
991 "metadata logging has not been enabled. HTTP body logging will be "
1012 if (
likely(xff_cfg != NULL)) {
1035 if (
unlikely(json_output_ctx == NULL)) {
1040 json_output_ctx->
eve_ctx = ajt;
1042 JsonAlertLogSetupMetadata(json_output_ctx, conf);
1043 json_output_ctx->
xff_cfg = JsonAlertLogGetXffCfg(conf);
1044 if (json_output_ctx->
xff_cfg == NULL) {
1048 output_ctx->
data = json_output_ctx;
1049 output_ctx->
DeInit = JsonAlertLogDeInitCtxSub;
1051 result.
ctx = output_ctx;
1056 if (json_output_ctx != NULL) {
1059 if (output_ctx != NULL) {
1069 JsonAlertLogInitCtxSub, JsonAlertLogger, JsonAlertLogCondition, JsonAlertLogThreadInit,
1070 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 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)
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *js, OutputJsonThreadCtx *ctx)
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 PACKET_ALERT_FLAG_TX_GUESSED
#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)
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.