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-bindings.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 
48 static int DetectEngineInspectMQTTConnectFlagsGeneric(ThreadVars *tv,
50  const Signature *s, const SigMatchData *smd,
51  Flow *f, uint8_t flags, void *alstate,
52  void *txv, uint64_t tx_id);
53 
55  MQTTFlagState username,
61 
62 /**
63  * \brief Registration function for mqtt.connect.flags: keyword
64  */
66 {
67  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].name = "mqtt.connect.flags";
68  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].desc = "match MQTT CONNECT variable header flags";
69  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].url = "/rules/mqtt-keywords.html#mqtt-connect-flags";
70  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].AppLayerTxMatch = DetectMQTTConnectFlagsMatch;
71  sigmatch_table[DETECT_AL_MQTT_CONNECT_FLAGS].Setup = DetectMQTTConnectFlagsSetup;
73 #ifdef UNITTESTS
75 #endif
76 
77  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
78 
79  DetectAppLayerInspectEngineRegister("mqtt.connect.flags",
81  DetectEngineInspectMQTTConnectFlagsGeneric);
82 
83  mqtt_connect_flags_id = DetectBufferTypeGetByName("mqtt.connect.flags");
84 }
85 
86 static int DetectEngineInspectMQTTConnectFlagsGeneric(ThreadVars *tv,
88  const Signature *s, const SigMatchData *smd,
89  Flow *f, uint8_t flags, void *alstate,
90  void *txv, uint64_t tx_id)
91 {
92  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
93  f, flags, alstate, txv, tx_id);
94 }
95 
96 /**
97  * \internal
98  * \brief Function to match variable header flags of an MQTT CONNECT Tx
99  *
100  * \param det_ctx Pointer to the pattern matcher thread.
101  * \param f Pointer to the current flow.
102  * \param flags Flags.
103  * \param state App layer state.
104  * \param txv Pointer to the transaction.
105  * \param s Pointer to the Signature.
106  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTConnectFlagsData.
107  *
108  * \retval 0 no match.
109  * \retval 1 match.
110  */
111 static int DetectMQTTConnectFlagsMatch(DetectEngineThreadCtx *det_ctx,
112  Flow *f, uint8_t flags, void *state,
113  void *txv, const Signature *s,
114  const SigMatchCtx *ctx)
115 {
117 
118  if (!de)
119  return 0;
120 
121  return rs_mqtt_tx_has_connect_flags(txv, de->username, de->password, de->will,
122  de->will_retain, de->clean_session);
123  }
124 
125 /**
126  * \internal
127  * \brief This function is used to parse options passed via mqtt.connect.flags: keyword
128  *
129  * \param rawstr Pointer to the user provided options
130  *
131  * \retval de pointer to DetectMQTTConnectFlagsData on success
132  * \retval NULL on failure
133  */
134 static DetectMQTTConnectFlagsData *DetectMQTTConnectFlagsParse(const char *rawstr)
135 {
137  int ret = 0;
138  int ov[MAX_SUBSTRINGS];
139 
140  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
141  if (ret < 1) {
142  SCLogError(SC_ERR_PCRE_MATCH, "invalid flag definition: %s", rawstr);
143  return NULL;
144  }
145 
146  de = SCCalloc(1, sizeof(DetectMQTTConnectFlagsData));
147  if (unlikely(de == NULL))
148  return NULL;
149  de->username = de->password = de->will = MQTT_DONT_CARE;
150  de->will_retain = de->clean_session = MQTT_DONT_CARE;
151 
152  char copy[strlen(rawstr)+1];
153  strlcpy(copy, rawstr, sizeof(copy));
154  char *xsaveptr = NULL;
155  char *flagv = strtok_r(copy, ",", &xsaveptr);
156  while (flagv != NULL) {
157  while (*flagv != '\0' && isblank(*flagv)) {
158  flagv++;
159  }
160  if (strlen(flagv) < 2) {
161  SCLogError(SC_ERR_UNKNOWN_VALUE, "malformed flag value: %s", flagv);
162  goto error;
163  } else {
164  int offset = 0;
165  MQTTFlagState fs_to_set = MQTT_MUST_BE_SET;
166  if (flagv[0] == '!') {
167  /* negated flag */
168  offset = 1; /* skip negation operator during comparison */
169  fs_to_set = MQTT_CANT_BE_SET;
170  }
171  if (strcmp(flagv+offset, "username") == 0) {
172  if (de->username != MQTT_DONT_CARE) {
173  SCLogError(SC_ERR_INVALID_VALUE, "duplicate flag definition: %s", flagv);
174  goto error;
175  }
176  de->username = fs_to_set;
177  } else if (strcmp(flagv+offset, "password") == 0) {
178  if (de->password != MQTT_DONT_CARE) {
179  SCLogError(SC_ERR_INVALID_VALUE, "duplicate flag definition: %s", flagv);
180  goto error;
181  }
182  de->password = fs_to_set;
183  } else if (strcmp(flagv+offset, "will") == 0) {
184  if (de->will != MQTT_DONT_CARE) {
185  SCLogError(SC_ERR_INVALID_VALUE, "duplicate flag definition: %s", flagv);
186  goto error;
187  }
188  de->will = fs_to_set;
189  } else if (strcmp(flagv+offset, "will_retain") == 0) {
190  if (de->will_retain != MQTT_DONT_CARE) {
191  SCLogError(SC_ERR_INVALID_VALUE, "duplicate flag definition: %s", flagv);
192  goto error;
193  }
194  de->will_retain = fs_to_set;
195  } else if (strcmp(flagv+offset, "clean_session") == 0) {
196  if (de->clean_session != MQTT_DONT_CARE) {
197  SCLogError(SC_ERR_INVALID_VALUE, "duplicate flag definition: %s", flagv);
198  goto error;
199  }
200  de->clean_session = fs_to_set;
201  } else {
202  SCLogError(SC_ERR_UNKNOWN_VALUE, "invalid flag definition: %s", flagv);
203  goto error;
204  }
205  }
206  flagv = strtok_r(NULL, ",", &xsaveptr);
207  }
208 
209  return de;
210 
211 error:
212  /* de can't be NULL here */
213  SCFree(de);
214  return NULL;
215 }
216 
217 /**
218  * \internal
219  * \brief this function is used to add the parsed type query into the current signature
220  *
221  * \param de_ctx pointer to the Detection Engine Context
222  * \param s pointer to the Current Signature
223  * \param rawstr pointer to the user provided options
224  *
225  * \retval 0 on Success
226  * \retval -1 on Failure
227  */
228 static int DetectMQTTConnectFlagsSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
229 {
231  SigMatch *sm = NULL;
232 
234  return -1;
235 
236  de = DetectMQTTConnectFlagsParse(rawstr);
237  if (de == NULL)
238  goto error;
239 
240  sm = SigMatchAlloc();
241  if (sm == NULL)
242  goto error;
243 
245  sm->ctx = (SigMatchCtx *)de;
246 
247  SigMatchAppendSMToList(s, sm, mqtt_connect_flags_id);
248 
249  return 0;
250 
251 error:
252  if (de != NULL)
253  SCFree(de);
254  if (sm != NULL)
255  SCFree(sm);
256  return -1;
257 }
258 
259 /**
260  * \internal
261  * \brief this function will free memory associated with DetectMQTTConnectFlagsData
262  *
263  * \param de pointer to DetectMQTTConnectFlagsData
264  */
266 {
267  if (de_ptr != NULL)
268  SCFree(de_ptr);
269 }
270 
271 /*
272  * ONLY TESTS BELOW THIS COMMENT
273  */
274 
275 #ifdef UNITTESTS
276 /**
277  * \test MQTTConnectFlagsTestParse01 is a test for a valid value
278  *
279  * \retval 1 on success
280  * \retval 0 on failure
281  */
282 static int MQTTConnectFlagsTestParse01 (void)
283 {
285  de = DetectMQTTConnectFlagsParse("username");
286  FAIL_IF_NULL(de);
288 
289  de = DetectMQTTConnectFlagsParse("username,password,will,will_retain,clean_session");
290  FAIL_IF_NULL(de);
292 
293  de = DetectMQTTConnectFlagsParse("!username,!password,!will,!will_retain,!clean_session");
294  FAIL_IF_NULL(de);
296 
297  de = DetectMQTTConnectFlagsParse(" username,password");
298  FAIL_IF_NULL(de);
300 
301  PASS;
302 }
303 
304 /**
305  * \test MQTTConnectFlagsTestParse02 is a test for an invalid value
306  *
307  * \retval 1 on success
308  * \retval 0 on failure
309  */
310 static int MQTTConnectFlagsTestParse02 (void)
311 {
313  de = DetectMQTTConnectFlagsParse("foobar");
314  if (de) {
316  FAIL;
317  }
318 
319  PASS;
320 }
321 
322 /**
323  * \test MQTTConnectFlagsTestParse03 is a test for an invalid value
324  *
325  * \retval 1 on success
326  * \retval 0 on failure
327  */
328 static int MQTTConnectFlagsTestParse03 (void)
329 {
331  de = DetectMQTTConnectFlagsParse("will,!");
332  if (de) {
334  FAIL;
335  }
336 
337  PASS;
338 }
339 
340 /**
341  * \test MQTTConnectFlagsTestParse04 is a test for an invalid value
342  *
343  * \retval 1 on success
344  * \retval 0 on failure
345  */
346 static int MQTTConnectFlagsTestParse04 (void)
347 {
349  de = DetectMQTTConnectFlagsParse("");
350  if (de) {
352  FAIL;
353  }
354 
355  PASS;
356 }
357 
358 /**
359  * \test MQTTConnectFlagsTestParse05 is a test for an invalid value
360  *
361  * \retval 1 on success
362  * \retval 0 on failure
363  */
364 static int MQTTConnectFlagsTestParse05 (void)
365 {
367  de = DetectMQTTConnectFlagsParse("username, username");
368  if (de) {
370  FAIL;
371  }
372  de = DetectMQTTConnectFlagsParse("!username, username");
373  if (de) {
375  FAIL;
376  }
377  de = DetectMQTTConnectFlagsParse("!username,password,!password");
378  if (de) {
380  FAIL;
381  }
382  de = DetectMQTTConnectFlagsParse("will, username,password, !will, will");
383  if (de) {
385  FAIL;
386  }
387 
388  PASS;
389 }
390 
391 
392 #endif /* UNITTESTS */
393 
394 /**
395  * \brief this function registers unit tests for MQTTConnectFlags
396  */
398 {
399 #ifdef UNITTESTS
400  UtRegisterTest("MQTTConnectFlagsTestParse01", MQTTConnectFlagsTestParse01);
401  UtRegisterTest("MQTTConnectFlagsTestParse02", MQTTConnectFlagsTestParse02);
402  UtRegisterTest("MQTTConnectFlagsTestParse03", MQTTConnectFlagsTestParse03);
403  UtRegisterTest("MQTTConnectFlagsTestParse04", MQTTConnectFlagsTestParse04);
404  UtRegisterTest("MQTTConnectFlagsTestParse05", MQTTConnectFlagsTestParse05);
405 #endif /* UNITTESTS */
406 }
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
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
SigTableElmt_::name
const char * name
Definition: detect.h:1211
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
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
DetectMQTTConnectFlagsFree
void DetectMQTTConnectFlagsFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-connect-flags.c:265
SigMatchData_
Data needed for Match()
Definition: detect.h:329
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
util-unittest.h
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
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:236
DetectMQTTConnectFlagsData_::will
MQTTFlagState will
Definition: detect-mqtt-connect-flags.c:57
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
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:54
DetectEngineThreadCtx_
Definition: detect.h:1010
SC_ERR_UNKNOWN_VALUE
@ SC_ERR_UNKNOWN_VALUE
Definition: util-error.h:159
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
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2423
conf.h
DetectMQTTConnectFlagsData
struct DetectMQTTConnectFlagsData_ DetectMQTTConnectFlagsData
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
DetectMQTTConnectFlagsData_::password
MQTTFlagState password
Definition: detect-mqtt-connect-flags.c:56
DETECT_AL_MQTT_CONNECT_FLAGS
@ DETECT_AL_MQTT_CONNECT_FLAGS
Definition: detect-engine-register.h:275
SigMatch_::type
uint8_t type
Definition: detect.h:321
DetectMQTTConnectFlagsData_::username
MQTTFlagState username
Definition: detect-mqtt-connect-flags.c:55
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
flags
uint8_t flags
Definition: decode-gre.h:0
DetectMQTTConnectFlagsData_::will_retain
MQTTFlagState will_retain
Definition: detect-mqtt-connect-flags.c:58
suricata-common.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
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-mqtt-connect-flags.c:35
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
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
DetectMQTTConnectFlagsRegister
void DetectMQTTConnectFlagsRegister(void)
Registration function for mqtt.connect.flags: keyword.
Definition: detect-mqtt-connect-flags.c:65
MQTTConnectFlagsRegisterTests
void MQTTConnectFlagsRegisterTests(void)
this function registers unit tests for MQTTConnectFlags
Definition: detect-mqtt-connect-flags.c:397
detect-mqtt-connect-flags.h
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
DetectMQTTConnectFlagsData_::clean_session
MQTTFlagState clean_session
Definition: detect-mqtt-connect-flags.c:59
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
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