suricata
detect-flowvar.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 Victor Julien <victor@inliniac.net>
22  *
23  * Simple flowvar content match part of the detection engine.
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 
29 #include "detect.h"
30 #include "detect-parse.h"
31 
32 #include "detect-content.h"
33 #include "threads.h"
34 #include "flow.h"
35 #include "flow-var.h"
36 #include "pkt-var.h"
37 #include "detect-flowvar.h"
38 
39 #include "util-spm.h"
40 #include "util-var-name.h"
41 #include "util-debug.h"
42 #include "util-print.h"
43 #include "util-spm.h"
44 
45 #define PARSE_REGEX "(.*),(.*)"
46 static DetectParseRegex parse_regex;
47 
49  const Signature *, const SigMatchCtx *);
50 static int DetectFlowvarSetup (DetectEngineCtx *, Signature *, const char *);
51 static int DetectFlowvarPostMatch(DetectEngineThreadCtx *det_ctx,
52  Packet *p, const Signature *s, const SigMatchCtx *ctx);
53 static void DetectFlowvarDataFree(DetectEngineCtx *, void *ptr);
54 
56 {
57  sigmatch_table[DETECT_FLOWVAR].name = "flowvar";
59  sigmatch_table[DETECT_FLOWVAR].Setup = DetectFlowvarSetup;
60  sigmatch_table[DETECT_FLOWVAR].Free = DetectFlowvarDataFree;
61 
62  /* post-match for flowvar storage */
63  sigmatch_table[DETECT_FLOWVAR_POSTMATCH].name = "__flowvar__postmatch__";
64  sigmatch_table[DETECT_FLOWVAR_POSTMATCH].Match = DetectFlowvarPostMatch;
66  sigmatch_table[DETECT_FLOWVAR_POSTMATCH].Free = DetectFlowvarDataFree;
67 
68  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
69 }
70 
71 /**
72  * \brief this function will SCFree memory associated with DetectFlowvarData
73  *
74  * \param cd pointer to DetectCotentData
75  */
76 static void DetectFlowvarDataFree(DetectEngineCtx *de_ctx, void *ptr)
77 {
78  if (ptr == NULL)
79  SCReturn;
80 
82 
83  if (fd->name)
84  SCFree(fd->name);
85  if (fd->content)
86  SCFree(fd->content);
87 
88  SCFree(fd);
89 }
90 
91 /*
92  * returns 0: no match
93  * 1: match
94  * -1: error
95  */
96 
98  const Signature *s, const SigMatchCtx *ctx)
99 {
100  int ret = 0;
102 
103  FlowVar *fv = FlowVarGet(p->flow, fd->idx);
104  if (fv != NULL) {
105  uint8_t *ptr = SpmSearch(fv->data.fv_str.value,
106  fv->data.fv_str.value_len,
107  fd->content, fd->content_len);
108  if (ptr != NULL)
109  ret = 1;
110  }
111 
112  return ret;
113 }
114 
115 static int DetectFlowvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
116 {
117  DetectFlowvarData *fd = NULL;
118  SigMatch *sm = NULL;
119  char varname[64], varcontent[64];
120  int ret = 0, res = 0;
121  size_t pcre2len;
122  uint8_t *content = NULL;
123  uint16_t contentlen = 0;
124  uint32_t contentflags = s->init_data->negated ? DETECT_CONTENT_NEGATED : 0;
125 
126  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
127  if (ret != 3) {
128  SCLogError(SC_ERR_PCRE_MATCH, "\"%s\" is not a valid setting for flowvar.", rawstr);
129  return -1;
130  }
131 
132  pcre2len = sizeof(varname);
133  res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)varname, &pcre2len);
134  if (res < 0) {
135  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
136  return -1;
137  }
138 
139  pcre2len = sizeof(varcontent);
140  res = pcre2_substring_copy_bynumber(
141  parse_regex.match, 2, (PCRE2_UCHAR8 *)varcontent, &pcre2len);
142  if (res < 0) {
143  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
144  return -1;
145  }
146 
147  int varcontent_index = 0;
148  if (strlen(varcontent) >= 2) {
149  if (varcontent[0] == '"')
150  varcontent_index++;
151  if (varcontent[strlen(varcontent)-1] == '"')
152  varcontent[strlen(varcontent)-1] = '\0';
153  }
154  SCLogDebug("varcontent %s", &varcontent[varcontent_index]);
155 
156  res = DetectContentDataParse("flowvar", &varcontent[varcontent_index], &content, &contentlen);
157  if (res == -1)
158  goto error;
159 
160  fd = SCMalloc(sizeof(DetectFlowvarData));
161  if (unlikely(fd == NULL))
162  goto error;
163  memset(fd, 0x00, sizeof(*fd));
164 
165  fd->content = SCMalloc(contentlen);
166  if (unlikely(fd->content == NULL))
167  goto error;
168 
169  memcpy(fd->content, content, contentlen);
170  fd->content_len = contentlen;
171  fd->flags = contentflags;
172 
173  fd->name = SCStrdup(varname);
174  if (unlikely(fd->name == NULL))
175  goto error;
177 
178  /* Okay so far so good, lets get this into a SigMatch
179  * and put it in the Signature. */
180  sm = SigMatchAlloc();
181  if (unlikely(sm == NULL))
182  goto error;
183 
184  sm->type = DETECT_FLOWVAR;
185  sm->ctx = (SigMatchCtx *)fd;
186 
188 
189  SCFree(content);
190  return 0;
191 
192 error:
193  if (fd != NULL)
194  DetectFlowvarDataFree(de_ctx, fd);
195  if (sm != NULL)
196  SCFree(sm);
197  if (content != NULL)
198  SCFree(content);
199  return -1;
200 }
201 
202 /** \brief Store flowvar in det_ctx so we can exec it post-match */
204  uint8_t *key, uint16_t key_len,
205  uint8_t *buffer, uint16_t len, int type)
206 {
207  DetectVarList *fs = SCCalloc(1, sizeof(*fs));
208  if (unlikely(fs == NULL))
209  return -1;
210 
211  fs->len = len;
212  fs->type = type;
213  fs->buffer = buffer;
214  fs->key = key;
215  fs->key_len = key_len;
216 
217  fs->next = det_ctx->varlist;
218  det_ctx->varlist = fs;
219  return 0;
220 }
221 
222 /** \brief Store flowvar in det_ctx so we can exec it post-match */
224  uint32_t idx,
225  uint8_t *buffer, uint16_t len, int type)
226 {
227  DetectVarList *fs = det_ctx->varlist;
228 
229  /* first check if we have had a previous match for this idx */
230  for ( ; fs != NULL; fs = fs->next) {
231  if (fs->idx == idx) {
232  /* we're replacing the older store */
233  SCFree(fs->buffer);
234  fs->buffer = NULL;
235  break;
236  }
237  }
238 
239  if (fs == NULL) {
240  fs = SCCalloc(1, sizeof(*fs));
241  if (unlikely(fs == NULL))
242  return -1;
243 
244  fs->idx = idx;
245 
246  fs->next = det_ctx->varlist;
247  det_ctx->varlist = fs;
248  }
249 
250  fs->len = len;
251  fs->type = type;
252  fs->buffer = buffer;
253  return 0;
254 }
255 
256 /** \brief Setup a post-match for flowvar storage
257  * We're piggyback riding the DetectFlowvarData struct
258  */
260 {
261  SigMatch *sm = NULL;
262  DetectFlowvarData *fv = NULL;
263 
264  fv = SCMalloc(sizeof(DetectFlowvarData));
265  if (unlikely(fv == NULL))
266  goto error;
267  memset(fv, 0x00, sizeof(*fv));
268 
269  /* we only need the idx */
270  fv->idx = idx;
271 
272  sm = SigMatchAlloc();
273  if (unlikely(sm == NULL))
274  goto error;
275 
277  sm->ctx = (SigMatchCtx *)fv;
278 
280  return 0;
281 error:
282  if (fv != NULL)
283  DetectFlowvarDataFree(de_ctx, fv);
284  return -1;
285 }
286 
287 /** \internal
288  * \brief post-match func to store flowvars in the flow
289  * \param sm sigmatch containing the idx to store
290  * \retval 1 or -1 in case of error
291  */
292 static int DetectFlowvarPostMatch(
293  DetectEngineThreadCtx *det_ctx,
294  Packet *p, const Signature *s, const SigMatchCtx *ctx)
295 {
296  DetectVarList *fs, *prev;
297  const DetectFlowvarData *fd;
298 
299  if (det_ctx->varlist == NULL)
300  return 1;
301 
302  fd = (const DetectFlowvarData *)ctx;
303 
304  prev = NULL;
305  fs = det_ctx->varlist;
306  while (fs != NULL) {
307  if (fd->idx == 0 || fd->idx == fs->idx) {
308  SCLogDebug("adding to the flow %u:", fs->idx);
309  //PrintRawDataFp(stdout, fs->buffer, fs->len);
310 
311  if (fs->type == DETECT_VAR_TYPE_FLOW_POSTMATCH && p && p->flow) {
312  FlowVarAddIdValue(p->flow, fs->idx, fs->buffer, fs->len);
313  /* memory at fs->buffer is now the responsibility of
314  * the flowvar code. */
315  } else if (fs->type == DETECT_VAR_TYPE_PKT_POSTMATCH && fs->key && p) {
316  /* pkt key/value */
317  if (PktVarAddKeyValue(p, (uint8_t *)fs->key, fs->key_len,
318  (uint8_t *)fs->buffer, fs->len) == -1)
319  {
320  SCFree(fs->key);
321  SCFree(fs->buffer);
322  /* the rest of fs is freed below */
323  }
324  } else if (fs->type == DETECT_VAR_TYPE_PKT_POSTMATCH && p) {
325  if (PktVarAdd(p, fs->idx, fs->buffer, fs->len) == -1) {
326  SCFree(fs->buffer);
327  /* the rest of fs is freed below */
328  }
329  }
330 
331  if (fs == det_ctx->varlist) {
332  det_ctx->varlist = fs->next;
333  SCFree(fs);
334  fs = det_ctx->varlist;
335  } else {
336  prev->next = fs->next;
337  SCFree(fs);
338  fs = prev->next;
339  }
340  } else {
341  prev = fs;
342  fs = fs->next;
343  }
344  }
345  return 1;
346 }
347 
348 /** \brief Handle flowvar candidate list in det_ctx: clean up the list
349  *
350  * Only called from DetectVarProcessList() when varlist is not NULL.
351  */
353 {
355 
356  do {
357  next = fs->next;
358 
359  if (fs->key) {
360  SCFree(fs->key);
361  }
362  SCFree(fs->buffer);
363  SCFree(fs);
364  fs = next;
365  } while (fs != NULL);
366 }
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
detect-content.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
DetectFlowvarPostMatchSetup
int DetectFlowvarPostMatchSetup(DetectEngineCtx *de_ctx, Signature *s, uint32_t idx)
Setup a post-match for flowvar storage We're piggyback riding the DetectFlowvarData struct.
Definition: detect-flowvar.c:259
DetectVarList_::idx
uint32_t idx
Definition: detect.h:689
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2476
DetectVarList_::buffer
uint8_t * buffer
Definition: detect.h:694
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1231
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1241
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectVarStoreMatch
int DetectVarStoreMatch(DetectEngineThreadCtx *det_ctx, uint32_t idx, uint8_t *buffer, uint16_t len, int type)
Store flowvar in det_ctx so we can exec it post-match.
Definition: detect-flowvar.c:223
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
FlowVarTypeStr::value_len
uint16_t value_len
Definition: flow-var.h:39
DetectVarList_::key_len
uint16_t key_len
Definition: detect.h:691
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
DetectVarList_
Definition: detect.h:688
DetectVarList_::len
uint16_t len
Definition: detect.h:690
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
FlowVar_::fv_str
FlowVarTypeStr fv_str
Definition: flow-var.h:57
util-var-name.h
DetectVarProcessListInternal
void DetectVarProcessListInternal(DetectVarList *fs, Flow *f, Packet *p)
Handle flowvar candidate list in det_ctx: clean up the list.
Definition: detect-flowvar.c:352
DETECT_VAR_TYPE_PKT_POSTMATCH
#define DETECT_VAR_TYPE_PKT_POSTMATCH
Definition: detect.h:684
PktVarAdd
int PktVarAdd(Packet *p, uint32_t id, uint8_t *value, uint16_t size)
add a key-value pktvar to the pkt
Definition: pkt-var.c:86
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1226
DetectEngineThreadCtx_::varlist
DetectVarList * varlist
Definition: detect.h:1156
detect-flowvar.h
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:89
FlowVar_::data
union FlowVar_::@111 data
decode.h
util-debug.h
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
type
uint8_t type
Definition: decode-icmpv4.h:0
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectFlowvarData_::flags
uint32_t flags
Definition: detect-flowvar.h:32
DetectEngineThreadCtx_
Definition: detect.h:1034
DETECT_FLOWVAR
@ DETECT_FLOWVAR
Definition: detect-engine-register.h:83
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DETECT_FLOWVAR_POSTMATCH
@ DETECT_FLOWVAR_POSTMATCH
Definition: detect-engine-register.h:84
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2599
SC_ERR_PCRE_COPY_SUBSTRING
@ SC_ERR_PCRE_COPY_SUBSTRING
Definition: util-error.h:358
util-print.h
DetectFlowvarData_
Definition: detect-flowvar.h:27
detect.h
pkt-var.h
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:80
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:317
DetectVarList_::next
struct DetectVarList_ * next
Definition: detect.h:696
SCReturn
#define SCReturn
Definition: util-debug.h:300
Packet_
Definition: decode.h:433
DetectFlowvarData_::name
char * name
Definition: detect-flowvar.h:28
FlowVarAddIdValue
void FlowVarAddIdValue(Flow *f, uint32_t idx, uint8_t *value, uint16_t size)
Definition: flow-var.c:113
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:611
SignatureInitData_::negated
bool negated
Definition: detect.h:495
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1209
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:236
DETECT_VAR_TYPE_FLOW_POSTMATCH
#define DETECT_VAR_TYPE_FLOW_POSTMATCH
Definition: detect.h:683
DetectVarList_::type
int type
Definition: detect.h:692
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:309
DetectContentDataParse
int DetectContentDataParse(const char *keyword, const char *contentstr, uint8_t **pstr, uint16_t *plen)
Parse a content string, ie "abc|DE|fgh".
Definition: detect-content.c:83
FlowVarTypeStr::value
uint8_t * value
Definition: flow-var.h:38
Packet_::flow
struct Flow_ * flow
Definition: decode.h:470
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:315
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:74
DetectFlowvarData_::content
uint8_t * content
Definition: detect-flowvar.h:30
util-spm.h
VarNameStoreSetupAdd
uint32_t VarNameStoreSetupAdd(const char *name, const enum VarTypes type)
add to staging or return existing id if already in there
Definition: util-var-name.c:323
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
DetectFlowvarRegister
void DetectFlowvarRegister(void)
Definition: detect-flowvar.c:55
DetectVarList_::key
uint8_t * key
Definition: detect.h:693
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectFlowvarData_::idx
uint32_t idx
Definition: detect-flowvar.h:29
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
SigMatch_
a single match condition for a signature
Definition: detect.h:314
VAR_TYPE_FLOW_VAR
@ VAR_TYPE_FLOW_VAR
Definition: util-var.h:37
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-flowvar.c:45
DetectVarStoreMatchKeyValue
int DetectVarStoreMatchKeyValue(DetectEngineThreadCtx *det_ctx, uint8_t *key, uint16_t key_len, uint8_t *buffer, uint16_t len, int type)
Store flowvar in det_ctx so we can exec it post-match.
Definition: detect-flowvar.c:203
SpmSearch
#define SpmSearch(text, textlen, needle, needlelen)
Definition: util-spm.h:99
PktVarAddKeyValue
int PktVarAddKeyValue(Packet *p, uint8_t *key, uint16_t ksize, uint8_t *value, uint16_t size)
add a key-value pktvar to the pkt
Definition: pkt-var.c:56
FlowVarGet
FlowVar * FlowVarGet(Flow *f, uint32_t idx)
get the flowvar with index 'idx' from the flow
Definition: flow-var.c:78
DetectFlowvarData_::content_len
uint16_t content_len
Definition: detect-flowvar.h:31
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
flow-var.h
FlowVar_
Definition: flow-var.h:48
DetectFlowvarMatch
int DetectFlowvarMatch(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect-flowvar.c:97
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:350