suricata
output-json-smb.c
Go to the documentation of this file.
1 /* Copyright (C) 2017-2020 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 "debug.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-unittest.h"
36 #include "util-buffer.h"
37 #include "util-debug.h"
38 #include "util-byte.h"
39 
40 #include "output.h"
41 #include "output-json.h"
42 
43 #include "app-layer.h"
44 #include "app-layer-parser.h"
45 
46 #include "output-json-smb.h"
47 
48 #include "rust.h"
49 
50 bool EveSMBAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
51 {
52  SMBState *state = FlowGetAppState(f);
53  if (state) {
54  SMBTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SMB, state, tx_id);
55  if (tx) {
56  return rs_smb_log_json_response(jb, state, tx);
57  }
58  }
59  return false;
60 }
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  OutputJsonThreadCtx *thread = thread_data;
66 
67  JsonBuilder *jb = CreateEveHeader(p, LOG_DIR_FLOW, "smb", NULL);
68  if (unlikely(jb == NULL)) {
69  return TM_ECODE_FAILED;
70  }
71 
72  jb_open_object(jb, "smb");
73  if (!rs_smb_log_json_response(jb, state, tx)) {
74  goto error;
75  }
76  jb_close(jb);
77 
78  EveAddCommonOptions(&thread->ctx->cfg, p, f, jb);
79  MemBufferReset(thread->buffer);
80  OutputJsonBuilderBuffer(jb, thread->file_ctx, &thread->buffer);
81 
82  jb_free(jb);
83  return TM_ECODE_OK;
84 
85 error:
86  jb_free(jb);
87  return TM_ECODE_FAILED;
88 }
89 
90 static OutputInitResult SMBLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
91 {
94  return OutputJsonLogInitSub(conf, parent_ctx);
95 }
96 
98 {
99  /* Register as an eve sub-module. */
100  OutputRegisterTxSubModule(LOGGER_JSON_SMB, "eve-log", "JsonSMBLog",
101  "eve-log.smb", SMBLogInitSub, ALPROTO_SMB,
102  JsonSMBLogger, JsonLogThreadInit,
103  JsonLogThreadDeinit, NULL);
104 
105  SCLogDebug("SMB JSON logger registered.");
106 }
util-byte.h
tm-threads.h
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:50
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:110
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:104
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonSMBLogRegister
void JsonSMBLogRegister(void)
Definition: output-json-smb.c:97
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
Flow_::proto
uint8_t proto
Definition: flow.h:365
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:75
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:978
rust.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:444
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1102
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:109
util-debug.h
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:846
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:456
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
app-layer-parser.h
Packet_
Definition: decode.h:414
conf.h
OutputJsonThreadCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:111
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1032
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
OutputJsonThreadCtx_::buffer
MemBuffer * buffer
Definition: output-json.h:112
suricata-common.h
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, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:409
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
ConfNode_
Definition: conf.h:32
util-buffer.h
LOGGER_JSON_SMB
@ LOGGER_JSON_SMB
Definition: suricata-common.h:459
output-json-smb.h
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:109
EveSMBAddMetadata
bool EveSMBAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *jb)
Definition: output-json-smb.c:50
debug.h
output.h
app-layer.h