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_LIBJANSSON
49 #include "rust.h"
50 #include "rust-smb-log-gen.h"
51 
52 json_t *JsonSMBAddMetadata(const Flow *f, uint64_t tx_id)
53 {
54  SMBState *state = FlowGetAppState(f);
55  if (state) {
56  SMBTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SMB, state, tx_id);
57  if (tx) {
58  return rs_smb_log_json_response(state, tx);
59  }
60  }
61 
62  return NULL;
63 }
64 
65 static int JsonSMBLogger(ThreadVars *tv, void *thread_data,
66  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
67 {
68  OutputJsonThreadCtx *thread = thread_data;
69  json_t *js, *smbjs;
70 
71  js = CreateJSONHeader(p, LOG_DIR_FLOW, "smb");
72  if (unlikely(js == NULL)) {
73  return TM_ECODE_FAILED;
74  }
75 
76  smbjs = rs_smb_log_json_response(state, tx);
77  if (unlikely(smbjs == NULL)) {
78  goto error;
79  }
80  json_object_set_new(js, "smb", smbjs);
81 
82  MemBufferReset(thread->buffer);
83  OutputJSONBuffer(js, thread->ctx->file_ctx, &thread->buffer);
84 
85  json_decref(js);
86  return TM_ECODE_OK;
87 
88 error:
89  json_decref(js);
90  return TM_ECODE_FAILED;
91 }
92 
93 static OutputInitResult SMBLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
94 {
97  return OutputJsonLogInitSub(conf, parent_ctx);
98 }
99 
100 void JsonSMBLogRegister(void)
101 {
102  /* Register as an eve sub-module. */
103  OutputRegisterTxSubModule(LOGGER_JSON_SMB, "eve-log", "JsonSMBLog",
104  "eve-log.smb", SMBLogInitSub, ALPROTO_SMB,
105  JsonSMBLogger, JsonLogThreadInit,
106  JsonLogThreadDeinit, NULL);
107 
108  SCLogDebug("SMB JSON logger registered.");
109 }
110 
111 #else /* No JSON support. */
112 
114 {
115 }
116 
117 #endif /* HAVE_LIBJANSSON */
118 
#define SCLogDebug(...)
Definition: util-debug.h:335
json_t * JsonSMBAddMetadata(const Flow *f, uint64_t tx_id)
struct SMBTransaction_ SMBTransaction
Definition: rust.h:57
uint8_t proto
Definition: flow.h:343
#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:397
Definition: conf.h:32
struct SMBState_ SMBState
Definition: rust.h:56
uint16_t tx_id
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1065
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:324