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