Go to the documentation of this file.
70 #define MODULE_NAME "JsonAlertLog"
72 #define LOG_JSON_PAYLOAD BIT_U16(0)
73 #define LOG_JSON_PACKET BIT_U16(1)
74 #define LOG_JSON_PAYLOAD_BASE64 BIT_U16(2)
75 #define LOG_JSON_TAGGED_PACKETS BIT_U16(3)
76 #define LOG_JSON_APP_LAYER BIT_U16(4)
77 #define LOG_JSON_FLOW BIT_U16(5)
78 #define LOG_JSON_HTTP_BODY BIT_U16(6)
79 #define LOG_JSON_HTTP_BODY_BASE64 BIT_U16(7)
80 #define LOG_JSON_RULE_METADATA BIT_U16(8)
81 #define LOG_JSON_RULE BIT_U16(9)
82 #define LOG_JSON_VERDICT BIT_U16(10)
83 #define LOG_JSON_WEBSOCKET_PAYLOAD BIT_U16(11)
84 #define LOG_JSON_WEBSOCKET_PAYLOAD_BASE64 BIT_U16(12)
85 #define LOG_JSON_PAYLOAD_LENGTH BIT_U16(13)
86 #define LOG_JSON_REFERENCE BIT_U16(14)
88 #define METADATA_DEFAULTS ( LOG_JSON_FLOW | \
89 LOG_JSON_APP_LAYER | \
90 LOG_JSON_RULE_METADATA)
92 #define JSON_BODY_LOGGING \
93 (LOG_JSON_HTTP_BODY | LOG_JSON_HTTP_BODY_BASE64 | LOG_JSON_WEBSOCKET_PAYLOAD | \
94 LOG_JSON_WEBSOCKET_PAYLOAD_BASE64)
96 #define JSON_STREAM_BUFFER_SIZE 4096
113 static void AlertJsonSourceTarget(
116 SCJbOpenObject(js,
"source");
118 SCJbSetString(js,
"ip", addr->
src_ip);
126 SCJbSetUint(js,
"port", addr->
sp);
130 SCJbSetString(js,
"ip", addr->
dst_ip);
138 SCJbSetUint(js,
"port", addr->
dp);
144 SCJbOpenObject(js,
"target");
146 SCJbSetString(js,
"ip", addr->
dst_ip);
154 SCJbSetUint(js,
"port", addr->
dp);
158 SCJbSetString(js,
"ip", addr->
src_ip);
166 SCJbSetUint(js,
"port", addr->
sp);
180 SCJbOpenArray(jb,
"references");
192 const char *key = kv->
key ? kv->
key :
"";
194 char kv_store[size_needed];
195 snprintf(kv_store, size_needed,
"%s%s", key, reference);
196 SCJbAppendString(jb, kv_store);
212 const char *action =
"allowed";
229 SCJbSetUint(js,
"tx_id", pa->
tx_id);
232 SCJbSetBool(js,
"tx_guessed",
true);
235 SCJbOpenObject(js,
"alert");
237 SCJbSetString(js,
"action", action);
241 SCJbSetUint(js,
"gid", pa->
s->
gid);
242 SCJbSetUint(js,
"signature_id", pa->
s->
id);
243 SCJbSetUint(js,
"rev", pa->
s->
rev);
246 SCJbSetString(js,
"signature", pa->
s->
msg ? pa->
s->
msg :
"");
248 SCJbSetUint(js,
"severity", pa->
s->
prio);
255 AlertJsonSourceTarget(
p, pa, js, addr);
259 AlertJsonReference(pa, js);
263 AlertJsonMetadata(pa, js);
267 SCJbOpenObject(js,
"context");
274 json_info = json_info->
next;
279 SCJbSetString(js,
"rule", pa->
s->
sig_str);
281 if (xff_buffer && xff_buffer[0]) {
282 SCJbSetString(js,
"xff", xff_buffer);
290 if (
p->
root == NULL) {
294 SCJbOpenObject(js,
"tunnel");
301 SCJbSetString(js,
"src_ip", addr.
src_ip);
302 SCJbSetUint(js,
"src_port", addr.
sp);
303 SCJbSetString(js,
"dest_ip", addr.
dst_ip);
304 SCJbSetUint(js,
"dest_port", addr.
dp);
305 SCJbSetString(js,
"proto", addr.
proto);
310 SCJbSetUint(js,
"pcap_cnt", pcap_cnt);
330 static void AlertAddAppLayerStates(
const Packet *
p,
const AppProto alproto,
const uint8_t sub_state,
335 if (sub_state == 0) {
336 SCJbSetString(jb,
"ts_progress",
338 SCJbSetString(jb,
"tc_progress",
343 SCJbSetString(jb,
"sub_state", sname);
345 SCJbSetString(jb,
"ts_progress",
347 alproto, sub_state, (uint8_t)
ts, STREAM_TOSERVER));
348 SCJbSetString(jb,
"tc_progress",
350 alproto, sub_state, (uint8_t)tc, STREAM_TOCLIENT));
355 const uint8_t sub_state,
const uint16_t option_flags)
359 void *state = FlowGetAppState(
p->
flow);
367 AlertAddAppLayerStates(
p,
proto, sub_state, tx, jb);
369 SCJsonBuilderMark mark = { 0, 0, 0 };
370 if (al && al->
LogTx) {
371 SCJbGetMark(jb, &mark);
379 if (!SCWebSocketLogDetails(tx, jb, pp, pb64)) {
380 SCJbRestoreMark(jb, &mark);
386 if (!al->
LogTx(tx, jb)) {
387 SCJbRestoreMark(jb, &mark);
395 SCJbOpenObject(jb,
"http");
407 SCJbGetMark(jb, &mark);
408 SCJbOpenObject(jb,
"smtp");
412 SCJbRestoreMark(jb, &mark);
414 SCJbGetMark(jb, &mark);
415 SCJbOpenObject(jb,
"email");
419 SCJbRestoreMark(jb, &mark);
424 SCJbGetMark(jb, &mark);
425 SCJbOpenObject(jb,
"rpc");
429 SCJbRestoreMark(jb, &mark);
432 SCJbGetMark(jb, &mark);
433 SCJbOpenObject(jb,
"nfs");
437 SCJbRestoreMark(jb, &mark);
441 SCJbGetMark(jb, &mark);
442 SCJbOpenObject(jb,
"smb");
446 SCJbRestoreMark(jb, &mark);
450 SCJbGetMark(jb, &mark);
452 SCJbRestoreMark(jb, &mark);
456 SCJbGetMark(jb, &mark);
457 SCJbOpenObject(jb,
"dcerpc");
458 if (
p->
proto == IPPROTO_TCP) {
459 if (!SCDcerpcLogJsonRecordTcp(state, tx, jb)) {
460 SCJbRestoreMark(jb, &mark);
463 if (!SCDcerpcLogJsonRecordUdp(state, tx, jb)) {
464 SCJbRestoreMark(jb, &mark);
476 const uint8_t direction =
492 SCJbOpenArray(jb,
"files");
505 static void AlertAddFrame(
512 if (frames_container == NULL)
517 if (
p->
proto == IPPROTO_TCP) {
521 frames = &frames_container->
toserver;
524 frames = &frames_container->
toclient;
530 }
else if (
p->
proto == IPPROTO_UDP) {
532 frames = &frames_container->
toserver;
534 frames = &frames_container->
toclient;
551 SCJbOpenObject(jb,
"verdict");
555 packet_action, alert_action);
571 SCJbOpenArray(jb,
"reject");
576 SCJbAppendString(jb,
"icmp-prohib");
579 SCJbAppendString(jb,
"tcp-reset");
604 static int AlertJsonStreamDataCallback(
605 void *cb_data,
const uint8_t *input,
const uint32_t input_len,
const uint64_t input_offset)
608 if (input_offset > cbd->
last_re) {
610 cbd->
payload,
"[%" PRIu64
" bytes missing]", input_offset - cbd->
last_re);
615 if (written < input_len)
617 cbd->
last_re = input_offset + input_len;
647 SCJbSetPrintAsciiString(
659 SCJbOpenObject(jb,
"firewall");
660 const char *hook = NULL;
661 char hook_string[256];
664 const uint8_t dir_flag =
671 const char *sub_state_name =
673 if (state != NULL && sub_state_name != NULL) {
674 snprintf(hook_string,
sizeof(hook_string),
"%s:%s:%s",
685 snprintf(hook_string,
sizeof(hook_string),
"%s:%s",
693 hook =
"packet:filter";
696 hook =
"packet:pre_flow";
699 hook =
"packet:pre_stream";
703 SCJbSetString(jb,
"hook", hook);
705 char policy_string[64] =
"";
707 if (strlen(policy_string) > 0) {
708 SCJbSetString(jb,
"policy", policy_string);
773 if (PacketIsTunnel(
p)) {
774 AlertJsonTunnel(
p, jb, &json_output_ctx->
eve_ctx->
cfg);
778 AlertJsonAddFirewall(jb, pa->
s);
781 if (
p->
flow != NULL) {
788 AlertAddFiles(
p, jb, pa->
tx_id);
795 SCJbSetString(jb,
"direction",
"to_server");
797 SCJbSetString(jb,
"direction",
"to_client");
801 SCJbOpenObject(jb,
"flow");
804 SCJbSetString(jb,
"src_ip", addr.
dst_ip);
805 SCJbSetString(jb,
"dest_ip", addr.
src_ip);
807 SCJbSetUint(jb,
"src_port", addr.
dp);
808 SCJbSetUint(jb,
"dest_port", addr.
sp);
811 SCJbSetString(jb,
"src_ip", addr.
src_ip);
812 SCJbSetString(jb,
"dest_ip", addr.
dst_ip);
814 SCJbSetUint(jb,
"src_port", addr.
sp);
815 SCJbSetUint(jb,
"dest_port", addr.
dp);
823 if (json_output_ctx->
flags &
825 int stream = (
p->
proto == IPPROTO_TCP) ?
832 if (stream &&
p->
flow != NULL) {
833 const bool stream_data_logged =
834 AlertJsonStreamData(json_output_ctx, aft,
p->
flow,
p, jb);
837 AlertAddPayload(json_output_ctx, jb,
p);
841 AlertAddPayload(json_output_ctx, jb,
p);
844 SCJbSetUint(jb,
"stream", stream);
904 if (PacketIsTunnel(
p)) {
905 AlertJsonTunnel(
p, jb, &json_output_ctx->
eve_ctx->
cfg);
933 if (PacketIsIPv4(
p) || PacketIsIPv6(
p)) {
934 return AlertJson(
tv, aft,
p);
936 return AlertJsonDecoderEvent(
tv, aft,
p);
946 static TmEcode JsonAlertLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
952 if (initdata == NULL)
954 SCLogDebug(
"Error getting context for EveLogAlert. \"initdata\" argument NULL");
1000 static void JsonAlertLogDeInitCtxSub(
OutputCtx *output_ctx)
1002 SCLogDebug(
"cleaning up sub output_ctx %p", output_ctx);
1006 if (json_output_ctx != NULL) {
1008 if (xff_cfg != NULL) {
1016 static void SetFlag(
const SCConfNode *conf,
const char *
name, uint16_t flag, uint16_t *out_flags)
1020 if (setting != NULL) {
1024 *out_flags &= ~flag;
1031 static bool warn_no_meta =
false;
1038 if (metadata != NULL) {
1043 if (rule_metadata) {
1067 static const char *deprecated_flags[] = {
"http",
"tls",
"ssh",
"smtp",
"dnp3",
"app-layer",
1069 for (
int i = 0; deprecated_flags[i] != NULL; i++) {
1071 SCLogWarning(
"Found deprecated eve-log.alert flag \"%s\", this flag has no effect",
1072 deprecated_flags[i]);
1078 if (payload_buffer_value != NULL) {
1082 "payload-buffer-size - %s. Killing engine",
1083 payload_buffer_value);
1085 }
else if (value == 0) {
1087 SCLogError(
"Error payload-buffer-size should not be 0");
1090 payload_buffer_size = value;
1096 SCLogWarning(
"HTTP body logging has been configured, however, "
1097 "metadata logging has not been enabled. HTTP body logging will be "
1100 warn_no_meta =
true;
1118 if (
likely(xff_cfg != NULL)) {
1141 if (
unlikely(json_output_ctx == NULL)) {
1146 json_output_ctx->
eve_ctx = ajt;
1148 JsonAlertLogSetupMetadata(json_output_ctx, conf);
1149 json_output_ctx->
xff_cfg = JsonAlertLogGetXffCfg(conf);
1150 if (json_output_ctx->
xff_cfg == NULL) {
1154 output_ctx->
data = json_output_ctx;
1155 output_ctx->
DeInit = JsonAlertLogDeInitCtxSub;
1157 result.
ctx = output_ctx;
1162 if (json_output_ctx != NULL) {
1165 if (output_ctx != NULL) {
1176 .ConditionFunc = JsonAlertLogCondition,
1177 .ThreadInitFunc = JsonAlertLogThreadInit,
1178 .ThreadDeinitFunc = JsonAlertLogThreadDeinit,
1179 .ThreadExitPrintStatsFunc = NULL,
1183 JsonAlertLogInitCtxSub, &output_logger_functions);
struct SCJsonBuilder SCJsonBuilder
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)
@ DETECT_TABLE_APP_FILTER
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)
uint8_t app_progress_hook
FramesContainer * AppLayerFramesGetContainer(const Flow *f)
uint64_t PcapPacketCntGet(const Packet *p)
@ DETECT_TABLE_PACKET_PRE_STREAM
HttpXFFCfg * parent_xff_cfg
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
void EveAddAppProto(Flow *f, SCJsonBuilder *js)
#define SIG_FLAG_DEST_IS_TARGET
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)
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its normalized string equivalent.
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)
#define SIG_FLAG_TOSERVER
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.
@ DETECT_TABLE_PACKET_PRE_FLOW
bool EveNFSAddMetadataRPC(const Flow *f, uint64_t tx_id, SCJsonBuilder *jb)
const char * PktSrcToString(enum PktSrcEnum pkt_src)
@ DETECT_TABLE_PACKET_FILTER
void DetectFirewallPolicyToString(const struct DetectFirewallPolicy *p, char *out, size_t out_size)
struct JsonAlertLogThread_ JsonAlertLogThread
#define SCLogWarning(...)
Macro used to log WARNING messages.
Signature reference list.
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *tx, uint8_t flags)
get the progress value for a tx/protocol
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)
const char * AppLayerParserGetSubStateProgressName(const AppProto alproto, const uint8_t sub_state, const uint8_t state, const uint8_t dir_flag)
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
const char * AppLayerParserGetSubStateName(const AppProto alproto, const uint8_t sub_state)
#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,...)
bool AppLayerParserSupportsSubStates(const AppProto alproto)
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)