suricata
detect-tcp-window.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 Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
22  *
23  * Implements the window 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 "detect-tcp-window.h"
33 #include "flow.h"
34 #include "flow-var.h"
35 
36 #include "util-debug.h"
37 #include "util-unittest.h"
38 #include "util-unittest-helper.h"
39 #include "util-byte.h"
40 
41 /**
42  * \brief Regex for parsing our window option
43  */
44 #define PARSE_REGEX "^\\s*([!])?\\s*([0-9]{1,9}+)\\s*$"
45 
46 static DetectParseRegex parse_regex;
47 
48 static int DetectWindowMatch(DetectEngineThreadCtx *, Packet *,
49  const Signature *, const SigMatchCtx *);
50 static int DetectWindowSetup(DetectEngineCtx *, Signature *, const char *);
51 #ifdef UNITTESTS
52 static void DetectWindowRegisterTests(void);
53 #endif
54 void DetectWindowFree(DetectEngineCtx *, void *);
55 
56 /**
57  * \brief Registration function for window: keyword
58  */
60 {
61  sigmatch_table[DETECT_WINDOW].name = "tcp.window";
63  sigmatch_table[DETECT_WINDOW].desc = "check for a specific TCP window size";
64  sigmatch_table[DETECT_WINDOW].url = "/rules/header-keywords.html#window";
65  sigmatch_table[DETECT_WINDOW].Match = DetectWindowMatch;
66  sigmatch_table[DETECT_WINDOW].Setup = DetectWindowSetup;
68 #ifdef UNITTESTS
69  sigmatch_table[DETECT_WINDOW].RegisterTests = DetectWindowRegisterTests;
70 #endif
71  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
72 }
73 
74 /**
75  * \brief This function is used to match the window size on a packet
76  *
77  * \param t pointer to thread vars
78  * \param det_ctx pointer to the pattern matcher thread
79  * \param p pointer to the current packet
80  * \param m pointer to the sigmatch that we will cast into DetectWindowData
81  *
82  * \retval 0 no match
83  * \retval 1 match
84  */
85 static int DetectWindowMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
86  const Signature *s, const SigMatchCtx *ctx)
87 {
88  const DetectWindowData *wd = (const DetectWindowData *)ctx;
89 
90  if ( !(PKT_IS_TCP(p)) || wd == NULL || PKT_IS_PSEUDOPKT(p)) {
91  return 0;
92  }
93 
94  if ( (!wd->negated && wd->size == TCP_GET_WINDOW(p)) || (wd->negated && wd->size != TCP_GET_WINDOW(p))) {
95  return 1;
96  }
97 
98  return 0;
99 }
100 
101 /**
102  * \brief This function is used to parse window options passed via window: keyword
103  *
104  * \param de_ctx Pointer to the detection engine context
105  * \param windowstr Pointer to the user provided window options (negation! and size)
106  *
107  * \retval wd pointer to DetectWindowData on success
108  * \retval NULL on failure
109  */
110 static DetectWindowData *DetectWindowParse(DetectEngineCtx *de_ctx, const char *windowstr)
111 {
112  DetectWindowData *wd = NULL;
113  int res = 0;
114  size_t pcre2len;
115 
116  pcre2_match_data *match = NULL;
117  int ret = DetectParsePcreExec(&parse_regex, &match, windowstr, 0, 0);
118  if (ret < 1 || ret > 3) {
119  SCLogError("pcre_exec parse error, ret %" PRId32 ", string %s", ret, windowstr);
120  goto error;
121  }
122 
123  wd = SCMalloc(sizeof(DetectWindowData));
124  if (unlikely(wd == NULL))
125  goto error;
126 
127  if (ret > 1) {
128  char copy_str[128] = "";
129  pcre2len = sizeof(copy_str);
130  res = SC_Pcre2SubstringCopy(match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
131  if (res < 0) {
132  SCLogError("pcre2_substring_copy_bynumber failed");
133  goto error;
134  }
135 
136  /* Detect if it's negated */
137  if (copy_str[0] == '!')
138  wd->negated = 1;
139  else
140  wd->negated = 0;
141 
142  if (ret > 2) {
143  pcre2len = sizeof(copy_str);
144  res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
145  if (res < 0) {
146  SCLogError("pcre2_substring_copy_bynumber failed");
147  goto error;
148  }
149 
150  /* Get the window size if it's a valid value (in packets, we
151  * should alert if this doesn't happen from decode) */
152  if (StringParseUint16(&wd->size, 10, 0, copy_str) < 0) {
153  goto error;
154  }
155  }
156  }
157 
158  pcre2_match_data_free(match);
159  return wd;
160 
161 error:
162  if (match) {
163  pcre2_match_data_free(match);
164  }
165  if (wd != NULL)
167  return NULL;
168 
169 }
170 
171 /**
172  * \brief this function is used to add the parsed window sizedata into the current signature
173  *
174  * \param de_ctx pointer to the Detection Engine Context
175  * \param s pointer to the Current Signature
176  * \param windowstr pointer to the user provided window options
177  *
178  * \retval 0 on Success
179  * \retval -1 on Failure
180  */
181 static int DetectWindowSetup (DetectEngineCtx *de_ctx, Signature *s, const char *windowstr)
182 {
183  DetectWindowData *wd = NULL;
184 
185  wd = DetectWindowParse(de_ctx, windowstr);
186  if (wd == NULL) goto error;
187 
188  /* Okay so far so good, lets get this into a SigMatch
189  * and put it in the Signature. */
190 
192  NULL) {
193  goto error;
194  }
196 
197  return 0;
198 
199 error:
200  if (wd != NULL)
202  return -1;
203 
204 }
205 
206 /**
207  * \brief this function will free memory associated with DetectWindowData
208  *
209  * \param wd pointer to DetectWindowData
210  */
212 {
213  DetectWindowData *wd = (DetectWindowData *)ptr;
214  SCFree(wd);
215 }
216 
217 #ifdef UNITTESTS /* UNITTESTS */
218 
219 /**
220  * \test DetectWindowTestParse01 is a test to make sure that we set the size correctly
221  * when given valid window opt
222  */
223 static int DetectWindowTestParse01 (void)
224 {
225  DetectWindowData *wd = NULL;
226  wd = DetectWindowParse(NULL, "35402");
227  FAIL_IF_NULL(wd);
228  FAIL_IF_NOT(wd->size == 35402);
229 
230  DetectWindowFree(NULL, wd);
231  PASS;
232 }
233 
234 /**
235  * \test DetectWindowTestParse02 is a test for setting the window opt negated
236  */
237 static int DetectWindowTestParse02 (void)
238 {
239  DetectWindowData *wd = NULL;
240  wd = DetectWindowParse(NULL, "!35402");
241  FAIL_IF_NULL(wd);
242  FAIL_IF_NOT(wd->negated == 1);
243  FAIL_IF_NOT(wd->size == 35402);
244 
245  DetectWindowFree(NULL, wd);
246  PASS;
247 }
248 
249 /**
250  * \test DetectWindowTestParse03 is a test to check for an empty value
251  */
252 static int DetectWindowTestParse03 (void)
253 {
254  DetectWindowData *wd = NULL;
255  wd = DetectWindowParse(NULL, "");
256  FAIL_IF_NOT_NULL(wd);
257 
258  DetectWindowFree(NULL, wd);
259  PASS;
260 }
261 
262 /**
263  * \test DetectWindowTestParse03 is a test to check for a big value
264  */
265 static int DetectWindowTestParse04 (void)
266 {
267  DetectWindowData *wd = NULL;
268  wd = DetectWindowParse(NULL, "1235402");
269  FAIL_IF_NOT_NULL(wd);
270 
271  DetectWindowFree(NULL, wd);
272  PASS;
273 }
274 
275 /**
276  * \test DetectWindowTestPacket01 is a test to check window with constructed packets
277  */
278 static int DetectWindowTestPacket01 (void)
279 {
280  uint8_t *buf = (uint8_t *)"Hi all!";
281  uint16_t buflen = strlen((char *)buf);
282  Packet *p[3];
283  p[0] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
284  p[1] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
285  p[2] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_ICMP);
286 
287  FAIL_IF(p[0] == NULL || p[1] == NULL || p[2] == NULL);
288 
289  /* TCP wwindow = 40 */
290  p[0]->tcph->th_win = htons(40);
291 
292  /* TCP window = 41 */
293  p[1]->tcph->th_win = htons(41);
294 
295  const char *sigs[2];
296  sigs[0]= "alert tcp any any -> any any (msg:\"Testing window 1\"; window:40; sid:1;)";
297  sigs[1]= "alert tcp any any -> any any (msg:\"Testing window 2\"; window:41; sid:2;)";
298 
299  uint32_t sid[2] = {1, 2};
300 
301  uint32_t results[3][2] = {
302  /* packet 0 match sid 1 but should not match sid 2 */
303  {1, 0},
304  /* packet 1 should not match */
305  {0, 1},
306  /* packet 2 should not match */
307  {0, 0} };
308  FAIL_IF(UTHGenericTest(p, 3, sigs, sid, (uint32_t *)results, 2) == 0);
309 
310  UTHFreePackets(p, 3);
311  PASS;
312 }
313 
314 /**
315  * \brief this function registers unit tests for DetectWindow
316  */
317 void DetectWindowRegisterTests(void)
318 {
319  UtRegisterTest("DetectWindowTestParse01", DetectWindowTestParse01);
320  UtRegisterTest("DetectWindowTestParse02", DetectWindowTestParse02);
321  UtRegisterTest("DetectWindowTestParse03", DetectWindowTestParse03);
322  UtRegisterTest("DetectWindowTestParse04", DetectWindowTestParse04);
323  UtRegisterTest("DetectWindowTestPacket01", DetectWindowTestPacket01);
324 }
325 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1296
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1295
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1283
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1293
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1074
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
results
struct DetectRfbSecresult_ results[]
TCPHdr_::th_win
uint16_t th_win
Definition: decode-tcp.h:149
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:337
DetectWindowFree
void DetectWindowFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectWindowData
Definition: detect-tcp-window.c:211
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:340
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2674
DetectWindowData_
Definition: detect-tcp-window.h:21
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
TCP_GET_WINDOW
#define TCP_GET_WINDOW(p)
Definition: decode-tcp.h:116
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:248
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectWindowRegister
void DetectWindowRegister(void)
Registration function for window: keyword.
Definition: detect-tcp-window.c:59
DetectEngineThreadCtx_
Definition: detect.h:1092
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2791
DetectWindowData_::negated
uint8_t negated
Definition: detect-tcp-window.h:22
detect.h
detect-tcp-window.h
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:111
Signature_::flags
uint32_t flags
Definition: detect.h:594
Packet_
Definition: decode.h:437
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1261
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our window option.
Definition: detect-tcp-window.c:44
DetectWindowData_::size
uint16_t size
Definition: detect-tcp-window.h:23
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:342
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
SigTableElmt_::alias
const char * alias
Definition: detect.h:1294
suricata-common.h
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:567
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
UTHGenericTest
int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
UTHGenericTest: function that perform a generic check taking care of as maximum common unittest eleme...
Definition: util-unittest-helper.c:586
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:593
SC_Pcre2SubstringCopy
int SC_Pcre2SubstringCopy(pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
Definition: detect-parse.c:2767
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447
DETECT_WINDOW
@ DETECT_WINDOW
Definition: detect-engine-register.h:38
flow.h
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1285
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:246
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:471