suricata
detect-mqtt-unsubscribe-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 DetectMQTTUnsubscribeTopicSetup(DetectEngineCtx *, Signature *, const char *);
57 
58 static int g_mqtt_unsubscribe_topic_buffer_id = 0;
59 
60 static uint32_t unsubscribe_topic_match_limit = 100;
61 
63  uint32_t local_id;
64  void *txv;
65 };
66 
67 static InspectionBuffer *MQTTUnsubscribeTopicGetData(DetectEngineThreadCtx *det_ctx,
68  const DetectEngineTransforms *transforms, Flow *f,
69  struct MQTTUnsubscribeTopicGetDataArgs *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_unsubscribe_topic(cbdata->txv, cbdata->local_id, &data, &data_len) == 0) {
83  return NULL;
84  }
85 
86  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
87 
88  SCReturnPtr(buffer, "InspectionBuffer");
89 }
90 
91 static uint8_t DetectEngineInspectMQTTUnsubscribeTopic(DetectEngineCtx *de_ctx,
93  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
94 {
95  uint32_t local_id = 0;
96 
97  const DetectEngineTransforms *transforms = NULL;
98  if (!engine->mpm) {
99  transforms = engine->v2.transforms;
100  }
101 
102  while ((unsubscribe_topic_match_limit == 0) || local_id < unsubscribe_topic_match_limit) {
103  struct MQTTUnsubscribeTopicGetDataArgs cbdata = { local_id, txv, };
104  InspectionBuffer *buffer =
105  MQTTUnsubscribeTopicGetData(det_ctx, transforms, f, &cbdata, engine->sm_list);
106  if (buffer == NULL || buffer->inspect == NULL)
107  break;
108 
109  det_ctx->buffer_offset = 0;
110  det_ctx->discontinue_matching = 0;
111  det_ctx->inspection_recursion_counter = 0;
112 
113  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
114  NULL, f,
115  (uint8_t *)buffer->inspect,
116  buffer->inspect_len,
119  if (match == 1) {
121  }
122  local_id++;
123  }
125 }
126 
128  int list_id;
129  const MpmCtx *mpm_ctx;
132 
133 /** \brief MQTTUnsubscribeTopic MQTTUnsubscribeTopic Mpm prefilter callback
134  *
135  * \param det_ctx detection engine thread ctx
136  * \param p packet to inspect
137  * \param f flow to inspect
138  * \param txv tx to inspect
139  * \param pectx inspection context
140  */
141 static void PrefilterTxMQTTUnsubscribeTopic(DetectEngineThreadCtx *det_ctx, const void *pectx,
142  Packet *p, Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd,
143  const uint8_t flags)
144 {
145  SCEnter();
146 
148  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
149  const int list_id = ctx->list_id;
150 
151  uint32_t local_id = 0;
152  while ((unsubscribe_topic_match_limit == 0) || local_id < unsubscribe_topic_match_limit) {
153  struct MQTTUnsubscribeTopicGetDataArgs cbdata = { local_id, txv };
154  InspectionBuffer *buffer =
155  MQTTUnsubscribeTopicGetData(det_ctx, ctx->transforms, f, &cbdata, list_id);
156  if (buffer == NULL)
157  break;
158 
159  if (buffer->inspect_len >= mpm_ctx->minlen) {
160  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
161  &det_ctx->mtcu, &det_ctx->pmq,
162  buffer->inspect, buffer->inspect_len);
163  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
164  }
165  local_id++;
166  }
167 }
168 
169 static void PrefilterMpmMQTTUnsubscribeTopicFree(void *ptr)
170 {
171  if (ptr != NULL)
172  SCFree(ptr);
173 }
174 
175 static int PrefilterMpmMQTTUnsubscribeTopicRegister(DetectEngineCtx *de_ctx,
176  SigGroupHead *sgh, MpmCtx *mpm_ctx,
177  const DetectBufferMpmRegistery *mpm_reg, int list_id)
178 {
179  PrefilterMpmMQTTUnsubscribeTopic *pectx = SCCalloc(1, sizeof(*pectx));
180  if (pectx == NULL)
181  return -1;
182  pectx->list_id = list_id;
183  pectx->mpm_ctx = mpm_ctx;
184  pectx->transforms = &mpm_reg->transforms;
185 
186  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxMQTTUnsubscribeTopic,
187  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
188  pectx, PrefilterMpmMQTTUnsubscribeTopicFree, mpm_reg->pname);
189 }
190 
191 /**
192  * \brief Registration function for keyword: mqtt.unsubscribe.topic
193  */
195 {
196  sigmatch_table[DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC].name = "mqtt.unsubscribe.topic";
197  sigmatch_table[DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC].desc = "sticky buffer to match MQTT UNSUBSCRIBE topic";
198  sigmatch_table[DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC].url = "/rules/mqtt-keywords.html#mqtt-unsubscribe-topic";
199  sigmatch_table[DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC].Setup = DetectMQTTUnsubscribeTopicSetup;
202 
203  intmax_t val = 0;
204  if (ConfGetInt("app-layer.protocols.mqtt.unsubscribe-topic-match-limit", &val)) {
205  unsubscribe_topic_match_limit = val;
206  }
207  if (unsubscribe_topic_match_limit <= 0) {
208  SCLogDebug("Using unrestricted MQTT UNSUBSCRIBE topic matching");
209  } else {
210  SCLogDebug("Using MQTT UNSUBSCRIBE topic match-limit setting of: %i",
211  unsubscribe_topic_match_limit);
212  }
213 
214  DetectAppLayerMpmRegister2("mqtt.unsubscribe.topic", SIG_FLAG_TOSERVER, 1,
215  PrefilterMpmMQTTUnsubscribeTopicRegister, NULL,
216  ALPROTO_MQTT, 1);
217 
218  DetectAppLayerInspectEngineRegister2("mqtt.unsubscribe.topic",
220  DetectEngineInspectMQTTUnsubscribeTopic, NULL);
221 
222  DetectBufferTypeSetDescriptionByName("mqtt.unsubscribe.topic",
223  "unsubscribe topic query");
224 
225  g_mqtt_unsubscribe_topic_buffer_id = DetectBufferTypeGetByName("mqtt.unsubscribe.topic");
226 }
227 
228 /**
229  * \brief setup the sticky buffer keyword used in the rule
230  *
231  * \param de_ctx Pointer to the Detection Engine Context
232  * \param s Pointer to the Signature to which the current keyword belongs
233  * \param str Should hold an empty string always
234  *
235  * \retval 0 On success
236  * \retval -1 On failure
237  */
238 
239 static int DetectMQTTUnsubscribeTopicSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
240 {
241  if (DetectBufferSetActiveList(s, g_mqtt_unsubscribe_topic_buffer_id) < 0)
242  return -1;
244  return -1;
245  return 0;
246 }
ConfGetInt
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:397
DetectEngineAppInspectionEngine_
Definition: detect.h:390
SigTableElmt_::url
const char * url
Definition: detect.h:1241
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1498
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:1053
DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC
@ DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC
Definition: detect-engine-register.h:303
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1447
SigTableElmt_::desc
const char * desc
Definition: detect.h:1240
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
flow-util.h
PrefilterMpmMQTTUnsubscribeTopic::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-mqtt-unsubscribe-topic.c:129
SigTableElmt_::name
const char * name
Definition: detect.h:1238
InspectionBuffer::initialized
bool initialized
Definition: detect.h:341
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1395
DetectEngineTransforms
Definition: detect.h:372
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:640
InspectionBuffer
Definition: detect.h:337
threads.h
Flow_
Flow data structure.
Definition: flow.h:357
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1134
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1232
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@87::@89 app_v2
DetectMQTTUnsubscribeTopicRegister
void DetectMQTTUnsubscribeTopicRegister(void)
Registration function for keyword: mqtt.unsubscribe.topic.
Definition: detect-mqtt-unsubscribe-topic.c:194
PrefilterMpmMQTTUnsubscribeTopic::list_id
int list_id
Definition: detect-mqtt-unsubscribe-topic.c:128
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
PrefilterMpmMQTTUnsubscribeTopic
struct PrefilterMpmMQTTUnsubscribeTopic PrefilterMpmMQTTUnsubscribeTopic
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:626
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1223
detect-pcre.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@84 v2
MQTTUnsubscribeTopicGetDataArgs
Definition: detect-mqtt-unsubscribe-topic.c:62
MQTTUnsubscribeTopicGetDataArgs::local_id
uint32_t local_id
Definition: detect-mqtt-unsubscribe-topic.c:63
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1132
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1079
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:396
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1025
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:339
detect-mqtt-unsubscribe-topic.h
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
util-profiling.h
Packet_
Definition: decode.h:428
PrefilterMpmMQTTUnsubscribeTopic
Definition: detect-mqtt-unsubscribe-topic.c:127
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:224
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:165
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1092
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
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1303
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:307
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:76
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1111
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:1431
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:340
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:338
str
#define str(s)
Definition: suricata-common.h:280
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
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:404
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
PrefilterMpmMQTTUnsubscribeTopic::transforms
const DetectEngineTransforms * transforms
Definition: detect-mqtt-unsubscribe-topic.c:130
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:1384
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1423
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1293
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1176
MpmCtx_
Definition: util-mpm.h:88
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
flow-var.h
MQTTUnsubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-unsubscribe-topic.c:64
app-layer.h