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  int ov[MAX_SUBSTRINGS];
139 
140  ret = DetectParsePcreExec(&parse_regex, template2str, 0, 0, ov, MAX_SUBSTRINGS);
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 = pcre_get_substring((char *) template2str, ov, MAX_SUBSTRINGS, 1, &str_ptr);
148  if (res < 0) {
149  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
150  goto error;
151  }
152  arg1 = (char *) str_ptr;
153  SCLogDebug("Arg1 \"%s\"", arg1);
154 
155  if (ret >= 3) {
156  res = pcre_get_substring((char *) template2str, ov, MAX_SUBSTRINGS, 2, &str_ptr);
157  if (res < 0) {
158  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
159  goto error;
160  }
161  arg2 = (char *) str_ptr;
162  SCLogDebug("Arg2 \"%s\"", arg2);
163 
164  if (ret >= 4) {
165  res = pcre_get_substring((char *) template2str, ov, MAX_SUBSTRINGS, 3, &str_ptr);
166  if (res < 0) {
167  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
168  goto error;
169  }
170  arg3 = (char *) str_ptr;
171  SCLogDebug("Arg3 \"%s\"", arg3);
172  }
173  }
174 
175  template2d = SCMalloc(sizeof (DetectTemplate2Data));
176  if (unlikely(template2d == NULL))
177  goto error;
178  template2d->arg1 = 0;
179  template2d->arg2 = 0;
180 
181  if (arg2 != NULL) {
182  /*set the values*/
183  switch(arg2[0]) {
184  case '<':
185  if (arg3 == NULL)
186  goto error;
187 
188  template2d->mode = DETECT_TEMPLATE2_LT;
189  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg3) < 0) {
190  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
191  " \"%s\"", arg3);
192  goto error;
193  }
194  SCLogDebug("template2 is %"PRIu8"",template2d->arg1);
195  if (strlen(arg1) > 0)
196  goto error;
197 
198  break;
199  case '>':
200  if (arg3 == NULL)
201  goto error;
202 
203  template2d->mode = DETECT_TEMPLATE2_GT;
204  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg3) < 0) {
205  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
206  " \"%s\"", arg3);
207  goto error;
208  }
209  SCLogDebug("template2 is %"PRIu8"",template2d->arg1);
210  if (strlen(arg1) > 0)
211  goto error;
212 
213  break;
214  case '-':
215  if (arg1 == NULL || strlen(arg1)== 0)
216  goto error;
217  if (arg3 == NULL || strlen(arg3)== 0)
218  goto error;
219 
220  template2d->mode = DETECT_TEMPLATE2_RA;
221  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg1) < 0) {
222  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
223  " \"%s\"", arg1);
224  goto error;
225  }
226  if (StringParseUint8(&template2d->arg2, 10, 0, (const char *)arg3) < 0) {
227  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid second arg:"
228  " \"%s\"", arg3);
229  goto error;
230  }
231  SCLogDebug("template2 is %"PRIu8" to %"PRIu8"",template2d->arg1, template2d->arg2);
232  if (template2d->arg1 >= template2d->arg2) {
233  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid template2 range. ");
234  goto error;
235  }
236  break;
237  default:
238  template2d->mode = DETECT_TEMPLATE2_EQ;
239 
240  if ((arg2 != NULL && strlen(arg2) > 0) ||
241  (arg3 != NULL && strlen(arg3) > 0) ||
242  (arg1 == NULL ||strlen(arg1) == 0))
243  goto error;
244 
245  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg1) < 0) {
246  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
247  " \"%s\"", arg1);
248  goto error;
249  }
250  break;
251  }
252  } else {
253  template2d->mode = DETECT_TEMPLATE2_EQ;
254 
255  if ((arg3 != NULL && strlen(arg3) > 0) ||
256  (arg1 == NULL ||strlen(arg1) == 0))
257  goto error;
258 
259  if (StringParseUint8(&template2d->arg1, 10, 0, (const char *)arg1) < 0) {
260  SCLogError(SC_ERR_INVALID_SIGNATURE, "Invalid first arg:"
261  " \"%s\"", arg1);
262  goto error;
263  }
264  }
265 
266  SCFree(arg1);
267  SCFree(arg2);
268  SCFree(arg3);
269  return template2d;
270 
271 error:
272  if (template2d)
273  SCFree(template2d);
274  if (arg1)
275  SCFree(arg1);
276  if (arg2)
277  SCFree(arg2);
278  if (arg3)
279  SCFree(arg3);
280  return NULL;
281 }
282 
283 /**
284  * \brief this function is used to atemplate2d the parsed template2 data into the current signature
285  *
286  * \param de_ctx pointer to the Detection Engine Context
287  * \param s pointer to the Current Signature
288  * \param template2str pointer to the user provided template2 options
289  *
290  * \retval 0 on Success
291  * \retval -1 on Failure
292  */
293 static int DetectTemplate2Setup (DetectEngineCtx *de_ctx, Signature *s, const char *template2str)
294 {
295  DetectTemplate2Data *template2d = DetectTemplate2Parse(template2str);
296  if (template2d == NULL)
297  return -1;
298 
299  SigMatch *sm = SigMatchAlloc();
300  if (sm == NULL) {
301  DetectTemplate2Free(de_ctx, template2d);
302  return -1;
303  }
304 
305  sm->type = DETECT_TEMPLATE2;
306  sm->ctx = (SigMatchCtx *)template2d;
307 
310 
311  return 0;
312 }
313 
314 /**
315  * \brief this function will free memory associated with DetectTemplate2Data
316  *
317  * \param ptr pointer to DetectTemplate2Data
318  */
320 {
321  DetectTemplate2Data *template2d = (DetectTemplate2Data *)ptr;
322  SCFree(template2d);
323 }
324 
325 /* prefilter code */
326 
327 static void
328 PrefilterPacketTemplate2Match(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
329 {
330  if (PKT_IS_PSEUDOPKT(p)) {
331  SCReturn;
332  }
333 
334  uint8_t ptemplate2;
335 /* TODO update */
336  if (PKT_IS_IPV4(p)) {
337  ptemplate2 = IPV4_GET_IPTTL(p);
338  } else if (PKT_IS_IPV6(p)) {
339  ptemplate2 = IPV6_GET_HLIM(p);
340  } else {
341  SCLogDebug("Packet is of not IPv4 or IPv6");
342  return;
343  }
344 
345  /* during setup Suricata will automatically see if there is another
346  * check that can be added: alproto, sport or dport */
347  const PrefilterPacketHeaderCtx *ctx = pectx;
348  if (PrefilterPacketHeaderExtraMatch(ctx, p) == FALSE)
349  return;
350 
351  /* if we match, add all the sigs that use this prefilter. This means
352  * that these will be inspected further */
353  if (Template2Match(ptemplate2, ctx->v1.u8[0], ctx->v1.u8[1], ctx->v1.u8[2]))
354  {
355  SCLogDebug("packet matches template2/hl %u", ptemplate2);
356  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
357  }
358 }
359 
360 static void
361 PrefilterPacketTemplate2Set(PrefilterPacketHeaderValue *v, void *smctx)
362 {
363  const DetectTemplate2Data *a = smctx;
364  v->u8[0] = a->mode;
365  v->u8[1] = a->arg1;
366  v->u8[2] = a->arg2;
367 }
368 
369 static bool
370 PrefilterPacketTemplate2Compare(PrefilterPacketHeaderValue v, void *smctx)
371 {
372  const DetectTemplate2Data *a = smctx;
373  if (v.u8[0] == a->mode &&
374  v.u8[1] == a->arg1 &&
375  v.u8[2] == a->arg2)
376  return TRUE;
377  return FALSE;
378 }
379 
380 static int PrefilterSetupTemplate2(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
381 {
383  PrefilterPacketTemplate2Set,
384  PrefilterPacketTemplate2Compare,
385  PrefilterPacketTemplate2Match);
386 }
387 
388 static bool PrefilterTemplate2IsPrefilterable(const Signature *s)
389 {
390  const SigMatch *sm;
391  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
392  switch (sm->type) {
393  case DETECT_TEMPLATE2:
394  return TRUE;
395  }
396  }
397  return FALSE;
398 }
399 
400 #ifdef UNITTESTS
401 #include "tests/detect-template2.c"
402 #endif
403 
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1213
DETECT_TEMPLATE2_EQ
#define DETECT_TEMPLATE2_EQ
Definition: detect-template2.h:28
SigTableElmt_::desc
const char * desc
Definition: detect.h:1212
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our options.
Definition: detect-template2.c:37
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1200
SigTableElmt_::name
const char * name
Definition: detect.h:1210
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1142
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition: decode.h:256
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1346
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
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:1110
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
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:319
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:1195
DetectTemplate2Data_::arg1
uint8_t arg1
Definition: detect-template2.h:33
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1198
DetectTemplate2Data_::arg2
uint8_t arg2
Definition: detect-template2.h:34
DETECT_TEMPLATE2_LT
#define DETECT_TEMPLATE2_LT
Definition: detect-template2.h:27
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:1009
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2476
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:323
DetectTemplate2Data_
Definition: detect-template2.h:32
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:89
TRUE
#define TRUE
Definition: suricata-common.h:33
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:322
DetectTemplate2RegisterTests
void DetectTemplate2RegisterTests(void)
this function registers unit tests for DetectTemplate2
Definition: detect-template2.c:145
FALSE
#define FALSE
Definition: suricata-common.h:34
SCReturn
#define SCReturn
Definition: util-debug.h:302
Signature_::flags
uint32_t flags
Definition: detect.h:528
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2406
Packet_
Definition: decode.h:412
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:596
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:1178
SignatureInitData_::smlists
struct SigMatch_ ** smlists
Definition: detect.h:521
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SigMatch_::type
uint8_t type
Definition: detect.h:320
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:314
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
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:407
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:1197
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
SigMatch_
a single match condition for a signature
Definition: detect.h:319
DETECT_TEMPLATE2
@ DETECT_TEMPLATE2
Definition: detect-engine-register.h:256
IPV6_GET_HLIM
#define IPV6_GET_HLIM(p)
Definition: decode-ipv6.h:90
StringParseUint8
int StringParseUint8(uint8_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:359
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:255
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:146
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1202
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223