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 #include "rust.h"
49 #include "rust-smb-log-gen.h"
50 
51 json_t *JsonSMBAddMetadata(const Flow *f, uint64_t tx_id)
52 {
53  SMBState *state = FlowGetAppState(f);
54  if (state) {
55  SMBTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SMB, state, tx_id);
56  if (tx) {
57  return rs_smb_log_json_response(state, tx);
58  }
59  }
60 
61  return NULL;
62 }
63 
64 static int JsonSMBLogger(ThreadVars *tv, void *thread_data,
65  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
66 {
67  OutputJsonThreadCtx *thread = thread_data;
68  json_t *js, *smbjs;
69 
70  js = CreateJSONHeader(p, LOG_DIR_FLOW, "smb");
71  if (unlikely(js == NULL)) {
72  return TM_ECODE_FAILED;
73  }
74 
75  smbjs = rs_smb_log_json_response(state, tx);
76  if (unlikely(smbjs == NULL)) {
77  goto error;
78  }
79  json_object_set_new(js, "smb", smbjs);
80 
81  MemBufferReset(thread->buffer);
82  OutputJSONBuffer(js, thread->ctx->file_ctx, &thread->buffer);
83 
84  json_decref(js);
85  return TM_ECODE_OK;
86 
87 error:
88  json_decref(js);
89  return TM_ECODE_FAILED;
90 }
91 
92 static OutputInitResult SMBLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
93 {
96  return OutputJsonLogInitSub(conf, parent_ctx);
97 }
98 
100 {
101  /* Register as an eve sub-module. */
102  OutputRegisterTxSubModule(LOGGER_JSON_SMB, "eve-log", "JsonSMBLog",
103  "eve-log.smb", SMBLogInitSub, ALPROTO_SMB,
104  JsonSMBLogger, JsonLogThreadInit,
105  JsonLogThreadDeinit, NULL);
106 
107  SCLogDebug("SMB JSON logger registered.");
108 }
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
#define SCLogDebug(...)
Definition: util-debug.h:335
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
OutputJsonCtx * ctx
Definition: output-json.h:86
struct SMBTransaction_ SMBTransaction
Definition: rust.h:57
uint8_t proto
Definition: flow.h:344
json_t * JsonSMBAddMetadata(const Flow *f, uint64_t tx_id)
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
void JsonSMBLogRegister(void)
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
LogFileCtx * file_ctx
Definition: output-json.h:79
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
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:402
Definition: conf.h:32
struct SMBState_ SMBState
Definition: rust.h:56
uint16_t tx_id
MemBuffer * buffer
Definition: output-json.h:87
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1068
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:325