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 
62  int local_id;
63  void *txv;
64 };
65 
66 static InspectionBuffer *MQTTUnsubscribeTopicGetData(DetectEngineThreadCtx *det_ctx,
67  const DetectEngineTransforms *transforms,
68  Flow *f, struct MQTTUnsubscribeTopicGetDataArgs *cbdata, int list_id, bool first)
69 {
70  SCEnter();
71 
74  if (buffer == NULL)
75  return NULL;
76  if (!first && buffer->inspect != NULL)
77  return buffer;
78 
79  const uint8_t *data;
80  uint32_t data_len;
81  if (rs_mqtt_tx_get_unsubscribe_topic(cbdata->txv, (uint16_t)cbdata->local_id,
82  &data, &data_len) == 0) {
83  return NULL;
84  }
85 
86  InspectionBufferSetup(buffer, data, data_len);
87  InspectionBufferApplyTransforms(buffer, transforms);
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  int local_id = 0;
99 
100  const DetectEngineTransforms *transforms = NULL;
101  if (!engine->mpm) {
102  transforms = engine->v2.transforms;
103  }
104 
105  while(1) {
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  int local_id = 0;
156  while(1) {
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 
207  DetectAppLayerMpmRegister2("mqtt.unsubscribe.topic", SIG_FLAG_TOSERVER, 1,
208  PrefilterMpmMQTTUnsubscribeTopicRegister, NULL,
209  ALPROTO_MQTT, 1);
210 
211  DetectAppLayerInspectEngineRegister2("mqtt.unsubscribe.topic",
213  DetectEngineInspectMQTTUnsubscribeTopic, NULL);
214 
215  DetectBufferTypeSetDescriptionByName("mqtt.unsubscribe.topic",
216  "unsubscribe topic query");
217 
218  g_mqtt_unsubscribe_topic_buffer_id = DetectBufferTypeGetByName("mqtt.unsubscribe.topic");
219 }
220 
221 /**
222  * \brief setup the sticky buffer keyword used in the rule
223  *
224  * \param de_ctx Pointer to the Detection Engine Context
225  * \param s Pointer to the Signature to which the current keyword belongs
226  * \param str Should hold an empty string always
227  *
228  * \retval 0 On success
229  * \retval -1 On failure
230  */
231 
232 static int DetectMQTTUnsubscribeTopicSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
233 {
234  if (DetectBufferSetActiveList(s, g_mqtt_unsubscribe_topic_buffer_id) < 0)
235  return -1;
237  return -1;
238  return 0;
239 }
DetectEngineAppInspectionEngine_
Definition: detect.h:402
SigTableElmt_::url
const char * url
Definition: detect.h:1214
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1480
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:1041
DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC
@ DETECT_AL_MQTT_UNSUBSCRIBE_TOPIC
Definition: detect-engine-register.h:285
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1404
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
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:1211
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1347
DetectEngineTransforms
Definition: detect.h:376
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:624
InspectionBuffer
Definition: detect.h:344
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1111
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1205
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:767
PrefilterMpmMQTTUnsubscribeTopic
struct PrefilterMpmMQTTUnsubscribeTopic PrefilterMpmMQTTUnsubscribeTopic
MQTTUnsubscribeTopicGetDataArgs::local_id
int local_id
Definition: detect-mqtt-unsubscribe-topic.c:62
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:611
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:104
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
detect-pcre.h
MQTTUnsubscribeTopicGetDataArgs
Definition: detect-mqtt-unsubscribe-topic.c:61
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1109
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:408
InspectionBufferMultipleForList
Definition: detect.h:364
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:1010
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
DetectEngineAppInspectionEngine_::mpm
uint16_t mpm
Definition: detect.h:406
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:346
detect-mqtt-unsubscribe-topic.h
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
Packet_
Definition: decode.h:414
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:232
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:162
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1076
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:426
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:46
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:613
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
InspectionBufferSetup
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1120
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1211
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1088
util-spm.h
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:347
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@93::@95 app_v2
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
str
#define str(s)
Definition: suricata-common.h:273
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:55
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:257
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:423
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
InspectionBufferGetMulti
InspectionBufferMultipleForList * InspectionBufferGetMulti(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1100
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1380
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:992
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:908
MpmCtx_
Definition: util-mpm.h:88
flow.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@91 v2
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
flow-var.h
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:35
MQTTUnsubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-unsubscribe-topic.c:63
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(InspectionBufferMultipleForList *fb, uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1074
debug.h
app-layer.h