suricata
detect-mqtt-type.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 #include "conf.h"
26 #include "detect.h"
27 #include "detect-parse.h"
28 #include "detect-engine.h"
30 #include "detect-mqtt-type.h"
31 #include "util-unittest.h"
32 
33 #include "rust.h"
34 
35 static int mqtt_type_id = 0;
36 
37 static int DetectMQTTTypeMatch(DetectEngineThreadCtx *det_ctx,
38  Flow *f, uint8_t flags, void *state,
39  void *txv, const Signature *s,
40  const SigMatchCtx *ctx);
41 static int DetectMQTTTypeSetup (DetectEngineCtx *, Signature *, const char *);
42 void MQTTTypeRegisterTests(void);
44 
45 /**
46  * \brief Registration function for mqtt.type: keyword
47  */
49 {
51  sigmatch_table[DETECT_AL_MQTT_TYPE].desc = "match MQTT control packet type";
52  sigmatch_table[DETECT_AL_MQTT_TYPE].url = "/rules/mqtt-keywords.html#mqtt-type";
53  sigmatch_table[DETECT_AL_MQTT_TYPE].AppLayerTxMatch = DetectMQTTTypeMatch;
54  sigmatch_table[DETECT_AL_MQTT_TYPE].Setup = DetectMQTTTypeSetup;
56 #ifdef UNITTESTS
58 #endif
59 
62 
63  mqtt_type_id = DetectBufferTypeGetByName("mqtt.type");
64 }
65 
66 /**
67  * \internal
68  * \brief Function to match control packet type of an MQTT Tx
69  *
70  * \param det_ctx Pointer to the pattern matcher thread.
71  * \param f Pointer to the current flow.
72  * \param flags Flags.
73  * \param state App layer state.
74  * \param txv Pointer to the transaction.
75  * \param s Pointer to the Signature.
76  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTTypeData.
77  *
78  * \retval 0 no match.
79  * \retval 1 match.
80  */
81 static int DetectMQTTTypeMatch(DetectEngineThreadCtx *det_ctx,
82  Flow *f, uint8_t flags, void *state,
83  void *txv, const Signature *s,
84  const SigMatchCtx *ctx)
85 {
86  const uint8_t *de = (const uint8_t *)ctx;
87 
88  if (!de)
89  return 0;
90 
91  return rs_mqtt_tx_has_type(txv, *de);
92 }
93 
94 /**
95  * \internal
96  * \brief This function is used to parse options passed via mqtt.type: keyword
97  *
98  * \param rawstr Pointer to the user provided options
99  *
100  * \retval de pointer to DetectMQTTTypeData on success
101  * \retval NULL on failure
102  */
103 static uint8_t *DetectMQTTTypeParse(const char *rawstr)
104 {
105  uint8_t *de = NULL;
106  int ret = 0;
107 
108  ret = rs_mqtt_cstr_message_code(rawstr);
109  // negative value denotes invalid input
110  if(ret < 0) {
111  SCLogError("unknown mqtt.type value %s", rawstr);
112  goto error;
113  }
114 
115  de = SCMalloc(sizeof(uint8_t));
116  if (unlikely(de == NULL))
117  goto error;
118 
119  *de = (uint8_t) ret;
120 
121  return de;
122 
123 error:
124  if (de != NULL)
125  SCFree(de);
126  return NULL;
127 }
128 
129 /**
130  * \internal
131  * \brief this function is used to add the parsed type query into the current signature
132  *
133  * \param de_ctx pointer to the Detection Engine Context
134  * \param s pointer to the Current Signature
135  * \param rawstr pointer to the user provided options
136  *
137  * \retval 0 on Success
138  * \retval -1 on Failure
139  */
140 static int DetectMQTTTypeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
141 {
142  uint8_t *de = NULL;
143 
145  return -1;
146 
147  de = DetectMQTTTypeParse(rawstr);
148  if (de == NULL)
149  goto error;
150 
151  if (SigMatchAppendSMToList(de_ctx, s, DETECT_AL_MQTT_TYPE, (SigMatchCtx *)de, mqtt_type_id) ==
152  NULL) {
153  goto error;
154  }
155 
156  return 0;
157 
158 error:
159  if (de != NULL)
160  SCFree(de);
161  return -1;
162 }
163 
164 /**
165  * \internal
166  * \brief this function will free memory associated with DetectMQTTTypeData
167  *
168  * \param de pointer to DetectMQTTTypeData
169  */
171 {
172  if (de_ptr != NULL)
173  SCFree(de_ptr);
174 }
175 
176 /*
177  * ONLY TESTS BELOW THIS COMMENT
178  */
179 
180 #ifdef UNITTESTS
181 /**
182  * \test MQTTTypeTestParse01 is a test for a valid value
183  *
184  * \retval 1 on success
185  * \retval 0 on failure
186  */
187 static int MQTTTypeTestParse01 (void)
188 {
189  uint8_t *de = NULL;
190  de = DetectMQTTTypeParse("CONNECT");
191  FAIL_IF_NULL(de);
192  FAIL_IF_NOT(*de == 1);
193  DetectMQTTTypeFree(NULL, de);
194 
195  de = DetectMQTTTypeParse("PINGRESP");
196  FAIL_IF_NULL(de);
197  FAIL_IF_NOT(*de == 13);
198  DetectMQTTTypeFree(NULL, de);
199 
200  PASS;
201 }
202 
203 /**
204  * \test MQTTTypeTestParse02 is a test for a valid value
205  *
206  * \retval 1 on success
207  * \retval 0 on failure
208  */
209 static int MQTTTypeTestParse02 (void)
210 {
211  uint8_t *de = NULL;
212  de = DetectMQTTTypeParse("auth");
213  FAIL_IF_NULL(de);
214  FAIL_IF_NOT(*de == 15);
215  DetectMQTTTypeFree(NULL, de);
216 
217  PASS;
218 }
219 
220 /**
221  * \test MQTTTypeTestParse03 is a test for an invalid value
222  *
223  * \retval 1 on success
224  * \retval 0 on failure
225  */
226 static int MQTTTypeTestParse03 (void)
227 {
228  uint8_t *de = NULL;
229  de = DetectMQTTTypeParse("invalidopt");
230  if (de) {
231  DetectMQTTTypeFree(NULL, de);
232  FAIL;
233  }
234 
235  de = DetectMQTTTypeParse("unassigned");
236  if (de) {
237  DetectMQTTTypeFree(NULL, de);
238  FAIL;
239  }
240 
241  PASS;
242 }
243 
244 #endif /* UNITTESTS */
245 
246 /**
247  * \brief this function registers unit tests for MQTTType
248  */
250 {
251 #ifdef UNITTESTS
252  UtRegisterTest("MQTTTypeTestParse01", MQTTTypeTestParse01);
253  UtRegisterTest("MQTTTypeTestParse02", MQTTTypeTestParse02);
254  UtRegisterTest("MQTTTypeTestParse03", MQTTTypeTestParse03);
255 #endif /* UNITTESTS */
256 }
SigTableElmt_::url
const char * url
Definition: detect.h:1296
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1295
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1283
SigTableElmt_::name
const char * name
Definition: detect.h:1293
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DETECT_AL_MQTT_TYPE
@ DETECT_AL_MQTT_TYPE
Definition: detect-engine-register.h:300
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_
Flow data structure.
Definition: flow.h:351
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
DetectMQTTTypeFree
void DetectMQTTTypeFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-type.c:170
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1264
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:263
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1092
de
uint8_t de
Definition: app-layer-htp.c:581
detect.h
DetectMQTTTypeRegister
void DetectMQTTTypeRegister(void)
Registration function for mqtt.type: keyword.
Definition: detect-mqtt-type.c:48
conf.h
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:342
detect-mqtt-type.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
MQTTTypeRegisterTests
void MQTTTypeRegisterTests(void)
this function registers unit tests for MQTTType
Definition: detect-mqtt-type.c:249
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2079
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:593
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447
MQTTSubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-subscribe-topic.c:64
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1285