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 
219  return -1;
220 
221  de = DetectMQTTConnectFlagsParse(rawstr);
222  if (de == NULL)
223  goto error;
224 
226  mqtt_connect_flags_id) == NULL) {
227  goto error;
228  }
229 
230  return 0;
231 
232 error:
233  if (de != NULL)
234  SCFree(de);
235  return -1;
236 }
237 
238 /**
239  * \internal
240  * \brief this function will free memory associated with DetectMQTTConnectFlagsData
241  *
242  * \param de pointer to DetectMQTTConnectFlagsData
243  */
245 {
246  if (de_ptr != NULL)
247  SCFree(de_ptr);
248 }
249 
250 /*
251  * ONLY TESTS BELOW THIS COMMENT
252  */
253 
254 #ifdef UNITTESTS
255 /**
256  * \test MQTTConnectFlagsTestParse01 is a test for a valid value
257  *
258  * \retval 1 on success
259  * \retval 0 on failure
260  */
261 static int MQTTConnectFlagsTestParse01 (void)
262 {
264  de = DetectMQTTConnectFlagsParse("username");
265  FAIL_IF_NULL(de);
267 
268  de = DetectMQTTConnectFlagsParse("username,password,will,will_retain,clean_session");
269  FAIL_IF_NULL(de);
271 
272  de = DetectMQTTConnectFlagsParse("!username,!password,!will,!will_retain,!clean_session");
273  FAIL_IF_NULL(de);
275 
276  de = DetectMQTTConnectFlagsParse(" username,password");
277  FAIL_IF_NULL(de);
279 
280  PASS;
281 }
282 
283 /**
284  * \test MQTTConnectFlagsTestParse02 is a test for an invalid value
285  *
286  * \retval 1 on success
287  * \retval 0 on failure
288  */
289 static int MQTTConnectFlagsTestParse02 (void)
290 {
292  de = DetectMQTTConnectFlagsParse("foobar");
293  if (de) {
295  FAIL;
296  }
297 
298  PASS;
299 }
300 
301 /**
302  * \test MQTTConnectFlagsTestParse03 is a test for an invalid value
303  *
304  * \retval 1 on success
305  * \retval 0 on failure
306  */
307 static int MQTTConnectFlagsTestParse03 (void)
308 {
310  de = DetectMQTTConnectFlagsParse("will,!");
311  if (de) {
313  FAIL;
314  }
315 
316  PASS;
317 }
318 
319 /**
320  * \test MQTTConnectFlagsTestParse04 is a test for an invalid value
321  *
322  * \retval 1 on success
323  * \retval 0 on failure
324  */
325 static int MQTTConnectFlagsTestParse04 (void)
326 {
328  de = DetectMQTTConnectFlagsParse("");
329  if (de) {
331  FAIL;
332  }
333 
334  PASS;
335 }
336 
337 /**
338  * \test MQTTConnectFlagsTestParse05 is a test for an invalid value
339  *
340  * \retval 1 on success
341  * \retval 0 on failure
342  */
343 static int MQTTConnectFlagsTestParse05 (void)
344 {
346  de = DetectMQTTConnectFlagsParse("username, username");
347  if (de) {
349  FAIL;
350  }
351  de = DetectMQTTConnectFlagsParse("!username, username");
352  if (de) {
354  FAIL;
355  }
356  de = DetectMQTTConnectFlagsParse("!username,password,!password");
357  if (de) {
359  FAIL;
360  }
361  de = DetectMQTTConnectFlagsParse("will, username,password, !will, will");
362  if (de) {
364  FAIL;
365  }
366 
367  PASS;
368 }
369 
370 
371 #endif /* UNITTESTS */
372 
373 /**
374  * \brief this function registers unit tests for MQTTConnectFlags
375  */
377 {
378 #ifdef UNITTESTS
379  UtRegisterTest("MQTTConnectFlagsTestParse01", MQTTConnectFlagsTestParse01);
380  UtRegisterTest("MQTTConnectFlagsTestParse02", MQTTConnectFlagsTestParse02);
381  UtRegisterTest("MQTTConnectFlagsTestParse03", MQTTConnectFlagsTestParse03);
382  UtRegisterTest("MQTTConnectFlagsTestParse04", MQTTConnectFlagsTestParse04);
383  UtRegisterTest("MQTTConnectFlagsTestParse05", MQTTConnectFlagsTestParse05);
384 #endif /* UNITTESTS */
385 }
SigTableElmt_::url
const char * url
Definition: detect.h:1296
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:1295
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1283
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1293
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:836
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1264
DetectMQTTConnectFlagsFree
void DetectMQTTConnectFlagsFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-connect-flags.c:244
rust.h
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2674
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
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:263
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:1092
de
uint8_t de
Definition: app-layer-htp.c:581
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2791
detect.h
conf.h
DetectMQTTConnectFlagsData
struct DetectMQTTConnectFlagsData_ DetectMQTTConnectFlagsData
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:305
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:342
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_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
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:593
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
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
MQTTConnectFlagsRegisterTests
void MQTTConnectFlagsRegisterTests(void)
this function registers unit tests for MQTTConnectFlags
Definition: detect-mqtt-connect-flags.c:376
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:1285