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.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 /**
49  * \brief Registration function for mqtt.connack.session_present: keyword
50  */
52 {
53  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].name = "mqtt.connack.session_present";
54  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].desc = "match MQTT CONNACK session present flag";
55  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].url = "/rules/mqtt-keywords.html#mqtt-connack-session-present";
56  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].AppLayerTxMatch = DetectMQTTConnackSessionPresentMatch;
57  sigmatch_table[DETECT_AL_MQTT_CONNACK_SESSION_PRESENT].Setup = DetectMQTTConnackSessionPresentSetup;
59 #ifdef UNITTESTS
61 #endif
62 
63  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
64 
65  DetectAppLayerInspectEngineRegister2("mqtt.connack.session_present", ALPROTO_MQTT,
67 
68  mqtt_connack_session_present_id = DetectBufferTypeGetByName("mqtt.connack.session_present");
69 }
70 
71 /**
72  * \internal
73  * \brief Function to match session_present flag of an MQTT CONNACK message
74  *
75  * \param det_ctx Pointer to the pattern matcher thread.
76  * \param f Pointer to the current flow.
77  * \param flags Flags.
78  * \param state App layer state.
79  * \param txv Pointer to the transaction.
80  * \param s Pointer to the Signature.
81  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTConnackSessionPresentData.
82  *
83  * \retval 0 no match.
84  * \retval 1 match.
85  */
86 static int DetectMQTTConnackSessionPresentMatch(DetectEngineThreadCtx *det_ctx,
87  Flow *f, uint8_t flags, void *state,
88  void *txv, const Signature *s,
89  const SigMatchCtx *ctx)
90 {
91  const bool *de = (const bool *)ctx;
92  bool value = false;
93 
94  if (!de)
95  return 0;
96 
97  if (rs_mqtt_tx_get_connack_sessionpresent(txv, &value) ==0 ) {
98  return 0;
99  }
100  if (value != *de) {
101  return 0;
102  }
103 
104  return 1;
105 }
106 
107 /**
108  * \internal
109  * \brief This function is used to parse options passed via mqtt.connack.session_present: keyword
110  *
111  * \param rawstr Pointer to the user provided options
112  *
113  * \retval de pointer to DetectMQTTConnackSessionPresentData on success
114  * \retval NULL on failure
115  */
116 static bool *DetectMQTTConnackSessionPresentParse(const char *rawstr)
117 {
118  bool *de = NULL;
119  de = SCMalloc(sizeof(bool));
120  if (unlikely(de == NULL))
121  return NULL;
122  *de = false;
123 
124  if (strcmp(rawstr, "yes") == 0) {
125  *de = true;
126  } else if (strcmp(rawstr, "true") == 0) {
127  *de = true;
128  } else if (strcmp(rawstr, "no") == 0) {
129  *de = false;
130  } else if (strcmp(rawstr, "false") == 0) {
131  *de = false;
132  } else {
133  SCLogError("invalid session_present flag definition: %s", rawstr);
134  goto error;
135  }
136 
137  return de;
138 
139 error:
140  /* de can't be NULL here */
141  SCFree(de);
142  return NULL;
143 }
144 
145 /**
146  * \internal
147  * \brief this function is used to add the parsed type query into the current signature
148  *
149  * \param de_ctx pointer to the Detection Engine Context
150  * \param s pointer to the Current Signature
151  * \param rawstr pointer to the user provided options
152  *
153  * \retval 0 on Success
154  * \retval -1 on Failure
155  */
156 static int DetectMQTTConnackSessionPresentSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
157 {
158  bool *de = NULL;
159  SigMatch *sm = NULL;
160 
162  return -1;
163 
164  de = DetectMQTTConnackSessionPresentParse(rawstr);
165  if (de == NULL)
166  goto error;
167 
168  sm = SigMatchAlloc();
169  if (sm == NULL)
170  goto error;
171 
173  sm->ctx = (SigMatchCtx *)de;
174 
175  SigMatchAppendSMToList(s, sm, mqtt_connack_session_present_id);
176 
177  return 0;
178 
179 error:
180  if (de != NULL)
181  SCFree(de);
182  if (sm != NULL)
183  SCFree(sm);
184  return -1;
185 }
186 
187 /**
188  * \internal
189  * \brief this function will free memory associated with DetectMQTTConnackSessionPresentData
190  *
191  * \param de pointer to DetectMQTTConnackSessionPresentData
192  */
194 {
195  if (de_ptr != NULL)
196  SCFree(de_ptr);
197 }
198 
199 /*
200  * ONLY TESTS BELOW THIS COMMENT
201  */
202 
203 #ifdef UNITTESTS
204 /**
205  * \test MQTTConnackSessionPresentTestParse01 is a test for a valid value
206  *
207  * \retval 1 on success
208  * \retval 0 on failure
209  */
210 static int MQTTConnackSessionPresentTestParse01 (void)
211 {
212  bool *de = NULL;
213 
214  de = DetectMQTTConnackSessionPresentParse("yes");
215  FAIL_IF_NULL(de);
217 
218  de = DetectMQTTConnackSessionPresentParse("true");
219  FAIL_IF_NULL(de);
221 
222  de = DetectMQTTConnackSessionPresentParse("false");
223  FAIL_IF_NULL(de);
225 
226  de = DetectMQTTConnackSessionPresentParse("no");
227  FAIL_IF_NULL(de);
229 
230  PASS;
231 }
232 
233 /**
234  * \test MQTTConnackSessionPresentTestParse02 is a test for an invalid value
235  *
236  * \retval 1 on success
237  * \retval 0 on failure
238  */
239 static int MQTTConnackSessionPresentTestParse02 (void)
240 {
241  bool *de = NULL;
242  de = DetectMQTTConnackSessionPresentParse("nix");
243  if (de) {
245  FAIL;
246  }
247 
248  PASS;
249 }
250 
251 /**
252  * \test MQTTConnackSessionPresentTestParse03 is a test for an invalid value
253  *
254  * \retval 1 on success
255  * \retval 0 on failure
256  */
257 static int MQTTConnackSessionPresentTestParse03 (void)
258 {
259  bool *de = NULL;
260  de = DetectMQTTConnackSessionPresentParse("");
261  if (de) {
263  FAIL;
264  }
265 
266  PASS;
267 }
268 
269 /**
270  * \test MQTTConnackSessionPresentTestParse04 is a test for an invalid value
271  *
272  * \retval 1 on success
273  * \retval 0 on failure
274  */
275 static int MQTTConnackSessionPresentTestParse04 (void)
276 {
277  bool *de = NULL;
278  de = DetectMQTTConnackSessionPresentParse(",");
279  if (de) {
281  FAIL;
282  }
283 
284  PASS;
285 }
286 
287 
288 #endif /* UNITTESTS */
289 
290 /**
291  * \brief this function registers unit tests for MQTTConnackSessionPresent
292  */
294 {
295 #ifdef UNITTESTS
296  UtRegisterTest("MQTTConnackSessionPresentTestParse01", MQTTConnackSessionPresentTestParse01);
297  UtRegisterTest("MQTTConnackSessionPresentTestParse02", MQTTConnackSessionPresentTestParse02);
298  UtRegisterTest("MQTTConnackSessionPresentTestParse03", MQTTConnackSessionPresentTestParse03);
299  UtRegisterTest("MQTTConnackSessionPresentTestParse04", MQTTConnackSessionPresentTestParse04);
300 #endif /* UNITTESTS */
301 }
SigTableElmt_::url
const char * url
Definition: detect.h:1287
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1703
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:437
SigTableElmt_::desc
const char * desc
Definition: detect.h:1286
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1274
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1284
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:826
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1255
rust.h
MQTTConnackSessionPresentRegisterTests
void MQTTConnackSessionPresentRegisterTests(void)
this function registers unit tests for MQTTConnackSessionPresent
Definition: detect-mqtt-connack-sessionpresent.c:293
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1269
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1124
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:255
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:1074
de
uint8_t de
Definition: app-layer-htp.c:577
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2747
detect.h
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-mqtt-connack-sessionpresent.c:35
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:343
detect-mqtt-connack-sessionpresent.h
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:216
conf.h
DETECT_AL_MQTT_CONNACK_SESSION_PRESENT
@ DETECT_AL_MQTT_CONNACK_SESSION_PRESENT
Definition: detect-engine-register.h:300
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:322
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:335
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:341
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
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:2122
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
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:581
SigMatch_
a single match condition for a signature
Definition: detect.h:340
DetectMQTTConnackSessionPresentRegister
void DetectMQTTConnackSessionPresentRegister(void)
Registration function for mqtt.connack.session_present: keyword.
Definition: detect-mqtt-connack-sessionpresent.c:51
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
DetectMQTTConnackSessionPresentFree
void DetectMQTTConnackSessionPresentFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-connack-sessionpresent.c:193
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1276