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  DetectAppLayerInspectEngineRegister("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 
161  return -1;
162 
163  de = DetectMQTTConnackSessionPresentParse(rawstr);
164  if (de == NULL)
165  goto error;
166 
168  mqtt_connack_session_present_id) == NULL) {
169  goto error;
170  }
171 
172  return 0;
173 
174 error:
175  if (de != NULL)
176  SCFree(de);
177  return -1;
178 }
179 
180 /**
181  * \internal
182  * \brief this function will free memory associated with DetectMQTTConnackSessionPresentData
183  *
184  * \param de pointer to DetectMQTTConnackSessionPresentData
185  */
187 {
188  if (de_ptr != NULL)
189  SCFree(de_ptr);
190 }
191 
192 /*
193  * ONLY TESTS BELOW THIS COMMENT
194  */
195 
196 #ifdef UNITTESTS
197 /**
198  * \test MQTTConnackSessionPresentTestParse01 is a test for a valid value
199  *
200  * \retval 1 on success
201  * \retval 0 on failure
202  */
203 static int MQTTConnackSessionPresentTestParse01 (void)
204 {
205  bool *de = NULL;
206 
207  de = DetectMQTTConnackSessionPresentParse("yes");
208  FAIL_IF_NULL(de);
210 
211  de = DetectMQTTConnackSessionPresentParse("true");
212  FAIL_IF_NULL(de);
214 
215  de = DetectMQTTConnackSessionPresentParse("false");
216  FAIL_IF_NULL(de);
218 
219  de = DetectMQTTConnackSessionPresentParse("no");
220  FAIL_IF_NULL(de);
222 
223  PASS;
224 }
225 
226 /**
227  * \test MQTTConnackSessionPresentTestParse02 is a test for an invalid value
228  *
229  * \retval 1 on success
230  * \retval 0 on failure
231  */
232 static int MQTTConnackSessionPresentTestParse02 (void)
233 {
234  bool *de = NULL;
235  de = DetectMQTTConnackSessionPresentParse("nix");
236  if (de) {
238  FAIL;
239  }
240 
241  PASS;
242 }
243 
244 /**
245  * \test MQTTConnackSessionPresentTestParse03 is a test for an invalid value
246  *
247  * \retval 1 on success
248  * \retval 0 on failure
249  */
250 static int MQTTConnackSessionPresentTestParse03 (void)
251 {
252  bool *de = NULL;
253  de = DetectMQTTConnackSessionPresentParse("");
254  if (de) {
256  FAIL;
257  }
258 
259  PASS;
260 }
261 
262 /**
263  * \test MQTTConnackSessionPresentTestParse04 is a test for an invalid value
264  *
265  * \retval 1 on success
266  * \retval 0 on failure
267  */
268 static int MQTTConnackSessionPresentTestParse04 (void)
269 {
270  bool *de = NULL;
271  de = DetectMQTTConnackSessionPresentParse(",");
272  if (de) {
274  FAIL;
275  }
276 
277  PASS;
278 }
279 
280 
281 #endif /* UNITTESTS */
282 
283 /**
284  * \brief this function registers unit tests for MQTTConnackSessionPresent
285  */
287 {
288 #ifdef UNITTESTS
289  UtRegisterTest("MQTTConnackSessionPresentTestParse01", MQTTConnackSessionPresentTestParse01);
290  UtRegisterTest("MQTTConnackSessionPresentTestParse02", MQTTConnackSessionPresentTestParse02);
291  UtRegisterTest("MQTTConnackSessionPresentTestParse03", MQTTConnackSessionPresentTestParse03);
292  UtRegisterTest("MQTTConnackSessionPresentTestParse04", MQTTConnackSessionPresentTestParse04);
293 #endif /* UNITTESTS */
294 }
SigTableElmt_::url
const char * url
Definition: detect.h:1299
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
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:1298
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1286
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1296
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:351
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1267
rust.h
MQTTConnackSessionPresentRegisterTests
void MQTTConnackSessionPresentRegisterTests(void)
this function registers unit tests for MQTTConnackSessionPresent
Definition: detect-mqtt-connack-sessionpresent.c:286
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:266
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:1095
de
uint8_t de
Definition: app-layer-htp.c:581
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2800
detect.h
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-mqtt-connack-sessionpresent.c:35
detect-mqtt-connack-sessionpresent.h
conf.h
DETECT_AL_MQTT_CONNACK_SESSION_PRESENT
@ DETECT_AL_MQTT_CONNACK_SESSION_PRESENT
Definition: detect-engine-register.h:312
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:345
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
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:2079
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:596
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:186
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1288