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 
44 #define PARSE_REGEX "(.*),(.*)"
45 static DetectParseRegex parse_regex;
46 
48  const Signature *, const SigMatchCtx *);
49 static int DetectFlowvarSetup (DetectEngineCtx *, Signature *, const char *);
50 static int DetectFlowvarPostMatch(DetectEngineThreadCtx *det_ctx,
51  Packet *p, const Signature *s, const SigMatchCtx *ctx);
52 static void DetectFlowvarDataFree(DetectEngineCtx *, void *ptr);
53 
55 {
56  sigmatch_table[DETECT_FLOWVAR].name = "flowvar";
58  sigmatch_table[DETECT_FLOWVAR].Setup = DetectFlowvarSetup;
59  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;
68 
69  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
70 }
71 
72 /**
73  * \brief this function will SCFree memory associated with DetectFlowvarData
74  *
75  * \param cd pointer to DetectCotentData
76  */
77 static void DetectFlowvarDataFree(DetectEngineCtx *de_ctx, void *ptr)
78 {
79  if (ptr == NULL)
80  SCReturn;
81 
83 
84  if (fd->name)
85  SCFree(fd->name);
86  if (fd->content)
87  SCFree(fd->content);
88 
89  SCFree(fd);
90 }
91 
92 /*
93  * returns 0: no match
94  * 1: match
95  * -1: error
96  */
97 
99  const Signature *s, const SigMatchCtx *ctx)
100 {
101  int ret = 0;
103 
104  FlowVar *fv = FlowVarGet(p->flow, fd->idx);
105  if (fv != NULL) {
106  uint8_t *ptr = SpmSearch(fv->data.fv_str.value,
107  fv->data.fv_str.value_len,
108  fd->content, fd->content_len);
109  if (ptr != NULL)
110  ret = 1;
111  }
112 
113  return ret;
114 }
115 
116 static int DetectFlowvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
117 {
118  DetectFlowvarData *fd = NULL;
119  SigMatch *sm = NULL;
120  char varname[64], varcontent[64];
121 #define MAX_SUBSTRINGS 30
122  int ret = 0, res = 0;
123  int ov[MAX_SUBSTRINGS];
124  uint8_t *content = NULL;
125  uint16_t contentlen = 0;
126  uint32_t contentflags = s->init_data->negated ? DETECT_CONTENT_NEGATED : 0;
127 
128  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
129  if (ret != 3) {
130  SCLogError(SC_ERR_PCRE_MATCH, "\"%s\" is not a valid setting for flowvar.", rawstr);
131  return -1;
132  }
133 
134  res = pcre_copy_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 1, varname, sizeof(varname));
135  if (res < 0) {
136  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre_copy_substring failed");
137  return -1;
138  }
139 
140  res = pcre_copy_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 2, varcontent, sizeof(varcontent));
141  if (res < 0) {
142  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre_copy_substring failed");
143  return -1;
144  }
145 
146  int varcontent_index = 0;
147  if (strlen(varcontent) >= 2) {
148  if (varcontent[0] == '"')
149  varcontent_index++;
150  if (varcontent[strlen(varcontent)-1] == '"')
151  varcontent[strlen(varcontent)-1] = '\0';
152  }
153  SCLogDebug("varcontent %s", &varcontent[varcontent_index]);
154 
155  res = DetectContentDataParse("flowvar", &varcontent[varcontent_index], &content, &contentlen);
156  if (res == -1)
157  goto error;
158 
159  fd = SCMalloc(sizeof(DetectFlowvarData));
160  if (unlikely(fd == NULL))
161  goto error;
162  memset(fd, 0x00, sizeof(*fd));
163 
164  fd->content = SCMalloc(contentlen);
165  if (unlikely(fd->content == NULL))
166  goto error;
167 
168  memcpy(fd->content, content, contentlen);
169  fd->content_len = contentlen;
170  fd->flags = contentflags;
171 
172  fd->name = SCStrdup(varname);
173  if (unlikely(fd->name == NULL))
174  goto error;
176 
177  /* Okay so far so good, lets get this into a SigMatch
178  * and put it in the Signature. */
179  sm = SigMatchAlloc();
180  if (unlikely(sm == NULL))
181  goto error;
182 
183  sm->type = DETECT_FLOWVAR;
184  sm->ctx = (SigMatchCtx *)fd;
185 
187 
188  SCFree(content);
189  return 0;
190 
191 error:
192  if (fd != NULL)
193  DetectFlowvarDataFree(de_ctx, fd);
194  if (sm != NULL)
195  SCFree(sm);
196  if (content != NULL)
197  SCFree(content);
198  return -1;
199 }
200 
201 /** \brief Store flowvar in det_ctx so we can exec it post-match */
203  uint8_t *key, uint16_t key_len,
204  uint8_t *buffer, uint16_t len, int type)
205 {
206  DetectVarList *fs = SCCalloc(1, sizeof(*fs));
207  if (unlikely(fs == NULL))
208  return -1;
209 
210  fs->len = len;
211  fs->type = type;
212  fs->buffer = buffer;
213  fs->key = key;
214  fs->key_len = key_len;
215 
216  fs->next = det_ctx->varlist;
217  det_ctx->varlist = fs;
218  return 0;
219 }
220 
221 /** \brief Store flowvar in det_ctx so we can exec it post-match */
223  uint32_t idx,
224  uint8_t *buffer, uint16_t len, int type)
225 {
226  DetectVarList *fs = det_ctx->varlist;
227 
228  /* first check if we have had a previous match for this idx */
229  for ( ; fs != NULL; fs = fs->next) {
230  if (fs->idx == idx) {
231  /* we're replacing the older store */
232  SCFree(fs->buffer);
233  fs->buffer = NULL;
234  break;
235  }
236  }
237 
238  if (fs == NULL) {
239  fs = SCCalloc(1, sizeof(*fs));
240  if (unlikely(fs == NULL))
241  return -1;
242 
243  fs->idx = idx;
244 
245  fs->next = det_ctx->varlist;
246  det_ctx->varlist = fs;
247  }
248 
249  fs->len = len;
250  fs->type = type;
251  fs->buffer = buffer;
252  return 0;
253 }
254 
255 /** \brief Setup a post-match for flowvar storage
256  * We're piggyback riding the DetectFlowvarData struct
257  */
259 {
260  SigMatch *sm = NULL;
261  DetectFlowvarData *fv = NULL;
262 
263  fv = SCMalloc(sizeof(DetectFlowvarData));
264  if (unlikely(fv == NULL))
265  goto error;
266  memset(fv, 0x00, sizeof(*fv));
267 
268  /* we only need the idx */
269  fv->idx = idx;
270 
271  sm = SigMatchAlloc();
272  if (unlikely(sm == NULL))
273  goto error;
274 
276  sm->ctx = (SigMatchCtx *)fv;
277 
279  return 0;
280 error:
281  if (fv != NULL)
282  DetectFlowvarDataFree(de_ctx, fv);
283  return -1;
284 }
285 
286 /** \internal
287  * \brief post-match func to store flowvars in the flow
288  * \param sm sigmatch containing the idx to store
289  * \retval 1 or -1 in case of error
290  */
291 static int DetectFlowvarPostMatch(
292  DetectEngineThreadCtx *det_ctx,
293  Packet *p, const Signature *s, const SigMatchCtx *ctx)
294 {
295  DetectVarList *fs, *prev;
296  const DetectFlowvarData *fd;
297 
298  if (det_ctx->varlist == NULL)
299  return 1;
300 
301  fd = (const DetectFlowvarData *)ctx;
302 
303  prev = NULL;
304  fs = det_ctx->varlist;
305  while (fs != NULL) {
306  if (fd->idx == 0 || fd->idx == fs->idx) {
307  SCLogDebug("adding to the flow %u:", fs->idx);
308  //PrintRawDataFp(stdout, fs->buffer, fs->len);
309 
310  if (fs->type == DETECT_VAR_TYPE_FLOW_POSTMATCH && p && p->flow) {
311  FlowVarAddIdValue(p->flow, fs->idx, fs->buffer, fs->len);
312  /* memory at fs->buffer is now the responsibility of
313  * the flowvar code. */
314  } else if (fs->type == DETECT_VAR_TYPE_PKT_POSTMATCH && fs->key && p) {
315  /* pkt key/value */
316  if (PktVarAddKeyValue(p, (uint8_t *)fs->key, fs->key_len,
317  (uint8_t *)fs->buffer, fs->len) == -1)
318  {
319  SCFree(fs->key);
320  SCFree(fs->buffer);
321  /* the rest of fs is freed below */
322  }
323  } else if (fs->type == DETECT_VAR_TYPE_PKT_POSTMATCH && p) {
324  if (PktVarAdd(p, fs->idx, fs->buffer, fs->len) == -1) {
325  SCFree(fs->buffer);
326  /* the rest of fs is freed below */
327  }
328  }
329 
330  if (fs == det_ctx->varlist) {
331  det_ctx->varlist = fs->next;
332  SCFree(fs);
333  fs = det_ctx->varlist;
334  } else {
335  prev->next = fs->next;
336  SCFree(fs);
337  fs = prev->next;
338  }
339  } else {
340  prev = fs;
341  fs = fs->next;
342  }
343  }
344  return 1;
345 }
346 
347 /** \brief Handle flowvar candidate list in det_ctx: clean up the list
348  *
349  * Only called from DetectVarProcessList() when varlist is not NULL.
350  */
352 {
354 
355  do {
356  next = fs->next;
357 
358  if (fs->key) {
359  SCFree(fs->key);
360  }
361  SCFree(fs->buffer);
362  SCFree(fs);
363  fs = next;
364  } while (fs != NULL);
365 }
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:258
DetectVarList_::idx
uint32_t idx
Definition: detect.h:658
DetectVarList_::buffer
uint8_t * buffer
Definition: detect.h:663
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1200
SigTableElmt_::name
const char * name
Definition: detect.h:1209
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:222
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
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:660
threads.h
Flow_
Flow data structure.
Definition: flow.h:343
DetectVarList_
Definition: detect.h:657
DetectVarList_::len
uint16_t len
Definition: detect.h:659
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
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:351
FlowVar_::data
union FlowVar_::@115 data
DETECT_VAR_TYPE_PKT_POSTMATCH
#define DETECT_VAR_TYPE_PKT_POSTMATCH
Definition: detect.h:653
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:1195
DetectEngineThreadCtx_::varlist
DetectVarList * varlist
Definition: detect.h:1125
detect-flowvar.h
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:98
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
DetectEngineThreadCtx_
Definition: detect.h:1009
DETECT_FLOWVAR
@ DETECT_FLOWVAR
Definition: detect-engine-register.h:81
DetectFlowvarData_::flags
uint8_t flags
Definition: detect-flowvar.h:32
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DETECT_FLOWVAR_POSTMATCH
@ DETECT_FLOWVAR_POSTMATCH
Definition: detect-engine-register.h:82
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2470
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:89
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:322
DetectVarList_::next
struct DetectVarList_ * next
Definition: detect.h:665
SCReturn
#define SCReturn
Definition: util-debug.h:302
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2400
Packet_
Definition: decode.h:411
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
DetectFlowvarData_::content_len
uint8_t content_len
Definition: detect-flowvar.h:31
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:596
SignatureInitData_::negated
bool negated
Definition: detect.h:484
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1178
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
DETECT_VAR_TYPE_FLOW_POSTMATCH
#define DETECT_VAR_TYPE_FLOW_POSTMATCH
Definition: detect.h:652
SigMatch_::type
uint8_t type
Definition: detect.h:320
DetectVarList_::type
int type
Definition: detect.h:661
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:314
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:78
FlowVarTypeStr::value
uint8_t * value
Definition: flow-var.h:38
Packet_::flow
struct Flow_ * flow
Definition: decode.h:448
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
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:257
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:54
DetectVarList_::key
uint8_t * key
Definition: detect.h:662
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:527
SigMatch_
a single match condition for a signature
Definition: detect.h:319
VAR_TYPE_FLOW_VAR
@ VAR_TYPE_FLOW_VAR
Definition: util-var.h:37
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-flowvar.c:44
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:202
SpmSearch
#define SpmSearch(text, textlen, needle, needlelen)
Definition: util-spm.h:101
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
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:98
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1201