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;
66 #ifdef UNITTESTS
67  sigmatch_table[DETECT_ITYPE].RegisterTests = DetectITypeRegisterTests;
68 #endif
69  sigmatch_table[DETECT_ITYPE].SupportsPrefilter = PrefilterITypeIsPrefilterable;
70  sigmatch_table[DETECT_ITYPE].SetupPrefilter = PrefilterSetupIType;
71 }
72 
73 /**
74  * \brief This function is used to match itype rule option set on a packet with those passed via
75  * itype:
76  *
77  * \param t pointer to thread vars
78  * \param det_ctx pointer to the pattern matcher thread
79  * \param p pointer to the current packet
80  * \param m pointer to the sigmatch that we will cast into DetectU8Data
81  *
82  * \retval 0 no match
83  * \retval 1 match
84  */
85 static int DetectITypeMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
86  const Signature *s, const SigMatchCtx *ctx)
87 {
89 
90  uint8_t pitype;
91  if (PacketIsICMPv4(p)) {
92  pitype = p->icmp_s.type;
93  } else if (PacketIsICMPv6(p)) {
94  const ICMPV6Hdr *icmpv6h = PacketGetICMPv6(p);
95  pitype = ICMPV6_GET_TYPE(icmpv6h);
96  } else {
97  /* Packet not ICMPv4 nor ICMPv6 */
98  return 0;
99  }
100 
101  const DetectU8Data *itd = (const DetectU8Data *)ctx;
102  return DetectU8Match(pitype, itd);
103 }
104 
105 /**
106  * \brief this function is used to add the parsed itype data into the current signature
107  *
108  * \param de_ctx pointer to the Detection Engine Context
109  * \param s pointer to the Current Signature
110  * \param itypestr pointer to the user provided itype options
111  *
112  * \retval 0 on Success
113  * \retval -1 on Failure
114  */
115 static int DetectITypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *itypestr)
116 {
117 
118  DetectU8Data *itd = NULL;
119 
120  itd = DetectU8Parse(itypestr);
121  if (itd == NULL)
122  return -1;
123 
125  de_ctx, s, DETECT_ITYPE, (SigMatchCtx *)itd, DETECT_SM_LIST_MATCH) == NULL) {
126  DetectITypeFree(de_ctx, itd);
127  return -1;
128  }
130 
131  return 0;
132 }
133 
134 /**
135  * \brief this function will free memory associated with DetectU8Data
136  *
137  * \param ptr pointer to DetectU8Data
138  */
140 {
141  DetectU8Data *itd = (DetectU8Data *)ptr;
142  SCDetectU8Free(itd);
143 }
144 
145 /* prefilter code
146  *
147  * Prefilter uses the U8Hash logic, where we setup a 256 entry array
148  * for each ICMP type. Each array element has the list of signatures
149  * that need to be inspected. */
150 
151 static void PrefilterPacketITypeMatch(DetectEngineThreadCtx *det_ctx,
152  Packet *p, const void *pectx)
153 {
155 
156  uint8_t pitype;
157  if (PacketIsICMPv4(p)) {
158  pitype = p->icmp_s.type;
159  } else if (PacketIsICMPv6(p)) {
160  const ICMPV6Hdr *icmpv6h = PacketGetICMPv6(p);
161  pitype = ICMPV6_GET_TYPE(icmpv6h);
162  } else {
163  /* Packet not ICMPv4 nor ICMPv6 */
164  return;
165  }
166 
167  const PrefilterPacketU8HashCtx *h = pectx;
168  const SigsArray *sa = h->array[pitype];
169  if (sa) {
170  PrefilterAddSids(&det_ctx->pmq, sa->sigs, sa->cnt);
171  }
172 }
173 
174 static int PrefilterSetupIType(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
175 {
177  PrefilterPacketU8Set, PrefilterPacketU8Compare, PrefilterPacketITypeMatch);
178 }
179 
180 static bool PrefilterITypeIsPrefilterable(const Signature *s)
181 {
182  return PrefilterIsPrefilterableById(s, DETECT_ITYPE);
183 }
184 
185 #ifdef UNITTESTS
186 
187 #include "detect-engine.h"
188 #include "detect-engine-mpm.h"
189 
190 /**
191  * \test DetectITypeParseTest01 is a test for setting a valid itype value
192  */
193 static int DetectITypeParseTest01(void)
194 {
195  DetectU8Data *itd = NULL;
196  itd = DetectU8Parse("8");
197  FAIL_IF_NULL(itd);
198  FAIL_IF_NOT(itd->arg1 == 8);
199  FAIL_IF_NOT(itd->mode == DETECT_UINT_EQ);
200  DetectITypeFree(NULL, itd);
201 
202  PASS;
203 }
204 
205 /**
206  * \test DetectITypeParseTest02 is a test for setting a valid itype value
207  * with ">" operator
208  */
209 static int DetectITypeParseTest02(void)
210 {
211  DetectU8Data *itd = NULL;
212  itd = DetectU8Parse(">8");
213  FAIL_IF_NULL(itd);
214  FAIL_IF_NOT(itd->arg1 == 8);
215  FAIL_IF_NOT(itd->mode == DETECT_UINT_GT);
216  DetectITypeFree(NULL, itd);
217 
218  PASS;
219 }
220 
221 /**
222  * \test DetectITypeParseTest03 is a test for setting a valid itype value
223  * with "<" operator
224  */
225 static int DetectITypeParseTest03(void)
226 {
227  DetectU8Data *itd = NULL;
228  itd = DetectU8Parse("<8");
229  FAIL_IF_NULL(itd);
230  FAIL_IF_NOT(itd->arg1 == 8);
231  FAIL_IF_NOT(itd->mode == DETECT_UINT_LT);
232  DetectITypeFree(NULL, itd);
233 
234  PASS;
235 }
236 
237 /**
238  * \test DetectITypeParseTest04 is a test for setting a valid itype value
239  * with "<>" operator
240  */
241 static int DetectITypeParseTest04(void)
242 {
243  DetectU8Data *itd = NULL;
244  itd = DetectU8Parse("8<>20");
245  FAIL_IF_NULL(itd);
246  FAIL_IF_NOT(itd->arg1 == 8);
247  FAIL_IF_NOT(itd->arg2 == 20);
248  FAIL_IF_NOT(itd->mode == DETECT_UINT_RA);
249  DetectITypeFree(NULL, itd);
250 
251  PASS;
252 }
253 
254 /**
255  * \test DetectITypeParseTest05 is a test for setting a valid itype value
256  * with spaces all around
257  */
258 static int DetectITypeParseTest05(void)
259 {
260  DetectU8Data *itd = NULL;
261  itd = DetectU8Parse(" 8 ");
262  FAIL_IF_NULL(itd);
263  FAIL_IF_NOT(itd->arg1 == 8);
264  FAIL_IF_NOT(itd->mode == DETECT_UINT_EQ);
265  DetectITypeFree(NULL, itd);
266 
267  PASS;
268 }
269 
270 /**
271  * \test DetectITypeParseTest06 is a test for setting a valid itype value
272  * with ">" operator and spaces all around
273  */
274 static int DetectITypeParseTest06(void)
275 {
276  DetectU8Data *itd = NULL;
277  itd = DetectU8Parse(" > 8 ");
278  FAIL_IF_NULL(itd);
279  FAIL_IF_NOT(itd->arg1 == 8);
280  FAIL_IF_NOT(itd->mode == DETECT_UINT_GT);
281  DetectITypeFree(NULL, itd);
282 
283  PASS;
284 }
285 
286 /**
287  * \test DetectITypeParseTest07 is a test for setting a valid itype value
288  * with "<>" operator and spaces all around
289  */
290 static int DetectITypeParseTest07(void)
291 {
292  DetectU8Data *itd = NULL;
293  itd = DetectU8Parse(" 8 <> 20 ");
294  FAIL_IF_NULL(itd);
295  FAIL_IF_NOT(itd->arg1 == 8);
296  FAIL_IF_NOT(itd->arg2 == 20);
297  FAIL_IF_NOT(itd->mode == DETECT_UINT_RA);
298  DetectITypeFree(NULL, itd);
299 
300  PASS;
301 }
302 
303 /**
304  * \test DetectITypeParseTest08 is a test for setting an invalid itype value
305  */
306 static int DetectITypeParseTest08(void)
307 {
308  DetectU8Data *itd = NULL;
309  itd = DetectU8Parse("> 8 <> 20");
310  FAIL_IF_NOT_NULL(itd);
311 
312  PASS;
313 }
314 
315 /**
316  * \brief this function registers unit tests for DetectIType
317  */
318 void DetectITypeRegisterTests(void)
319 {
320  UtRegisterTest("DetectITypeParseTest01", DetectITypeParseTest01);
321  UtRegisterTest("DetectITypeParseTest02", DetectITypeParseTest02);
322  UtRegisterTest("DetectITypeParseTest03", DetectITypeParseTest03);
323  UtRegisterTest("DetectITypeParseTest04", DetectITypeParseTest04);
324  UtRegisterTest("DetectITypeParseTest05", DetectITypeParseTest05);
325  UtRegisterTest("DetectITypeParseTest06", DetectITypeParseTest06);
326  UtRegisterTest("DetectITypeParseTest07", DetectITypeParseTest07);
327  UtRegisterTest("DetectITypeParseTest08", DetectITypeParseTest08);
328 }
329 #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:139
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1461
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:316
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1445
SigTableElmt_::name
const char * name
Definition: detect.h:1458
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1323
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1628
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
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
PrefilterPacketU8HashCtx_::array
SigsArray * array[256]
Definition: detect-engine-prefilter-common.h:53
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1348
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DETECT_UINT_EQ
#define DETECT_UINT_EQ
Definition: detect-engine-uint.h:35
Packet_::icmp_s
struct Packet_::@33::@40 icmp_s
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect.h:1681
DETECT_UINT_GT
#define DETECT_UINT_GT
Definition: detect-engine-uint.h:32
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
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:1443
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
PrefilterSetupPacketHeaderU8Hash
int PrefilterSetupPacketHeaderU8Hash(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, SignatureMask mask, 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:462
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1245
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:388
detect.h
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
PrefilterPacketU8Set
void PrefilterPacketU8Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:90
detect-itype.h
Signature_::flags
uint32_t flags
Definition: detect.h:669
Packet_
Definition: decode.h:501
detect-engine-build.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1420
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
ICMPV6Hdr_
Definition: decode-icmpv6.h:129
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
SigsArray_
Definition: detect-engine-prefilter-common.h:46
SigsArray_::cnt
uint32_t cnt
Definition: detect-engine-prefilter-common.h:48
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1442
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
SIGMATCH_INFO_UINT8
#define SIGMATCH_INFO_UINT8
Definition: detect.h:1687
ICMPV6_GET_TYPE
#define ICMPV6_GET_TYPE(icmp6h)
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
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
DETECT_UINT_RA
#define DETECT_UINT_RA
Definition: detect-engine-uint.h:34
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1447
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:254