suricata
output-json-mqtt.c
Go to the documentation of this file.
1/* Copyright (C) 2020-2021 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 Sascha Steinbiss <sascha@steinbiss.name>
22 */
23
24#include "suricata-common.h"
25#include "detect.h"
26#include "pkt-var.h"
27#include "conf.h"
28
29#include "threads.h"
30#include "threadvars.h"
31#include "tm-threads.h"
32
33#include "util-unittest.h"
34#include "util-buffer.h"
35#include "util-debug.h"
36#include "util-byte.h"
37#include "util-misc.h"
38
39#include "output.h"
40#include "output-json.h"
41
42#include "app-layer.h"
43#include "app-layer-parser.h"
44
45#include "output-json-mqtt.h"
46#include "rust.h"
47
48#define MQTT_LOG_PASSWORDS BIT_U32(0)
49#define MQTT_DEFAULT_FLAGS (MQTT_LOG_PASSWORDS)
50#define MQTT_DEFAULT_MAXLOGLEN 1024
51
56
62
63bool JsonMQTTAddMetadata(void *vtx, SCJsonBuilder *js)
64{
65 return SCMqttLoggerLog(vtx, MQTT_DEFAULT_FLAGS, MQTT_DEFAULT_MAXLOGLEN, js);
66}
67
68static int JsonMQTTLogger(ThreadVars *tv, void *thread_data,
69 const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
70{
71 LogMQTTLogThread *thread = thread_data;
73
74 if (SCMqttTxIsToClient((MQTTTransaction *)tx)) {
76 } else {
78 }
79
80 SCJsonBuilder *js = CreateEveHeader(p, dir, "mqtt", NULL, thread->mqttlog_ctx->eve_ctx);
81 if (unlikely(js == NULL)) {
82 return TM_ECODE_FAILED;
83 }
84
85 if (!SCMqttLoggerLog(tx, thread->mqttlog_ctx->flags, thread->mqttlog_ctx->max_log_len, js))
86 goto error;
87
88 OutputJsonBuilderBuffer(tv, p, p->flow, js, thread->ctx);
89 SCJbFree(js);
90
91 return TM_ECODE_OK;
92
93error:
94 SCJbFree(js);
95 return TM_ECODE_FAILED;
96}
97
98static void OutputMQTTLogDeInitCtxSub(OutputCtx *output_ctx)
99{
100 LogMQTTFileCtx *mqttlog_ctx = (LogMQTTFileCtx *)output_ctx->data;
101 SCFree(mqttlog_ctx);
102 SCFree(output_ctx);
103}
104
105static void JsonMQTTLogParseConfig(SCConfNode *conf, LogMQTTFileCtx *mqttlog_ctx)
106{
107 const char *query = SCConfNodeLookupChildValue(conf, "passwords");
108 if (query != NULL) {
109 if (SCConfValIsTrue(query)) {
110 mqttlog_ctx->flags |= MQTT_LOG_PASSWORDS;
111 } else {
112 mqttlog_ctx->flags &= ~MQTT_LOG_PASSWORDS;
113 }
114 } else {
115 mqttlog_ctx->flags |= MQTT_LOG_PASSWORDS;
116 }
117 uint32_t max_log_len = MQTT_DEFAULT_MAXLOGLEN;
118 query = SCConfNodeLookupChildValue(conf, "string-log-limit");
119 if (query != NULL) {
120 if (ParseSizeStringU32(query, &max_log_len) < 0) {
121 SCLogError("Error parsing string-log-limit from config - %s, ", query);
122 exit(EXIT_FAILURE);
123 }
124 }
125 mqttlog_ctx->max_log_len = max_log_len;
126}
127
128static OutputInitResult OutputMQTTLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
129{
130 OutputInitResult result = { NULL, false };
131 OutputJsonCtx *ajt = parent_ctx->data;
132
133 LogMQTTFileCtx *mqttlog_ctx = SCCalloc(1, sizeof(*mqttlog_ctx));
134 if (unlikely(mqttlog_ctx == NULL)) {
135 return result;
136 }
137 mqttlog_ctx->eve_ctx = ajt;
138
139 OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
140 if (unlikely(output_ctx == NULL)) {
141 SCFree(mqttlog_ctx);
142 return result;
143 }
144 output_ctx->data = mqttlog_ctx;
145 output_ctx->DeInit = OutputMQTTLogDeInitCtxSub;
146
147 JsonMQTTLogParseConfig(conf, mqttlog_ctx);
148
150
151 result.ctx = output_ctx;
152 result.ok = true;
153 return result;
154}
155
156static TmEcode JsonMQTTLogThreadInit(ThreadVars *t, const void *initdata, void **data)
157{
158 LogMQTTLogThread *thread = SCCalloc(1, sizeof(*thread));
159 if (unlikely(thread == NULL)) {
160 return TM_ECODE_FAILED;
161 }
162
163 if (initdata == NULL) {
164 SCLogDebug("Error getting context for EveLogMQTT. \"initdata\" is NULL.");
165 SCFree(thread);
166 return TM_ECODE_FAILED;
167 }
168
169 thread->mqttlog_ctx = ((OutputCtx *)initdata)->data;
170 thread->ctx = CreateEveThreadCtx(t, thread->mqttlog_ctx->eve_ctx);
171 if (unlikely(thread->ctx == NULL)) {
172 SCFree(thread);
173 return TM_ECODE_FAILED;
174 }
175
176 *data = (void *)thread;
177
178 return TM_ECODE_OK;
179}
180
181static TmEcode JsonMQTTLogThreadDeinit(ThreadVars *t, void *data)
182{
183 LogMQTTLogThread *thread = (LogMQTTLogThread *)data;
184 if (thread == NULL) {
185 return TM_ECODE_OK;
186 }
187 FreeEveThreadCtx(thread->ctx);
188 SCFree(thread);
189 return TM_ECODE_OK;
190}
191
193{
194 OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonMQTTLog", "eve-log.mqtt",
195 OutputMQTTLogInitSub, ALPROTO_MQTT, JsonMQTTLogger, JsonMQTTLogThreadInit,
196 JsonMQTTLogThreadDeinit);
197}
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
@ ALPROTO_MQTT
int SCConfValIsTrue(const char *val)
Check if a value is true.
Definition conf.c:551
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition conf.c:824
ThreadVars * tv
SCOutputJsonLogDirection
@ LOG_DIR_FLOW_TOSERVER
@ LOG_DIR_FLOW_TOCLIENT
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
#define MQTT_DEFAULT_MAXLOGLEN
bool JsonMQTTAddMetadata(void *vtx, SCJsonBuilder *js)
#define MQTT_LOG_PASSWORDS
#define MQTT_DEFAULT_FLAGS
void JsonMQTTLogRegister(void)
struct LogMQTTLogThread_ LogMQTTLogThread
struct LogMQTTFileCtx_ LogMQTTFileCtx
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
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)
Definition output.c:406
Flow data structure.
Definition flow.h:356
OutputJsonCtx * eve_ctx
OutputJsonThreadCtx * ctx
LogMQTTFileCtx * mqttlog_ctx
void * data
Definition tm-modules.h:91
void(* DeInit)(struct OutputCtx_ *)
Definition tm-modules.h:94
OutputCtx * ctx
Definition output.h:47
struct Flow_ * flow
Definition decode.h:546
Per thread variable structure.
Definition threadvars.h:58
@ LOGGER_JSON_TX
@ TM_ECODE_FAILED
@ TM_ECODE_OK
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
int ParseSizeStringU32(const char *size, uint32_t *res)
Definition util-misc.c:173
#define unlikely(expr)