suricata
detect-mqtt-subscribe-topic.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-2022 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 
26 #include "app-layer.h"
27 #include "app-layer-parser.h"
28 
29 #include "conf.h"
30 #include "decode.h"
31 #include "detect.h"
32 #include "detect-content.h"
33 #include "detect-parse.h"
34 #include "detect-pcre.h"
35 #include "detect-engine.h"
37 #include "detect-engine-mpm.h"
40 #include "util-unittest.h"
41 #include "util-unittest-helper.h"
42 
43 #include "rust-bindings.h"
44 
45 #include "threads.h"
46 
47 #include "flow.h"
48 #include "flow-util.h"
49 #include "flow-var.h"
50 
51 #include "util-debug.h"
52 #include "util-spm.h"
53 #include "util-print.h"
54 #include "util-profiling.h"
55 
56 static int DetectMQTTSubscribeTopicSetup(DetectEngineCtx *, Signature *, const char *);
57 
58 static int g_mqtt_subscribe_topic_buffer_id = 0;
59 
60 static uint32_t subscribe_topic_match_limit = 100;
61 
63  uint32_t local_id;
64  void *txv;
65 };
66 
67 static InspectionBuffer *MQTTSubscribeTopicGetData(DetectEngineThreadCtx *det_ctx,
68  const DetectEngineTransforms *transforms, Flow *f,
69  struct MQTTSubscribeTopicGetDataArgs *cbdata, int list_id)
70 {
71  SCEnter();
72 
73  InspectionBuffer *buffer =
74  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
75  if (buffer == NULL)
76  return NULL;
77  if (buffer->initialized)
78  return buffer;
79 
80  const uint8_t *data;
81  uint32_t data_len;
82  if (rs_mqtt_tx_get_subscribe_topic(cbdata->txv, cbdata->local_id, &data, &data_len) == 0) {
84  return NULL;
85  }
86 
87  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
88 
89  SCReturnPtr(buffer, "InspectionBuffer");
90 }
91 
92 static uint8_t DetectEngineInspectMQTTSubscribeTopic(DetectEngineCtx *de_ctx,
94  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
95 {
96  uint32_t local_id = 0;
97 
98  const DetectEngineTransforms *transforms = NULL;
99  if (!engine->mpm) {
100  transforms = engine->v2.transforms;
101  }
102 
103  while ((subscribe_topic_match_limit == 0) || local_id < subscribe_topic_match_limit) {
104  struct MQTTSubscribeTopicGetDataArgs cbdata = { local_id, txv, };
105  InspectionBuffer *buffer =
106  MQTTSubscribeTopicGetData(det_ctx, transforms, f, &cbdata, engine->sm_list);
107  if (buffer == NULL || buffer->inspect == NULL)
108  break;
109 
110  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
111  buffer->inspect, buffer->inspect_len, buffer->inspect_offset,
113  if (match) {
115  }
116  local_id++;
117  }
119 }
120 
122  int list_id;
123  const MpmCtx *mpm_ctx;
126 
127 /** \brief MQTTSubscribeTopic MQTTSubscribeTopic Mpm prefilter callback
128  *
129  * \param det_ctx detection engine thread ctx
130  * \param p packet to inspect
131  * \param f flow to inspect
132  * \param txv tx to inspect
133  * \param pectx inspection context
134  */
135 static void PrefilterTxMQTTSubscribeTopic(DetectEngineThreadCtx *det_ctx, const void *pectx,
136  Packet *p, Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd,
137  const uint8_t flags)
138 {
139  SCEnter();
140 
142  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
143  const int list_id = ctx->list_id;
144 
145  uint32_t local_id = 0;
146  while ((subscribe_topic_match_limit == 0) || local_id < subscribe_topic_match_limit) {
147  struct MQTTSubscribeTopicGetDataArgs cbdata = { local_id, txv };
148  InspectionBuffer *buffer =
149  MQTTSubscribeTopicGetData(det_ctx, ctx->transforms, f, &cbdata, list_id);
150  if (buffer == NULL)
151  break;
152 
153  if (buffer->inspect_len >= mpm_ctx->minlen) {
154  (void)mpm_table[mpm_ctx->mpm_type].Search(
155  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
156  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
157  }
158  local_id++;
159  }
160 }
161 
162 static void PrefilterMpmMQTTSubscribeTopicFree(void *ptr)
163 {
164  if (ptr != NULL)
165  SCFree(ptr);
166 }
167 
168 static int PrefilterMpmMQTTSubscribeTopicRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
169  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
170 {
171  PrefilterMpmMQTTSubscribeTopic *pectx = SCCalloc(1, sizeof(*pectx));
172  if (pectx == NULL)
173  return -1;
174  pectx->list_id = list_id;
175  pectx->mpm_ctx = mpm_ctx;
176  pectx->transforms = &mpm_reg->transforms;
177 
178  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxMQTTSubscribeTopic,
179  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
180  pectx, PrefilterMpmMQTTSubscribeTopicFree, mpm_reg->pname);
181 }
182 
183 /**
184  * \brief Registration function for keyword: mqtt.subscribe.topic
185  */
187 {
188  sigmatch_table[DETECT_AL_MQTT_SUBSCRIBE_TOPIC].name = "mqtt.subscribe.topic";
189  sigmatch_table[DETECT_AL_MQTT_SUBSCRIBE_TOPIC].desc = "sticky buffer to match MQTT SUBSCRIBE topic";
190  sigmatch_table[DETECT_AL_MQTT_SUBSCRIBE_TOPIC].url = "/rules/mqtt-keywords.html#mqtt-subscribe-topic";
191  sigmatch_table[DETECT_AL_MQTT_SUBSCRIBE_TOPIC].Setup = DetectMQTTSubscribeTopicSetup;
194 
195  intmax_t val = 0;
196  if (ConfGetInt("app-layer.protocols.mqtt.subscribe-topic-match-limit", &val)) {
197  subscribe_topic_match_limit = val;
198  }
199  if (subscribe_topic_match_limit <= 0) {
200  SCLogDebug("Using unrestricted MQTT SUBSCRIBE topic matching");
201  } else {
202  SCLogDebug("Using MQTT SUBSCRIBE topic match-limit setting of: %u",
203  subscribe_topic_match_limit);
204  }
205 
206  DetectAppLayerMpmRegister2("mqtt.subscribe.topic", SIG_FLAG_TOSERVER, 1,
207  PrefilterMpmMQTTSubscribeTopicRegister, NULL,
208  ALPROTO_MQTT, 1);
209 
210  DetectAppLayerInspectEngineRegister2("mqtt.subscribe.topic",
212  DetectEngineInspectMQTTSubscribeTopic, NULL);
213 
214  DetectBufferTypeSetDescriptionByName("mqtt.subscribe.topic",
215  "subscribe topic query");
216 
217  g_mqtt_subscribe_topic_buffer_id = DetectBufferTypeGetByName("mqtt.subscribe.topic");
218 
219  DetectBufferTypeSupportsMultiInstance("mqtt.subscribe.topic");
220 }
221 
222 /**
223  * \brief setup the sticky buffer keyword used in the rule
224  *
225  * \param de_ctx Pointer to the Detection Engine Context
226  * \param s Pointer to the Signature to which the current keyword belongs
227  * \param str Should hold an empty string always
228  *
229  * \retval 0 On success
230  * \retval -1 On failure
231  */
232 
233 static int DetectMQTTSubscribeTopicSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
234 {
235  if (DetectBufferSetActiveList(de_ctx, s, g_mqtt_subscribe_topic_buffer_id) < 0)
236  return -1;
238  return -1;
239  return 0;
240 }
ConfGetInt
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:399
DetectEngineAppInspectionEngine_
Definition: detect.h:421
SigTableElmt_::url
const char * url
Definition: detect.h:1284
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:425
detect-content.h
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
detect-engine.h
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1487
PrefilterMpmMQTTSubscribeTopic
Definition: detect-mqtt-subscribe-topic.c:121
SigTableElmt_::desc
const char * desc
Definition: detect.h:1283
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1281
InspectionBuffer::initialized
bool initialized
Definition: detect.h:372
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1435
DetectEngineTransforms
Definition: detect.h:403
DetectMQTTSubscribeTopicRegister
void DetectMQTTSubscribeTopicRegister(void)
Registration function for keyword: mqtt.subscribe.topic.
Definition: detect-mqtt-subscribe-topic.c:186
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1387
InspectionBuffer
Definition: detect.h:368
threads.h
Flow_
Flow data structure.
Definition: flow.h:349
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1189
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1275
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:832
PrefilterMpmMQTTSubscribeTopic::list_id
int list_id
Definition: detect-mqtt-subscribe-topic.c:122
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1074
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:674
detect-mqtt-subscribe-topic.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1266
detect-pcre.h
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:687
detect-engine-prefilter.h
util-unittest.h
MQTTSubscribeTopicGetDataArgs::local_id
uint32_t local_id
Definition: detect-mqtt-subscribe-topic.c:63
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1124
MQTTSubscribeTopicGetDataArgs
Definition: detect-mqtt-subscribe-topic.c:62
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:427
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:259
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1598
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1080
PrefilterMpmMQTTSubscribeTopic
struct PrefilterMpmMQTTSubscribeTopic PrefilterMpmMQTTSubscribeTopic
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:39
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:370
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
util-profiling.h
DETECT_AL_MQTT_SUBSCRIBE_TOPIC
@ DETECT_AL_MQTT_SUBSCRIBE_TOPIC
Definition: detect-engine-register.h:308
Packet_
Definition: decode.h:430
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:216
conf.h
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:163
DetectEngineThreadCtx_::mtc
MpmThreadCtx mtc
Definition: detect.h:1188
PrefilterMpmMQTTSubscribeTopic::transforms
const DetectEngineTransforms * transforms
Definition: detect-mqtt-subscribe-topic.c:124
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:438
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1343
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:287
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:49
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@86::@88 app_v2
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
util-spm.h
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1611
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, PrefilterTxFn PrefilterTxFunc, AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:270
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:38
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:371
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@83 v2
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:369
str
#define str(s)
Definition: suricata-common.h:291
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:587
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:435
PrefilterMpmMQTTSubscribeTopic::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-mqtt-subscribe-topic.c:123
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1551
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1463
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1221
MpmCtx_
Definition: util-mpm.h:88
flow.h
DetectEngineContentInspection
bool DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, const uint32_t buffer_len, const uint32_t stream_start_offset, const uint8_t flags, const enum DetectContentInspectionType inspection_mode)
wrapper around DetectEngineContentInspectionInternal to return true/false only
Definition: detect-engine-content-inspection.c:724
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
flow-var.h
MQTTSubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-subscribe-topic.c:64
app-layer.h
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:676