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 ipopts: 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  SigMatch *sm = NULL;
144 
146  return -1;
147 
148  de = DetectMQTTTypeParse(rawstr);
149  if (de == NULL)
150  goto error;
151 
152  sm = SigMatchAlloc();
153  if (sm == NULL)
154  goto error;
155 
157  sm->ctx = (SigMatchCtx *)de;
158 
159  SigMatchAppendSMToList(s, sm, mqtt_type_id);
160 
161  return 0;
162 
163 error:
164  if (de != NULL)
165  SCFree(de);
166  if (sm != NULL)
167  SCFree(sm);
168  return -1;
169 }
170 
171 /**
172  * \internal
173  * \brief this function will free memory associated with DetectMQTTTypeData
174  *
175  * \param de pointer to DetectMQTTTypeData
176  */
178 {
179  if (de_ptr != NULL)
180  SCFree(de_ptr);
181 }
182 
183 /*
184  * ONLY TESTS BELOW THIS COMMENT
185  */
186 
187 #ifdef UNITTESTS
188 /**
189  * \test MQTTTypeTestParse01 is a test for a valid value
190  *
191  * \retval 1 on success
192  * \retval 0 on failure
193  */
194 static int MQTTTypeTestParse01 (void)
195 {
196  uint8_t *de = NULL;
197  de = DetectMQTTTypeParse("CONNECT");
198  FAIL_IF_NULL(de);
199  FAIL_IF_NOT(*de == 1);
200  DetectMQTTTypeFree(NULL, de);
201 
202  de = DetectMQTTTypeParse("PINGRESP");
203  FAIL_IF_NULL(de);
204  FAIL_IF_NOT(*de == 13);
205  DetectMQTTTypeFree(NULL, de);
206 
207  PASS;
208 }
209 
210 /**
211  * \test MQTTTypeTestParse02 is a test for a valid value
212  *
213  * \retval 1 on success
214  * \retval 0 on failure
215  */
216 static int MQTTTypeTestParse02 (void)
217 {
218  uint8_t *de = NULL;
219  de = DetectMQTTTypeParse("auth");
220  FAIL_IF_NULL(de);
221  FAIL_IF_NOT(*de == 15);
222  DetectMQTTTypeFree(NULL, de);
223 
224  PASS;
225 }
226 
227 /**
228  * \test MQTTTypeTestParse03 is a test for an invalid value
229  *
230  * \retval 1 on success
231  * \retval 0 on failure
232  */
233 static int MQTTTypeTestParse03 (void)
234 {
235  uint8_t *de = NULL;
236  de = DetectMQTTTypeParse("invalidopt");
237  if (de) {
238  DetectMQTTTypeFree(NULL, de);
239  FAIL;
240  }
241 
242  de = DetectMQTTTypeParse("unassigned");
243  if (de) {
244  DetectMQTTTypeFree(NULL, de);
245  FAIL;
246  }
247 
248  PASS;
249 }
250 
251 #endif /* UNITTESTS */
252 
253 /**
254  * \brief this function registers unit tests for MQTTType
255  */
257 {
258 #ifdef UNITTESTS
259  UtRegisterTest("MQTTTypeTestParse01", MQTTTypeTestParse01);
260  UtRegisterTest("MQTTTypeTestParse02", MQTTTypeTestParse02);
261  UtRegisterTest("MQTTTypeTestParse03", MQTTTypeTestParse03);
262 #endif /* UNITTESTS */
263 }
SigTableElmt_::url
const char * url
Definition: detect.h:1287
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1703
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:437
SigTableElmt_::desc
const char * desc
Definition: detect.h:1286
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1274
SigTableElmt_::name
const char * name
Definition: detect.h:1284
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DETECT_AL_MQTT_TYPE
@ DETECT_AL_MQTT_TYPE
Definition: detect-engine-register.h:289
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:826
DetectMQTTTypeFree
void DetectMQTTTypeFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-type.c:177
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1255
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1269
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:1124
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:255
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:1074
de
uint8_t de
Definition: app-layer-htp.c:577
detect.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:343
DetectMQTTTypeRegister
void DetectMQTTTypeRegister(void)
Registration function for ipopts: keyword.
Definition: detect-mqtt-type.c:48
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:216
conf.h
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:322
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:335
detect-mqtt-type.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:341
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
MQTTTypeRegisterTests
void MQTTTypeRegisterTests(void)
this function registers unit tests for MQTTType
Definition: detect-mqtt-type.c:256
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:2122
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:581
SigMatch_
a single match condition for a signature
Definition: detect.h:340
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
MQTTSubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-subscribe-topic.c:64
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1276