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 static int DetectEngineInspectMQTTTypeGeneric(DetectEngineCtx *de_ctx,
46  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
47  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
48 
49 /**
50  * \brief Registration function for ipopts: keyword
51  */
53 {
55  sigmatch_table[DETECT_AL_MQTT_TYPE].desc = "match MQTT control packet type";
56  sigmatch_table[DETECT_AL_MQTT_TYPE].url = "/rules/mqtt-keywords.html#mqtt-type";
57  sigmatch_table[DETECT_AL_MQTT_TYPE].AppLayerTxMatch = DetectMQTTTypeMatch;
58  sigmatch_table[DETECT_AL_MQTT_TYPE].Setup = DetectMQTTTypeSetup;
60 #ifdef UNITTESTS
62 #endif
63 
65  DetectEngineInspectMQTTTypeGeneric, NULL);
66 
67  mqtt_type_id = DetectBufferTypeGetByName("mqtt.type");
68 }
69 
70 static int DetectEngineInspectMQTTTypeGeneric(DetectEngineCtx *de_ctx,
71  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
72  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
73 {
75  de_ctx, det_ctx, s, engine->smd, f, flags, alstate, txv, tx_id);
76 }
77 
78 /**
79  * \internal
80  * \brief Function to match control packet type of an MQTT Tx
81  *
82  * \param det_ctx Pointer to the pattern matcher thread.
83  * \param f Pointer to the current flow.
84  * \param flags Flags.
85  * \param state App layer state.
86  * \param txv Pointer to the transaction.
87  * \param s Pointer to the Signature.
88  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTTypeData.
89  *
90  * \retval 0 no match.
91  * \retval 1 match.
92  */
93 static int DetectMQTTTypeMatch(DetectEngineThreadCtx *det_ctx,
94  Flow *f, uint8_t flags, void *state,
95  void *txv, const Signature *s,
96  const SigMatchCtx *ctx)
97 {
98  const uint8_t *de = (const uint8_t *)ctx;
99 
100  if (!de)
101  return 0;
102 
103  return rs_mqtt_tx_has_type(txv, *de);
104 }
105 
106 /**
107  * \internal
108  * \brief This function is used to parse options passed via mqtt.type: keyword
109  *
110  * \param rawstr Pointer to the user provided options
111  *
112  * \retval de pointer to DetectMQTTTypeData on success
113  * \retval NULL on failure
114  */
115 static uint8_t *DetectMQTTTypeParse(const char *rawstr)
116 {
117  uint8_t *de = NULL;
118  int ret = 0;
119 
120  ret = rs_mqtt_cstr_message_code(rawstr);
121  // negative value denotes invalid input
122  if(ret < 0) {
123  SCLogError(SC_ERR_UNKNOWN_VALUE, "unknown mqtt.type value %s", rawstr);
124  goto error;
125  }
126 
127  de = SCMalloc(sizeof(uint8_t));
128  if (unlikely(de == NULL))
129  goto error;
130 
131  *de = (uint8_t) ret;
132 
133  return de;
134 
135 error:
136  if (de != NULL)
137  SCFree(de);
138  return NULL;
139 }
140 
141 /**
142  * \internal
143  * \brief this function is used to add the parsed type query into the current signature
144  *
145  * \param de_ctx pointer to the Detection Engine Context
146  * \param s pointer to the Current Signature
147  * \param rawstr pointer to the user provided options
148  *
149  * \retval 0 on Success
150  * \retval -1 on Failure
151  */
152 static int DetectMQTTTypeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
153 {
154  uint8_t *de = NULL;
155  SigMatch *sm = NULL;
156 
158  return -1;
159 
160  de = DetectMQTTTypeParse(rawstr);
161  if (de == NULL)
162  goto error;
163 
164  sm = SigMatchAlloc();
165  if (sm == NULL)
166  goto error;
167 
169  sm->ctx = (SigMatchCtx *)de;
170 
171  SigMatchAppendSMToList(s, sm, mqtt_type_id);
172 
173  return 0;
174 
175 error:
176  if (de != NULL)
177  SCFree(de);
178  if (sm != NULL)
179  SCFree(sm);
180  return -1;
181 }
182 
183 /**
184  * \internal
185  * \brief this function will free memory associated with DetectMQTTTypeData
186  *
187  * \param de pointer to DetectMQTTTypeData
188  */
190 {
191  if (de_ptr != NULL)
192  SCFree(de_ptr);
193 }
194 
195 /*
196  * ONLY TESTS BELOW THIS COMMENT
197  */
198 
199 #ifdef UNITTESTS
200 /**
201  * \test MQTTTypeTestParse01 is a test for a valid value
202  *
203  * \retval 1 on success
204  * \retval 0 on failure
205  */
206 static int MQTTTypeTestParse01 (void)
207 {
208  uint8_t *de = NULL;
209  de = DetectMQTTTypeParse("CONNECT");
210  FAIL_IF_NULL(de);
211  FAIL_IF_NOT(*de == 1);
212  DetectMQTTTypeFree(NULL, de);
213 
214  de = DetectMQTTTypeParse("PINGRESP");
215  FAIL_IF_NULL(de);
216  FAIL_IF_NOT(*de == 13);
217  DetectMQTTTypeFree(NULL, de);
218 
219  PASS;
220 }
221 
222 /**
223  * \test MQTTTypeTestParse02 is a test for a valid value
224  *
225  * \retval 1 on success
226  * \retval 0 on failure
227  */
228 static int MQTTTypeTestParse02 (void)
229 {
230  uint8_t *de = NULL;
231  de = DetectMQTTTypeParse("auth");
232  FAIL_IF_NULL(de);
233  FAIL_IF_NOT(*de == 15);
234  DetectMQTTTypeFree(NULL, de);
235 
236  PASS;
237 }
238 
239 /**
240  * \test MQTTTypeTestParse03 is a test for an invalid value
241  *
242  * \retval 1 on success
243  * \retval 0 on failure
244  */
245 static int MQTTTypeTestParse03 (void)
246 {
247  uint8_t *de = NULL;
248  de = DetectMQTTTypeParse("invalidopt");
249  if (de) {
250  DetectMQTTTypeFree(NULL, de);
251  FAIL;
252  }
253 
254  de = DetectMQTTTypeParse("unassigned");
255  if (de) {
256  DetectMQTTTypeFree(NULL, de);
257  FAIL;
258  }
259 
260  PASS;
261 }
262 
263 #endif /* UNITTESTS */
264 
265 /**
266  * \brief this function registers unit tests for MQTTType
267  */
269 {
270 #ifdef UNITTESTS
271  UtRegisterTest("MQTTTypeTestParse01", MQTTTypeTestParse01);
272  UtRegisterTest("MQTTTypeTestParse02", MQTTTypeTestParse02);
273  UtRegisterTest("MQTTTypeTestParse03", MQTTTypeTestParse03);
274 #endif /* UNITTESTS */
275 }
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
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:1269
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
DetectEngineInspectGenericList
int DetectEngineInspectGenericList(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:1941
SigTableElmt_::name
const char * name
Definition: detect.h:1267
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DETECT_AL_MQTT_TYPE
@ DETECT_AL_MQTT_TYPE
Definition: detect-engine-register.h:273
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_
Flow data structure.
Definition: flow.h:353
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
DetectMQTTTypeFree
void DetectMQTTTypeFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-type.c:189
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1238
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
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:1077
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:1060
de
uint8_t de
Definition: app-layer-htp.c:563
SC_ERR_UNKNOWN_VALUE
@ SC_ERR_UNKNOWN_VALUE
Definition: util-error.h:159
detect.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
DetectMQTTTypeRegister
void DetectMQTTTypeRegister(void)
Registration function for ipopts: keyword.
Definition: detect-mqtt-type.c:52
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
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
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:316
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:415
detect-mqtt-type.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:322
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:268
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
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:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
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:65
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259