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-bindings.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 static int DetectEngineInspectMQTTTypeGeneric(ThreadVars *tv,
47  const Signature *s, const SigMatchData *smd,
48  Flow *f, uint8_t flags, void *alstate,
49  void *txv, uint64_t tx_id);
50 
51 /**
52  * \brief Registration function for ipopts: keyword
53  */
55 {
57  sigmatch_table[DETECT_AL_MQTT_TYPE].desc = "match MQTT control packet type";
58  sigmatch_table[DETECT_AL_MQTT_TYPE].url = "/rules/mqtt-keywords.html#mqtt-type";
59  sigmatch_table[DETECT_AL_MQTT_TYPE].AppLayerTxMatch = DetectMQTTTypeMatch;
60  sigmatch_table[DETECT_AL_MQTT_TYPE].Setup = DetectMQTTTypeSetup;
62 #ifdef UNITTESTS
64 #endif
65 
68  DetectEngineInspectMQTTTypeGeneric);
69 
70  mqtt_type_id = DetectBufferTypeGetByName("mqtt.type");
71 }
72 
73 static int DetectEngineInspectMQTTTypeGeneric(ThreadVars *tv,
75  const Signature *s, const SigMatchData *smd,
76  Flow *f, uint8_t flags, void *alstate,
77  void *txv, uint64_t tx_id)
78 {
79  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
80  f, flags, alstate, txv, tx_id);
81 }
82 
83 /**
84  * \internal
85  * \brief Function to match control packet type of an MQTT Tx
86  *
87  * \param det_ctx Pointer to the pattern matcher thread.
88  * \param f Pointer to the current flow.
89  * \param flags Flags.
90  * \param state App layer state.
91  * \param txv Pointer to the transaction.
92  * \param s Pointer to the Signature.
93  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTTypeData.
94  *
95  * \retval 0 no match.
96  * \retval 1 match.
97  */
98 static int DetectMQTTTypeMatch(DetectEngineThreadCtx *det_ctx,
99  Flow *f, uint8_t flags, void *state,
100  void *txv, const Signature *s,
101  const SigMatchCtx *ctx)
102 {
103  const uint8_t *de = (const uint8_t *)ctx;
104 
105  if (!de)
106  return 0;
107 
108  return rs_mqtt_tx_has_type(txv, *de);
109 }
110 
111 /**
112  * \internal
113  * \brief This function is used to parse options passed via mqtt.type: keyword
114  *
115  * \param rawstr Pointer to the user provided options
116  *
117  * \retval de pointer to DetectMQTTTypeData on success
118  * \retval NULL on failure
119  */
120 static uint8_t *DetectMQTTTypeParse(const char *rawstr)
121 {
122  uint8_t *de = NULL;
123  int ret = 0;
124 
125  ret = rs_mqtt_cstr_message_code(rawstr);
126  // negative value denotes invalid input
127  if(ret < 0) {
128  SCLogError(SC_ERR_UNKNOWN_VALUE, "unknown mqtt.type value %s", rawstr);
129  goto error;
130  }
131 
132  de = SCMalloc(sizeof(uint8_t));
133  if (unlikely(de == NULL))
134  goto error;
135 
136  *de = (uint8_t) ret;
137 
138  return de;
139 
140 error:
141  if (de != NULL)
142  SCFree(de);
143  return NULL;
144 }
145 
146 /**
147  * \internal
148  * \brief this function is used to add the parsed type query into the current signature
149  *
150  * \param de_ctx pointer to the Detection Engine Context
151  * \param s pointer to the Current Signature
152  * \param rawstr pointer to the user provided options
153  *
154  * \retval 0 on Success
155  * \retval -1 on Failure
156  */
157 static int DetectMQTTTypeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
158 {
159  uint8_t *de = NULL;
160  SigMatch *sm = NULL;
161 
163  return -1;
164 
165  de = DetectMQTTTypeParse(rawstr);
166  if (de == NULL)
167  goto error;
168 
169  sm = SigMatchAlloc();
170  if (sm == NULL)
171  goto error;
172 
174  sm->ctx = (SigMatchCtx *)de;
175 
176  SigMatchAppendSMToList(s, sm, mqtt_type_id);
177 
178  return 0;
179 
180 error:
181  if (de != NULL)
182  SCFree(de);
183  if (sm != NULL)
184  SCFree(sm);
185  return -1;
186 }
187 
188 /**
189  * \internal
190  * \brief this function will free memory associated with DetectMQTTTypeData
191  *
192  * \param de pointer to DetectMQTTTypeData
193  */
195 {
196  if (de_ptr != NULL)
197  SCFree(de_ptr);
198 }
199 
200 /*
201  * ONLY TESTS BELOW THIS COMMENT
202  */
203 
204 #ifdef UNITTESTS
205 /**
206  * \test MQTTTypeTestParse01 is a test for a valid value
207  *
208  * \retval 1 on success
209  * \retval 0 on failure
210  */
211 static int MQTTTypeTestParse01 (void)
212 {
213  uint8_t *de = NULL;
214  de = DetectMQTTTypeParse("CONNECT");
215  FAIL_IF_NULL(de);
216  FAIL_IF_NOT(*de == 1);
217  DetectMQTTTypeFree(NULL, de);
218 
219  de = DetectMQTTTypeParse("PINGRESP");
220  FAIL_IF_NULL(de);
221  FAIL_IF_NOT(*de == 13);
222  DetectMQTTTypeFree(NULL, de);
223 
224  PASS;
225 }
226 
227 /**
228  * \test MQTTTypeTestParse02 is a test for a valid value
229  *
230  * \retval 1 on success
231  * \retval 0 on failure
232  */
233 static int MQTTTypeTestParse02 (void)
234 {
235  uint8_t *de = NULL;
236  de = DetectMQTTTypeParse("auth");
237  FAIL_IF_NULL(de);
238  FAIL_IF_NOT(*de == 15);
239  DetectMQTTTypeFree(NULL, de);
240 
241  PASS;
242 }
243 
244 /**
245  * \test MQTTTypeTestParse03 is a test for an invalid value
246  *
247  * \retval 1 on success
248  * \retval 0 on failure
249  */
250 static int MQTTTypeTestParse03 (void)
251 {
252  uint8_t *de = NULL;
253  de = DetectMQTTTypeParse("invalidopt");
254  if (de) {
255  DetectMQTTTypeFree(NULL, de);
256  FAIL;
257  }
258 
259  de = DetectMQTTTypeParse("unassigned");
260  if (de) {
261  DetectMQTTTypeFree(NULL, de);
262  FAIL;
263  }
264 
265  PASS;
266 }
267 
268 #endif /* UNITTESTS */
269 
270 /**
271  * \brief this function registers unit tests for MQTTType
272  */
274 {
275 #ifdef UNITTESTS
276  UtRegisterTest("MQTTTypeTestParse01", MQTTTypeTestParse01);
277  UtRegisterTest("MQTTTypeTestParse02", MQTTTypeTestParse02);
278  UtRegisterTest("MQTTTypeTestParse03", MQTTTypeTestParse03);
279 #endif /* UNITTESTS */
280 }
SigTableElmt_::url
const char * url
Definition: detect.h:1214
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1480
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:1213
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
SigTableElmt_::name
const char * name
Definition: detect.h:1211
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DETECT_AL_MQTT_TYPE
@ DETECT_AL_MQTT_TYPE
Definition: detect-engine-register.h:270
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:767
DetectMQTTTypeFree
void DetectMQTTTypeFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-type.c:194
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1182
SigMatchData_
Data needed for Match()
Definition: detect.h:329
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
Definition: util-unittest.h:82
DetectEngineInspectGenericList
int DetectEngineInspectGenericList(ThreadVars *tv, const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, const uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1596
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
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:1010
SC_ERR_UNKNOWN_VALUE
@ SC_ERR_UNKNOWN_VALUE
Definition: util-error.h:159
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
de
int de
Definition: app-layer-htp.c:575
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
DetectMQTTTypeRegister
void DetectMQTTTypeRegister(void)
Registration function for ipopts: keyword.
Definition: detect-mqtt-type.c:54
conf.h
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:321
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:315
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:73
MQTTTypeRegisterTests
void MQTTTypeRegisterTests(void)
this function registers unit tests for MQTTType
Definition: detect-mqtt-type.c:273
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:55
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback)
register inspect engine at start up time
Definition: detect-engine.c:171
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
MQTTSubscribeTopicGetDataArgs::txv
void * txv
Definition: detect-mqtt-subscribe-topic.c:63
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203