suricata
detect-mqtt-reason-code.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"
31 #include "util-byte.h"
32 #include "util-unittest.h"
33 
34 #include "rust.h"
35 
36 #define PARSE_REGEX "^\\s*\\d+\\s*$"
37 static DetectParseRegex parse_regex;
38 
39 static int mqtt_reason_code_id = 0;
40 
41 static int DetectMQTTReasonCodeMatch(DetectEngineThreadCtx *det_ctx,
42  Flow *f, uint8_t flags, void *state,
43  void *txv, const Signature *s,
44  const SigMatchCtx *ctx);
45 static int DetectMQTTReasonCodeSetup (DetectEngineCtx *, Signature *, const char *);
48 
49 /**
50  * \brief Registration function for mqtt.reason_code: keyword
51  */
53 {
54  sigmatch_table[DETECT_AL_MQTT_REASON_CODE].name = "mqtt.reason_code";
55  sigmatch_table[DETECT_AL_MQTT_REASON_CODE].alias = "mqtt.connack.return_code";
56  sigmatch_table[DETECT_AL_MQTT_REASON_CODE].desc = "match MQTT 5.0+ reason code";
57  sigmatch_table[DETECT_AL_MQTT_REASON_CODE].url = "/rules/mqtt-keywords.html#mqtt-reason-code";
58  sigmatch_table[DETECT_AL_MQTT_REASON_CODE].AppLayerTxMatch = DetectMQTTReasonCodeMatch;
59  sigmatch_table[DETECT_AL_MQTT_REASON_CODE].Setup = DetectMQTTReasonCodeSetup;
61 #ifdef UNITTESTS
63 #endif
64 
65  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
66 
69 
70  mqtt_reason_code_id = DetectBufferTypeGetByName("mqtt.reason_code");
71 }
72 
73 /**
74  * \internal
75  * \brief Function to match reason code of an MQTT 5.0 Tx
76  *
77  * \param det_ctx Pointer to the pattern matcher thread.
78  * \param f Pointer to the current flow.
79  * \param flags Flags.
80  * \param state App layer state.
81  * \param txv Pointer to the transaction.
82  * \param s Pointer to the Signature.
83  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTReasonCodeData.
84  *
85  * \retval 0 no match.
86  * \retval 1 match.
87  */
88 static int DetectMQTTReasonCodeMatch(DetectEngineThreadCtx *det_ctx,
89  Flow *f, uint8_t flags, void *state,
90  void *txv, const Signature *s,
91  const SigMatchCtx *ctx)
92 {
93  const uint8_t *de = (const uint8_t *)ctx;
94  uint8_t code;
95 
96  if (!de)
97  return 0;
98 
99  if (rs_mqtt_tx_get_reason_code(txv, &code) == 0) {
100  /* this function does not return a code that needs to be compared,
101  so we can just return the result of the check implemented in
102  Rust */
103  return rs_mqtt_tx_unsuback_has_reason_code(txv, *de);
104  } else {
105  if (code == *de)
106  return 1;
107  }
108  return 0;
109 }
110 
111 /**
112  * \internal
113  * \brief This function is used to parse options passed via mqtt.reason_code: keyword
114  *
115  * \param rawstr Pointer to the user provided options
116  *
117  * \retval de pointer to DetectMQTTReasonCodeData on success
118  * \retval NULL on failure
119  */
120 static uint8_t *DetectMQTTReasonCodeParse(const char *rawstr)
121 {
122  uint8_t *de = NULL;
123  int ret = 0;
124  uint8_t val;
125 
126  ret = StringParseUint8(&val, 10, 0, rawstr);
127  if (ret < 0) {
128  SCLogError(SC_ERR_UNKNOWN_VALUE, "invalid MQTT reason code: %s", rawstr);
129  return NULL;
130  }
131 
132  de = SCMalloc(sizeof(uint8_t));
133  if (unlikely(de == NULL))
134  return NULL;
135  *de = (uint8_t) val;
136 
137  return de;
138 }
139 
140 /**
141  * \internal
142  * \brief this function is used to add the parsed sigmatch into the current signature
143  *
144  * \param de_ctx pointer to the Detection Engine Context
145  * \param s pointer to the Current Signature
146  * \param rawstr pointer to the user provided options
147  *
148  * \retval 0 on Success
149  * \retval -1 on Failure
150  */
151 static int DetectMQTTReasonCodeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
152 {
153  uint8_t *de = NULL;
154  SigMatch *sm = NULL;
155 
157  return -1;
158 
159  de = DetectMQTTReasonCodeParse(rawstr);
160  if (de == NULL)
161  goto error;
162 
163  sm = SigMatchAlloc();
164  if (sm == NULL)
165  goto error;
166 
168  sm->ctx = (SigMatchCtx *)de;
169 
170  SigMatchAppendSMToList(s, sm, mqtt_reason_code_id);
171 
172  return 0;
173 
174 error:
175  if (de != NULL)
176  SCFree(de);
177  if (sm != NULL)
178  SCFree(sm);
179  return -1;
180 }
181 
182 /**
183  * \internal
184  * \brief this function will free memory associated with DetectMQTTReasonCodeData
185  *
186  * \param de pointer to DetectMQTTReasonCodeData
187  */
189 {
190  if (de_ptr != NULL)
191  SCFree(de_ptr);
192 }
193 
194 /*
195  * ONLY TESTS BELOW THIS COMMENT
196  */
197 
198 #ifdef UNITTESTS
199 /**
200  * \test MQTTReasonCodeTestParse01 is a test for a valid value
201  *
202  * \retval 1 on success
203  * \retval 0 on failure
204  */
205 static int MQTTReasonCodeTestParse01 (void)
206 {
207  uint8_t *de = NULL;
208 
209  de = DetectMQTTReasonCodeParse("3");
210  FAIL_IF_NULL(de);
211  FAIL_IF_NOT(*de == 3);
213 
214  de = DetectMQTTReasonCodeParse(" 4");
215  FAIL_IF_NULL(de);
216  FAIL_IF_NOT(*de == 4);
218 
219  de = DetectMQTTReasonCodeParse(" 5");
220  FAIL_IF_NULL(de);
221  FAIL_IF_NOT(*de == 5);
223 
224  de = DetectMQTTReasonCodeParse("255");
225  FAIL_IF_NULL(de);
226  FAIL_IF_NOT(*de == 255);
228 
229  PASS;
230 }
231 
232 /**
233  * \test MQTTReasonCodeTestParse02 is a test for an invalid value
234  *
235  * \retval 1 on success
236  * \retval 0 on failure
237  */
238 static int MQTTReasonCodeTestParse02 (void)
239 {
240  uint8_t *de = NULL;
241  de = DetectMQTTReasonCodeParse("6X");
242  if (de) {
244  FAIL;
245  }
246 
247  PASS;
248 }
249 
250 /**
251  * \test MQTTReasonCodeTestParse03 is a test for an invalid value
252  *
253  * \retval 1 on success
254  * \retval 0 on failure
255  */
256 static int MQTTReasonCodeTestParse03 (void)
257 {
258  uint8_t *de = NULL;
259  de = DetectMQTTReasonCodeParse("");
260  if (de) {
262  FAIL;
263  }
264 
265  PASS;
266 }
267 
268 /**
269  * \test MQTTReasonCodeTestParse04 is a test for an invalid value
270  *
271  * \retval 1 on success
272  * \retval 0 on failure
273  */
274 static int MQTTReasonCodeTestParse04 (void)
275 {
276  uint8_t *de = NULL;
277  de = DetectMQTTReasonCodeParse("256");
278  if (de) {
280  FAIL;
281  }
282 
283  PASS;
284 }
285 
286 
287 
288 #endif /* UNITTESTS */
289 
290 /**
291  * \brief this function registers unit tests for MQTTReasonCode
292  */
294 {
295 #ifdef UNITTESTS
296  UtRegisterTest("MQTTReasonCodeTestParse01", MQTTReasonCodeTestParse01);
297  UtRegisterTest("MQTTReasonCodeTestParse02", MQTTReasonCodeTestParse02);
298  UtRegisterTest("MQTTReasonCodeTestParse03", MQTTReasonCodeTestParse03);
299  UtRegisterTest("MQTTReasonCodeTestParse04", MQTTReasonCodeTestParse04);
300 #endif /* UNITTESTS */
301 }
util-byte.h
DetectMQTTReasonCodeRegister
void DetectMQTTReasonCodeRegister(void)
Registration function for mqtt.reason_code: keyword.
Definition: detect-mqtt-reason-code.c:52
SigTableElmt_::url
const char * url
Definition: detect.h:1248
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:1247
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1235
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1245
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
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:785
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1216
rust.h
DETECT_AL_MQTT_REASON_CODE
@ DETECT_AL_MQTT_REASON_CODE
Definition: detect-engine-register.h:288
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
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:1086
StringParseUint8
int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:359
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:230
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:1034
de
uint8_t de
Definition: app-layer-htp.c:568
SC_ERR_UNKNOWN_VALUE
@ SC_ERR_UNKNOWN_VALUE
Definition: util-error.h:159
DetectMQTTReasonCodeFree
void DetectMQTTReasonCodeFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-reason-code.c:188
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2598
detect.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:317
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:227
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-mqtt-reason-code.c:36
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:309
MQTTReasonCodeRegisterTests
void MQTTReasonCodeRegisterTests(void)
this function registers unit tests for MQTTReasonCode
Definition: detect-mqtt-reason-code.c:293
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1246
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:315
detect-mqtt-reason-code.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
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:1951
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:540
SigMatch_
a single match condition for a signature
Definition: detect.h:314
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
code
uint8_t code
Definition: decode-icmpv4.h:1
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1237