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