suricata
detect-stream_size.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 Gurvinder Singh <gurvindersinghdahiya@gmail.com>
22  *
23  * Stream size for the engine.
24  */
25 
26 #include "suricata-common.h"
27 #include "stream-tcp.h"
28 #include "util-unittest-helper.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 
33 #include "flow.h"
34 #include "detect-stream_size.h"
35 #include "stream-tcp-private.h"
37 #include "detect-engine-uint.h"
38 #include "util-debug.h"
39 #include "util-byte.h"
40 
41 
42 /*prototypes*/
43 static int DetectStreamSizeMatch (DetectEngineThreadCtx *, Packet *,
44  const Signature *, const SigMatchCtx *);
45 static int DetectStreamSizeSetup (DetectEngineCtx *, Signature *, const char *);
47 #ifdef UNITTESTS
48 static void DetectStreamSizeRegisterTests(void);
49 #endif
50 static int PrefilterSetupStreamSize(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
51 static bool PrefilterStreamSizeIsPrefilterable(const Signature *s);
52 
53 /**
54  * \brief Registration function for stream_size: keyword
55  */
56 
58 {
59  sigmatch_table[DETECT_STREAM_SIZE].name = "stream_size";
60  sigmatch_table[DETECT_STREAM_SIZE].desc = "match on amount of bytes of a stream";
61  sigmatch_table[DETECT_STREAM_SIZE].url = "/rules/flow-keywords.html#stream-size";
62  sigmatch_table[DETECT_STREAM_SIZE].Match = DetectStreamSizeMatch;
63  sigmatch_table[DETECT_STREAM_SIZE].Setup = DetectStreamSizeSetup;
65 #ifdef UNITTESTS
66  sigmatch_table[DETECT_STREAM_SIZE].RegisterTests = DetectStreamSizeRegisterTests;
67 #endif
68  sigmatch_table[DETECT_STREAM_SIZE].SupportsPrefilter = PrefilterStreamSizeIsPrefilterable;
69  sigmatch_table[DETECT_STREAM_SIZE].SetupPrefilter = PrefilterSetupStreamSize;
70 }
71 
72 static int DetectStreamSizeMatchAux(const DetectStreamSizeData *sd, const TcpSession *ssn)
73 {
74  int ret = 0;
75  uint32_t csdiff = 0;
76  uint32_t ssdiff = 0;
77 
78  if (sd->flags == StreamSizeServer) {
79  /* get the server stream size */
80  ssdiff = ssn->server.next_seq - ssn->server.isn;
81  ret = DetectU32Match(ssdiff, &sd->du32);
82 
83  } else if (sd->flags == StreamSizeClient) {
84  /* get the client stream size */
85  csdiff = ssn->client.next_seq - ssn->client.isn;
86  ret = DetectU32Match(csdiff, &sd->du32);
87 
88  } else if (sd->flags == StreamSizeBoth) {
89  ssdiff = ssn->server.next_seq - ssn->server.isn;
90  csdiff = ssn->client.next_seq - ssn->client.isn;
91 
92  if (DetectU32Match(ssdiff, &sd->du32) && DetectU32Match(csdiff, &sd->du32))
93  ret = 1;
94 
95  } else if (sd->flags == StreamSizeEither) {
96  ssdiff = ssn->server.next_seq - ssn->server.isn;
97  csdiff = ssn->client.next_seq - ssn->client.isn;
98 
99  if (DetectU32Match(ssdiff, &sd->du32) || DetectU32Match(csdiff, &sd->du32))
100  ret = 1;
101  }
102  return ret;
103 }
104 
105 /**
106  * \brief This function is used to match Stream size rule option on a packet with those passed via
107  * stream_size:
108  *
109  * \param t pointer to thread vars
110  * \param det_ctx pointer to the pattern matcher thread
111  * \param p pointer to the current packet
112  * \param m pointer to the sigmatch that we will cast into DetectStreamSizeData
113  *
114  * \retval 0 no match
115  * \retval 1 match
116  */
117 static int DetectStreamSizeMatch(
118  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
119 {
120  const DetectStreamSizeData *sd = (const DetectStreamSizeData *)ctx;
121 
122  if (!(PacketIsTCP(p)))
123  return 0;
124  if (p->flow == NULL || p->flow->protoctx == NULL)
125  return 0;
126 
127  const TcpSession *ssn = (TcpSession *)p->flow->protoctx;
128 
129  SCReturnInt(DetectStreamSizeMatchAux(sd, ssn));
130 }
131 
132 /**
133  * \brief this function is used to add the parsed stream size data into the current signature
134  *
135  * \param de_ctx pointer to the Detection Engine Context
136  * \param s pointer to the Current Signature
137  * \param streamstr pointer to the user provided stream size options
138  *
139  * \retval 0 on Success
140  * \retval -1 on Failure
141  */
142 static int DetectStreamSizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *streamstr)
143 {
144  DetectStreamSizeData *sd = SCDetectStreamSizeParse(streamstr);
145  if (sd == NULL)
146  return -1;
147 
151  return -1;
152  }
153  return 0;
154 }
155 
156 /**
157  * \brief this function will free memory associated with DetectStreamSizeData
158  *
159  * \param ptr pointer to DetectStreamSizeData
160  */
162 {
163  SCDetectStreamSizeFree(ptr);
164 }
165 
166 /* prefilter code */
167 
168 static void PrefilterPacketStreamsizeMatch(
169  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
170 {
171  if (!(PacketIsTCP(p)))
172  return;
173 
174  if (p->flow == NULL || p->flow->protoctx == NULL)
175  return;
176 
177  /* during setup Suricata will automatically see if there is another
178  * check that can be added: alproto, sport or dport */
179  const PrefilterPacketHeaderCtx *ctx = pectx;
180  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
181  return;
182 
183  DetectStreamSizeData dsd;
184  dsd.du32.mode = ctx->v1.u8[0];
185  dsd.flags = ctx->v1.u8[1];
186  dsd.du32.arg1 = ctx->v1.u32[2];
187  const TcpSession *ssn = (TcpSession *)p->flow->protoctx;
188  /* if we match, add all the sigs that use this prefilter. This means
189  * that these will be inspected further */
190  if (DetectStreamSizeMatchAux(&dsd, ssn)) {
191  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
192  }
193 }
194 
195 static void PrefilterPacketStreamSizeSet(PrefilterPacketHeaderValue *v, void *smctx)
196 {
197  const DetectStreamSizeData *a = smctx;
198  v->u8[0] = a->du32.mode;
199  v->u8[1] = a->flags;
200  v->u32[2] = a->du32.arg1;
201 }
202 
203 static bool PrefilterPacketStreamSizeCompare(PrefilterPacketHeaderValue v, void *smctx)
204 {
205  const DetectStreamSizeData *a = smctx;
206  return v.u8[0] == a->du32.mode && v.u8[1] == a->flags && v.u32[2] == a->du32.arg1;
207 }
208 
209 static int PrefilterSetupStreamSize(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
210 {
212  PrefilterPacketStreamSizeSet, PrefilterPacketStreamSizeCompare,
213  PrefilterPacketStreamsizeMatch);
214 }
215 
216 static bool PrefilterStreamSizeIsPrefilterable(const Signature *s)
217 {
218  const SigMatch *sm;
219  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL; sm = sm->next) {
220  switch (sm->type) {
221  case DETECT_STREAM_SIZE:
222  return true;
223  }
224  }
225  return false;
226 }
227 
228 #ifdef UNITTESTS
229 /**
230  * \test DetectStreamSizeParseTest01 is a test to make sure that we parse the
231  * user options correctly, when given valid stream_size options.
232  */
233 
234 static int DetectStreamSizeParseTest01 (void)
235 {
236  int result = 0;
237  DetectStreamSizeData *sd = NULL;
238  sd = SCDetectStreamSizeParse("server,<,6");
239  if (sd != NULL) {
240  if (sd->flags & StreamSizeServer && sd->du32.mode == DETECT_UINT_LT && sd->du32.arg1 == 6)
241  result = 1;
242  DetectStreamSizeFree(NULL, sd);
243  }
244 
245  return result;
246 }
247 
248 /**
249  * \test DetectStreamSizeParseTest02 is a test to make sure that we detect the
250  * invalid stream_size options.
251  */
252 
253 static int DetectStreamSizeParseTest02 (void)
254 {
255  int result = 1;
256  DetectStreamSizeData *sd = NULL;
257  sd = SCDetectStreamSizeParse("invalidoption,<,6");
258  if (sd != NULL) {
259  printf("expected: NULL got 0x%02X %" PRIu32 ": ", sd->flags, sd->du32.arg1);
260  result = 0;
261  DetectStreamSizeFree(NULL, sd);
262  }
263 
264  return result;
265 }
266 
267 /**
268  * \test DetectStreamSizeParseTest03 is a test to make sure that we match the
269  * packet correctly provided valid stream size.
270  */
271 
272 static int DetectStreamSizeParseTest03 (void)
273 {
274  TcpSession ssn;
275  ThreadVars tv;
278  FAIL_IF_NULL(p);
279  Signature s;
280  SigMatch sm;
281  TcpStream client;
282  Flow f;
283  TCPHdr tcph;
284 
285  memset(&ssn, 0, sizeof(TcpSession));
286  memset(&tv, 0, sizeof(ThreadVars));
287  memset(&dtx, 0, sizeof(DetectEngineThreadCtx));
288  memset(&s, 0, sizeof(Signature));
289  memset(&sm, 0, sizeof(SigMatch));
290  memset(&client, 0, sizeof(TcpStream));
291  memset(&f, 0, sizeof(Flow));
292  memset(&tcph, 0, sizeof(TCPHdr));
293 
294  DetectStreamSizeData *sd = SCDetectStreamSizeParse("client,>,8");
295  FAIL_IF_NULL(sd);
296  FAIL_IF_NOT(sd->flags & StreamSizeClient);
297  FAIL_IF_NOT(sd->du32.mode == DETECT_UINT_GT);
298  FAIL_IF_NOT(sd->du32.arg1 == 8);
299 
300  client.next_seq = 20;
301  client.isn = 10;
302  ssn.client = client;
303  f.protoctx = &ssn;
304  p->flow = &f;
305  PacketSetTCP(p, (uint8_t *)&tcph);
306  sm.ctx = (SigMatchCtx*)sd;
307 
308  int result = DetectStreamSizeMatch(&dtx, p, &s, sm.ctx);
309  FAIL_IF_NOT(result == 1);
310  DetectStreamSizeFree(NULL, sd);
311  PacketFree(p);
312  PASS;
313 }
314 
315 /**
316  * \test DetectStreamSizeParseTest04 is a test to make sure that we match the
317  * stream_size against invalid packet parameters.
318  */
319 
320 static int DetectStreamSizeParseTest04 (void)
321 {
322  TcpSession ssn;
323  ThreadVars tv;
326  FAIL_IF_NULL(p);
327  Signature s;
328  SigMatch sm;
329  TcpStream client;
330  Flow f;
331  IPV4Hdr ip4h;
332 
333  memset(&ssn, 0, sizeof(TcpSession));
334  memset(&tv, 0, sizeof(ThreadVars));
335  memset(&dtx, 0, sizeof(DetectEngineThreadCtx));
336  memset(&s, 0, sizeof(Signature));
337  memset(&sm, 0, sizeof(SigMatch));
338  memset(&client, 0, sizeof(TcpStream));
339  memset(&f, 0, sizeof(Flow));
340  memset(&ip4h, 0, sizeof(IPV4Hdr));
341 
342  DetectStreamSizeData *sd = SCDetectStreamSizeParse(" client , > , 8 ");
343  FAIL_IF_NULL(sd);
344  FAIL_IF_NOT(sd->flags & StreamSizeClient);
345  FAIL_IF_NOT(sd->du32.mode == DETECT_UINT_GT);
346  FAIL_IF_NOT(sd->du32.arg1 == 8);
347 
348  client.next_seq = 20;
349  client.isn = 12;
350  ssn.client = client;
351  f.protoctx = &ssn;
352  p->flow = &f;
353  UTHSetIPV4Hdr(p, &ip4h);
354  sm.ctx = (SigMatchCtx*)sd;
355 
356  FAIL_IF(DetectStreamSizeMatch(&dtx, p, &s, sm.ctx));
357 
358  PacketFree(p);
359  DetectStreamSizeFree(NULL, sd);
360  PASS;
361 }
362 
363 /**
364  * \brief this function registers unit tests for DetectStreamSize
365  */
366 void DetectStreamSizeRegisterTests(void)
367 {
368  UtRegisterTest("DetectStreamSizeParseTest01", DetectStreamSizeParseTest01);
369  UtRegisterTest("DetectStreamSizeParseTest02", DetectStreamSizeParseTest02);
370  UtRegisterTest("DetectStreamSizeParseTest03", DetectStreamSizeParseTest03);
371  UtRegisterTest("DetectStreamSizeParseTest04", DetectStreamSizeParseTest04);
372 }
373 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
TcpStream_
Definition: stream-tcp-private.h:106
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
TcpStream_::isn
uint32_t isn
Definition: stream-tcp-private.h:113
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:666
DetectU32Match
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
Definition: detect-engine-uint.c:31
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
stream-tcp.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1730
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
DETECT_UINT_LT
#define DETECT_UINT_LT
Definition: detect-engine-uint.h:37
ssn
TcpSession ssn
Definition: fuzz_dataset.c:31
Flow_
Flow data structure.
Definition: flow.h:359
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1429
UTHSetIPV4Hdr
void UTHSetIPV4Hdr(Packet *p, IPV4Hdr *ip4h)
Definition: util-unittest-helper.c:251
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DETECT_STREAM_SIZE
@ DETECT_STREAM_SIZE
Definition: detect-engine-register.h:129
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:24
p
Packet * p
Definition: fuzz_dataset.c:30
DETECT_UINT_GT
#define DETECT_UINT_GT
Definition: detect-engine-uint.h:32
Flow_::protoctx
void * protoctx
Definition: flow.h:438
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
detect-stream_size.h
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1527
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1316
SIG_MASK_REQUIRE_FLOW
#define SIG_MASK_REQUIRE_FLOW
Definition: detect.h:316
PrefilterPacketHeaderValue::u32
uint32_t u32[4]
Definition: detect-engine-prefilter-common.h:26
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
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:364
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:221
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:118
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:363
Packet_
Definition: decode.h:516
stream-tcp-private.h
DetectStreamSizeFree
void DetectStreamSizeFree(DetectEngineCtx *de_ctx, void *)
this function will free memory associated with DetectStreamSizeData
Definition: detect-stream_size.c:161
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:775
PrefilterSetupPacketHeader
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, SignatureMask mask, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
Definition: detect-engine-prefilter-common.c:470
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
IPV4Hdr_
Definition: decode-ipv4.h:72
Packet_::flow
struct Flow_ * flow
Definition: decode.h:564
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:361
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:297
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
TcpStream_::next_seq
uint32_t next_seq
Definition: stream-tcp-private.h:114
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:260
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:296
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1526
DetectStreamSizeRegister
void DetectStreamSizeRegister(void)
Registration function for stream_size: keyword.
Definition: detect-stream_size.c:57
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
SigMatch_
a single match condition for a signature
Definition: detect.h:360
PrefilterPacketHeaderValue
Definition: detect-engine-prefilter-common.h:23
detect-engine-prefilter-common.h
TcpSession_
Definition: stream-tcp-private.h:283
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
TCPHdr_
Definition: decode-tcp.h:149
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
f
Flow f
Definition: fuzz_dataset.c:32