suricata
output-json-snmp.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2019 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 Pierre Chifflier <chifflier@wzdftpd.net>
22  *
23  * Implement JSON/eve logging app-layer SNMP.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-unittest.h"
37 #include "util-buffer.h"
38 #include "util-debug.h"
39 #include "util-byte.h"
40 
41 #include "output.h"
42 #include "output-json.h"
43 
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46 
47 #include "app-layer-snmp.h"
48 #include "output-json-snmp.h"
49 
50 #include "rust.h"
51 
52 typedef struct LogSNMPFileCtx_ {
56 
57 typedef struct LogSNMPLogThread_ {
61 
62 static int JsonSNMPLogger(ThreadVars *tv, void *thread_data,
63  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
64 {
65  SNMPTransaction *snmptx = tx;
66  LogSNMPLogThread *thread = thread_data;
67  json_t *js, *snmpjs;
68 
69  js = CreateJSONHeader(p, LOG_DIR_PACKET, "snmp", NULL);
70  if (unlikely(js == NULL)) {
71  return TM_ECODE_FAILED;
72  }
73 
74  JsonAddCommonOptions(&thread->snmplog_ctx->cfg, p, f, js);
75 
76  snmpjs = rs_snmp_log_json_response(state, snmptx);
77  if (unlikely(snmpjs == NULL)) {
78  goto error;
79  }
80  json_object_set_new(js, "snmp", snmpjs);
81 
82  MemBufferReset(thread->buffer);
83  OutputJSONBuffer(js, thread->snmplog_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 void OutputSNMPLogDeInitCtxSub(OutputCtx *output_ctx)
94 {
95  LogSNMPFileCtx *snmplog_ctx = (LogSNMPFileCtx *)output_ctx->data;
96  SCFree(snmplog_ctx);
97  SCFree(output_ctx);
98 }
99 
100 static OutputInitResult OutputSNMPLogInitSub(ConfNode *conf,
101  OutputCtx *parent_ctx)
102 {
103  OutputInitResult result = { NULL, false };
104  OutputJsonCtx *ajt = parent_ctx->data;
105 
106  LogSNMPFileCtx *snmplog_ctx = SCCalloc(1, sizeof(*snmplog_ctx));
107  if (unlikely(snmplog_ctx == NULL)) {
108  return result;
109  }
110  snmplog_ctx->file_ctx = ajt->file_ctx;
111  snmplog_ctx->cfg = ajt->cfg;
112 
113  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
114  if (unlikely(output_ctx == NULL)) {
115  SCFree(snmplog_ctx);
116  return result;
117  }
118  output_ctx->data = snmplog_ctx;
119  output_ctx->DeInit = OutputSNMPLogDeInitCtxSub;
120 
121  SCLogDebug("SNMP log sub-module initialized.");
122 
124 
125  result.ctx = output_ctx;
126  result.ok = true;
127  return result;
128 }
129 
130 static TmEcode JsonSNMPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
131 {
132  LogSNMPLogThread *thread = SCCalloc(1, sizeof(*thread));
133  if (unlikely(thread == NULL)) {
134  return TM_ECODE_FAILED;
135  }
136 
137  if (initdata == NULL) {
138  SCLogDebug("Error getting context for EveLogSNMP. \"initdata\" is NULL.");
139  SCFree(thread);
140  return TM_ECODE_FAILED;
141  }
142 
144  if (unlikely(thread->buffer == NULL)) {
145  SCFree(thread);
146  return TM_ECODE_FAILED;
147  }
148 
149  thread->snmplog_ctx = ((OutputCtx *)initdata)->data;
150  *data = (void *)thread;
151 
152  return TM_ECODE_OK;
153 }
154 
155 static TmEcode JsonSNMPLogThreadDeinit(ThreadVars *t, void *data)
156 {
157  LogSNMPLogThread *thread = (LogSNMPLogThread *)data;
158  if (thread == NULL) {
159  return TM_ECODE_OK;
160  }
161  if (thread->buffer != NULL) {
162  MemBufferFree(thread->buffer);
163  }
164  SCFree(thread);
165  return TM_ECODE_OK;
166 }
167 
169 {
170  /* Register as an eve sub-module. */
171  OutputRegisterTxSubModule(LOGGER_JSON_SNMP, "eve-log", "JsonSNMPLog",
172  "eve-log.snmp", OutputSNMPLogInitSub, ALPROTO_SNMP,
173  JsonSNMPLogger, JsonSNMPLogThreadInit,
174  JsonSNMPLogThreadDeinit, NULL);
175 
176  SCLogDebug("SNMP JSON logger registered.");
177 }
util-byte.h
tm-threads.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:111
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
LogSNMPFileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-snmp.c:54
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:62
threads.h
OutputJsonCtx_
Definition: output-json.h:108
Flow_
Flow data structure.
Definition: flow.h:343
OutputJsonCommonSettings_
Definition: output-json.h:99
LogFileCtx_
Definition: util-logopenfile.h:52
JsonSNMPLogRegister
void JsonSNMPLogRegister(void)
Definition: output-json-snmp.c:168
rust.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
OutputCtx_
Definition: tm-modules.h:78
ALPROTO_SNMP
@ ALPROTO_SNMP
Definition: app-layer-protos.h:52
util-debug.h
LogSNMPLogThread_::buffer
MemBuffer * buffer
Definition: output-json-snmp.c:59
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
Packet_
Definition: decode.h:411
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
MemBuffer_
Definition: util-buffer.h:27
LOGGER_JSON_SNMP
@ LOGGER_JSON_SNMP
Definition: suricata-common.h:463
LogSNMPLogThread_::snmplog_ctx
LogSNMPFileCtx * snmplog_ctx
Definition: output-json-snmp.c:58
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:38
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
LogSNMPLogThread
struct LogSNMPLogThread_ LogSNMPLogThread
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
JsonAddCommonOptions
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:662
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:410
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
app-layer-snmp.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:109
OutputJSONBuffer
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:1402
LogSNMPLogThread_
Definition: output-json-snmp.c:57
LogSNMPFileCtx_
Definition: output-json-snmp.c:52
LogSNMPFileCtx
struct LogSNMPFileCtx_ LogSNMPFileCtx
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
LogSNMPFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-snmp.c:53
SNMPTransaction
struct SNMPTransaction_ SNMPTransaction
Definition: app-layer-snmp.h:31
output-json-snmp.h
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h
CreateJSONHeader
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:1204