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-unittest.h"
53 #include "util-spm.h"
54 #include "util-print.h"
55 #include "util-profiling.h"
56 
57 static int DetectMQTTSubscribeTopicSetup(DetectEngineCtx *, Signature *, const char *);
58 
59 static int g_mqtt_subscribe_topic_buffer_id = 0;
60 
61 static uint32_t subscribe_topic_match_limit = 100;
62 
64  uint32_t local_id;
65  void *txv;
66 };
67 
68 static InspectionBuffer *MQTTSubscribeTopicGetData(DetectEngineThreadCtx *det_ctx,
69  const DetectEngineTransforms *transforms,
70  Flow *f, struct MQTTSubscribeTopicGetDataArgs *cbdata, int list_id, bool first)
71 {
72  SCEnter();
73 
74  InspectionBuffer *buffer =
75  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
76  if (buffer == NULL)
77  return NULL;
78  if (!first && buffer->inspect != NULL)
79  return buffer;
80 
81  const uint8_t *data;
82  uint32_t data_len;
83  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 = MQTTSubscribeTopicGetData(det_ctx,
106  transforms, f, &cbdata, engine->sm_list, false);
107  if (buffer == NULL || buffer->inspect == NULL)
108  break;
109 
110  det_ctx->buffer_offset = 0;
111  det_ctx->discontinue_matching = 0;
112  det_ctx->inspection_recursion_counter = 0;
113 
114  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
115  NULL, f,
116  (uint8_t *)buffer->inspect,
117  buffer->inspect_len,
120  if (match == 1) {
122  }
123  local_id++;
124  }
126 }
127 
129  int list_id;
130  const MpmCtx *mpm_ctx;
133 
134 /** \brief MQTTSubscribeTopic MQTTSubscribeTopic Mpm prefilter callback
135  *
136  * \param det_ctx detection engine thread ctx
137  * \param p packet to inspect
138  * \param f flow to inspect
139  * \param txv tx to inspect
140  * \param pectx inspection context
141  */
142 static void PrefilterTxMQTTSubscribeTopic(DetectEngineThreadCtx *det_ctx,
143  const void *pectx,
144  Packet *p, Flow *f, void *txv,
145  const uint64_t idx, const uint8_t flags)
146 {
147  SCEnter();
148 
150  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
151  const int list_id = ctx->list_id;
152 
153  uint32_t local_id = 0;
154  while ((subscribe_topic_match_limit == 0) || local_id < subscribe_topic_match_limit) {
155  struct MQTTSubscribeTopicGetDataArgs cbdata = { local_id, txv };
156  InspectionBuffer *buffer = MQTTSubscribeTopicGetData(det_ctx, ctx->transforms,
157  f, &cbdata, list_id, true);
158  if (buffer == NULL)
159  break;
160 
161  if (buffer->inspect_len >= mpm_ctx->minlen) {
162  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
163  &det_ctx->mtcu, &det_ctx->pmq,
164  buffer->inspect, buffer->inspect_len);
165  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
166  }
167  local_id++;
168  }
169 }
170 
171 static void PrefilterMpmMQTTSubscribeTopicFree(void *ptr)
172 {
173  if (ptr != NULL)
174  SCFree(ptr);
175 }
176 
177 static int PrefilterMpmMQTTSubscribeTopicRegister(DetectEngineCtx *de_ctx,
178  SigGroupHead *sgh, MpmCtx *mpm_ctx,
179  const DetectBufferMpmRegistery *mpm_reg, int list_id)
180 {
181  PrefilterMpmMQTTSubscribeTopic *pectx = SCCalloc(1, sizeof(*pectx));
182  if (pectx == NULL)
183  return -1;
184  pectx->list_id = list_id;
185  pectx->mpm_ctx = mpm_ctx;
186  pectx->transforms = &mpm_reg->transforms;
187 
188  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxMQTTSubscribeTopic,
189  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
190  pectx, PrefilterMpmMQTTSubscribeTopicFree, mpm_reg->pname);
191 }
192 
193 /**
194  * \brief Registration function for keyword: mqtt.subscribe.topic
195  */
197 {
198  sigmatch_table[DETECT_AL_MQTT_SUBSCRIBE_TOPIC].name = "mqtt.subscribe.topic";
199  sigmatch_table[DETECT_AL_MQTT_SUBSCRIBE_TOPIC].desc = "sticky buffer to match MQTT SUBSCRIBE topic";
200  sigmatch_table[DETECT_AL_MQTT_SUBSCRIBE_TOPIC].url = "/rules/mqtt-keywords.html#mqtt-subscribe-topic";
201  sigmatch_table[DETECT_AL_MQTT_SUBSCRIBE_TOPIC].Setup = DetectMQTTSubscribeTopicSetup;
204 
205  intmax_t val = 0;
206  if (ConfGetInt("app-layer.protocols.mqtt.subscribe-topic-match-limit", &val)) {
207  subscribe_topic_match_limit = val;
208  }
209  if (subscribe_topic_match_limit <= 0) {
210  SCLogDebug("Using unrestricted MQTT SUBSCRIBE topic matching");
211  } else {
212  SCLogDebug("Using MQTT SUBSCRIBE topic match-limit setting of: %u",
213  subscribe_topic_match_limit);
214  }
215 
216  DetectAppLayerMpmRegister2("mqtt.subscribe.topic", SIG_FLAG_TOSERVER, 1,
217  PrefilterMpmMQTTSubscribeTopicRegister, NULL,
218  ALPROTO_MQTT, 1);
219 
220  DetectAppLayerInspectEngineRegister2("mqtt.subscribe.topic",
222  DetectEngineInspectMQTTSubscribeTopic, NULL);
223 
224  DetectBufferTypeSetDescriptionByName("mqtt.subscribe.topic",
225  "subscribe topic query");
226 
227  g_mqtt_subscribe_topic_buffer_id = DetectBufferTypeGetByName("mqtt.subscribe.topic");
228 }
229 
230 /**
231  * \brief setup the sticky buffer keyword used in the rule
232  *
233  * \param de_ctx Pointer to the Detection Engine Context
234  * \param s Pointer to the Signature to which the current keyword belongs
235  * \param str Should hold an empty string always
236  *
237  * \retval 0 On success
238  * \retval -1 On failure
239  */
240 
241 static int DetectMQTTSubscribeTopicSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
242 {
243  if (DetectBufferSetActiveList(s, g_mqtt_subscribe_topic_buffer_id) < 0)
244  return -1;
246  return -1;
247  return 0;
248 }
ConfGetInt
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:392
DetectEngineAppInspectionEngine_
Definition: detect.h:390
SigTableElmt_::url
const char * url
Definition: detect.h:1248
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:394
detect-content.h
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1062
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1455
PrefilterMpmMQTTSubscribeTopic
Definition: detect-mqtt-subscribe-topic.c:128
SigTableElmt_::desc
const char * desc
Definition: detect.h:1247
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1245
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1403
DetectEngineTransforms
Definition: detect.h:372
DetectMQTTSubscribeTopicRegister
void DetectMQTTSubscribeTopicRegister(void)
Registration function for keyword: mqtt.subscribe.topic.
Definition: detect-mqtt-subscribe-topic.c:196
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:640
InspectionBuffer
Definition: detect.h:338
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1141
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1239
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@87::@89 app_v2
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
PrefilterMpmMQTTSubscribeTopic::list_id
int list_id
Definition: detect-mqtt-subscribe-topic.c:129
detect-mqtt-subscribe-topic.h
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:626
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
detect-pcre.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@84 v2
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1139
util-unittest.h
MQTTSubscribeTopicGetDataArgs::local_id
uint32_t local_id
Definition: detect-mqtt-subscribe-topic.c:64
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
MQTTSubscribeTopicGetDataArgs
Definition: detect-mqtt-subscribe-topic.c:63
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:396
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:230
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1034
PrefilterMpmMQTTSubscribeTopic
struct PrefilterMpmMQTTSubscribeTopic PrefilterMpmMQTTSubscribeTopic
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:298
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:340
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:298
Packet_
Definition: decode.h:434
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:227
conf.h
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:314
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:165
PrefilterMpmMQTTSubscribeTopic::transforms
const DetectEngineTransforms * transforms
Definition: detect-mqtt-subscribe-topic.c:131
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1101
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:407
DetectEngineContentInspection
uint8_t DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode)
Run the actual payload match functions.
Definition: detect-engine-content-inspection.c:106
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:304
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:47
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:628
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1118
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:1436
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:341
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:339
str
#define str(s)
Definition: suricata-common.h:272
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags), AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:270
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:404
PrefilterMpmMQTTSubscribeTopic::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-mqtt-subscribe-topic.c:130
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
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:1389
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1431
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1300
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1183
MpmCtx_
Definition: util-mpm.h:88
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
flow-var.h
MQTTSubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-subscribe-topic.c:65
app-layer.h