suricata
detect-mqtt-connect-flags.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-unittest.h"
32 
33 #include "rust.h"
34 
35 #define PARSE_REGEX "(?: *,?!?(?:username|password|will|will_retain|clean_session))+"
36 static DetectParseRegex parse_regex;
37 
38 static int mqtt_connect_flags_id = 0;
39 
40 static int DetectMQTTConnectFlagsMatch(DetectEngineThreadCtx *det_ctx,
41  Flow *f, uint8_t flags, void *state,
42  void *txv, const Signature *s,
43  const SigMatchCtx *ctx);
44 static int DetectMQTTConnectFlagsSetup (DetectEngineCtx *, Signature *, const char *);
47 
49  MQTTFlagState username,
55 
56 /**
57  * \brief Registration function for mqtt.connect.flags: keyword
58  */
60 {
61  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].name = "mqtt.connect.flags";
62  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].desc = "match MQTT CONNECT variable header flags";
63  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].url = "/rules/mqtt-keywords.html#mqtt-connect-flags";
64  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].AppLayerTxMatch = DetectMQTTConnectFlagsMatch;
65  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].Setup = DetectMQTTConnectFlagsSetup;
67 #ifdef UNITTESTS
69 #endif
70 
71  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
72 
75 
76  mqtt_connect_flags_id = DetectBufferTypeGetByName("mqtt.connect.flags");
77 }
78 
79 /**
80  * \internal
81  * \brief Function to match variable header flags of an MQTT CONNECT Tx
82  *
83  * \param det_ctx Pointer to the pattern matcher thread.
84  * \param f Pointer to the current flow.
85  * \param flags Flags.
86  * \param state App layer state.
87  * \param txv Pointer to the transaction.
88  * \param s Pointer to the Signature.
89  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTConnectFlagsData.
90  *
91  * \retval 0 no match.
92  * \retval 1 match.
93  */
94 static int DetectMQTTConnectFlagsMatch(DetectEngineThreadCtx *det_ctx,
95  Flow *f, uint8_t flags, void *state,
96  void *txv, const Signature *s,
97  const SigMatchCtx *ctx)
98 {
100 
101  if (!de)
102  return 0;
103 
104  return rs_mqtt_tx_has_connect_flags(txv, de->username, de->password, de->will,
105  de->will_retain, de->clean_session);
106  }
107 
108 /**
109  * \internal
110  * \brief This function is used to parse options passed via mqtt.connect.flags: keyword
111  *
112  * \param rawstr Pointer to the user provided options
113  *
114  * \retval de pointer to DetectMQTTConnectFlagsData on success
115  * \retval NULL on failure
116  */
117 static DetectMQTTConnectFlagsData *DetectMQTTConnectFlagsParse(const char *rawstr)
118 {
119  char copy[strlen(rawstr) + 1];
120 
122  if (unlikely(de == NULL))
123  return NULL;
124 
125  pcre2_match_data *match = NULL;
126  int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
127  if (ret < 1) {
128  SCLogError("invalid flag definition: %s", rawstr);
129  goto error;
130  }
131 
132  de->username = de->password = de->will = MQTT_DONT_CARE;
133  de->will_retain = de->clean_session = MQTT_DONT_CARE;
134 
135  strlcpy(copy, rawstr, sizeof(copy));
136  char *xsaveptr = NULL;
137  char *flagv = strtok_r(copy, ",", &xsaveptr);
138  while (flagv != NULL) {
139  while (*flagv != '\0' && isblank(*flagv)) {
140  flagv++;
141  }
142  if (strlen(flagv) < 2) {
143  SCLogError("malformed flag value: %s", flagv);
144  goto error;
145  } else {
146  int offset = 0;
147  MQTTFlagState fs_to_set = MQTT_MUST_BE_SET;
148  if (flagv[0] == '!') {
149  /* negated flag */
150  offset = 1; /* skip negation operator during comparison */
151  fs_to_set = MQTT_CANT_BE_SET;
152  }
153  if (strcmp(flagv+offset, "username") == 0) {
154  if (de->username != MQTT_DONT_CARE) {
155  SCLogError("duplicate flag definition: %s", flagv);
156  goto error;
157  }
158  de->username = fs_to_set;
159  } else if (strcmp(flagv+offset, "password") == 0) {
160  if (de->password != MQTT_DONT_CARE) {
161  SCLogError("duplicate flag definition: %s", flagv);
162  goto error;
163  }
164  de->password = fs_to_set;
165  } else if (strcmp(flagv+offset, "will") == 0) {
166  if (de->will != MQTT_DONT_CARE) {
167  SCLogError("duplicate flag definition: %s", flagv);
168  goto error;
169  }
170  de->will = fs_to_set;
171  } else if (strcmp(flagv+offset, "will_retain") == 0) {
172  if (de->will_retain != MQTT_DONT_CARE) {
173  SCLogError("duplicate flag definition: %s", flagv);
174  goto error;
175  }
176  de->will_retain = fs_to_set;
177  } else if (strcmp(flagv+offset, "clean_session") == 0) {
178  if (de->clean_session != MQTT_DONT_CARE) {
179  SCLogError("duplicate flag definition: %s", flagv);
180  goto error;
181  }
182  de->clean_session = fs_to_set;
183  } else {
184  SCLogError("invalid flag definition: %s", flagv);
185  goto error;
186  }
187  }
188  flagv = strtok_r(NULL, ",", &xsaveptr);
189  }
190 
191  pcre2_match_data_free(match);
192  return de;
193 
194 error:
195  if (match) {
196  pcre2_match_data_free(match);
197  }
198  if (de)
199  SCFree(de);
200  return NULL;
201 }
202 
203 /**
204  * \internal
205  * \brief this function is used to add the parsed type query into the current signature
206  *
207  * \param de_ctx pointer to the Detection Engine Context
208  * \param s pointer to the Current Signature
209  * \param rawstr pointer to the user provided options
210  *
211  * \retval 0 on Success
212  * \retval -1 on Failure
213  */
214 static int DetectMQTTConnectFlagsSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
215 {
217  SigMatch *sm = NULL;
218 
220  return -1;
221 
222  de = DetectMQTTConnectFlagsParse(rawstr);
223  if (de == NULL)
224  goto error;
225 
226  sm = SigMatchAlloc();
227  if (sm == NULL)
228  goto error;
229 
231  sm->ctx = (SigMatchCtx *)de;
232 
233  SigMatchAppendSMToList(s, sm, mqtt_connect_flags_id);
234 
235  return 0;
236 
237 error:
238  if (de != NULL)
239  SCFree(de);
240  if (sm != NULL)
241  SCFree(sm);
242  return -1;
243 }
244 
245 /**
246  * \internal
247  * \brief this function will free memory associated with DetectMQTTConnectFlagsData
248  *
249  * \param de pointer to DetectMQTTConnectFlagsData
250  */
252 {
253  if (de_ptr != NULL)
254  SCFree(de_ptr);
255 }
256 
257 /*
258  * ONLY TESTS BELOW THIS COMMENT
259  */
260 
261 #ifdef UNITTESTS
262 /**
263  * \test MQTTConnectFlagsTestParse01 is a test for a valid value
264  *
265  * \retval 1 on success
266  * \retval 0 on failure
267  */
268 static int MQTTConnectFlagsTestParse01 (void)
269 {
271  de = DetectMQTTConnectFlagsParse("username");
272  FAIL_IF_NULL(de);
274 
275  de = DetectMQTTConnectFlagsParse("username,password,will,will_retain,clean_session");
276  FAIL_IF_NULL(de);
278 
279  de = DetectMQTTConnectFlagsParse("!username,!password,!will,!will_retain,!clean_session");
280  FAIL_IF_NULL(de);
282 
283  de = DetectMQTTConnectFlagsParse(" username,password");
284  FAIL_IF_NULL(de);
286 
287  PASS;
288 }
289 
290 /**
291  * \test MQTTConnectFlagsTestParse02 is a test for an invalid value
292  *
293  * \retval 1 on success
294  * \retval 0 on failure
295  */
296 static int MQTTConnectFlagsTestParse02 (void)
297 {
299  de = DetectMQTTConnectFlagsParse("foobar");
300  if (de) {
302  FAIL;
303  }
304 
305  PASS;
306 }
307 
308 /**
309  * \test MQTTConnectFlagsTestParse03 is a test for an invalid value
310  *
311  * \retval 1 on success
312  * \retval 0 on failure
313  */
314 static int MQTTConnectFlagsTestParse03 (void)
315 {
317  de = DetectMQTTConnectFlagsParse("will,!");
318  if (de) {
320  FAIL;
321  }
322 
323  PASS;
324 }
325 
326 /**
327  * \test MQTTConnectFlagsTestParse04 is a test for an invalid value
328  *
329  * \retval 1 on success
330  * \retval 0 on failure
331  */
332 static int MQTTConnectFlagsTestParse04 (void)
333 {
335  de = DetectMQTTConnectFlagsParse("");
336  if (de) {
338  FAIL;
339  }
340 
341  PASS;
342 }
343 
344 /**
345  * \test MQTTConnectFlagsTestParse05 is a test for an invalid value
346  *
347  * \retval 1 on success
348  * \retval 0 on failure
349  */
350 static int MQTTConnectFlagsTestParse05 (void)
351 {
353  de = DetectMQTTConnectFlagsParse("username, username");
354  if (de) {
356  FAIL;
357  }
358  de = DetectMQTTConnectFlagsParse("!username, username");
359  if (de) {
361  FAIL;
362  }
363  de = DetectMQTTConnectFlagsParse("!username,password,!password");
364  if (de) {
366  FAIL;
367  }
368  de = DetectMQTTConnectFlagsParse("will, username,password, !will, will");
369  if (de) {
371  FAIL;
372  }
373 
374  PASS;
375 }
376 
377 
378 #endif /* UNITTESTS */
379 
380 /**
381  * \brief this function registers unit tests for MQTTConnectFlags
382  */
384 {
385 #ifdef UNITTESTS
386  UtRegisterTest("MQTTConnectFlagsTestParse01", MQTTConnectFlagsTestParse01);
387  UtRegisterTest("MQTTConnectFlagsTestParse02", MQTTConnectFlagsTestParse02);
388  UtRegisterTest("MQTTConnectFlagsTestParse03", MQTTConnectFlagsTestParse03);
389  UtRegisterTest("MQTTConnectFlagsTestParse04", MQTTConnectFlagsTestParse04);
390  UtRegisterTest("MQTTConnectFlagsTestParse05", MQTTConnectFlagsTestParse05);
391 #endif /* UNITTESTS */
392 }
SigTableElmt_::url
const char * url
Definition: detect.h:1287
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1703
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:437
SigTableElmt_::desc
const char * desc
Definition: detect.h:1286
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1274
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1284
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:826
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1255
DetectMQTTConnectFlagsFree
void DetectMQTTConnectFlagsFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-connect-flags.c:251
rust.h
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2623
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1269
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1124
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:255
DetectMQTTConnectFlagsData_::will
MQTTFlagState will
Definition: detect-mqtt-connect-flags.c:51
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectMQTTConnectFlagsData_
Definition: detect-mqtt-connect-flags.c:48
DetectEngineThreadCtx_
Definition: detect.h:1074
de
uint8_t de
Definition: app-layer-htp.c:577
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2747
detect.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:343
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:216
conf.h
DetectMQTTConnectFlagsData
struct DetectMQTTConnectFlagsData_ DetectMQTTConnectFlagsData
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:322
DetectMQTTConnectFlagsData_::password
MQTTFlagState password
Definition: detect-mqtt-connect-flags.c:50
DETECT_AL_MQTT_CONNECT_FLAGS
@ DETECT_AL_MQTT_CONNECT_FLAGS
Definition: detect-engine-register.h:294
DetectMQTTConnectFlagsData_::username
MQTTFlagState username
Definition: detect-mqtt-connect-flags.c:49
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:335
flags
uint8_t flags
Definition: decode-gre.h:0
DetectMQTTConnectFlagsData_::will_retain
MQTTFlagState will_retain
Definition: detect-mqtt-connect-flags.c:52
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:341
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
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:2122
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-mqtt-connect-flags.c:35
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:581
SigMatch_
a single match condition for a signature
Definition: detect.h:340
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
DetectMQTTConnectFlagsRegister
void DetectMQTTConnectFlagsRegister(void)
Registration function for mqtt.connect.flags: keyword.
Definition: detect-mqtt-connect-flags.c:59
MQTTConnectFlagsRegisterTests
void MQTTConnectFlagsRegisterTests(void)
this function registers unit tests for MQTTConnectFlags
Definition: detect-mqtt-connect-flags.c:383
detect-mqtt-connect-flags.h
DetectMQTTConnectFlagsData_::clean_session
MQTTFlagState clean_session
Definition: detect-mqtt-connect-flags.c:53
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1276