suricata
detect-mqtt-connack-sessionpresent.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 "^true|false|yes|no$"
36 static DetectParseRegex parse_regex;
37 
38 static int mqtt_connack_session_present_id = 0;
39 
40 static int DetectMQTTConnackSessionPresentMatch(DetectEngineThreadCtx *det_ctx,
41  Flow *f, uint8_t flags, void *state,
42  void *txv, const Signature *s,
43  const SigMatchCtx *ctx);
44 static int DetectMQTTConnackSessionPresentSetup (DetectEngineCtx *, Signature *, const char *);
47 
48 static int DetectEngineInspectMQTTConnackSessionPresentGeneric(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 
54 /**
55  * \brief Registration function for mqtt.connack.session_present: keyword
56  */
58 {
59  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].name = "mqtt.connack.session_present";
60  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].desc = "match MQTT CONNACK session present flag";
61  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].url = "/rules/mqtt-keywords.html#mqtt-connack-session-present";
62  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].AppLayerTxMatch = DetectMQTTConnackSessionPresentMatch;
63  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].Setup = DetectMQTTConnackSessionPresentSetup;
65 #ifdef UNITTESTS
67 #endif
68 
69  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
70 
71  DetectAppLayerInspectEngineRegister("mqtt.connack.session_present",
73  DetectEngineInspectMQTTConnackSessionPresentGeneric);
74 
75  mqtt_connack_session_present_id = DetectBufferTypeGetByName("mqtt.connack.session_present");
76 }
77 
78 static int DetectEngineInspectMQTTConnackSessionPresentGeneric(ThreadVars *tv,
80  const Signature *s, const SigMatchData *smd,
81  Flow *f, uint8_t flags, void *alstate,
82  void *txv, uint64_t tx_id)
83 {
84  return DetectEngineInspectGenericList(tv, de_ctx, det_ctx, s, smd,
85  f, flags, alstate, txv, tx_id);
86 }
87 
88 /**
89  * \internal
90  * \brief Function to match session_present flag of an MQTT CONNACK message
91  *
92  * \param det_ctx Pointer to the pattern matcher thread.
93  * \param f Pointer to the current flow.
94  * \param flags Flags.
95  * \param state App layer state.
96  * \param txv Pointer to the transaction.
97  * \param s Pointer to the Signature.
98  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTConnackSessionPresentData.
99  *
100  * \retval 0 no match.
101  * \retval 1 match.
102  */
103 static int DetectMQTTConnackSessionPresentMatch(DetectEngineThreadCtx *det_ctx,
104  Flow *f, uint8_t flags, void *state,
105  void *txv, const Signature *s,
106  const SigMatchCtx *ctx)
107 {
108  const bool *de = (const bool *)ctx;
109  bool value = false;
110 
111  if (!de)
112  return 0;
113 
114  if (rs_mqtt_tx_get_connack_sessionpresent(txv, &value) ==0 ) {
115  return 0;
116  }
117  if (value != *de) {
118  return 0;
119  }
120 
121  return 1;
122 }
123 
124 /**
125  * \internal
126  * \brief This function is used to parse options passed via mqtt.connack.session_present: keyword
127  *
128  * \param rawstr Pointer to the user provided options
129  *
130  * \retval de pointer to DetectMQTTConnackSessionPresentData on success
131  * \retval NULL on failure
132  */
133 static bool *DetectMQTTConnackSessionPresentParse(const char *rawstr)
134 {
135  bool *de = NULL;
136  de = SCMalloc(sizeof(bool));
137  if (unlikely(de == NULL))
138  return NULL;
139  *de = false;
140 
141  if (strcmp(rawstr, "yes") == 0) {
142  *de = true;
143  } else if (strcmp(rawstr, "true") == 0) {
144  *de = true;
145  } else if (strcmp(rawstr, "no") == 0) {
146  *de = false;
147  } else if (strcmp(rawstr, "false") == 0) {
148  *de = false;
149  } else {
150  SCLogError(SC_ERR_UNKNOWN_VALUE, "invalid session_present flag definition: %s", rawstr);
151  goto error;
152  }
153 
154  return de;
155 
156 error:
157  /* de can't be NULL here */
158  SCFree(de);
159  return NULL;
160 }
161 
162 /**
163  * \internal
164  * \brief this function is used to add the parsed type query into the current signature
165  *
166  * \param de_ctx pointer to the Detection Engine Context
167  * \param s pointer to the Current Signature
168  * \param rawstr pointer to the user provided options
169  *
170  * \retval 0 on Success
171  * \retval -1 on Failure
172  */
173 static int DetectMQTTConnackSessionPresentSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
174 {
175  bool *de = NULL;
176  SigMatch *sm = NULL;
177 
179  return -1;
180 
181  de = DetectMQTTConnackSessionPresentParse(rawstr);
182  if (de == NULL)
183  goto error;
184 
185  sm = SigMatchAlloc();
186  if (sm == NULL)
187  goto error;
188 
190  sm->ctx = (SigMatchCtx *)de;
191 
192  SigMatchAppendSMToList(s, sm, mqtt_connack_session_present_id);
193 
194  return 0;
195 
196 error:
197  if (de != NULL)
198  SCFree(de);
199  if (sm != NULL)
200  SCFree(sm);
201  return -1;
202 }
203 
204 /**
205  * \internal
206  * \brief this function will free memory associated with DetectMQTTConnackSessionPresentData
207  *
208  * \param de pointer to DetectMQTTConnackSessionPresentData
209  */
211 {
212  if (de_ptr != NULL)
213  SCFree(de_ptr);
214 }
215 
216 /*
217  * ONLY TESTS BELOW THIS COMMENT
218  */
219 
220 #ifdef UNITTESTS
221 /**
222  * \test MQTTConnackSessionPresentTestParse01 is a test for a valid value
223  *
224  * \retval 1 on success
225  * \retval 0 on failure
226  */
227 static int MQTTConnackSessionPresentTestParse01 (void)
228 {
229  bool *de = NULL;
230 
231  de = DetectMQTTConnackSessionPresentParse("yes");
232  FAIL_IF_NULL(de);
234 
235  de = DetectMQTTConnackSessionPresentParse("true");
236  FAIL_IF_NULL(de);
238 
239  de = DetectMQTTConnackSessionPresentParse("false");
240  FAIL_IF_NULL(de);
242 
243  de = DetectMQTTConnackSessionPresentParse("no");
244  FAIL_IF_NULL(de);
246 
247  PASS;
248 }
249 
250 /**
251  * \test MQTTConnackSessionPresentTestParse02 is a test for an invalid value
252  *
253  * \retval 1 on success
254  * \retval 0 on failure
255  */
256 static int MQTTConnackSessionPresentTestParse02 (void)
257 {
258  bool *de = NULL;
259  de = DetectMQTTConnackSessionPresentParse("nix");
260  if (de) {
262  FAIL;
263  }
264 
265  PASS;
266 }
267 
268 /**
269  * \test MQTTConnackSessionPresentTestParse03 is a test for an invalid value
270  *
271  * \retval 1 on success
272  * \retval 0 on failure
273  */
274 static int MQTTConnackSessionPresentTestParse03 (void)
275 {
276  bool *de = NULL;
277  de = DetectMQTTConnackSessionPresentParse("");
278  if (de) {
280  FAIL;
281  }
282 
283  PASS;
284 }
285 
286 /**
287  * \test MQTTConnackSessionPresentTestParse04 is a test for an invalid value
288  *
289  * \retval 1 on success
290  * \retval 0 on failure
291  */
292 static int MQTTConnackSessionPresentTestParse04 (void)
293 {
294  bool *de = NULL;
295  de = DetectMQTTConnackSessionPresentParse(",");
296  if (de) {
298  FAIL;
299  }
300 
301  PASS;
302 }
303 
304 
305 #endif /* UNITTESTS */
306 
307 /**
308  * \brief this function registers unit tests for MQTTConnackSessionPresent
309  */
311 {
312 #ifdef UNITTESTS
313  UtRegisterTest("MQTTConnackSessionPresentTestParse01", MQTTConnackSessionPresentTestParse01);
314  UtRegisterTest("MQTTConnackSessionPresentTestParse02", MQTTConnackSessionPresentTestParse02);
315  UtRegisterTest("MQTTConnackSessionPresentTestParse03", MQTTConnackSessionPresentTestParse03);
316  UtRegisterTest("MQTTConnackSessionPresentTestParse04", MQTTConnackSessionPresentTestParse04);
317 #endif /* UNITTESTS */
318 }
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
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
SigTableElmt_::name
const char * name
Definition: detect.h:1211
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
MQTTConnackSessionPresentRegisterTests
void MQTTConnackSessionPresentRegisterTests(void)
this function registers unit tests for MQTTConnackSessionPresent
Definition: detect-mqtt-connack-sessionpresent.c:310
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
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
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
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-mqtt-connack-sessionpresent.c:35
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
detect-mqtt-connack-sessionpresent.h
conf.h
DETECT_AL_MQTT_CONNACK_SESSION_PRESENT
@ DETECT_AL_MQTT_CONNACK_SESSION_PRESENT
Definition: detect-engine-register.h:281
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:321
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
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
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
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
DetectMQTTConnackSessionPresentRegister
void DetectMQTTConnackSessionPresentRegister(void)
Registration function for mqtt.connack.session_present: keyword.
Definition: detect-mqtt-connack-sessionpresent.c:57
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:55
DetectMQTTConnackSessionPresentFree
void DetectMQTTConnackSessionPresentFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-connack-sessionpresent.c:210
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
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