suricata
output-json-smb.c
Go to the documentation of this file.
1 /* Copyright (C) 2017-2018 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 #ifdef HAVE_RUST
49 #ifdef HAVE_LIBJANSSON
50 #include "rust.h"
51 #include "rust-smb-log-gen.h"
52 
53 json_t *JsonSMBAddMetadata(const Flow *f, uint64_t tx_id)
54 {
55  SMBState *state = FlowGetAppState(f);
56  if (state) {
57  SMBTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SMB, state, tx_id);
58  if (tx) {
59  return rs_smb_log_json_response(state, tx);
60  }
61  }
62 
63  return NULL;
64 }
65 
66 static int JsonSMBLogger(ThreadVars *tv, void *thread_data,
67  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
68 {
69  OutputJsonThreadCtx *thread = thread_data;
70  json_t *js, *smbjs;
71 
72  js = CreateJSONHeader(p, LOG_DIR_FLOW, "smb");
73  if (unlikely(js == NULL)) {
74  return TM_ECODE_FAILED;
75  }
76 
77  smbjs = rs_smb_log_json_response(state, tx);
78  if (unlikely(smbjs == NULL)) {
79  goto error;
80  }
81  json_object_set_new(js, "smb", smbjs);
82 
83  MemBufferReset(thread->buffer);
84  OutputJSONBuffer(js, thread->ctx->file_ctx, &thread->buffer);
85 
86  json_decref(js);
87  return TM_ECODE_OK;
88 
89 error:
90  json_decref(js);
91  return TM_ECODE_FAILED;
92 }
93 
94 static OutputInitResult SMBLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
95 {
98  return OutputJsonLogInitSub(conf, parent_ctx);
99 }
100 
101 void JsonSMBLogRegister(void)
102 {
103  /* Register as an eve sub-module. */
104  OutputRegisterTxSubModule(LOGGER_JSON_SMB, "eve-log", "JsonSMBLog",
105  "eve-log.smb", SMBLogInitSub, ALPROTO_SMB,
106  JsonSMBLogger, JsonLogThreadInit,
107  JsonLogThreadDeinit, NULL);
108 
109  SCLogDebug("SMB JSON logger registered.");
110 }
111 
112 #else /* No JSON support. */
113 
114 void JsonSMBLogRegister(void)
115 {
116 }
117 
118 #endif /* HAVE_LIBJANSSON */
119 
120 #else /* no rust */
121 
123 {
124 }
125 
126 #endif /* HAVE_RUST */
#define SCLogDebug(...)
Definition: util-debug.h:335
struct SMBTransaction_ SMBTransaction
Definition: rust.h:57
uint8_t proto
Definition: flow.h:346
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
void JsonSMBLogRegister(void)
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
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:399
Definition: conf.h:32
uint16_t tx_id
void * FlowGetAppState(const Flow *f)
Definition: flow.c:997
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:327