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 "debug.h"
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 
33 #include "detect-tcp-window.h"
34 #include "flow.h"
35 #include "flow-var.h"
36 
37 #include "util-debug.h"
38 #include "util-unittest.h"
39 #include "util-unittest-helper.h"
40 #include "util-byte.h"
41 
42 /**
43  * \brief Regex for parsing our window option
44  */
45 #define PARSE_REGEX "^\\s*([!])?\\s*([0-9]{1,9}+)\\s*$"
46 
47 static DetectParseRegex parse_regex;
48 
49 static int DetectWindowMatch(DetectEngineThreadCtx *, Packet *,
50  const Signature *, const SigMatchCtx *);
51 static int DetectWindowSetup(DetectEngineCtx *, Signature *, const char *);
52 #ifdef UNITTESTS
53 static void DetectWindowRegisterTests(void);
54 #endif
55 void DetectWindowFree(DetectEngineCtx *, void *);
56 
57 /**
58  * \brief Registration function for window: keyword
59  */
61 {
62  sigmatch_table[DETECT_WINDOW].name = "tcp.window";
64  sigmatch_table[DETECT_WINDOW].desc = "check for a specific TCP window size";
65  sigmatch_table[DETECT_WINDOW].url = "/rules/header-keywords.html#window";
66  sigmatch_table[DETECT_WINDOW].Match = DetectWindowMatch;
67  sigmatch_table[DETECT_WINDOW].Setup = DetectWindowSetup;
69 #ifdef UNITTESTS
70  sigmatch_table[DETECT_WINDOW].RegisterTests = DetectWindowRegisterTests;
71 #endif
72  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
73 }
74 
75 /**
76  * \brief This function is used to match the window size on a packet
77  *
78  * \param t pointer to thread vars
79  * \param det_ctx pointer to the pattern matcher thread
80  * \param p pointer to the current packet
81  * \param m pointer to the sigmatch that we will cast into DetectWindowData
82  *
83  * \retval 0 no match
84  * \retval 1 match
85  */
86 static int DetectWindowMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
87  const Signature *s, const SigMatchCtx *ctx)
88 {
89  const DetectWindowData *wd = (const DetectWindowData *)ctx;
90 
91  if ( !(PKT_IS_TCP(p)) || wd == NULL || PKT_IS_PSEUDOPKT(p)) {
92  return 0;
93  }
94 
95  if ( (!wd->negated && wd->size == TCP_GET_WINDOW(p)) || (wd->negated && wd->size != TCP_GET_WINDOW(p))) {
96  return 1;
97  }
98 
99  return 0;
100 }
101 
102 /**
103  * \brief This function is used to parse window options passed via window: keyword
104  *
105  * \param de_ctx Pointer to the detection engine context
106  * \param windowstr Pointer to the user provided window options (negation! and size)
107  *
108  * \retval wd pointer to DetectWindowData on success
109  * \retval NULL on failure
110  */
111 static DetectWindowData *DetectWindowParse(DetectEngineCtx *de_ctx, const char *windowstr)
112 {
113  DetectWindowData *wd = NULL;
114  int ret = 0, res = 0;
115  size_t pcre2len;
116 
117  ret = DetectParsePcreExec(&parse_regex, windowstr, 0, 0);
118  if (ret < 1 || ret > 3) {
119  SCLogError(SC_ERR_PCRE_MATCH, "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(parse_regex.match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
131  if (res < 0) {
132  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "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(
145  parse_regex.match, 2, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
146  if (res < 0) {
147  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_copy_bynumber failed");
148  goto error;
149  }
150 
151  /* Get the window size if it's a valid value (in packets, we
152  * should alert if this doesn't happend from decode) */
153  if (StringParseUint16(&wd->size, 10, 0, copy_str) < 0) {
154  goto error;
155  }
156  }
157  }
158 
159  return wd;
160 
161 error:
162  if (wd != NULL)
164  return NULL;
165 
166 }
167 
168 /**
169  * \brief this function is used to add the parsed window sizedata into the current signature
170  *
171  * \param de_ctx pointer to the Detection Engine Context
172  * \param s pointer to the Current Signature
173  * \param windowstr pointer to the user provided window options
174  *
175  * \retval 0 on Success
176  * \retval -1 on Failure
177  */
178 static int DetectWindowSetup (DetectEngineCtx *de_ctx, Signature *s, const char *windowstr)
179 {
180  DetectWindowData *wd = NULL;
181  SigMatch *sm = NULL;
182 
183  wd = DetectWindowParse(de_ctx, windowstr);
184  if (wd == NULL) goto error;
185 
186  /* Okay so far so good, lets get this into a SigMatch
187  * and put it in the Signature. */
188  sm = SigMatchAlloc();
189  if (sm == NULL)
190  goto error;
191 
192  sm->type = DETECT_WINDOW;
193  sm->ctx = (SigMatchCtx *)wd;
194 
197 
198  return 0;
199 
200 error:
201  if (wd != NULL) DetectWindowFree(de_ctx, wd);
202  if (sm != NULL) SCFree(sm);
203  return -1;
204 
205 }
206 
207 /**
208  * \brief this function will free memory associated with DetectWindowData
209  *
210  * \param wd pointer to DetectWindowData
211  */
213 {
214  DetectWindowData *wd = (DetectWindowData *)ptr;
215  SCFree(wd);
216 }
217 
218 #ifdef UNITTESTS /* UNITTESTS */
219 
220 /**
221  * \test DetectWindowTestParse01 is a test to make sure that we set the size correctly
222  * when given valid window opt
223  */
224 static int DetectWindowTestParse01 (void)
225 {
226  int result = 0;
227  DetectWindowData *wd = NULL;
228  wd = DetectWindowParse(NULL, "35402");
229  if (wd != NULL &&wd->size==35402) {
230  DetectWindowFree(NULL, wd);
231  result = 1;
232  }
233 
234  return result;
235 }
236 
237 /**
238  * \test DetectWindowTestParse02 is a test for setting the window opt negated
239  */
240 static int DetectWindowTestParse02 (void)
241 {
242  int result = 0;
243  DetectWindowData *wd = NULL;
244  wd = DetectWindowParse(NULL, "!35402");
245  if (wd != NULL) {
246  if (wd->negated == 1 && wd->size==35402) {
247  result = 1;
248  } else {
249  printf("expected wd->negated=1 and wd->size=35402\n");
250  }
251  DetectWindowFree(NULL, wd);
252  }
253 
254  return result;
255 }
256 
257 /**
258  * \test DetectWindowTestParse03 is a test to check for an empty value
259  */
260 static int DetectWindowTestParse03 (void)
261 {
262  int result = 0;
263  DetectWindowData *wd = NULL;
264  wd = DetectWindowParse(NULL, "");
265  if (wd == NULL) {
266  result = 1;
267  } else {
268  printf("expected a NULL pointer (It was an empty string)\n");
269  }
270  DetectWindowFree(NULL, wd);
271 
272  return result;
273 }
274 
275 /**
276  * \test DetectWindowTestParse03 is a test to check for a big value
277  */
278 static int DetectWindowTestParse04 (void)
279 {
280  int result = 0;
281  DetectWindowData *wd = NULL;
282  wd = DetectWindowParse(NULL, "1235402");
283  if (wd != NULL) {
284  printf("expected a NULL pointer (It was exceeding the MAX window size)\n");
285  DetectWindowFree(NULL, wd);
286  }else
287  result=1;
288 
289  return result;
290 }
291 
292 /**
293  * \test DetectWindowTestPacket01 is a test to check window with constructed packets
294  */
295 static int DetectWindowTestPacket01 (void)
296 {
297  int result = 0;
298  uint8_t *buf = (uint8_t *)"Hi all!";
299  uint16_t buflen = strlen((char *)buf);
300  Packet *p[3];
301  p[0] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
302  p[1] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
303  p[2] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_ICMP);
304 
305  if (p[0] == NULL || p[1] == NULL ||p[2] == NULL)
306  goto end;
307 
308  /* TCP wwindow = 40 */
309  p[0]->tcph->th_win = htons(40);
310 
311  /* TCP window = 41 */
312  p[1]->tcph->th_win = htons(41);
313 
314  const char *sigs[2];
315  sigs[0]= "alert tcp any any -> any any (msg:\"Testing window 1\"; window:40; sid:1;)";
316  sigs[1]= "alert tcp any any -> any any (msg:\"Testing window 2\"; window:41; sid:2;)";
317 
318  uint32_t sid[2] = {1, 2};
319 
320  uint32_t results[3][2] = {
321  /* packet 0 match sid 1 but should not match sid 2 */
322  {1, 0},
323  /* packet 1 should not match */
324  {0, 1},
325  /* packet 2 should not match */
326  {0, 0} };
327  result = UTHGenericTest(p, 3, sigs, sid, (uint32_t *) results, 2);
328 
329  UTHFreePackets(p, 3);
330 end:
331  return result;
332 }
333 
334 /**
335  * \brief this function registers unit tests for DetectWindow
336  */
337 void DetectWindowRegisterTests(void)
338 {
339  UtRegisterTest("DetectWindowTestParse01", DetectWindowTestParse01);
340  UtRegisterTest("DetectWindowTestParse02", DetectWindowTestParse02);
341  UtRegisterTest("DetectWindowTestParse03", DetectWindowTestParse03);
342  UtRegisterTest("DetectWindowTestParse04", DetectWindowTestParse04);
343  UtRegisterTest("DetectWindowTestPacket01", DetectWindowTestPacket01);
344 }
345 #endif /* UNITTESTS */
util-byte.h
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
SigTableElmt_::url
const char * url
Definition: detect.h:1270
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2474
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1267
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1224
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[]
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:336
DetectWindowFree
void DetectWindowFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectWindowData
Definition: detect-tcp-window.c:212
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:337
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
DetectWindowData_
Definition: detect-tcp-window.h:24
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
util-unittest.h
util-unittest-helper.h
TCP_GET_WINDOW
#define TCP_GET_WINDOW(p)
Definition: decode-tcp.h:116
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:264
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
DetectWindowRegister
void DetectWindowRegister(void)
Registration function for window: keyword.
Definition: detect-tcp-window.c:60
DetectEngineThreadCtx_
Definition: detect.h:1060
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2597
DetectWindowData_::negated
uint8_t negated
Definition: detect-tcp-window.h:25
detect.h
detect-tcp-window.h
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:89
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
Signature_::flags
uint32_t flags
Definition: detect.h:549
Packet_
Definition: decode.h:427
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1235
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our window option.
Definition: detect-tcp-window.c:45
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
DetectWindowData_::size
uint16_t size
Definition: detect-tcp-window.h:26
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:316
SigTableElmt_::alias
const char * alias
Definition: detect.h:1268
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:322
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:547
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
UTHGenericTest
int UTHGenericTest(Packet **pkt, int numpkts, const char *sigs[], uint32_t sids[], uint32_t *results, int numsigs)
UTHGenericTest: function that perfom a generic check taking care of as maximum common unittest elemen...
Definition: util-unittest-helper.c:604
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
SC_Pcre2SubstringCopy
int SC_Pcre2SubstringCopy(pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
Definition: detect-parse.c:2573
DETECT_WINDOW
@ DETECT_WINDOW
Definition: detect-engine-register.h:38
flow.h
flow-var.h
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223
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:468