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 {
120  int ret = 0;
121 
122  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
123  if (ret < 1) {
124  SCLogError(SC_ERR_PCRE_MATCH, "invalid flag definition: %s", rawstr);
125  return NULL;
126  }
127 
128  de = SCCalloc(1, sizeof(DetectMQTTConnectFlagsData));
129  if (unlikely(de == NULL))
130  return NULL;
131  de->username = de->password = de->will = MQTT_DONT_CARE;
132  de->will_retain = de->clean_session = MQTT_DONT_CARE;
133 
134  char copy[strlen(rawstr)+1];
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(SC_ERR_UNKNOWN_VALUE, "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(SC_ERR_INVALID_VALUE, "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(SC_ERR_INVALID_VALUE, "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(SC_ERR_INVALID_VALUE, "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(SC_ERR_INVALID_VALUE, "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(SC_ERR_INVALID_VALUE, "duplicate flag definition: %s", flagv);
180  goto error;
181  }
182  de->clean_session = fs_to_set;
183  } else {
184  SCLogError(SC_ERR_UNKNOWN_VALUE, "invalid flag definition: %s", flagv);
185  goto error;
186  }
187  }
188  flagv = strtok_r(NULL, ",", &xsaveptr);
189  }
190 
191  return de;
192 
193 error:
194  /* de can't be NULL here */
195  SCFree(de);
196  return NULL;
197 }
198 
199 /**
200  * \internal
201  * \brief this function is used to add the parsed type query into the current signature
202  *
203  * \param de_ctx pointer to the Detection Engine Context
204  * \param s pointer to the Current Signature
205  * \param rawstr pointer to the user provided options
206  *
207  * \retval 0 on Success
208  * \retval -1 on Failure
209  */
210 static int DetectMQTTConnectFlagsSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
211 {
213  SigMatch *sm = NULL;
214 
216  return -1;
217 
218  de = DetectMQTTConnectFlagsParse(rawstr);
219  if (de == NULL)
220  goto error;
221 
222  sm = SigMatchAlloc();
223  if (sm == NULL)
224  goto error;
225 
227  sm->ctx = (SigMatchCtx *)de;
228 
229  SigMatchAppendSMToList(s, sm, mqtt_connect_flags_id);
230 
231  return 0;
232 
233 error:
234  if (de != NULL)
235  SCFree(de);
236  if (sm != NULL)
237  SCFree(sm);
238  return -1;
239 }
240 
241 /**
242  * \internal
243  * \brief this function will free memory associated with DetectMQTTConnectFlagsData
244  *
245  * \param de pointer to DetectMQTTConnectFlagsData
246  */
248 {
249  if (de_ptr != NULL)
250  SCFree(de_ptr);
251 }
252 
253 /*
254  * ONLY TESTS BELOW THIS COMMENT
255  */
256 
257 #ifdef UNITTESTS
258 /**
259  * \test MQTTConnectFlagsTestParse01 is a test for a valid value
260  *
261  * \retval 1 on success
262  * \retval 0 on failure
263  */
264 static int MQTTConnectFlagsTestParse01 (void)
265 {
267  de = DetectMQTTConnectFlagsParse("username");
268  FAIL_IF_NULL(de);
270 
271  de = DetectMQTTConnectFlagsParse("username,password,will,will_retain,clean_session");
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");
280  FAIL_IF_NULL(de);
282 
283  PASS;
284 }
285 
286 /**
287  * \test MQTTConnectFlagsTestParse02 is a test for an invalid value
288  *
289  * \retval 1 on success
290  * \retval 0 on failure
291  */
292 static int MQTTConnectFlagsTestParse02 (void)
293 {
295  de = DetectMQTTConnectFlagsParse("foobar");
296  if (de) {
298  FAIL;
299  }
300 
301  PASS;
302 }
303 
304 /**
305  * \test MQTTConnectFlagsTestParse03 is a test for an invalid value
306  *
307  * \retval 1 on success
308  * \retval 0 on failure
309  */
310 static int MQTTConnectFlagsTestParse03 (void)
311 {
313  de = DetectMQTTConnectFlagsParse("will,!");
314  if (de) {
316  FAIL;
317  }
318 
319  PASS;
320 }
321 
322 /**
323  * \test MQTTConnectFlagsTestParse04 is a test for an invalid value
324  *
325  * \retval 1 on success
326  * \retval 0 on failure
327  */
328 static int MQTTConnectFlagsTestParse04 (void)
329 {
331  de = DetectMQTTConnectFlagsParse("");
332  if (de) {
334  FAIL;
335  }
336 
337  PASS;
338 }
339 
340 /**
341  * \test MQTTConnectFlagsTestParse05 is a test for an invalid value
342  *
343  * \retval 1 on success
344  * \retval 0 on failure
345  */
346 static int MQTTConnectFlagsTestParse05 (void)
347 {
349  de = DetectMQTTConnectFlagsParse("username, username");
350  if (de) {
352  FAIL;
353  }
354  de = DetectMQTTConnectFlagsParse("!username, username");
355  if (de) {
357  FAIL;
358  }
359  de = DetectMQTTConnectFlagsParse("!username,password,!password");
360  if (de) {
362  FAIL;
363  }
364  de = DetectMQTTConnectFlagsParse("will, username,password, !will, will");
365  if (de) {
367  FAIL;
368  }
369 
370  PASS;
371 }
372 
373 
374 #endif /* UNITTESTS */
375 
376 /**
377  * \brief this function registers unit tests for MQTTConnectFlags
378  */
380 {
381 #ifdef UNITTESTS
382  UtRegisterTest("MQTTConnectFlagsTestParse01", MQTTConnectFlagsTestParse01);
383  UtRegisterTest("MQTTConnectFlagsTestParse02", MQTTConnectFlagsTestParse02);
384  UtRegisterTest("MQTTConnectFlagsTestParse03", MQTTConnectFlagsTestParse03);
385  UtRegisterTest("MQTTConnectFlagsTestParse04", MQTTConnectFlagsTestParse04);
386  UtRegisterTest("MQTTConnectFlagsTestParse05", MQTTConnectFlagsTestParse05);
387 #endif /* UNITTESTS */
388 }
SigTableElmt_::url
const char * url
Definition: detect.h:1248
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:1247
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2475
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:1235
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1245
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:785
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1216
DetectMQTTConnectFlagsFree
void DetectMQTTConnectFlagsFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-connect-flags.c:247
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
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:230
DetectMQTTConnectFlagsData_::will
MQTTFlagState will
Definition: detect-mqtt-connect-flags.c:51
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:48
DetectEngineThreadCtx_
Definition: detect.h:1034
de
uint8_t de
Definition: app-layer-htp.c:568
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:2598
detect.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:317
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:227
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:50
DETECT_AL_MQTT_CONNECT_FLAGS
@ DETECT_AL_MQTT_CONNECT_FLAGS
Definition: detect-engine-register.h:289
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:309
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:315
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:255
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:1951
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:540
SigMatch_
a single match condition for a signature
Definition: detect.h:314
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:379
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
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:1237