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
34bool 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
47typedef struct LogSmbFileCtx_ {
48 uint64_t flags;
49 // generic context needed for init by CreateEveThreadCtx
50 // comes from parent in SMBLogInitSub
53
54// wrapper structure
55typedef struct LogSmbLogThread_ {
56 // generic structure
58 // smb-specific structure
61
62static 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 =
68 CreateEveHeaderWithTxId(p, LOG_DIR_FLOW, "smb", NULL, tx_id, thread->ctx->ctx);
69 if (unlikely(jb == NULL)) {
70 return TM_ECODE_FAILED;
71 }
72
73 SCJbOpenObject(jb, "smb");
74 if (!SCSmbLogJsonResponse(jb, state, tx, thread->smblog_ctx->flags)) {
75 goto error;
76 }
77 SCJbClose(jb);
78
79 OutputJsonBuilderBuffer(tv, p, p->flow, jb, thread->ctx);
80
81 SCJbFree(jb);
82 return TM_ECODE_OK;
83
84error:
85 SCJbFree(jb);
86 return TM_ECODE_FAILED;
87}
88
89static void LogSmbLogDeInitCtxSub(OutputCtx *output_ctx)
90{
91 LogSmbFileCtx *smblog_ctx = (LogSmbFileCtx *)output_ctx->data;
92 SCFree(smblog_ctx);
93 SCFree(output_ctx);
94}
95
96static OutputInitResult SMBLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
97{
100 OutputInitResult r = OutputJsonLogInitSub(conf, parent_ctx);
101 if (r.ok) {
102 // generic init is ok, try smb-specific one
103 LogSmbFileCtx *smblog_ctx = SCCalloc(1, sizeof(LogSmbFileCtx));
104 if (unlikely(smblog_ctx == NULL)) {
105 SCFree(r.ctx);
106 r.ctx = NULL;
107 r.ok = false;
108 return r;
109 }
110 smblog_ctx->eve_ctx = parent_ctx->data;
111 // parse config for flags/types to log
112 smblog_ctx->flags = SCSmbLogParseConfig(conf);
113 r.ctx->data = smblog_ctx;
114 r.ctx->DeInit = LogSmbLogDeInitCtxSub;
115 }
116 return r;
117}
118
119static TmEcode LogSmbLogThreadInit(ThreadVars *t, const void *initdata, void **data)
120{
121 if (initdata == NULL) {
122 return TM_ECODE_FAILED;
123 }
124
125 LogSmbLogThread *aft = SCCalloc(1, sizeof(LogSmbLogThread));
126 if (unlikely(aft == NULL)) {
127 return TM_ECODE_FAILED;
128 }
129
130 aft->smblog_ctx = ((OutputCtx *)initdata)->data;
131 aft->ctx = CreateEveThreadCtx(t, aft->smblog_ctx->eve_ctx);
132 if (!aft->ctx) {
133 SCFree(aft);
134 return TM_ECODE_FAILED;
135 }
136
137 *data = (void *)aft;
138 return TM_ECODE_OK;
139}
140
141// LogSmbLogThread structure wraps a generic OutputJsonThreadCtx
142// created by CreateEveThreadCtx
143static TmEcode LogSmbLogThreadDeinit(ThreadVars *t, void *data)
144{
145 LogSmbLogThread *aft = (LogSmbLogThread *)data;
146 TmEcode r = JsonLogThreadDeinit(t, aft->ctx);
147 SCFree(aft);
148 return r;
149}
150
152{
153 /* Register as an eve sub-module. */
154 OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonSMBLog", "eve-log.smb", SMBLogInitSub,
155 ALPROTO_SMB, JsonSMBLogger, LogSmbLogThreadInit, LogSmbLogThreadDeinit);
156
157 SCLogDebug("SMB JSON logger registered.");
158}
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
@ ALPROTO_SMB
ThreadVars * tv
@ LOG_DIR_FLOW
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
OutputInitResult OutputJsonLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
bool EveSMBAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *jb)
struct LogSmbLogThread_ LogSmbLogThread
void JsonSMBLogRegister(void)
struct LogSmbFileCtx_ LogSmbFileCtx
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
SCJsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
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:406
Flow data structure.
Definition flow.h:356
uint8_t proto
Definition flow.h:378
OutputJsonCtx * eve_ctx
LogSmbFileCtx * smblog_ctx
OutputJsonThreadCtx * ctx
void * data
Definition tm-modules.h:91
void(* DeInit)(struct OutputCtx_ *)
Definition tm-modules.h:94
OutputCtx * ctx
Definition output.h:47
OutputJsonCtx * ctx
Definition output-json.h:84
struct Flow_ * flow
Definition decode.h:546
Per thread variable structure.
Definition threadvars.h:58
@ LOGGER_JSON_TX
@ TM_ECODE_FAILED
@ TM_ECODE_OK
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define unlikely(expr)