suricata
detect-mqtt-unsubscribe-topic.c
Go to the documentation of this file.
1 /* Copyright (C) 2020 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 "debug.h"
31 #include "decode.h"
32 #include "detect.h"
33 #include "detect-content.h"
34 #include "detect-parse.h"
35 #include "detect-pcre.h"
36 #include "detect-engine.h"
38 #include "detect-engine-mpm.h"
41 #include "util-unittest.h"
42 #include "util-unittest-helper.h"
43 
44 #include "rust-bindings.h"
45 
46 #include "threads.h"
47 
48 #include "flow.h"
49 #include "flow-util.h"
50 #include "flow-var.h"
51 
52 #include "util-debug.h"
53 #include "util-unittest.h"
54 #include "util-spm.h"
55 #include "util-print.h"
56 
57 static int DetectMQTTUnsubscribeTopicSetup(DetectEngineCtx *, Signature *, const char *);
58 
59 static int g_mqtt_unsubscribe_topic_buffer_id = 0;
60 
61 static uint32_t unsubscribe_topic_match_limit = 100;
62 
64  uint32_t local_id;
65  void *txv;
66 };
67 
68 static InspectionBuffer *MQTTUnsubscribeTopicGetData(DetectEngineThreadCtx *det_ctx,
69  const DetectEngineTransforms *transforms,
70  Flow *f, struct MQTTUnsubscribeTopicGetDataArgs *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_unsubscribe_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 int DetectEngineInspectMQTTUnsubscribeTopic(
94  const DetectEngineAppInspectionEngine *engine,
95  const Signature *s,
96  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
97 {
98  uint32_t local_id = 0;
99 
100  const DetectEngineTransforms *transforms = NULL;
101  if (!engine->mpm) {
102  transforms = engine->v2.transforms;
103  }
104 
105  while ((unsubscribe_topic_match_limit == 0) || local_id < unsubscribe_topic_match_limit) {
106  struct MQTTUnsubscribeTopicGetDataArgs cbdata = { local_id, txv, };
107  InspectionBuffer *buffer = MQTTUnsubscribeTopicGetData(det_ctx,
108  transforms, f, &cbdata, engine->sm_list, false);
109  if (buffer == NULL || buffer->inspect == NULL)
110  break;
111 
112  det_ctx->buffer_offset = 0;
113  det_ctx->discontinue_matching = 0;
114  det_ctx->inspection_recursion_counter = 0;
115 
116  const int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
117  NULL, f,
118  (uint8_t *)buffer->inspect,
119  buffer->inspect_len,
122  if (match == 1) {
124  }
125  local_id++;
126  }
128 }
129 
131  int list_id;
132  const MpmCtx *mpm_ctx;
135 
136 /** \brief MQTTUnsubscribeTopic MQTTUnsubscribeTopic Mpm prefilter callback
137  *
138  * \param det_ctx detection engine thread ctx
139  * \param p packet to inspect
140  * \param f flow to inspect
141  * \param txv tx to inspect
142  * \param pectx inspection context
143  */
144 static void PrefilterTxMQTTUnsubscribeTopic(DetectEngineThreadCtx *det_ctx,
145  const void *pectx,
146  Packet *p, Flow *f, void *txv,
147  const uint64_t idx, const uint8_t flags)
148 {
149  SCEnter();
150 
152  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
153  const int list_id = ctx->list_id;
154 
155  uint32_t local_id = 0;
156  while ((unsubscribe_topic_match_limit == 0) || local_id < unsubscribe_topic_match_limit) {
157  struct MQTTUnsubscribeTopicGetDataArgs cbdata = { local_id, txv };
158  InspectionBuffer *buffer = MQTTUnsubscribeTopicGetData(det_ctx, ctx->transforms,
159  f, &cbdata, list_id, true);
160  if (buffer == NULL)
161  break;
162 
163  if (buffer->inspect_len >= mpm_ctx->minlen) {
164  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
165  &det_ctx->mtcu, &det_ctx->pmq,
166  buffer->inspect, buffer->inspect_len);
167  }
168  local_id++;
169  }
170 }
171 
172 static void PrefilterMpmMQTTUnsubscribeTopicFree(void *ptr)
173 {
174  if (ptr != NULL)
175  SCFree(ptr);
176 }
177 
178 static int PrefilterMpmMQTTUnsubscribeTopicRegister(DetectEngineCtx *de_ctx,
179  SigGroupHead *sgh, MpmCtx *mpm_ctx,
180  const DetectBufferMpmRegistery *mpm_reg, int list_id)
181 {
182  PrefilterMpmMQTTUnsubscribeTopic *pectx = SCCalloc(1, sizeof(*pectx));
183  if (pectx == NULL)
184  return -1;
185  pectx->list_id = list_id;
186  pectx->mpm_ctx = mpm_ctx;
187  pectx->transforms = &mpm_reg->transforms;
188 
189  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxMQTTUnsubscribeTopic,
190  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
191  pectx, PrefilterMpmMQTTUnsubscribeTopicFree, mpm_reg->pname);
192 }
193 
194 /**
195  * \brief Registration function for keyword: mqtt.unsubscribe.topic
196  */
198 {
199  sigmatch_table[DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC].name = "mqtt.unsubscribe.topic";
200  sigmatch_table[DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC].desc = "sticky buffer to match MQTT UNSUBSCRIBE topic";
201  sigmatch_table[DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC].url = "/rules/mqtt-keywords.html#mqtt-unsubscribe-topic";
202  sigmatch_table[DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC].Setup = DetectMQTTUnsubscribeTopicSetup;
205 
206  intmax_t val = 0;
207  if (ConfGetInt("app-layer.protocols.mqtt.unsubscribe-topic-match-limit", &val)) {
208  unsubscribe_topic_match_limit = val;
209  }
210  if (unsubscribe_topic_match_limit <= 0) {
211  SCLogDebug("Using unrestricted MQTT UNSUBSCRIBE topic matching");
212  } else {
213  SCLogDebug("Using MQTT UNSUBSCRIBE topic match-limit setting of: %i",
214  unsubscribe_topic_match_limit);
215  }
216 
217  DetectAppLayerMpmRegister2("mqtt.unsubscribe.topic", SIG_FLAG_TOSERVER, 1,
218  PrefilterMpmMQTTUnsubscribeTopicRegister, NULL,
219  ALPROTO_MQTT, 1);
220 
221  DetectAppLayerInspectEngineRegister2("mqtt.unsubscribe.topic",
223  DetectEngineInspectMQTTUnsubscribeTopic, NULL);
224 
225  DetectBufferTypeSetDescriptionByName("mqtt.unsubscribe.topic",
226  "unsubscribe topic query");
227 
228  g_mqtt_unsubscribe_topic_buffer_id = DetectBufferTypeGetByName("mqtt.unsubscribe.topic");
229 }
230 
231 /**
232  * \brief setup the sticky buffer keyword used in the rule
233  *
234  * \param de_ctx Pointer to the Detection Engine Context
235  * \param s Pointer to the Signature to which the current keyword belongs
236  * \param str Should hold an empty string always
237  *
238  * \retval 0 On success
239  * \retval -1 On failure
240  */
241 
242 static int DetectMQTTUnsubscribeTopicSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
243 {
244  if (DetectBufferSetActiveList(s, g_mqtt_unsubscribe_topic_buffer_id) < 0)
245  return -1;
247  return -1;
248  return 0;
249 }
ConfGetInt
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:436
DetectEngineAppInspectionEngine_
Definition: detect.h:398
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:402
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:1088
DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC
@ DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC
Definition: detect-engine-register.h:288
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1477
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
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:132
SigTableElmt_::name
const char * name
Definition: detect.h:1267
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1425
DetectEngineTransforms
Definition: detect.h:379
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:648
InspectionBuffer
Definition: detect.h:345
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1167
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1261
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:197
PrefilterMpmMQTTUnsubscribeTopic::list_id
int list_id
Definition: detect-mqtt-unsubscribe-topic.c:131
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
PrefilterMpmMQTTUnsubscribeTopic
struct PrefilterMpmMQTTUnsubscribeTopic PrefilterMpmMQTTUnsubscribeTopic
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:634
DetectEngineContentInspection
int 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:102
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
detect-pcre.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@84 v2
MQTTUnsubscribeTopicGetDataArgs
Definition: detect-mqtt-unsubscribe-topic.c:63
MQTTUnsubscribeTopicGetDataArgs::local_id
uint32_t local_id
Definition: detect-mqtt-unsubscribe-topic.c:64
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1165
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1077
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:404
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
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:347
detect-mqtt-unsubscribe-topic.h
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
Packet_
Definition: decode.h:427
PrefilterMpmMQTTUnsubscribeTopic
Definition: detect-mqtt-unsubscribe-topic.c:130
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:225
conf.h
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
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:1127
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:415
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
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:47
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:636
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:1144
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:1427
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:348
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:346
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:548
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:412
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
PrefilterMpmMQTTUnsubscribeTopic::transforms
const DetectEngineTransforms * transforms
Definition: detect-mqtt-unsubscribe-topic.c:133
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:1380
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1453
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1291
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1174
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:65
debug.h
app-layer.h