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 
38 #include "output.h"
39 #include "output-json.h"
40 
41 #include "app-layer.h"
42 #include "app-layer-parser.h"
43 
44 #include "app-layer-mqtt.h"
45 #include "output-json-mqtt.h"
46 #include "rust.h"
47 
48 #define MQTT_LOG_PASSWORDS BIT_U32(0)
49 #define MQTT_DEFAULTS (MQTT_LOG_PASSWORDS)
50 
51 typedef struct LogMQTTFileCtx_ {
52  uint32_t flags;
55 
56 typedef struct LogMQTTLogThread_ {
58  uint32_t count;
61 
62 bool JsonMQTTAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
63 {
64  MQTTState *state = FlowGetAppState(f);
65  if (state) {
66  MQTTTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_MQTT, state, tx_id);
67  if (tx) {
68  return rs_mqtt_logger_log(tx, MQTT_DEFAULTS, js);
69  }
70  }
71 
72  return false;
73 }
74 
75 static int JsonMQTTLogger(ThreadVars *tv, void *thread_data,
76  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
77 {
78  LogMQTTLogThread *thread = thread_data;
79  enum OutputJsonLogDirection dir;
80 
81  if (rs_mqtt_tx_is_toclient((MQTTTransaction*) tx)) {
83  } else {
85  }
86 
87  JsonBuilder *js = CreateEveHeader(p, dir, "mqtt", NULL, thread->mqttlog_ctx->eve_ctx);
88  if (unlikely(js == NULL)) {
89  return TM_ECODE_FAILED;
90  }
91 
92  if (!rs_mqtt_logger_log(tx, thread->mqttlog_ctx->flags, js))
93  goto error;
94 
95  OutputJsonBuilderBuffer(js, thread->ctx);
96  jb_free(js);
97 
98  return TM_ECODE_OK;
99 
100 error:
101  jb_free(js);
102  return TM_ECODE_FAILED;
103 }
104 
105 static void OutputMQTTLogDeInitCtxSub(OutputCtx *output_ctx)
106 {
107  LogMQTTFileCtx *mqttlog_ctx = (LogMQTTFileCtx *)output_ctx->data;
108  SCFree(mqttlog_ctx);
109  SCFree(output_ctx);
110 }
111 
112 static void JsonMQTTLogParseConfig(ConfNode *conf, LogMQTTFileCtx *mqttlog_ctx)
113 {
114  const char *query = ConfNodeLookupChildValue(conf, "passwords");
115  if (query != NULL) {
116  if (ConfValIsTrue(query)) {
117  mqttlog_ctx->flags |= MQTT_LOG_PASSWORDS;
118  } else {
119  mqttlog_ctx->flags &= ~MQTT_LOG_PASSWORDS;
120  }
121  } else {
122  mqttlog_ctx->flags |= MQTT_LOG_PASSWORDS;
123  }
124 }
125 
126 static OutputInitResult OutputMQTTLogInitSub(ConfNode *conf,
127  OutputCtx *parent_ctx)
128 {
129  OutputInitResult result = { NULL, false };
130  OutputJsonCtx *ajt = parent_ctx->data;
131 
132  LogMQTTFileCtx *mqttlog_ctx = SCCalloc(1, sizeof(*mqttlog_ctx));
133  if (unlikely(mqttlog_ctx == NULL)) {
134  return result;
135  }
136  mqttlog_ctx->eve_ctx = ajt;
137 
138  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
139  if (unlikely(output_ctx == NULL)) {
140  SCFree(mqttlog_ctx);
141  return result;
142  }
143  output_ctx->data = mqttlog_ctx;
144  output_ctx->DeInit = OutputMQTTLogDeInitCtxSub;
145 
146  JsonMQTTLogParseConfig(conf, mqttlog_ctx);
147 
149 
150  result.ctx = output_ctx;
151  result.ok = true;
152  return result;
153 }
154 
155 static TmEcode JsonMQTTLogThreadInit(ThreadVars *t, const void *initdata, void **data)
156 {
157  LogMQTTLogThread *thread = SCCalloc(1, sizeof(*thread));
158  if (unlikely(thread == NULL)) {
159  return TM_ECODE_FAILED;
160  }
161 
162  if (initdata == NULL) {
163  SCLogDebug("Error getting context for EveLogMQTT. \"initdata\" is NULL.");
164  SCFree(thread);
165  return TM_ECODE_FAILED;
166  }
167 
168  thread->mqttlog_ctx = ((OutputCtx *)initdata)->data;
169  thread->ctx = CreateEveThreadCtx(t, thread->mqttlog_ctx->eve_ctx);
170  if (unlikely(thread->ctx == NULL)) {
171  SCFree(thread);
172  return TM_ECODE_FAILED;
173  }
174 
175  *data = (void *)thread;
176 
177  return TM_ECODE_OK;
178 }
179 
180 static TmEcode JsonMQTTLogThreadDeinit(ThreadVars *t, void *data)
181 {
182  LogMQTTLogThread *thread = (LogMQTTLogThread *)data;
183  if (thread == NULL) {
184  return TM_ECODE_OK;
185  }
186  FreeEveThreadCtx(thread->ctx);
187  SCFree(thread);
188  return TM_ECODE_OK;
189 }
190 
192 {
193  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonMQTTLog", "eve-log.mqtt",
194  OutputMQTTLogInitSub, ALPROTO_MQTT, JsonMQTTLogger, JsonMQTTLogThreadInit,
195  JsonMQTTLogThreadDeinit, NULL);
196 }
util-byte.h
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
output-json-mqtt.h
LogMQTTLogThread_
Definition: output-json-mqtt.c:56
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
Flow_::proto
uint8_t proto
Definition: flow.h:369
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:347
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:927
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
rust.h
LogMQTTLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-mqtt.c:59
MQTT_LOG_PASSWORDS
#define MQTT_LOG_PASSWORDS
Definition: output-json-mqtt.c:48
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:537
OutputCtx_::data
void * data
Definition: tm-modules.h:87
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:84
OutputJsonThreadCtx_
Definition: output-json.h:89
LogMQTTFileCtx_::flags
uint32_t flags
Definition: output-json-mqtt.c:52
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:477
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:786
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
JsonMQTTLogRegister
void JsonMQTTLogRegister(void)
Definition: output-json-mqtt.c:191
Packet_
Definition: decode.h:430
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
LOG_DIR_FLOW_TOCLIENT
@ LOG_DIR_FLOW_TOCLIENT
Definition: output-json.h:41
LogMQTTLogThread_::mqttlog_ctx
LogMQTTFileCtx * mqttlog_ctx
Definition: output-json-mqtt.c:57
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1127
OutputInitResult_
Definition: output.h:46
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:90
LogMQTTFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-mqtt.c:53
LogMQTTFileCtx_
Definition: output-json-mqtt.c:51
OutputJsonLogDirection
OutputJsonLogDirection
Definition: output-json.h:38
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:411
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:462
LOG_DIR_FLOW_TOSERVER
@ LOG_DIR_FLOW_TOSERVER
Definition: output-json.h:42
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
LogMQTTLogThread
struct LogMQTTLogThread_ LogMQTTLogThread
util-buffer.h
MQTT_DEFAULTS
#define MQTT_DEFAULTS
Definition: output-json-mqtt.c:49
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
LogMQTTLogThread_::count
uint32_t count
Definition: output-json-mqtt.c:58
app-layer-mqtt.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
JsonMQTTAddMetadata
bool JsonMQTTAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-mqtt.c:62
output.h
app-layer.h
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:814
LogMQTTFileCtx
struct LogMQTTFileCtx_ LogMQTTFileCtx