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