suricata
detect-itype.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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 Gerardo Iglesias <iglesiasg@gmail.com>
22  *
23  * Implements itype keyword support
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 
29 #include "detect.h"
30 #include "detect-parse.h"
32 #include "detect-engine-build.h"
33 
34 #include "detect-itype.h"
35 #include "detect-engine-uint.h"
36 
37 #include "util-byte.h"
38 #include "util-unittest.h"
39 #include "util-unittest-helper.h"
40 #include "util-debug.h"
41 
42 
43 static int DetectITypeMatch(DetectEngineThreadCtx *, Packet *,
44  const Signature *, const SigMatchCtx *);
45 static int DetectITypeSetup(DetectEngineCtx *, Signature *, const char *);
46 #ifdef UNITTESTS
47 static void DetectITypeRegisterTests(void);
48 #endif
49 void DetectITypeFree(DetectEngineCtx *, void *);
50 
51 static int PrefilterSetupIType(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
52 static bool PrefilterITypeIsPrefilterable(const Signature *s);
53 
54 /**
55  * \brief Registration function for itype: keyword
56  */
58 {
59  sigmatch_table[DETECT_ITYPE].name = "itype";
60  sigmatch_table[DETECT_ITYPE].desc = "match on a specific ICMP type";
61  sigmatch_table[DETECT_ITYPE].url = "/rules/header-keywords.html#itype";
62  sigmatch_table[DETECT_ITYPE].Match = DetectITypeMatch;
63  sigmatch_table[DETECT_ITYPE].Setup = DetectITypeSetup;
65 #ifdef UNITTESTS
66  sigmatch_table[DETECT_ITYPE].RegisterTests = DetectITypeRegisterTests;
67 #endif
68  sigmatch_table[DETECT_ITYPE].SupportsPrefilter = PrefilterITypeIsPrefilterable;
69  sigmatch_table[DETECT_ITYPE].SetupPrefilter = PrefilterSetupIType;
70 }
71 
72 /**
73  * \brief This function is used to match itype rule option set on a packet with those passed via
74  * itype:
75  *
76  * \param t pointer to thread vars
77  * \param det_ctx pointer to the pattern matcher thread
78  * \param p pointer to the current packet
79  * \param m pointer to the sigmatch that we will cast into DetectU8Data
80  *
81  * \retval 0 no match
82  * \retval 1 match
83  */
84 static int DetectITypeMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
85  const Signature *s, const SigMatchCtx *ctx)
86 {
87  if (PKT_IS_PSEUDOPKT(p))
88  return 0;
89 
90  uint8_t pitype;
91  if (PKT_IS_ICMPV4(p)) {
92  pitype = ICMPV4_GET_TYPE(p);
93  } else if (PKT_IS_ICMPV6(p)) {
94  pitype = ICMPV6_GET_TYPE(p);
95  } else {
96  /* Packet not ICMPv4 nor ICMPv6 */
97  return 0;
98  }
99 
100  const DetectU8Data *itd = (const DetectU8Data *)ctx;
101  return DetectU8Match(pitype, itd);
102 }
103 
104 /**
105  * \brief This function is used to parse itype options passed via itype: keyword
106  *
107  * \param de_ctx Pointer to the detection engine context
108  * \param itypestr Pointer to the user provided itype options
109  *
110  * \retval itd pointer to DetectU8Data on success
111  * \retval NULL on failure
112  */
113 static DetectU8Data *DetectITypeParse(DetectEngineCtx *de_ctx, const char *itypestr)
114 {
115  return DetectU8Parse(itypestr);
116 }
117 
118 /**
119  * \brief this function is used to add the parsed itype data into the current signature
120  *
121  * \param de_ctx pointer to the Detection Engine Context
122  * \param s pointer to the Current Signature
123  * \param itypestr pointer to the user provided itype options
124  *
125  * \retval 0 on Success
126  * \retval -1 on Failure
127  */
128 static int DetectITypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *itypestr)
129 {
130 
131  DetectU8Data *itd = NULL;
132  SigMatch *sm = NULL;
133 
134  itd = DetectITypeParse(de_ctx, itypestr);
135  if (itd == NULL) goto error;
136 
137  sm = SigMatchAlloc();
138  if (sm == NULL) goto error;
139 
140  sm->type = DETECT_ITYPE;
141  sm->ctx = (SigMatchCtx *)itd;
142 
145 
146  return 0;
147 
148 error:
149  if (itd != NULL) DetectITypeFree(de_ctx, itd);
150  if (sm != NULL) SCFree(sm);
151  return -1;
152 }
153 
154 /**
155  * \brief this function will free memory associated with DetectU8Data
156  *
157  * \param ptr pointer to DetectU8Data
158  */
160 {
161  DetectU8Data *itd = (DetectU8Data *)ptr;
162  rs_detect_u8_free(itd);
163 }
164 
165 /* prefilter code
166  *
167  * Prefilter uses the U8Hash logic, where we setup a 256 entry array
168  * for each ICMP type. Each array element has the list of signatures
169  * that need to be inspected. */
170 
171 static void PrefilterPacketITypeMatch(DetectEngineThreadCtx *det_ctx,
172  Packet *p, const void *pectx)
173 {
174  if (PKT_IS_PSEUDOPKT(p)) {
175  SCReturn;
176  }
177 
178  uint8_t pitype;
179  if (PKT_IS_ICMPV4(p)) {
180  pitype = ICMPV4_GET_TYPE(p);
181  } else if (PKT_IS_ICMPV6(p)) {
182  pitype = ICMPV6_GET_TYPE(p);
183  } else {
184  /* Packet not ICMPv4 nor ICMPv6 */
185  return;
186  }
187 
188  const PrefilterPacketU8HashCtx *h = pectx;
189  const SigsArray *sa = h->array[pitype];
190  if (sa) {
191  PrefilterAddSids(&det_ctx->pmq, sa->sigs, sa->cnt);
192  }
193 }
194 
195 static int PrefilterSetupIType(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
196 {
198  PrefilterPacketU8Compare, PrefilterPacketITypeMatch);
199 }
200 
201 static bool PrefilterITypeIsPrefilterable(const Signature *s)
202 {
203  const SigMatch *sm;
204  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
205  switch (sm->type) {
206  case DETECT_ITYPE:
207  return true;
208  }
209  }
210  return false;
211 }
212 
213 #ifdef UNITTESTS
214 
215 #include "detect-engine.h"
216 #include "detect-engine-mpm.h"
217 
218 /**
219  * \test DetectITypeParseTest01 is a test for setting a valid itype value
220  */
221 static int DetectITypeParseTest01(void)
222 {
223  DetectU8Data *itd = NULL;
224  itd = DetectITypeParse(NULL, "8");
225  FAIL_IF_NULL(itd);
226  FAIL_IF_NOT(itd->arg1 == 8);
227  FAIL_IF_NOT(itd->mode == DETECT_UINT_EQ);
228  DetectITypeFree(NULL, itd);
229 
230  PASS;
231 }
232 
233 /**
234  * \test DetectITypeParseTest02 is a test for setting a valid itype value
235  * with ">" operator
236  */
237 static int DetectITypeParseTest02(void)
238 {
239  DetectU8Data *itd = NULL;
240  itd = DetectITypeParse(NULL, ">8");
241  FAIL_IF_NULL(itd);
242  FAIL_IF_NOT(itd->arg1 == 8);
243  FAIL_IF_NOT(itd->mode == DETECT_UINT_GT);
244  DetectITypeFree(NULL, itd);
245 
246  PASS;
247 }
248 
249 /**
250  * \test DetectITypeParseTest03 is a test for setting a valid itype value
251  * with "<" operator
252  */
253 static int DetectITypeParseTest03(void)
254 {
255  DetectU8Data *itd = NULL;
256  itd = DetectITypeParse(NULL, "<8");
257  FAIL_IF_NULL(itd);
258  FAIL_IF_NOT(itd->arg1 == 8);
259  FAIL_IF_NOT(itd->mode == DETECT_UINT_LT);
260  DetectITypeFree(NULL, itd);
261 
262  PASS;
263 }
264 
265 /**
266  * \test DetectITypeParseTest04 is a test for setting a valid itype value
267  * with "<>" operator
268  */
269 static int DetectITypeParseTest04(void)
270 {
271  DetectU8Data *itd = NULL;
272  itd = DetectITypeParse(NULL, "8<>20");
273  FAIL_IF_NULL(itd);
274  FAIL_IF_NOT(itd->arg1 == 8);
275  FAIL_IF_NOT(itd->arg2 == 20);
276  FAIL_IF_NOT(itd->mode == DETECT_UINT_RA);
277  DetectITypeFree(NULL, itd);
278 
279  PASS;
280 }
281 
282 /**
283  * \test DetectITypeParseTest05 is a test for setting a valid itype value
284  * with spaces all around
285  */
286 static int DetectITypeParseTest05(void)
287 {
288  DetectU8Data *itd = NULL;
289  itd = DetectITypeParse(NULL, " 8 ");
290  FAIL_IF_NULL(itd);
291  FAIL_IF_NOT(itd->arg1 == 8);
292  FAIL_IF_NOT(itd->mode == DETECT_UINT_EQ);
293  DetectITypeFree(NULL, itd);
294 
295  PASS;
296 }
297 
298 /**
299  * \test DetectITypeParseTest06 is a test for setting a valid itype value
300  * with ">" operator and spaces all around
301  */
302 static int DetectITypeParseTest06(void)
303 {
304  DetectU8Data *itd = NULL;
305  itd = DetectITypeParse(NULL, " > 8 ");
306  FAIL_IF_NULL(itd);
307  FAIL_IF_NOT(itd->arg1 == 8);
308  FAIL_IF_NOT(itd->mode == DETECT_UINT_GT);
309  DetectITypeFree(NULL, itd);
310 
311  PASS;
312 }
313 
314 /**
315  * \test DetectITypeParseTest07 is a test for setting a valid itype value
316  * with "<>" operator and spaces all around
317  */
318 static int DetectITypeParseTest07(void)
319 {
320  DetectU8Data *itd = NULL;
321  itd = DetectITypeParse(NULL, " 8 <> 20 ");
322  FAIL_IF_NULL(itd);
323  FAIL_IF_NOT(itd->arg1 == 8);
324  FAIL_IF_NOT(itd->arg2 == 20);
325  FAIL_IF_NOT(itd->mode == DETECT_UINT_RA);
326  DetectITypeFree(NULL, itd);
327 
328  PASS;
329 }
330 
331 /**
332  * \test DetectITypeParseTest08 is a test for setting an invalid itype value
333  */
334 static int DetectITypeParseTest08(void)
335 {
336  DetectU8Data *itd = NULL;
337  itd = DetectITypeParse(NULL, "> 8 <> 20");
338  FAIL_IF_NOT_NULL(itd);
339  DetectITypeFree(NULL, itd);
340 
341  PASS;
342 }
343 
344 /**
345  * \brief this function registers unit tests for DetectIType
346  */
347 void DetectITypeRegisterTests(void)
348 {
349  UtRegisterTest("DetectITypeParseTest01", DetectITypeParseTest01);
350  UtRegisterTest("DetectITypeParseTest02", DetectITypeParseTest02);
351  UtRegisterTest("DetectITypeParseTest03", DetectITypeParseTest03);
352  UtRegisterTest("DetectITypeParseTest04", DetectITypeParseTest04);
353  UtRegisterTest("DetectITypeParseTest05", DetectITypeParseTest05);
354  UtRegisterTest("DetectITypeParseTest06", DetectITypeParseTest06);
355  UtRegisterTest("DetectITypeParseTest07", DetectITypeParseTest07);
356  UtRegisterTest("DetectITypeParseTest08", DetectITypeParseTest08);
357 }
358 #endif /* UNITTESTS */
util-byte.h
DetectITypeRegister
void DetectITypeRegister(void)
Registration function for itype: keyword.
Definition: detect-itype.c:57
DetectITypeFree
void DetectITypeFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectU8Data
Definition: detect-itype.c:159
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1287
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
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:566
SigTableElmt_::desc
const char * desc
Definition: detect.h:1286
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1274
SigTableElmt_::name
const char * name
Definition: detect.h:1284
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1053
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1438
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DETECT_UINT_LT
#define DETECT_UINT_LT
Definition: detect-engine-uint.h:37
PrefilterPacketU8HashCtx_::array
SigsArray * array[256]
Definition: detect-engine-prefilter-common.h:53
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1181
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:826
DETECT_UINT_EQ
#define DETECT_UINT_EQ
Definition: detect-engine-uint.h:35
DETECT_UINT_GT
#define DETECT_UINT_GT
Definition: detect-engine-uint.h:32
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1269
DETECT_ITYPE
@ DETECT_ITYPE
Definition: detect-engine-register.h:47
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1272
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
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
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
ICMPV4_GET_TYPE
#define ICMPV4_GET_TYPE(p)
Definition: decode-icmpv4.h:233
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:344
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:108
PrefilterPacketU8Set
void PrefilterPacketU8Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:90
detect-itype.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:343
PKT_IS_ICMPV6
#define PKT_IS_ICMPV6(p)
Definition: decode.h:250
SCReturn
#define SCReturn
Definition: util-debug.h:273
Signature_::flags
uint32_t flags
Definition: detect.h:582
Packet_
Definition: decode.h:430
detect-engine-build.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:653
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1252
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:322
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:335
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
SigsArray_::sigs
SigIntId * sigs
Definition: detect-engine-prefilter-common.h:47
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:341
SigsArray_
Definition: detect-engine-prefilter-common.h:46
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:129
SigsArray_::cnt
uint32_t cnt
Definition: detect-engine-prefilter-common.h:48
PKT_IS_ICMPV4
#define PKT_IS_ICMPV4(p)
Definition: decode.h:249
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1271
detect-parse.h
Signature_
Signature container.
Definition: detect.h:581
SigMatch_
a single match condition for a signature
Definition: detect.h:340
ICMPV6_GET_TYPE
#define ICMPV6_GET_TYPE(p)
Definition: decode-icmpv6.h:101
PrefilterPacketU8Compare
bool PrefilterPacketU8Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:98
PrefilterPacketU8HashCtx_
Definition: detect-engine-prefilter-common.h:52
detect-engine-prefilter-common.h
PrefilterSetupPacketHeaderU8Hash
int PrefilterSetupPacketHeaderU8Hash(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
Definition: detect-engine-prefilter-common.c:407
DETECT_UINT_RA
#define DETECT_UINT_RA
Definition: detect-engine-uint.h:34
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1276
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:242