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