suricata
output-json-smb.c
Go to the documentation of this file.
1 /* Copyright (C) 2017-2021 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Implement JSON/eve logging app-layer SMB.
24  */
25 
26 #include "suricata-common.h"
27 #include "util-buffer.h"
28 #include "output.h"
29 #include "output-json.h"
30 #include "app-layer-parser.h"
31 #include "output-json-smb.h"
32 #include "rust.h"
33 
34 bool EveSMBAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *jb)
35 {
36  SMBState *state = FlowGetAppState(f);
37  if (state) {
38  SMBTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SMB, state, tx_id);
39  if (tx) {
40  // flags 0 means log all
41  return SCSmbLogJsonResponse(jb, state, tx, 0);
42  }
43  }
44  return false;
45 }
46 
47 typedef struct LogSmbFileCtx_ {
48  uint64_t flags;
49  // generic context needed for init by CreateEveThreadCtx
50  // comes from parent in SMBLogInitSub
53 
54 // wrapper structure
55 typedef struct LogSmbLogThread_ {
56  // generic structure
58  // smb-specific structure
61 
62 static int JsonSMBLogger(ThreadVars *tv, void *thread_data,
63  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
64 {
65  LogSmbLogThread *thread = thread_data;
66 
67  SCJsonBuilder *jb = CreateEveHeader(p, LOG_DIR_FLOW, "smb", NULL, thread->ctx->ctx);
68  if (unlikely(jb == NULL)) {
69  return TM_ECODE_FAILED;
70  }
71 
72  SCJbOpenObject(jb, "smb");
73  if (!SCSmbLogJsonResponse(jb, state, tx, thread->smblog_ctx->flags)) {
74  goto error;
75  }
76  SCJbClose(jb);
77 
78  OutputJsonBuilderBuffer(tv, p, p->flow, jb, thread->ctx);
79 
80  SCJbFree(jb);
81  return TM_ECODE_OK;
82 
83 error:
84  SCJbFree(jb);
85  return TM_ECODE_FAILED;
86 }
87 
88 static void LogSmbLogDeInitCtxSub(OutputCtx *output_ctx)
89 {
90  LogSmbFileCtx *smblog_ctx = (LogSmbFileCtx *)output_ctx->data;
91  SCFree(smblog_ctx);
92  SCFree(output_ctx);
93 }
94 
95 static OutputInitResult SMBLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
96 {
99  OutputInitResult r = OutputJsonLogInitSub(conf, parent_ctx);
100  if (r.ok) {
101  // generic init is ok, try smb-specific one
102  LogSmbFileCtx *smblog_ctx = SCCalloc(1, sizeof(LogSmbFileCtx));
103  if (unlikely(smblog_ctx == NULL)) {
104  SCFree(r.ctx);
105  r.ctx = NULL;
106  r.ok = false;
107  return r;
108  }
109  smblog_ctx->eve_ctx = parent_ctx->data;
110  // parse config for flags/types to log
111  smblog_ctx->flags = SCSmbLogParseConfig(conf);
112  r.ctx->data = smblog_ctx;
113  r.ctx->DeInit = LogSmbLogDeInitCtxSub;
114  }
115  return r;
116 }
117 
118 static TmEcode LogSmbLogThreadInit(ThreadVars *t, const void *initdata, void **data)
119 {
120  if (initdata == NULL) {
121  return TM_ECODE_FAILED;
122  }
123 
124  LogSmbLogThread *aft = SCCalloc(1, sizeof(LogSmbLogThread));
125  if (unlikely(aft == NULL)) {
126  return TM_ECODE_FAILED;
127  }
128 
129  aft->smblog_ctx = ((OutputCtx *)initdata)->data;
130  aft->ctx = CreateEveThreadCtx(t, aft->smblog_ctx->eve_ctx);
131  if (!aft->ctx) {
132  SCFree(aft);
133  return TM_ECODE_FAILED;
134  }
135 
136  *data = (void *)aft;
137  return TM_ECODE_OK;
138 }
139 
140 // LogSmbLogThread structure wraps a generic OutputJsonThreadCtx
141 // created by CreateEveThreadCtx
142 static TmEcode LogSmbLogThreadDeinit(ThreadVars *t, void *data)
143 {
144  LogSmbLogThread *aft = (LogSmbLogThread *)data;
145  TmEcode r = JsonLogThreadDeinit(t, aft->ctx);
146  SCFree(aft);
147  return r;
148 }
149 
151 {
152  /* Register as an eve sub-module. */
153  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonSMBLog", "eve-log.smb", SMBLogInitSub,
154  ALPROTO_SMB, JsonSMBLogger, LogSmbLogThreadInit, LogSmbLogThreadDeinit);
155 
156  SCLogDebug("SMB JSON logger registered.");
157 }
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:84
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonSMBLogRegister
void JsonSMBLogRegister(void)
Definition: output-json-smb.c:150
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Flow_::proto
uint8_t proto
Definition: flow.h:378
CreateEveHeader
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:834
OutputJsonCtx_
Definition: output-json.h:75
Flow_
Flow data structure.
Definition: flow.h:356
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Definition: output.c:383
LogSmbLogThread_::smblog_ctx
LogSmbFileCtx * smblog_ctx
Definition: output-json-smb.c:59
rust.h
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:992
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
OutputCtx_::data
void * data
Definition: tm-modules.h:87
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
EveSMBAddMetadata
bool EveSMBAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *jb)
Definition: output-json-smb.c:34
OutputCtx_
Definition: tm-modules.h:84
OutputJsonThreadCtx_
Definition: output-json.h:83
LogSmbFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-smb.c:51
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:82
LogSmbLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-smb.c:57
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:490
LogSmbFileCtx_::flags
uint64_t flags
Definition: output-json-smb.c:48
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
LogSmbLogThread
struct LogSmbLogThread_ LogSmbLogThread
OutputInitResult_::ok
bool ok
Definition: output.h:48
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-eve-bindgen.h:33
app-layer-parser.h
LogSmbFileCtx_
Definition: output-json-smb.c:47
Packet_
Definition: decode.h:484
LogSmbFileCtx
struct LogSmbFileCtx_ LogSmbFileCtx
TmEcode
TmEcode
Definition: tm-threads-common.h:80
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1118
OutputInitResult_
Definition: output.h:46
Packet_::flow
struct Flow_ * flow
Definition: decode.h:529
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:90
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:477
SCFree
#define SCFree(p)
Definition: util-mem.h:61
LogSmbLogThread_
Definition: output-json-smb.c:55
util-buffer.h
output-json-smb.h
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:43
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:132
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCConfNode_
Definition: conf.h:32
output.h