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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/* 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 Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
*
* Implements the window keyword.
*/
#include "suricata-common.h"
#include "decode.h"
#include "detect.h"
#include "detect-parse.h"
#include "detect-tcp-window.h"
#include "flow.h"
#include "flow-var.h"
#include "util-debug.h"
#include "util-unittest.h"
#include "util-unittest-helper.h"
#include "util-byte.h"
/**
* \brief Regex for parsing our window option
*/
#define PARSE_REGEX "^\\s*([!])?\\s*([0-9]{1,9}+)\\s*$"
static DetectParseRegex parse_regex;
static int DetectWindowMatch(DetectEngineThreadCtx *, Packet *,
const Signature *, const SigMatchCtx *);
static int DetectWindowSetup(DetectEngineCtx *, Signature *, const char *);
#ifdef UNITTESTS
static void DetectWindowRegisterTests(void);
#endif
void DetectWindowFree(DetectEngineCtx *, void *);
/**
* \brief Registration function for window: keyword
*/
void DetectWindowRegister (void)
{
sigmatch_table[DETECT_WINDOW].name = "tcp.window";
sigmatch_table[DETECT_WINDOW].alias = "window";
sigmatch_table[DETECT_WINDOW].desc = "check for a specific TCP window size";
sigmatch_table[DETECT_WINDOW].url = "/rules/header-keywords.html#window";
sigmatch_table[DETECT_WINDOW].Match = DetectWindowMatch;
sigmatch_table[DETECT_WINDOW].Setup = DetectWindowSetup;
sigmatch_table[DETECT_WINDOW].Free = DetectWindowFree;
#ifdef UNITTESTS
sigmatch_table[DETECT_WINDOW].RegisterTests = DetectWindowRegisterTests;
#endif
DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
}
/**
* \brief This function is used to match the window size on a packet
*
* \param t pointer to thread vars
* \param det_ctx pointer to the pattern matcher thread
* \param p pointer to the current packet
* \param m pointer to the sigmatch that we will cast into DetectWindowData
*
* \retval 0 no match
* \retval 1 match
*/
static int DetectWindowMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
const Signature *s, const SigMatchCtx *ctx)
{
const DetectWindowData *wd = (const DetectWindowData *)ctx;
if ( !(PKT_IS_TCP(p)) || wd == NULL || PKT_IS_PSEUDOPKT(p)) {
return 0;
}
if ( (!wd->negated && wd->size == TCP_GET_WINDOW(p)) || (wd->negated && wd->size != TCP_GET_WINDOW(p))) {
return 1;
}
return 0;
}
/**
* \brief This function is used to parse window options passed via window: keyword
*
* \param de_ctx Pointer to the detection engine context
* \param windowstr Pointer to the user provided window options (negation! and size)
*
* \retval wd pointer to DetectWindowData on success
* \retval NULL on failure
*/
static DetectWindowData *DetectWindowParse(DetectEngineCtx *de_ctx, const char *windowstr)
{
DetectWindowData *wd = NULL;
int res = 0;
size_t pcre2len;
pcre2_match_data *match = NULL;
int ret = DetectParsePcreExec(&parse_regex, &match, windowstr, 0, 0);
if (ret < 1 || ret > 3) {
SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, windowstr);
goto error;
}
wd = SCMalloc(sizeof(DetectWindowData));
if (unlikely(wd == NULL))
goto error;
if (ret > 1) {
char copy_str[128] = "";
pcre2len = sizeof(copy_str);
res = SC_Pcre2SubstringCopy(match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
if (res < 0) {
SCLogError("pcre2_substring_copy_bynumber failed");
goto error;
}
/* Detect if it's negated */
if (copy_str[0] == '!')
wd->negated = 1;
else
wd->negated = 0;
if (ret > 2) {
pcre2len = sizeof(copy_str);
res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
if (res < 0) {
SCLogError("pcre2_substring_copy_bynumber failed");
goto error;
}
/* Get the window size if it's a valid value (in packets, we
* should alert if this doesn't happen from decode) */
if (StringParseUint16(&wd->size, 10, 0, copy_str) < 0) {
goto error;
}
}
}
pcre2_match_data_free(match);
return wd;
error:
if (match) {
pcre2_match_data_free(match);
}
if (wd != NULL)
DetectWindowFree(de_ctx, wd);
return NULL;
}
/**
* \brief this function is used to add the parsed window sizedata into the current signature
*
* \param de_ctx pointer to the Detection Engine Context
* \param s pointer to the Current Signature
* \param windowstr pointer to the user provided window options
*
* \retval 0 on Success
* \retval -1 on Failure
*/
static int DetectWindowSetup (DetectEngineCtx *de_ctx, Signature *s, const char *windowstr)
{
DetectWindowData *wd = NULL;
SigMatch *sm = NULL;
wd = DetectWindowParse(de_ctx, windowstr);
if (wd == NULL) goto error;
/* Okay so far so good, lets get this into a SigMatch
* and put it in the Signature. */
sm = SigMatchAlloc();
if (sm == NULL)
goto error;
sm->type = DETECT_WINDOW;
sm->ctx = (SigMatchCtx *)wd;
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH);
s->flags |= SIG_FLAG_REQUIRE_PACKET;
return 0;
error:
if (wd != NULL) DetectWindowFree(de_ctx, wd);
if (sm != NULL) SCFree(sm);
return -1;
}
/**
* \brief this function will free memory associated with DetectWindowData
*
* \param wd pointer to DetectWindowData
*/
void DetectWindowFree(DetectEngineCtx *de_ctx, void *ptr)
{
DetectWindowData *wd = (DetectWindowData *)ptr;
SCFree(wd);
}
#ifdef UNITTESTS /* UNITTESTS */
/**
* \test DetectWindowTestParse01 is a test to make sure that we set the size correctly
* when given valid window opt
*/
static int DetectWindowTestParse01 (void)
{
DetectWindowData *wd = NULL;
wd = DetectWindowParse(NULL, "35402");
FAIL_IF_NULL(wd);
FAIL_IF_NOT(wd->size == 35402);
DetectWindowFree(NULL, wd);
PASS;
}
/**
* \test DetectWindowTestParse02 is a test for setting the window opt negated
*/
static int DetectWindowTestParse02 (void)
{
DetectWindowData *wd = NULL;
wd = DetectWindowParse(NULL, "!35402");
FAIL_IF_NULL(wd);
FAIL_IF_NOT(wd->negated == 1);
FAIL_IF_NOT(wd->size == 35402);
DetectWindowFree(NULL, wd);
PASS;
}
/**
* \test DetectWindowTestParse03 is a test to check for an empty value
*/
static int DetectWindowTestParse03 (void)
{
DetectWindowData *wd = NULL;
wd = DetectWindowParse(NULL, "");
FAIL_IF_NOT_NULL(wd);
DetectWindowFree(NULL, wd);
PASS;
}
/**
* \test DetectWindowTestParse03 is a test to check for a big value
*/
static int DetectWindowTestParse04 (void)
{
DetectWindowData *wd = NULL;
wd = DetectWindowParse(NULL, "1235402");
FAIL_IF_NOT_NULL(wd);
DetectWindowFree(NULL, wd);
PASS;
}
/**
* \test DetectWindowTestPacket01 is a test to check window with constructed packets
*/
static int DetectWindowTestPacket01 (void)
{
uint8_t *buf = (uint8_t *)"Hi all!";
uint16_t buflen = strlen((char *)buf);
Packet *p[3];
p[0] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
p[1] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
p[2] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_ICMP);
FAIL_IF(p[0] == NULL || p[1] == NULL || p[2] == NULL);
/* TCP wwindow = 40 */
p[0]->tcph->th_win = htons(40);
/* TCP window = 41 */
p[1]->tcph->th_win = htons(41);
const char *sigs[2];
sigs[0]= "alert tcp any any -> any any (msg:\"Testing window 1\"; window:40; sid:1;)";
sigs[1]= "alert tcp any any -> any any (msg:\"Testing window 2\"; window:41; sid:2;)";
uint32_t sid[2] = {1, 2};
uint32_t results[3][2] = {
/* packet 0 match sid 1 but should not match sid 2 */
{1, 0},
/* packet 1 should not match */
{0, 1},
/* packet 2 should not match */
{0, 0} };
FAIL_IF(UTHGenericTest(p, 3, sigs, sid, (uint32_t *)results, 2) == 0);
UTHFreePackets(p, 3);
PASS;
}
/**
* \brief this function registers unit tests for DetectWindow
*/
void DetectWindowRegisterTests(void)
{
UtRegisterTest("DetectWindowTestParse01", DetectWindowTestParse01);
UtRegisterTest("DetectWindowTestParse02", DetectWindowTestParse02);
UtRegisterTest("DetectWindowTestParse03", DetectWindowTestParse03);
UtRegisterTest("DetectWindowTestParse04", DetectWindowTestParse04);
UtRegisterTest("DetectWindowTestPacket01", DetectWindowTestPacket01);
}
#endif /* UNITTESTS */
|