suricata
detect-mqtt-protocol-version.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"
30 #include "detect-engine-uint.h"
32 #include "util-byte.h"
33 #include "util-unittest.h"
34 
35 #include "rust.h"
36 
37 static int mqtt_protocol_version_id = 0;
38 
39 static int DetectMQTTProtocolVersionMatch(DetectEngineThreadCtx *det_ctx,
40  Flow *f, uint8_t flags, void *state,
41  void *txv, const Signature *s,
42  const SigMatchCtx *ctx);
43 static int DetectMQTTProtocolVersionSetup (DetectEngineCtx *, Signature *, const char *);
46 
47 /**
48  * \brief Registration function for mqtt.protocol_version: keyword
49  */
51 {
52  sigmatch_table[DETECT_AL_MQTT_PROTOCOL_VERSION].name = "mqtt.protocol_version";
53  sigmatch_table[DETECT_AL_MQTT_PROTOCOL_VERSION].desc = "match MQTT protocol version";
54  sigmatch_table[DETECT_AL_MQTT_PROTOCOL_VERSION].url = "/rules/mqtt-keywords.html#mqtt-protocol-version";
55  sigmatch_table[DETECT_AL_MQTT_PROTOCOL_VERSION].AppLayerTxMatch = DetectMQTTProtocolVersionMatch;
56  sigmatch_table[DETECT_AL_MQTT_PROTOCOL_VERSION].Setup = DetectMQTTProtocolVersionSetup;
58 #ifdef UNITTESTS
60 #endif
61 
64 
65  mqtt_protocol_version_id = DetectBufferTypeGetByName("mqtt.protocol_version");
66 }
67 
68 /**
69  * \internal
70  * \brief Function to match protocol version of an MQTT Tx
71  *
72  * \param det_ctx Pointer to the pattern matcher thread.
73  * \param f Pointer to the current flow.
74  * \param flags Flags.
75  * \param state App layer state.
76  * \param txv Pointer to the transaction.
77  * \param s Pointer to the Signature.
78  * \param ctx Pointer to the sigmatch that we will cast into DetectMQTTProtocolVersionData.
79  *
80  * \retval 0 no match.
81  * \retval 1 match.
82  */
83 static int DetectMQTTProtocolVersionMatch(DetectEngineThreadCtx *det_ctx,
84  Flow *f, uint8_t flags, void *state,
85  void *txv, const Signature *s,
86  const SigMatchCtx *ctx)
87 {
88  const DetectU8Data *de = (const DetectU8Data *)ctx;
89  uint8_t version;
90 
91  version = rs_mqtt_tx_get_protocol_version(state);
92 
93  return DetectU8Match(version, de);
94 }
95 
96 /**
97  * \internal
98  * \brief this function is used to add the parsed sigmatch into the current signature
99  *
100  * \param de_ctx pointer to the Detection Engine Context
101  * \param s pointer to the Current Signature
102  * \param rawstr pointer to the user provided options
103  *
104  * \retval 0 on Success
105  * \retval -1 on Failure
106  */
107 static int DetectMQTTProtocolVersionSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
108 {
109  SigMatch *sm = NULL;
110  DetectU8Data *de = NULL;
111 
113  return -1;
114 
115  de = DetectU8Parse(rawstr);
116  if (de == NULL)
117  return -1;
118 
119  sm = SigMatchAlloc();
120  if (sm == NULL)
121  goto error;
122 
124  sm->ctx = (SigMatchCtx *)de;
125 
126  SigMatchAppendSMToList(s, sm, mqtt_protocol_version_id);
127 
128  return 0;
129 
130 error:
131  if (de != NULL)
132  rs_detect_u8_free(de);
133  if (sm != NULL)
134  SCFree(sm);
135  return -1;
136 }
137 
138 /**
139  * \internal
140  * \brief this function will free memory associated with DetectMQTTProtocolVersionData
141  *
142  * \param de pointer to DetectMQTTProtocolVersionData
143  */
145 {
146  rs_detect_u8_free(de_ptr);
147 }
148 
149 /*
150  * ONLY TESTS BELOW THIS COMMENT
151  */
152 
153 #ifdef UNITTESTS
154 /**
155  * \test MQTTProtocolVersionTestParse01 is a test for a valid value
156  *
157  * \retval 1 on success
158  * \retval 0 on failure
159  */
160 static int MQTTProtocolVersionTestParse01 (void)
161 {
164 
166  "alert ip any any -> any any (mqtt.protocol_version:3; sid:1; rev:1;)");
167  FAIL_IF_NULL(sig);
168 
170  "alert ip any any -> any any (mqtt.protocol_version:3; sid:2; rev:1;)");
171  FAIL_IF_NULL(sig);
172 
174 
175  PASS;
176 }
177 
178 /**
179  * \test MQTTProtocolVersionTestParse02 is a test for a valid value
180  *
181  * \retval 1 on success
182  * \retval 0 on failure
183  */
184 static int MQTTProtocolVersionTestParse02 (void)
185 {
188 
190  "alert ip any any -> any any (mqtt.protocol_version:>3; sid:1; rev:1;)");
191  FAIL_IF_NULL(sig);
192 
194  "alert ip any any -> any any (mqtt.protocol_version:<44; sid:2; rev:1;)");
195  FAIL_IF_NULL(sig);
196 
198 
199  PASS;
200 }
201 
202 /**
203  * \test MQTTProtocolVersionTestParse03 is a test for an invalid value
204  *
205  * \retval 1 on success
206  * \retval 0 on failure
207  */
208 static int MQTTProtocolVersionTestParse03 (void)
209 {
212 
214  "alert ip any any -> any any (mqtt.protocol_version:; sid:1; rev:1;)");
215  FAIL_IF_NOT_NULL(sig);
216 
218 
219  PASS;
220 }
221 
222 /**
223  * \test MQTTProtocolVersionTestParse04 is a test for an invalid value
224  *
225  * \retval 1 on success
226  * \retval 0 on failure
227  */
228 static int MQTTProtocolVersionTestParse04 (void)
229 {
232 
234  "alert ip any any -> any any (mqtt.protocol_version:<444; sid:1; rev:1;)");
235  FAIL_IF_NOT_NULL(sig);
236 
238 
239  PASS;
240 }
241 
242 #endif /* UNITTESTS */
243 
244 /**
245  * \brief this function registers unit tests for MQTTProtocolVersion
246  */
248 {
249 #ifdef UNITTESTS
250  UtRegisterTest("MQTTProtocolVersionTestParse01", MQTTProtocolVersionTestParse01);
251  UtRegisterTest("MQTTProtocolVersionTestParse02", MQTTProtocolVersionTestParse02);
252  UtRegisterTest("MQTTProtocolVersionTestParse03", MQTTProtocolVersionTestParse03);
253  UtRegisterTest("MQTTProtocolVersionTestParse04", MQTTProtocolVersionTestParse04);
254 #endif /* UNITTESTS */
255 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1238
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1493
DetectMQTTProtocolVersionRegister
void DetectMQTTProtocolVersionRegister(void)
Registration function for mqtt.protocol_version: keyword.
Definition: detect-mqtt-protocol-version.c:50
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:1237
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1225
SigTableElmt_::name
const char * name
Definition: detect.h:1235
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:784
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2445
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1206
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1220
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1086
DetectU8Parse
DetectUintData_u8 * DetectU8Parse(const char *u8str)
This function is used to parse u8 options passed via some u8 keyword.
Definition: detect-engine-uint.c:85
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
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:1024
de
uint8_t de
Definition: app-layer-htp.c:572
detect.h
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:316
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:228
conf.h
DETECT_AL_MQTT_PROTOCOL_VERSION
@ DETECT_AL_MQTT_PROTOCOL_VERSION
Definition: detect-engine-register.h:291
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:238
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:308
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2434
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:314
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
version
uint8_t version
Definition: decode-gre.h:1
MQTTProtocolVersionRegisterTests
void MQTTProtocolVersionRegisterTests(void)
this function registers unit tests for MQTTProtocolVersion
Definition: detect-mqtt-protocol-version.c:247
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
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:539
SigMatch_
a single match condition for a signature
Definition: detect.h:313
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2406
DetectMQTTProtocolVersionFree
void DetectMQTTProtocolVersionFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-protocol-version.c:144
detect-mqtt-protocol-version.h
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:352
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1227