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 #include "util-unittest-helper.h"
30 
31 #include "detect.h"
32 #include "detect-parse.h"
33 
34 #include "flow.h"
35 #include "detect-stream_size.h"
36 #include "stream-tcp-private.h"
38 #include "detect-engine-uint.h"
39 #include "util-debug.h"
40 #include "util-byte.h"
41 
42 
43 /*prototypes*/
44 static int DetectStreamSizeMatch (DetectEngineThreadCtx *, Packet *,
45  const Signature *, const SigMatchCtx *);
46 static int DetectStreamSizeSetup (DetectEngineCtx *, Signature *, const char *);
48 #ifdef UNITTESTS
49 static void DetectStreamSizeRegisterTests(void);
50 #endif
51 static int PrefilterSetupStreamSize(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
52 static bool PrefilterStreamSizeIsPrefilterable(const Signature *s);
53 
54 /**
55  * \brief Registration function for stream_size: keyword
56  */
57 
59 {
60  sigmatch_table[DETECT_STREAM_SIZE].name = "stream_size";
61  sigmatch_table[DETECT_STREAM_SIZE].desc = "match on amount of bytes of a stream";
62  sigmatch_table[DETECT_STREAM_SIZE].url = "/rules/flow-keywords.html#stream-size";
63  sigmatch_table[DETECT_STREAM_SIZE].Match = DetectStreamSizeMatch;
64  sigmatch_table[DETECT_STREAM_SIZE].Setup = DetectStreamSizeSetup;
66 #ifdef UNITTESTS
67  sigmatch_table[DETECT_STREAM_SIZE].RegisterTests = DetectStreamSizeRegisterTests;
68 #endif
69  sigmatch_table[DETECT_STREAM_SIZE].SupportsPrefilter = PrefilterStreamSizeIsPrefilterable;
70  sigmatch_table[DETECT_STREAM_SIZE].SetupPrefilter = PrefilterSetupStreamSize;
71 }
72 
73 static int DetectStreamSizeMatchAux(const DetectStreamSizeData *sd, const TcpSession *ssn)
74 {
75  int ret = 0;
76  uint32_t csdiff = 0;
77  uint32_t ssdiff = 0;
78 
79  if (sd->flags == StreamSizeServer) {
80  /* get the server stream size */
81  ssdiff = ssn->server.next_seq - ssn->server.isn;
82  ret = DetectU32Match(ssdiff, &sd->du32);
83 
84  } else if (sd->flags == StreamSizeClient) {
85  /* get the client stream size */
86  csdiff = ssn->client.next_seq - ssn->client.isn;
87  ret = DetectU32Match(csdiff, &sd->du32);
88 
89  } else if (sd->flags == StreamSizeBoth) {
90  ssdiff = ssn->server.next_seq - ssn->server.isn;
91  csdiff = ssn->client.next_seq - ssn->client.isn;
92 
93  if (DetectU32Match(ssdiff, &sd->du32) && DetectU32Match(csdiff, &sd->du32))
94  ret = 1;
95 
96  } else if (sd->flags == StreamSizeEither) {
97  ssdiff = ssn->server.next_seq - ssn->server.isn;
98  csdiff = ssn->client.next_seq - ssn->client.isn;
99 
100  if (DetectU32Match(ssdiff, &sd->du32) || DetectU32Match(csdiff, &sd->du32))
101  ret = 1;
102  }
103  return ret;
104 }
105 
106 /**
107  * \brief This function is used to match Stream size rule option on a packet with those passed via
108  * stream_size:
109  *
110  * \param t pointer to thread vars
111  * \param det_ctx pointer to the pattern matcher thread
112  * \param p pointer to the current packet
113  * \param m pointer to the sigmatch that we will cast into DetectStreamSizeData
114  *
115  * \retval 0 no match
116  * \retval 1 match
117  */
118 static int DetectStreamSizeMatch(
119  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
120 {
121  const DetectStreamSizeData *sd = (const DetectStreamSizeData *)ctx;
122 
123  if (!(PacketIsTCP(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 = SCDetectStreamSizeParse(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  SCDetectStreamSizeFree(ptr);
165 }
166 
167 /* prefilter code */
168 
169 static void PrefilterPacketStreamsizeMatch(
170  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
171 {
172  if (!(PacketIsTCP(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  return v.u8[0] == a->du32.mode && v.u8[1] == a->flags && v.u32[2] == a->du32.arg1;
208 }
209 
210 static int PrefilterSetupStreamSize(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
211 {
213  PrefilterPacketStreamSizeSet, PrefilterPacketStreamSizeCompare,
214  PrefilterPacketStreamsizeMatch);
215 }
216 
217 static bool PrefilterStreamSizeIsPrefilterable(const Signature *s)
218 {
219  const SigMatch *sm;
220  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL; sm = sm->next) {
221  switch (sm->type) {
222  case DETECT_STREAM_SIZE:
223  return true;
224  }
225  }
226  return false;
227 }
228 
229 #ifdef UNITTESTS
230 /**
231  * \test DetectStreamSizeParseTest01 is a test to make sure that we parse the
232  * user options correctly, when given valid stream_size options.
233  */
234 
235 static int DetectStreamSizeParseTest01 (void)
236 {
237  int result = 0;
238  DetectStreamSizeData *sd = NULL;
239  sd = SCDetectStreamSizeParse("server,<,6");
240  if (sd != NULL) {
241  if (sd->flags & StreamSizeServer && sd->du32.mode == DETECT_UINT_LT && sd->du32.arg1 == 6)
242  result = 1;
243  DetectStreamSizeFree(NULL, sd);
244  }
245 
246  return result;
247 }
248 
249 /**
250  * \test DetectStreamSizeParseTest02 is a test to make sure that we detect the
251  * invalid stream_size options.
252  */
253 
254 static int DetectStreamSizeParseTest02 (void)
255 {
256  int result = 1;
257  DetectStreamSizeData *sd = NULL;
258  sd = SCDetectStreamSizeParse("invalidoption,<,6");
259  if (sd != NULL) {
260  printf("expected: NULL got 0x%02X %" PRIu32 ": ", sd->flags, sd->du32.arg1);
261  result = 0;
262  DetectStreamSizeFree(NULL, sd);
263  }
264 
265  return result;
266 }
267 
268 /**
269  * \test DetectStreamSizeParseTest03 is a test to make sure that we match the
270  * packet correctly provided valid stream size.
271  */
272 
273 static int DetectStreamSizeParseTest03 (void)
274 {
275  TcpSession ssn;
276  ThreadVars tv;
278  Packet *p = PacketGetFromAlloc();
279  FAIL_IF_NULL(p);
280  Signature s;
281  SigMatch sm;
282  TcpStream client;
283  Flow f;
284  TCPHdr tcph;
285 
286  memset(&ssn, 0, sizeof(TcpSession));
287  memset(&tv, 0, sizeof(ThreadVars));
288  memset(&dtx, 0, sizeof(DetectEngineThreadCtx));
289  memset(&s, 0, sizeof(Signature));
290  memset(&sm, 0, sizeof(SigMatch));
291  memset(&client, 0, sizeof(TcpStream));
292  memset(&f, 0, sizeof(Flow));
293  memset(&tcph, 0, sizeof(TCPHdr));
294 
295  DetectStreamSizeData *sd = SCDetectStreamSizeParse("client,>,8");
296  FAIL_IF_NULL(sd);
297  FAIL_IF_NOT(sd->flags & StreamSizeClient);
298  FAIL_IF_NOT(sd->du32.mode == DETECT_UINT_GT);
299  FAIL_IF_NOT(sd->du32.arg1 == 8);
300 
301  client.next_seq = 20;
302  client.isn = 10;
303  ssn.client = client;
304  f.protoctx = &ssn;
305  p->flow = &f;
306  PacketSetTCP(p, (uint8_t *)&tcph);
307  sm.ctx = (SigMatchCtx*)sd;
308 
309  int result = DetectStreamSizeMatch(&dtx, p, &s, sm.ctx);
310  FAIL_IF_NOT(result == 1);
311  DetectStreamSizeFree(NULL, sd);
312  PacketFree(p);
313  PASS;
314 }
315 
316 /**
317  * \test DetectStreamSizeParseTest04 is a test to make sure that we match the
318  * stream_size against invalid packet parameters.
319  */
320 
321 static int DetectStreamSizeParseTest04 (void)
322 {
323  TcpSession ssn;
324  ThreadVars tv;
326  Packet *p = PacketGetFromAlloc();
327  FAIL_IF_NULL(p);
328  Signature s;
329  SigMatch sm;
330  TcpStream client;
331  Flow f;
332  IPV4Hdr ip4h;
333 
334  memset(&ssn, 0, sizeof(TcpSession));
335  memset(&tv, 0, sizeof(ThreadVars));
336  memset(&dtx, 0, sizeof(DetectEngineThreadCtx));
337  memset(&s, 0, sizeof(Signature));
338  memset(&sm, 0, sizeof(SigMatch));
339  memset(&client, 0, sizeof(TcpStream));
340  memset(&f, 0, sizeof(Flow));
341  memset(&ip4h, 0, sizeof(IPV4Hdr));
342 
343  DetectStreamSizeData *sd = SCDetectStreamSizeParse(" client , > , 8 ");
344  FAIL_IF_NULL(sd);
345  FAIL_IF_NOT(sd->flags & StreamSizeClient);
346  FAIL_IF_NOT(sd->du32.mode == DETECT_UINT_GT);
347  FAIL_IF_NOT(sd->du32.arg1 == 8);
348 
349  client.next_seq = 20;
350  client.isn = 12;
351  ssn.client = client;
352  f.protoctx = &ssn;
353  p->flow = &f;
354  UTHSetIPV4Hdr(p, &ip4h);
355  sm.ctx = (SigMatchCtx*)sd;
356 
357  FAIL_IF(DetectStreamSizeMatch(&dtx, p, &s, sm.ctx));
358 
359  PacketFree(p);
360  DetectStreamSizeFree(NULL, sd);
361  PASS;
362 }
363 
364 /**
365  * \brief this function registers unit tests for DetectStreamSize
366  */
367 void DetectStreamSizeRegisterTests(void)
368 {
369  UtRegisterTest("DetectStreamSizeParseTest01", DetectStreamSizeParseTest01);
370  UtRegisterTest("DetectStreamSizeParseTest02", DetectStreamSizeParseTest02);
371  UtRegisterTest("DetectStreamSizeParseTest03", DetectStreamSizeParseTest03);
372  UtRegisterTest("DetectStreamSizeParseTest04", DetectStreamSizeParseTest04);
373 }
374 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1471
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:646
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:1470
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1455
SigTableElmt_::name
const char * name
Definition: detect.h:1468
stream-tcp.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1638
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:347
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1358
UTHSetIPV4Hdr
void UTHSetIPV4Hdr(Packet *p, IPV4Hdr *ip4h)
Definition: util-unittest-helper.c:126
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:937
DETECT_STREAM_SIZE
@ DETECT_STREAM_SIZE
Definition: detect-engine-register.h:122
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:24
DETECT_UINT_GT
#define DETECT_UINT_GT
Definition: detect-engine-uint.h:32
Flow_::protoctx
void * protoctx
Definition: flow.h:426
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1450
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
detect-stream_size.h
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1453
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:19
DetectEngineThreadCtx_
Definition: detect.h:1252
SIG_MASK_REQUIRE_FLOW
#define SIG_MASK_REQUIRE_FLOW
Definition: detect.h:312
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:387
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:360
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:225
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:359
Packet_
Definition: decode.h:505
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
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:751
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:1430
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
IPV4Hdr_
Definition: decode-ipv4.h:72
Packet_::flow
struct Flow_ * flow
Definition: decode.h:553
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:357
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:297
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
TcpStream_::next_seq
uint32_t next_seq
Definition: stream-tcp-private.h:114
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:264
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:296
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1452
DetectStreamSizeRegister
void DetectStreamSizeRegister(void)
Registration function for stream_size: keyword.
Definition: detect-stream_size.c:58
detect-parse.h
Signature_
Signature container.
Definition: detect.h:672
SigMatch_
a single match condition for a signature
Definition: detect.h:356
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:1457