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("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 
156  return -1;
157 
158  de = DetectMQTTReasonCodeParse(rawstr);
159  if (de == NULL)
160  goto error;
161 
163  mqtt_reason_code_id) == NULL) {
164  goto error;
165  }
166 
167  return 0;
168 
169 error:
170  if (de != NULL)
171  SCFree(de);
172  return -1;
173 }
174 
175 /**
176  * \internal
177  * \brief this function will free memory associated with DetectMQTTReasonCodeData
178  *
179  * \param de pointer to DetectMQTTReasonCodeData
180  */
182 {
183  if (de_ptr != NULL)
184  SCFree(de_ptr);
185 }
186 
187 /*
188  * ONLY TESTS BELOW THIS COMMENT
189  */
190 
191 #ifdef UNITTESTS
192 /**
193  * \test MQTTReasonCodeTestParse01 is a test for a valid value
194  *
195  * \retval 1 on success
196  * \retval 0 on failure
197  */
198 static int MQTTReasonCodeTestParse01 (void)
199 {
200  uint8_t *de = NULL;
201 
202  de = DetectMQTTReasonCodeParse("3");
203  FAIL_IF_NULL(de);
204  FAIL_IF_NOT(*de == 3);
206 
207  de = DetectMQTTReasonCodeParse(" 4");
208  FAIL_IF_NULL(de);
209  FAIL_IF_NOT(*de == 4);
211 
212  de = DetectMQTTReasonCodeParse(" 5");
213  FAIL_IF_NULL(de);
214  FAIL_IF_NOT(*de == 5);
216 
217  de = DetectMQTTReasonCodeParse("255");
218  FAIL_IF_NULL(de);
219  FAIL_IF_NOT(*de == 255);
221 
222  PASS;
223 }
224 
225 /**
226  * \test MQTTReasonCodeTestParse02 is a test for an invalid value
227  *
228  * \retval 1 on success
229  * \retval 0 on failure
230  */
231 static int MQTTReasonCodeTestParse02 (void)
232 {
233  uint8_t *de = NULL;
234  de = DetectMQTTReasonCodeParse("6X");
235  if (de) {
237  FAIL;
238  }
239 
240  PASS;
241 }
242 
243 /**
244  * \test MQTTReasonCodeTestParse03 is a test for an invalid value
245  *
246  * \retval 1 on success
247  * \retval 0 on failure
248  */
249 static int MQTTReasonCodeTestParse03 (void)
250 {
251  uint8_t *de = NULL;
252  de = DetectMQTTReasonCodeParse("");
253  if (de) {
255  FAIL;
256  }
257 
258  PASS;
259 }
260 
261 /**
262  * \test MQTTReasonCodeTestParse04 is a test for an invalid value
263  *
264  * \retval 1 on success
265  * \retval 0 on failure
266  */
267 static int MQTTReasonCodeTestParse04 (void)
268 {
269  uint8_t *de = NULL;
270  de = DetectMQTTReasonCodeParse("256");
271  if (de) {
273  FAIL;
274  }
275 
276  PASS;
277 }
278 
279 
280 
281 #endif /* UNITTESTS */
282 
283 /**
284  * \brief this function registers unit tests for MQTTReasonCode
285  */
287 {
288 #ifdef UNITTESTS
289  UtRegisterTest("MQTTReasonCodeTestParse01", MQTTReasonCodeTestParse01);
290  UtRegisterTest("MQTTReasonCodeTestParse02", MQTTReasonCodeTestParse02);
291  UtRegisterTest("MQTTReasonCodeTestParse03", MQTTReasonCodeTestParse03);
292  UtRegisterTest("MQTTReasonCodeTestParse04", MQTTReasonCodeTestParse04);
293 #endif /* UNITTESTS */
294 }
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:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
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:1298
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1286
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1296
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:351
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1267
rust.h
DETECT_AL_MQTT_REASON_CODE
@ DETECT_AL_MQTT_REASON_CODE
Definition: detect-engine-register.h:304
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
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:1072
StringParseUint8
int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:361
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
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:1095
de
uint8_t de
Definition: app-layer-htp.c:581
DetectMQTTReasonCodeFree
void DetectMQTTReasonCodeFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-reason-code.c:181
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2791
detect.h
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-mqtt-reason-code.c:36
conf.h
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:345
MQTTReasonCodeRegisterTests
void MQTTReasonCodeRegisterTests(void)
this function registers unit tests for MQTTReasonCode
Definition: detect-mqtt-reason-code.c:286
flags
uint8_t flags
Definition: decode-gre.h:0
SigTableElmt_::alias
const char * alias
Definition: detect.h:1297
suricata-common.h
detect-mqtt-reason-code.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
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:2079
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:596
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1288