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 #include "detect-engine-uint.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-helper.h"
39 #include "util-byte.h"
40 
41 static int DetectWindowMatch(DetectEngineThreadCtx *, Packet *,
42  const Signature *, const SigMatchCtx *);
43 static int DetectWindowSetup(DetectEngineCtx *, Signature *, const char *);
44 #ifdef UNITTESTS
45 static void DetectWindowRegisterTests(void);
46 #endif
47 void DetectWindowFree(DetectEngineCtx *, void *);
48 
49 /**
50  * \brief Registration function for window: keyword
51  */
53 {
54  sigmatch_table[DETECT_WINDOW].name = "tcp.window";
56  sigmatch_table[DETECT_WINDOW].desc = "check for a specific TCP window size";
57  sigmatch_table[DETECT_WINDOW].url = "/rules/header-keywords.html#window";
58  sigmatch_table[DETECT_WINDOW].Match = DetectWindowMatch;
59  sigmatch_table[DETECT_WINDOW].Setup = DetectWindowSetup;
62 #ifdef UNITTESTS
63  sigmatch_table[DETECT_WINDOW].RegisterTests = DetectWindowRegisterTests;
64 #endif
65 }
66 
67 /**
68  * \brief This function is used to match the window size on a packet
69  *
70  * \param t pointer to thread vars
71  * \param det_ctx pointer to the pattern matcher thread
72  * \param p pointer to the current packet
73  * \param m pointer to the sigmatch that we will cast into DetectWindowData
74  *
75  * \retval 0 no match
76  * \retval 1 match
77  */
78 static int DetectWindowMatch(DetectEngineThreadCtx *det_ctx, Packet *p,
79  const Signature *s, const SigMatchCtx *ctx)
80 {
81  const DetectU16Data *wd = (const DetectU16Data *)ctx;
82 
84  if (!(PacketIsTCP(p)) || wd == NULL) {
85  return 0;
86  }
87 
88  const uint16_t window = TCP_GET_RAW_WINDOW(PacketGetTCP(p));
89  return DetectU16Match(window, wd);
90 }
91 
92 /**
93  * \brief this function is used to add the parsed window sizedata into the current signature
94  *
95  * \param de_ctx pointer to the Detection Engine Context
96  * \param s pointer to the Current Signature
97  * \param windowstr pointer to the user provided window options
98  *
99  * \retval 0 on Success
100  * \retval -1 on Failure
101  */
102 static int DetectWindowSetup (DetectEngineCtx *de_ctx, Signature *s, const char *windowstr)
103 {
104  DetectU16Data *wd = SCDetectU16Parse(windowstr);
105  if (wd == NULL)
106  return -1;
107 
108  /* Okay so far so good, lets get this into a SigMatch
109  * and put it in the Signature. */
110 
112  de_ctx, s, DETECT_WINDOW, (SigMatchCtx *)wd, DETECT_SM_LIST_MATCH) == NULL) {
114  return -1;
115  }
117  return 0;
118 }
119 
120 /**
121  * \brief this function will free memory associated with DetectWindowData
122  *
123  * \param wd pointer to DetectWindowData
124  */
126 {
127  SCDetectU16Free(ptr);
128 }
129 
130 #ifdef UNITTESTS /* UNITTESTS */
131 
132 /**
133  * \test DetectWindowTestParse01 is a test to make sure that we set the size correctly
134  * when given valid window opt
135  */
136 static int DetectWindowTestParse01 (void)
137 {
138  DetectU16Data *wd = SCDetectU16Parse("35402");
139  FAIL_IF_NULL(wd);
140  FAIL_IF_NOT(wd->arg1 == 35402);
141 
142  DetectWindowFree(NULL, wd);
143  PASS;
144 }
145 
146 /**
147  * \test DetectWindowTestParse02 is a test for setting the window opt negated
148  */
149 static int DetectWindowTestParse02 (void)
150 {
151  DetectU16Data *wd = SCDetectU16Parse("!35402");
152  FAIL_IF_NULL(wd);
153  FAIL_IF_NOT(wd->mode == DetectUintModeNe);
154  FAIL_IF_NOT(wd->arg1 == 35402);
155 
156  DetectWindowFree(NULL, wd);
157  PASS;
158 }
159 
160 /**
161  * \test DetectWindowTestParse03 is a test to check for an empty value
162  */
163 static int DetectWindowTestParse03 (void)
164 {
165  DetectU16Data *wd = SCDetectU16Parse("");
166  FAIL_IF_NOT_NULL(wd);
167  PASS;
168 }
169 
170 /**
171  * \test DetectWindowTestParse03 is a test to check for a big value
172  */
173 static int DetectWindowTestParse04 (void)
174 {
175  DetectU16Data *wd = SCDetectU16Parse("1235402");
176  FAIL_IF_NOT_NULL(wd);
177  PASS;
178 }
179 
180 /**
181  * \test DetectWindowTestPacket01 is a test to check window with constructed packets
182  */
183 static int DetectWindowTestPacket01 (void)
184 {
185  uint8_t *buf = (uint8_t *)"Hi all!";
186  uint16_t buflen = strlen((char *)buf);
187  Packet *p[3];
188  p[0] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
189  p[1] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_TCP);
190  p[2] = UTHBuildPacket((uint8_t *)buf, buflen, IPPROTO_ICMP);
191 
192  FAIL_IF(p[0] == NULL || p[1] == NULL || p[2] == NULL);
193 
194  /* TCP wwindow = 40 */
195  p[0]->l4.hdrs.tcph->th_win = htons(40);
196 
197  /* TCP window = 41 */
198  p[1]->l4.hdrs.tcph->th_win = htons(41);
199 
200  const char *sigs[2];
201  sigs[0]= "alert tcp any any -> any any (msg:\"Testing window 1\"; window:40; sid:1;)";
202  sigs[1]= "alert tcp any any -> any any (msg:\"Testing window 2\"; window:41; sid:2;)";
203 
204  uint32_t sid[2] = {1, 2};
205 
206  uint32_t results[3][2] = {
207  /* packet 0 match sid 1 but should not match sid 2 */
208  {1, 0},
209  /* packet 1 should not match */
210  {0, 1},
211  /* packet 2 should not match */
212  {0, 0} };
213  FAIL_IF(UTHGenericTest(p, 3, sigs, sid, (uint32_t *)results, 2) == 0);
214 
215  UTHFreePackets(p, 3);
216  PASS;
217 }
218 
219 /**
220  * \brief this function registers unit tests for DetectWindow
221  */
222 void DetectWindowRegisterTests(void)
223 {
224  UtRegisterTest("DetectWindowTestParse01", DetectWindowTestParse01);
225  UtRegisterTest("DetectWindowTestParse02", DetectWindowTestParse02);
226  UtRegisterTest("DetectWindowTestParse03", DetectWindowTestParse03);
227  UtRegisterTest("DetectWindowTestParse04", DetectWindowTestParse04);
228  UtRegisterTest("DetectWindowTestPacket01", DetectWindowTestPacket01);
229 }
230 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
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:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
SigTableElmt_::name
const char * name
Definition: detect.h:1542
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1364
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
TCPHdr_::th_win
uint16_t th_win
Definition: decode-tcp.h:156
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectWindowFree
void DetectWindowFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectWindowData
Definition: detect-tcp-window.c:125
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:241
p
Packet * p
Definition: fuzz_dataset.c:30
TCP_GET_RAW_WINDOW
#define TCP_GET_RAW_WINDOW(tcph)
Definition: decode-tcp.h:83
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
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
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
SIGMATCH_INFO_UINT16
#define SIGMATCH_INFO_UINT16
Definition: detect-engine-register.h:344
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectWindowRegister
void DetectWindowRegister(void)
Registration function for window: keyword.
Definition: detect-tcp-window.c:52
DetectEngineThreadCtx_
Definition: detect.h:1316
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:424
detect.h
detect-tcp-window.h
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:118
Signature_::flags
uint32_t flags
Definition: detect.h:693
Packet_
Definition: decode.h:516
Packet_::l4
struct PacketL4 l4
Definition: decode.h:616
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1504
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:355
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
DetectU16Match
int DetectU16Match(const uint16_t parg, const DetectUintData_u16 *du16)
Definition: detect-engine-uint.c:105
SigTableElmt_::alias
const char * alias
Definition: detect.h:1543
suricata-common.h
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:578
PacketL4::L4Hdrs::tcph
TCPHdr * tcph
Definition: decode.h:481
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
PacketL4::hdrs
union PacketL4::L4Hdrs hdrs
DETECT_WINDOW
@ DETECT_WINDOW
Definition: detect-engine-register.h:38
DetectU16Data
DetectUintData_u16 DetectU16Data
Definition: detect-engine-uint.h:42
flow.h
flow-var.h
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:257
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:453