1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/* Copyright (C) 2007-2020 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*
* Implements the pktvar keyword
*/
#include "suricata-common.h"
#include "decode.h"
#include "detect.h"
#include "detect-parse.h"
#include "threads.h"
#include "pkt-var.h"
#include "detect-pktvar.h"
#include "detect-content.h"
#include "util-spm.h"
#include "util-debug.h"
#include "util-var-name.h"
#define PARSE_REGEX "(.*),(.*)"
static DetectParseRegex parse_regex;
static int DetectPktvarMatch (DetectEngineThreadCtx *, Packet *,
const Signature *, const SigMatchCtx *);
static int DetectPktvarSetup (DetectEngineCtx *, Signature *, const char *);
static void DetectPktvarFree(DetectEngineCtx *, void *data);
void DetectPktvarRegister (void)
{
sigmatch_table[DETECT_PKTVAR].name = "pktvar";
sigmatch_table[DETECT_PKTVAR].Match = DetectPktvarMatch;
sigmatch_table[DETECT_PKTVAR].Setup = DetectPktvarSetup;
sigmatch_table[DETECT_PKTVAR].Free = DetectPktvarFree;
DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
}
/*
* returns 0: no match
* 1: match
* -1: error
*/
static int DetectPktvarMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
const Signature *s, const SigMatchCtx *ctx)
{
int ret = 0;
const DetectPktvarData *pd = (const DetectPktvarData *)ctx;
PktVar *pv = PktVarGet(p, pd->id);
if (pv != NULL) {
uint8_t *ptr = SpmSearch(pv->value, pv->value_len, pd->content, pd->content_len);
if (ptr != NULL)
ret = 1;
}
return ret;
}
static void DetectPktvarFree(DetectEngineCtx *de_ctx, void *ptr)
{
DetectPktvarData *data = ptr;
if (data != NULL) {
VarNameStoreUnregister(data->id, VAR_TYPE_PKT_VAR);
SCFree(data->content);
SCFree(data);
}
}
static int DetectPktvarSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
{
char *varname = NULL, *varcontent = NULL;
int res = 0;
size_t pcre2_len;
uint8_t *content = NULL;
uint16_t len = 0;
pcre2_match_data *match = NULL;
int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
if (ret != 3) {
SCLogError("\"%s\" is not a valid setting for pktvar.", rawstr);
goto error;
}
const char *str_ptr;
res = pcre2_substring_get_bynumber(match, 1, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
if (res < 0) {
SCLogError("pcre2_substring_get_bynumber failed");
goto error;
}
varname = (char *)str_ptr;
res = pcre2_substring_get_bynumber(match, 2, (PCRE2_UCHAR8 **)&str_ptr, &pcre2_len);
if (res < 0) {
pcre2_substring_free((PCRE2_UCHAR8 *)varname);
SCLogError("pcre2_substring_get_bynumber failed");
goto error;
}
varcontent = (char *)str_ptr;
SCLogDebug("varname '%s', varcontent '%s'", varname, varcontent);
char *parse_content;
if (strlen(varcontent) >= 2 && varcontent[0] == '"' &&
varcontent[strlen(varcontent) - 1] == '"')
{
parse_content = varcontent + 1;
varcontent[strlen(varcontent) - 1] = '\0';
} else {
parse_content = varcontent;
}
ret = DetectContentDataParse("pktvar", parse_content, &content, &len);
if (ret == -1 || content == NULL) {
pcre2_substring_free((PCRE2_UCHAR8 *)varname);
pcre2_substring_free((PCRE2_UCHAR8 *)varcontent);
goto error;
}
pcre2_substring_free((PCRE2_UCHAR8 *)varcontent);
DetectPktvarData *cd = SCCalloc(1, sizeof(DetectPktvarData));
if (unlikely(cd == NULL)) {
pcre2_substring_free((PCRE2_UCHAR8 *)varname);
SCFree(content);
goto error;
}
cd->content = content;
cd->content_len = len;
cd->id = VarNameStoreRegister(varname, VAR_TYPE_PKT_VAR);
pcre2_substring_free((PCRE2_UCHAR8 *)varname);
/* Okay so far so good, lets get this into a SigMatch
* and put it in the Signature. */
SigMatch *sm = SigMatchAlloc();
if (unlikely(sm == NULL)) {
DetectPktvarFree(de_ctx, cd);
goto error;
}
sm->type = DETECT_PKTVAR;
sm->ctx = (SigMatchCtx *)cd;
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH);
pcre2_match_data_free(match);
return 0;
error:
if (match) {
pcre2_match_data_free(match);
}
return -1;
}
|