suricata
detect-pktvar.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  * Implements the pktvar keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 
29 #include "detect.h"
30 #include "detect-parse.h"
31 
32 #include "threads.h"
33 #include "pkt-var.h"
34 #include "detect-pktvar.h"
35 #include "detect-content.h"
36 #include "util-spm.h"
37 #include "util-debug.h"
38 #include "util-var-name.h"
39 
40 #define PARSE_REGEX "(.*),(.*)"
41 static DetectParseRegex parse_regex;
42 
43 static int DetectPktvarMatch (DetectEngineThreadCtx *, Packet *,
44  const Signature *, const SigMatchCtx *);
45 static int DetectPktvarSetup (DetectEngineCtx *, Signature *, const char *);
46 static void DetectPktvarFree(DetectEngineCtx *, void *data);
47 
49 {
50  sigmatch_table[DETECT_PKTVAR].name = "pktvar";
51  sigmatch_table[DETECT_PKTVAR].Match = DetectPktvarMatch;
52  sigmatch_table[DETECT_PKTVAR].Setup = DetectPktvarSetup;
53  sigmatch_table[DETECT_PKTVAR].Free = DetectPktvarFree;
54 
55  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
56 }
57 
58 /*
59  * returns 0: no match
60  * 1: match
61  * -1: error
62  */
63 
64 static int DetectPktvarMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
65  const Signature *s, const SigMatchCtx *ctx)
66 {
67  int ret = 0;
68  const DetectPktvarData *pd = (const DetectPktvarData *)ctx;
69 
70  PktVar *pv = PktVarGet(p, pd->id);
71  if (pv != NULL) {
72  uint8_t *ptr = SpmSearch(pv->value, pv->value_len, pd->content, pd->content_len);
73  if (ptr != NULL)
74  ret = 1;
75  }
76 
77  return ret;
78 }
79 
80 static void DetectPktvarFree(DetectEngineCtx *de_ctx, void *ptr)
81 {
82  DetectPktvarData *data = ptr;
83  if (data != NULL) {
84  SCFree(data->content);
85  SCFree(data);
86  }
87 }
88 
89 static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
90 {
91  char *varname = NULL, *varcontent = NULL;
92  int ret = 0, res = 0;
93  size_t pcre2_len;
94  uint8_t *content = NULL;
95  uint16_t len = 0;
96 
97  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
98  if (ret != 3) {
99  SCLogError(SC_ERR_PCRE_MATCH, "\"%s\" is not a valid setting for pktvar.", rawstr);
100  return -1;
101  }
102 
103  const char *str_ptr;
104  res = pcre2_substring_get_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
105  if (res < 0) {
106  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_get_bynumber failed");
107  return -1;
108  }
109  varname = (char *)str_ptr;
110 
111  res = pcre2_substring_get_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
112  if (res < 0) {
113  pcre2_substring_free((PCRE2_UCHAR8 *)varname);
114  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_get_bynumber failed");
115  return -1;
116  }
117  varcontent = (char *)str_ptr;
118 
119  SCLogDebug("varname '%s', varcontent '%s'", varname, varcontent);
120 
121  char *parse_content;
122  if (strlen(varcontent) >= 2 && varcontent[0] == '"' &&
123  varcontent[strlen(varcontent) - 1] == '"')
124  {
125  parse_content = varcontent + 1;
126  varcontent[strlen(varcontent) - 1] = '\0';
127  } else {
128  parse_content = varcontent;
129  }
130 
131  ret = DetectContentDataParse("pktvar", parse_content, &content, &len);
132  if (ret == -1 || content == NULL) {
133  pcre2_substring_free((PCRE2_UCHAR8 *)varname);
134  pcre2_substring_free((PCRE2_UCHAR8 *)varcontent);
135  return -1;
136  }
137  pcre2_substring_free((PCRE2_UCHAR8 *)varcontent);
138 
139  DetectPktvarData *cd = SCCalloc(1, sizeof(DetectPktvarData));
140  if (unlikely(cd == NULL)) {
141  pcre2_substring_free((PCRE2_UCHAR8 *)varname);
142  SCFree(content);
143  return -1;
144  }
145 
146  cd->content = content;
147  cd->content_len = len;
148  cd->id = VarNameStoreSetupAdd(varname, VAR_TYPE_PKT_VAR);
149  pcre2_substring_free((PCRE2_UCHAR8 *)varname);
150 
151  /* Okay so far so good, lets get this into a SigMatch
152  * and put it in the Signature. */
153  SigMatch *sm = SigMatchAlloc();
154  if (unlikely(sm == NULL)) {
155  DetectPktvarFree(de_ctx, cd);
156  return -1;
157  }
158  sm->type = DETECT_PKTVAR;
159  sm->ctx = (SigMatchCtx *)cd;
160 
162  return 0;
163 }
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:47
detect-content.h
PktVarGet
PktVar * PktVarGet(Packet *p, uint32_t id)
Definition: pkt-var.c:40
len
uint8_t len
Definition: app-layer-dnp3.h:2
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2488
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1225
DetectParseRegex
Definition: detect-parse.h:44
SigTableElmt_::name
const char * name
Definition: detect.h:1235
detect-pktvar.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectPktvarData_::id
uint32_t id
Definition: detect-pktvar.h:28
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
threads.h
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:784
util-var-name.h
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
DetectPktvarData_::content_len
uint16_t content_len
Definition: detect-pktvar.h:29
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1220
DetectPktvarData_::content
uint8_t * content
Definition: detect-pktvar.h:31
decode.h
util-debug.h
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:1024
PktVar_::value
uint8_t * value
Definition: decode.h:320
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2611
detect.h
pkt-var.h
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:78
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:316
Packet_
Definition: decode.h:428
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1203
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-pktvar.c:40
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:238
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:308
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
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:314
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
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
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:539
SigMatch_
a single match condition for a signature
Definition: detect.h:313
PktVar_::value_len
uint16_t value_len
Definition: decode.h:318
DetectPktvarRegister
void DetectPktvarRegister(void)
Definition: detect-pktvar.c:48
PktVar_
Definition: decode.h:312
SpmSearch
#define SpmSearch(text, textlen, needle, needlelen)
Definition: util-spm.h:99
DetectPktvarData_
Definition: detect-pktvar.h:27
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:352
VAR_TYPE_PKT_VAR
@ VAR_TYPE_PKT_VAR
Definition: util-var.h:32
DETECT_PKTVAR
@ DETECT_PKTVAR
Definition: detect-engine-register.h:88