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