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