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