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  DetectU8Data *de = NULL;
110 
112  return -1;
113 
114  de = DetectU8Parse(rawstr);
115  if (de == NULL)
116  return -1;
117 
119  mqtt_protocol_version_id) == NULL) {
120  goto error;
121  }
122 
123  return 0;
124 
125 error:
126  if (de != NULL)
127  rs_detect_u8_free(de);
128  return -1;
129 }
130 
131 /**
132  * \internal
133  * \brief this function will free memory associated with DetectMQTTProtocolVersionData
134  *
135  * \param de pointer to DetectMQTTProtocolVersionData
136  */
138 {
139  rs_detect_u8_free(de_ptr);
140 }
141 
142 /*
143  * ONLY TESTS BELOW THIS COMMENT
144  */
145 
146 #ifdef UNITTESTS
147 /**
148  * \test MQTTProtocolVersionTestParse01 is a test for a valid value
149  *
150  * \retval 1 on success
151  * \retval 0 on failure
152  */
153 static int MQTTProtocolVersionTestParse01 (void)
154 {
157 
159  "alert ip any any -> any any (mqtt.protocol_version:3; sid:1; rev:1;)");
160  FAIL_IF_NULL(sig);
161 
163  "alert ip any any -> any any (mqtt.protocol_version:3; sid:2; rev:1;)");
164  FAIL_IF_NULL(sig);
165 
167 
168  PASS;
169 }
170 
171 /**
172  * \test MQTTProtocolVersionTestParse02 is a test for a valid value
173  *
174  * \retval 1 on success
175  * \retval 0 on failure
176  */
177 static int MQTTProtocolVersionTestParse02 (void)
178 {
181 
183  "alert ip any any -> any any (mqtt.protocol_version:>3; sid:1; rev:1;)");
184  FAIL_IF_NULL(sig);
185 
187  "alert ip any any -> any any (mqtt.protocol_version:<44; sid:2; rev:1;)");
188  FAIL_IF_NULL(sig);
189 
191 
192  PASS;
193 }
194 
195 /**
196  * \test MQTTProtocolVersionTestParse03 is a test for an invalid value
197  *
198  * \retval 1 on success
199  * \retval 0 on failure
200  */
201 static int MQTTProtocolVersionTestParse03 (void)
202 {
205 
207  "alert ip any any -> any any (mqtt.protocol_version:; sid:1; rev:1;)");
208  FAIL_IF_NOT_NULL(sig);
209 
211 
212  PASS;
213 }
214 
215 /**
216  * \test MQTTProtocolVersionTestParse04 is a test for an invalid value
217  *
218  * \retval 1 on success
219  * \retval 0 on failure
220  */
221 static int MQTTProtocolVersionTestParse04 (void)
222 {
225 
227  "alert ip any any -> any any (mqtt.protocol_version:<444; sid:1; rev:1;)");
228  FAIL_IF_NOT_NULL(sig);
229 
231 
232  PASS;
233 }
234 
235 #endif /* UNITTESTS */
236 
237 /**
238  * \brief this function registers unit tests for MQTTProtocolVersion
239  */
241 {
242 #ifdef UNITTESTS
243  UtRegisterTest("MQTTProtocolVersionTestParse01", MQTTProtocolVersionTestParse01);
244  UtRegisterTest("MQTTProtocolVersionTestParse02", MQTTProtocolVersionTestParse02);
245  UtRegisterTest("MQTTProtocolVersionTestParse03", MQTTProtocolVersionTestParse03);
246  UtRegisterTest("MQTTProtocolVersionTestParse04", MQTTProtocolVersionTestParse04);
247 #endif /* UNITTESTS */
248 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1296
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
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:1295
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1283
SigTableElmt_::name
const char * name
Definition: detect.h:1293
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_
Flow data structure.
Definition: flow.h:350
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2580
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1264
rust.h
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2620
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
util-unittest.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1119
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:263
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:1092
de
uint8_t de
Definition: app-layer-htp.c:580
detect.h
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
conf.h
DETECT_AL_MQTT_PROTOCOL_VERSION
@ DETECT_AL_MQTT_PROTOCOL_VERSION
Definition: detect-engine-register.h:303
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:342
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_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
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:240
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:2126
detect-parse.h
Signature_
Signature container.
Definition: detect.h:593
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2541
DetectMQTTProtocolVersionFree
void DetectMQTTProtocolVersionFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-protocol-version.c:137
detect-mqtt-protocol-version.h
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:216
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:1285