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.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 
121  const DetectStreamSizeData *sd = (const DetectStreamSizeData *)ctx;
122 
123  if (!(PKT_IS_TCP(p)))
124  return 0;
125  if (p->flow == NULL || p->flow->protoctx == NULL)
126  return 0;
127 
128  const TcpSession *ssn = (TcpSession *)p->flow->protoctx;
129 
130  SCReturnInt(DetectStreamSizeMatchAux(sd, ssn));
131 }
132 
133 /**
134  * \brief this function is used to add the parsed stream size data into the current signature
135  *
136  * \param de_ctx pointer to the Detection Engine Context
137  * \param s pointer to the Current Signature
138  * \param streamstr pointer to the user provided stream size options
139  *
140  * \retval 0 on Success
141  * \retval -1 on Failure
142  */
143 static int DetectStreamSizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *streamstr)
144 {
145  DetectStreamSizeData *sd = rs_detect_stream_size_parse(streamstr);
146  if (sd == NULL)
147  return -1;
148 
152  return -1;
153  }
154  return 0;
155 }
156 
157 /**
158  * \brief this function will free memory associated with DetectStreamSizeData
159  *
160  * \param ptr pointer to DetectStreamSizeData
161  */
163 {
164  rs_detect_stream_size_free(ptr);
165 }
166 
167 /* prefilter code */
168 
169 static void PrefilterPacketStreamsizeMatch(
170  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
171 {
172  if (!(PKT_IS_TCP(p)) || PKT_IS_PSEUDOPKT(p))
173  return;
174 
175  if (p->flow == NULL || p->flow->protoctx == NULL)
176  return;
177 
178  /* during setup Suricata will automatically see if there is another
179  * check that can be added: alproto, sport or dport */
180  const PrefilterPacketHeaderCtx *ctx = pectx;
181  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
182  return;
183 
184  DetectStreamSizeData dsd;
185  dsd.du32.mode = ctx->v1.u8[0];
186  dsd.flags = ctx->v1.u8[1];
187  dsd.du32.arg1 = ctx->v1.u32[2];
188  const TcpSession *ssn = (TcpSession *)p->flow->protoctx;
189  /* if we match, add all the sigs that use this prefilter. This means
190  * that these will be inspected further */
191  if (DetectStreamSizeMatchAux(&dsd, ssn)) {
192  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
193  }
194 }
195 
196 static void PrefilterPacketStreamSizeSet(PrefilterPacketHeaderValue *v, void *smctx)
197 {
198  const DetectStreamSizeData *a = smctx;
199  v->u8[0] = a->du32.mode;
200  v->u8[1] = a->flags;
201  v->u32[2] = a->du32.arg1;
202 }
203 
204 static bool PrefilterPacketStreamSizeCompare(PrefilterPacketHeaderValue v, void *smctx)
205 {
206  const DetectStreamSizeData *a = smctx;
207  if (v.u8[0] == a->du32.mode && v.u8[1] == a->flags && v.u32[2] == a->du32.arg1)
208  return true;
209  return false;
210 }
211 
212 static int PrefilterSetupStreamSize(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
213 {
214  return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_STREAM_SIZE, PrefilterPacketStreamSizeSet,
215  PrefilterPacketStreamSizeCompare, PrefilterPacketStreamsizeMatch);
216 }
217 
218 static bool PrefilterStreamSizeIsPrefilterable(const Signature *s)
219 {
220  const SigMatch *sm;
221  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL; sm = sm->next) {
222  switch (sm->type) {
223  case DETECT_STREAM_SIZE:
224  return true;
225  }
226  }
227  return false;
228 }
229 
230 #ifdef UNITTESTS
231 /**
232  * \test DetectStreamSizeParseTest01 is a test to make sure that we parse the
233  * user options correctly, when given valid stream_size options.
234  */
235 
236 static int DetectStreamSizeParseTest01 (void)
237 {
238  int result = 0;
239  DetectStreamSizeData *sd = NULL;
240  sd = rs_detect_stream_size_parse("server,<,6");
241  if (sd != NULL) {
242  if (sd->flags & StreamSizeServer && sd->du32.mode == DETECT_UINT_LT && sd->du32.arg1 == 6)
243  result = 1;
244  DetectStreamSizeFree(NULL, sd);
245  }
246 
247  return result;
248 }
249 
250 /**
251  * \test DetectStreamSizeParseTest02 is a test to make sure that we detect the
252  * invalid stream_size options.
253  */
254 
255 static int DetectStreamSizeParseTest02 (void)
256 {
257  int result = 1;
258  DetectStreamSizeData *sd = NULL;
259  sd = rs_detect_stream_size_parse("invalidoption,<,6");
260  if (sd != NULL) {
261  printf("expected: NULL got 0x%02X %" PRIu32 ": ", sd->flags, sd->du32.arg1);
262  result = 0;
263  DetectStreamSizeFree(NULL, sd);
264  }
265 
266  return result;
267 }
268 
269 /**
270  * \test DetectStreamSizeParseTest03 is a test to make sure that we match the
271  * packet correctly provided valid stream size.
272  */
273 
274 static int DetectStreamSizeParseTest03 (void)
275 {
276 
277  int result = 0;
278  DetectStreamSizeData *sd = NULL;
279  TcpSession ssn;
280  ThreadVars tv;
282  Packet *p = PacketGetFromAlloc();
283  if (unlikely(p == NULL))
284  return 0;
285  Signature s;
286  SigMatch sm;
287  TcpStream client;
288  Flow f;
289  TCPHdr tcph;
290 
291  memset(&ssn, 0, sizeof(TcpSession));
292  memset(&tv, 0, sizeof(ThreadVars));
293  memset(&dtx, 0, sizeof(DetectEngineThreadCtx));
294  memset(&s, 0, sizeof(Signature));
295  memset(&sm, 0, sizeof(SigMatch));
296  memset(&client, 0, sizeof(TcpStream));
297  memset(&f, 0, sizeof(Flow));
298  memset(&tcph, 0, sizeof(TCPHdr));
299 
300  sd = rs_detect_stream_size_parse("client,>,8");
301  if (sd != NULL) {
302  if (!(sd->flags & StreamSizeClient)) {
303  printf("sd->flags not STREAM_SIZE_CLIENT: ");
304  DetectStreamSizeFree(NULL, sd);
305  SCFree(p);
306  return 0;
307  }
308 
309  if (sd->du32.mode != DETECT_UINT_GT) {
310  printf("sd->mode not DETECTSSIZE_GT: ");
311  DetectStreamSizeFree(NULL, sd);
312  SCFree(p);
313  return 0;
314  }
315 
316  if (sd->du32.arg1 != 8) {
317  printf("sd->ssize is %" PRIu32 ", not 8: ", sd->du32.arg1);
318  DetectStreamSizeFree(NULL, sd);
319  SCFree(p);
320  return 0;
321  }
322  } else {
323  printf("sd == NULL: ");
324  SCFree(p);
325  return 0;
326  }
327 
328  client.next_seq = 20;
329  client.isn = 10;
330  ssn.client = client;
331  f.protoctx = &ssn;
332  p->flow = &f;
333  p->tcph = &tcph;
334  sm.ctx = (SigMatchCtx*)sd;
335 
336  result = DetectStreamSizeMatch(&dtx, p, &s, sm.ctx);
337  if (result == 0) {
338  printf("result 0 != 1: ");
339  }
340  DetectStreamSizeFree(NULL, sd);
341  SCFree(p);
342  return result;
343 }
344 
345 /**
346  * \test DetectStreamSizeParseTest04 is a test to make sure that we match the
347  * stream_size against invalid packet parameters.
348  */
349 
350 static int DetectStreamSizeParseTest04 (void)
351 {
352 
353  int result = 0;
354  DetectStreamSizeData *sd = NULL;
355  TcpSession ssn;
356  ThreadVars tv;
358  Packet *p = PacketGetFromAlloc();
359  if (unlikely(p == NULL))
360  return 0;
361  Signature s;
362  SigMatch sm;
363  TcpStream client;
364  Flow f;
365  IPV4Hdr ip4h;
366 
367  memset(&ssn, 0, sizeof(TcpSession));
368  memset(&tv, 0, sizeof(ThreadVars));
369  memset(&dtx, 0, sizeof(DetectEngineThreadCtx));
370  memset(&s, 0, sizeof(Signature));
371  memset(&sm, 0, sizeof(SigMatch));
372  memset(&client, 0, sizeof(TcpStream));
373  memset(&f, 0, sizeof(Flow));
374  memset(&ip4h, 0, sizeof(IPV4Hdr));
375 
376  sd = rs_detect_stream_size_parse(" client , > , 8 ");
377  if (sd != NULL) {
378  if (!(sd->flags & StreamSizeClient) && sd->du32.mode != DETECT_UINT_GT &&
379  sd->du32.arg1 != 8) {
380  SCFree(p);
381  return 0;
382  }
383  } else
384  {
385  SCFree(p);
386  return 0;
387  }
388 
389  client.next_seq = 20;
390  client.isn = 12;
391  ssn.client = client;
392  f.protoctx = &ssn;
393  p->flow = &f;
394  p->ip4h = &ip4h;
395  sm.ctx = (SigMatchCtx*)sd;
396 
397  if (!DetectStreamSizeMatch(&dtx, p, &s, sm.ctx))
398  result = 1;
399 
400  SCFree(p);
401  return result;
402 }
403 
404 /**
405  * \brief this function registers unit tests for DetectStreamSize
406  */
407 void DetectStreamSizeRegisterTests(void)
408 {
409  UtRegisterTest("DetectStreamSizeParseTest01", DetectStreamSizeParseTest01);
410  UtRegisterTest("DetectStreamSizeParseTest02", DetectStreamSizeParseTest02);
411  UtRegisterTest("DetectStreamSizeParseTest03", DetectStreamSizeParseTest03);
412  UtRegisterTest("DetectStreamSizeParseTest04", DetectStreamSizeParseTest04);
413 }
414 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1296
TcpStream_
Definition: stream-tcp-private.h:106
TcpStream_::isn
uint32_t isn
Definition: stream-tcp-private.h:113
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:578
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:1295
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1283
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:1071
stream-tcp.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1445
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
DETECT_UINT_LT
#define DETECT_UINT_LT
Definition: detect-engine-uint.h:37
Flow_
Flow data structure.
Definition: flow.h:350
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1201
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
DETECT_STREAM_SIZE
@ DETECT_STREAM_SIZE
Definition: detect-engine-register.h:99
PrefilterPacketHeaderCtx_::sigs_array
SigIntId * sigs_array
Definition: detect-engine-prefilter-common.h:43
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:24
PrefilterPacketHeaderCtx_::sigs_cnt
uint32_t sigs_cnt
Definition: detect-engine-prefilter-common.h:42
DETECT_UINT_GT
#define DETECT_UINT_GT
Definition: detect-engine-uint.h:32
Flow_::protoctx
void * protoctx
Definition: flow.h:440
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
util-unittest.h
detect-stream_size.h
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1281
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:247
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1092
PrefilterPacketHeaderValue::u32
uint32_t u32[4]
Definition: detect-engine-prefilter-common.h:26
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:351
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:111
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:350
Packet_
Definition: decode.h:436
stream-tcp-private.h
DetectStreamSizeFree
void DetectStreamSizeFree(DetectEngineCtx *de_ctx, void *)
this function will free memory associated with DetectStreamSizeData
Definition: detect-stream_size.c:162
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:544
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:662
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1261
PrefilterPacketHeaderCtx_::v1
PrefilterPacketHeaderValue v1
Definition: detect-engine-prefilter-common.h:36
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:342
IPV4Hdr_
Definition: decode-ipv4.h:72
Packet_::flow
struct Flow_ * flow
Definition: decode.h:475
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:348
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:566
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:295
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
PrefilterSetupPacketHeader
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, 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:417
TcpStream_::next_seq
uint32_t next_seq
Definition: stream-tcp-private.h:114
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:173
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:294
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1280
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:593
SigMatch_
a single match condition for a signature
Definition: detect.h:347
PrefilterPacketHeaderValue
Definition: detect-engine-prefilter-common.h:23
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-engine-prefilter-common.h
TcpSession_
Definition: stream-tcp-private.h:283
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1285