suricata
detect-template2.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 XXX
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "util-byte.h"
27 
28 #include "detect.h"
29 #include "detect-parse.h"
31 
32 #include "detect-template2.h"
33 
34 /**
35  * \brief Regex for parsing our options
36  */
37 #define PARSE_REGEX "^\\s*([0-9]*)?\\s*([<>=-]+)?\\s*([0-9]+)?\\s*$"
38 
39 static DetectParseRegex parse_regex;
40 
41 /* prototypes */
42 static int DetectTemplate2Match (DetectEngineThreadCtx *, Packet *,
43  const Signature *, const SigMatchCtx *);
44 static int DetectTemplate2Setup (DetectEngineCtx *, Signature *, const char *);
45 void DetectTemplate2Free (DetectEngineCtx *, void *);
46 #ifdef UNITTESTS
48 #endif
49 static int PrefilterSetupTemplate2(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
50 static bool PrefilterTemplate2IsPrefilterable(const Signature *s);
51 
52 /**
53  * \brief Registration function for template2: keyword
54  */
55 
57 {
58  sigmatch_table[DETECT_TEMPLATE2].name = "template2";
59  sigmatch_table[DETECT_TEMPLATE2].desc = "TODO describe the keyword";
60  sigmatch_table[DETECT_TEMPLATE2].url = "/rules/header-keywords.html#template2";
61  sigmatch_table[DETECT_TEMPLATE2].Match = DetectTemplate2Match;
62  sigmatch_table[DETECT_TEMPLATE2].Setup = DetectTemplate2Setup;
64 #ifdef UNITTESTS
66 #endif
67  sigmatch_table[DETECT_TEMPLATE2].SupportsPrefilter = PrefilterTemplate2IsPrefilterable;
68  sigmatch_table[DETECT_TEMPLATE2].SetupPrefilter = PrefilterSetupTemplate2;
69 
70  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
71  return;
72 }
73 
74 static inline int Template2Match(const uint8_t parg, const uint8_t mode,
75  const uint8_t darg1, const uint8_t darg2)
76 {
77  if (mode == DETECT_TEMPLATE2_EQ && parg == darg1)
78  return 1;
79  else if (mode == DETECT_TEMPLATE2_LT && parg < darg1)
80  return 1;
81  else if (mode == DETECT_TEMPLATE2_GT && parg > darg1)
82  return 1;
83  else if (mode == DETECT_TEMPLATE2_RA && (parg > darg1 && parg < darg2))
84  return 1;
85 
86  return 0;
87 }
88 
89 /**
90  * \brief This function is used to match TEMPLATE2 rule option on a packet with those passed via template2:
91  *
92  * \param t pointer to thread vars
93  * \param det_ctx pointer to the pattern matcher thread
94  * \param p pointer to the current packet
95  * \param m pointer to the sigmatch that we will cast into DetectTemplate2Data
96  *
97  * \retval 0 no match
98  * \retval 1 match
99  */
100 static int DetectTemplate2Match (DetectEngineThreadCtx *det_ctx, Packet *p,
101  const Signature *s, const SigMatchCtx *ctx)
102 {
103 
104  if (PKT_IS_PSEUDOPKT(p))
105  return 0;
106 
107  /* TODO replace this */
108  uint8_t ptemplate2;
109  if (PKT_IS_IPV4(p)) {
110  ptemplate2 = IPV4_GET_IPTTL(p);
111  } else if (PKT_IS_IPV6(p)) {
112  ptemplate2 = IPV6_GET_HLIM(p);
113  } else {
114  SCLogDebug("Packet is of not IPv4 or IPv6");
115  return 0;
116  }
117 
118  const DetectTemplate2Data *template2d = (const DetectTemplate2Data *)ctx;
119  return Template2Match(ptemplate2, template2d->mode, template2d->arg1, template2d->arg2);
120 }
121 
122 /**
123  * \brief This function is used to parse template2 options passed via template2: keyword
124  *
125  * \param template2str Pointer to the user provided template2 options
126  *
127  * \retval template2d pointer to DetectTemplate2Data on success
128  * \retval NULL on failure
129  */
130 
131 static DetectTemplate2Data *DetectTemplate2Parse (const char *template2str)
132 {
133  DetectTemplate2Data *template2d = NULL;
134  char *arg1 = NULL;
135  char *arg2 = NULL;
136  char *arg3 = NULL;
137  int ret = 0, res = 0;
138  size_t pcre2_len;
139 
140  ret = DetectParsePcreExec(&parse_regex, template2str, 0, 0);
141  if (ret < 2 || ret > 4) {
142  SCLogError(SC_ERR_PCRE_MATCH, "parse error, ret %" PRId32 "", ret);
143  goto error;
144  }
145  const char *str_ptr;
146 
147  res = pcre2_substring_get_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
148  if (res < 0) {
149  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_get_bynumber failed");
150  goto error;
151  }
152  arg1 = (char *) str_ptr;
153  SCLogDebug("Arg1 \"%s\"", arg1);
154 
155  if (ret >= 3) {
156  res = pcre2_substring_get_bynumber(
157  parse_regex.match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
158  if (res < 0) {
159  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_get_bynumber failed");
160  goto error;
161  }
162  arg2 = (char *) str_ptr;
163  SCLogDebug("Arg2 \"%s\"", arg2);
164 
165  if (ret >= 4) {
166  res = pcre2_substring_get_bynumber(
167  parse_regex.match, 3, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
168  if (res < 0) {
169  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_get_bynumber failed");
170  goto error;
171  }
172  arg3 = (char *) str_ptr;
173  SCLogDebug("Arg3 \"%s\"", arg3);
174  }
175  }
176 
177  template2d = SCMalloc(sizeof (DetectTemplate2Data));
178  if (unlikely(template2d == NULL))
179  goto error;
180  template2d->arg1 = 0;
181  template2d->arg2 = 0;
182 
183  if (arg2 != NULL) {
184  /*set the values*/
185  switch(arg2[0]) {
186  case '<':
187  if (arg3 == NULL)
188  goto error;
189 
190  template2d->mode = DETECT_TEMPLATE2_LT;
191  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg3) < 0) {
192  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
193  " \"%s\"", arg3);
194  goto error;
195  }
196  SCLogDebug("template2 is %"PRIu8"",template2d->arg1);
197  if (strlen(arg1) > 0)
198  goto error;
199 
200  break;
201  case '>':
202  if (arg3 == NULL)
203  goto error;
204 
205  template2d->mode = DETECT_TEMPLATE2_GT;
206  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg3) < 0) {
207  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
208  " \"%s\"", arg3);
209  goto error;
210  }
211  SCLogDebug("template2 is %"PRIu8"",template2d->arg1);
212  if (strlen(arg1) > 0)
213  goto error;
214 
215  break;
216  case '-':
217  if (arg1 == NULL || strlen(arg1)== 0)
218  goto error;
219  if (arg3 == NULL || strlen(arg3)== 0)
220  goto error;
221 
222  template2d->mode = DETECT_TEMPLATE2_RA;
223  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg1) < 0) {
224  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
225  " \"%s\"", arg1);
226  goto error;
227  }
228  if (StringParseUint8(&template2d->arg2, 10, 0, (const char *)arg3) < 0) {
229  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid second arg:"
230  " \"%s\"", arg3);
231  goto error;
232  }
233  SCLogDebug("template2 is %"PRIu8" to %"PRIu8"",template2d->arg1, template2d->arg2);
234  if (template2d->arg1 >= template2d->arg2) {
235  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid template2 range. ");
236  goto error;
237  }
238  break;
239  default:
240  template2d->mode = DETECT_TEMPLATE2_EQ;
241 
242  if ((arg2 != NULL && strlen(arg2) > 0) ||
243  (arg3 != NULL && strlen(arg3) > 0) ||
244  (arg1 == NULL ||strlen(arg1) == 0))
245  goto error;
246 
247  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg1) < 0) {
248  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
249  " \"%s\"", arg1);
250  goto error;
251  }
252  break;
253  }
254  } else {
255  template2d->mode = DETECT_TEMPLATE2_EQ;
256 
257  if ((arg3 != NULL && strlen(arg3) > 0) ||
258  (arg1 == NULL ||strlen(arg1) == 0))
259  goto error;
260 
261  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg1) < 0) {
262  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
263  " \"%s\"", arg1);
264  goto error;
265  }
266  }
267 
268  pcre2_substring_free((PCRE2_UCHAR8 *)arg1);
269  pcre2_substring_free((PCRE2_UCHAR8 *)arg2);
270  pcre2_substring_free((PCRE2_UCHAR8 *)arg3);
271  return template2d;
272 
273 error:
274  if (template2d)
275  SCFree(template2d);
276  if (arg1)
277  pcre2_substring_free((PCRE2_UCHAR8 *)arg1);
278  if (arg2)
279  pcre2_substring_free((PCRE2_UCHAR8 *)arg2);
280  if (arg3)
281  pcre2_substring_free((PCRE2_UCHAR8 *)arg3);
282  return NULL;
283 }
284 
285 /**
286  * \brief this function is used to add the parsed template2 data into the current signature
287  *
288  * \param de_ctx pointer to the Detection Engine Context
289  * \param s pointer to the Current Signature
290  * \param template2str pointer to the user provided template2 options
291  *
292  * \retval 0 on Success
293  * \retval -1 on Failure
294  */
295 static int DetectTemplate2Setup (DetectEngineCtx *de_ctx, Signature *s, const char *template2str)
296 {
297  DetectTemplate2Data *template2d = DetectTemplate2Parse(template2str);
298  if (template2d == NULL)
299  return -1;
300 
301  SigMatch *sm = SigMatchAlloc();
302  if (sm == NULL) {
303  DetectTemplate2Free(de_ctx, template2d);
304  return -1;
305  }
306 
307  sm->type = DETECT_TEMPLATE2;
308  sm->ctx = (SigMatchCtx *)template2d;
309 
312 
313  return 0;
314 }
315 
316 /**
317  * \brief this function will free memory associated with DetectTemplate2Data
318  *
319  * \param ptr pointer to DetectTemplate2Data
320  */
322 {
323  DetectTemplate2Data *template2d = (DetectTemplate2Data *)ptr;
324  SCFree(template2d);
325 }
326 
327 /* prefilter code */
328 
329 static void
330 PrefilterPacketTemplate2Match(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
331 {
332  if (PKT_IS_PSEUDOPKT(p)) {
333  SCReturn;
334  }
335 
336  uint8_t ptemplate2;
337 /* TODO update */
338  if (PKT_IS_IPV4(p)) {
339  ptemplate2 = IPV4_GET_IPTTL(p);
340  } else if (PKT_IS_IPV6(p)) {
341  ptemplate2 = IPV6_GET_HLIM(p);
342  } else {
343  SCLogDebug("Packet is of not IPv4 or IPv6");
344  return;
345  }
346 
347  /* during setup Suricata will automatically see if there is another
348  * check that can be added: alproto, sport or dport */
349  const PrefilterPacketHeaderCtx *ctx = pectx;
350  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
351  return;
352 
353  /* if we match, add all the sigs that use this prefilter. This means
354  * that these will be inspected further */
355  if (Template2Match(ptemplate2, ctx->v1.u8[0], ctx->v1.u8[1], ctx->v1.u8[2]))
356  {
357  SCLogDebug("packet matches template2/hl %u", ptemplate2);
358  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
359  }
360 }
361 
362 static void
363 PrefilterPacketTemplate2Set(PrefilterPacketHeaderValue *v, void *smctx)
364 {
365  const DetectTemplate2Data *a = smctx;
366  v->u8[0] = a->mode;
367  v->u8[1] = a->arg1;
368  v->u8[2] = a->arg2;
369 }
370 
371 static bool
372 PrefilterPacketTemplate2Compare(PrefilterPacketHeaderValue v, void *smctx)
373 {
374  const DetectTemplate2Data *a = smctx;
375  if (v.u8[0] == a->mode &&
376  v.u8[1] == a->arg1 &&
377  v.u8[2] == a->arg2)
378  return true;
379  return false;
380 }
381 
382 static int PrefilterSetupTemplate2(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
383 {
385  PrefilterPacketTemplate2Set,
386  PrefilterPacketTemplate2Compare,
387  PrefilterPacketTemplate2Match);
388 }
389 
390 static bool PrefilterTemplate2IsPrefilterable(const Signature *s)
391 {
392  const SigMatch *sm;
393  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
394  switch (sm->type) {
395  case DETECT_TEMPLATE2:
396  return true;
397  }
398  }
399  return false;
400 }
401 
402 #ifdef UNITTESTS
403 #include "tests/detect-template2.c"
404 #endif
405 
util-byte.h
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DETECT_TEMPLATE2_EQ
#define DETECT_TEMPLATE2_EQ
Definition: detect-template2.h:28
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our options.
Definition: detect-template2.c:37
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2474
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1267
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1224
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition: decode.h:263
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1425
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DetectTemplate2Data_::mode
uint8_t mode
Definition: detect-template2.h:36
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1167
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
detect-template2.h
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
PrefilterPacketHeaderCtx_::sigs_array
SigIntId * sigs_array
Definition: detect-engine-prefilter-common.h:41
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:22
DetectTemplate2Free
void DetectTemplate2Free(DetectEngineCtx *, void *)
this function will free memory associated with DetectTemplate2Data
Definition: detect-template2.c:321
PrefilterPacketHeaderCtx_::sigs_cnt
uint32_t sigs_cnt
Definition: detect-engine-prefilter-common.h:40
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
DETECT_TEMPLATE2_RA
#define DETECT_TEMPLATE2_RA
Definition: detect-template2.h:30
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
DetectTemplate2Data_::arg1
uint8_t arg1
Definition: detect-template2.h:33
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1255
DetectTemplate2Data_::arg2
uint8_t arg2
Definition: detect-template2.h:34
DETECT_TEMPLATE2_LT
#define DETECT_TEMPLATE2_LT
Definition: detect-template2.h:27
StringParseUint8
int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:359
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:33
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2597
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:325
DetectTemplate2Data_
Definition: detect-template2.h:32
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:89
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
DetectTemplate2RegisterTests
void DetectTemplate2RegisterTests(void)
this function registers unit tests for DetectTemplate2
Definition: detect-template2.c:145
SCReturn
#define SCReturn
Definition: util-debug.h:302
Signature_::flags
uint32_t flags
Definition: detect.h:549
Packet_
Definition: decode.h:427
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:619
DETECT_TEMPLATE2_GT
#define DETECT_TEMPLATE2_GT
Definition: detect-template2.h:29
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1235
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:542
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
PrefilterPacketHeaderCtx_::v1
PrefilterPacketHeaderValue v1
Definition: detect-engine-prefilter-common.h:34
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:316
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
PrefilterSetupPacketHeader
int PrefilterSetupPacketHeader(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:417
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1254
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
DETECT_TEMPLATE2
@ DETECT_TEMPLATE2
Definition: detect-engine-register.h:259
IPV6_GET_HLIM
#define IPV6_GET_HLIM(p)
Definition: decode-ipv6.h:90
PrefilterPacketHeaderValue
Definition: detect-engine-prefilter-common.h:21
detect-engine-prefilter-common.h
detect-template2.c
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:262
DetectTemplate2Register
void DetectTemplate2Register(void)
Registration function for template2: keyword.
Definition: detect-template2.c:56
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
IPV4_GET_IPTTL
#define IPV4_GET_IPTTL(p)
Definition: decode-ipv4.h:145
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223