suricata
detect-engine-content-inspection.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2023 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 Anoop Saldanha <anoopsaldanha@gmail.com>
22  *
23  * Performs content inspection on any buffer supplied.
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-engine.h"
32 #include "detect-parse.h"
33 
34 #include "rust.h"
35 
36 #include "detect-asn1.h"
37 #include "detect-content.h"
38 #include "detect-pcre.h"
39 #include "detect-isdataat.h"
40 #include "detect-bytetest.h"
41 #include "detect-bytemath.h"
42 #include "detect-bytejump.h"
43 #include "detect-byte-extract.h"
44 #include "detect-replace.h"
46 #include "detect-uricontent.h"
47 #include "detect-urilen.h"
48 #include "detect-engine-uint.h"
49 #include "detect-bsize.h"
50 #include "detect-lua.h"
51 #include "detect-base64-decode.h"
52 #include "detect-base64-data.h"
53 #include "detect-dataset.h"
54 #include "detect-datarep.h"
55 
56 #include "util-spm.h"
57 #include "util-debug.h"
58 #include "util-print.h"
59 #include "util-validate.h"
60 
61 #include "util-unittest.h"
62 #include "util-unittest-helper.h"
63 #include "util-profiling.h"
64 
65 #ifdef HAVE_LUA
66 #include "util-lua.h"
67 #endif
68 
69 #ifdef UNITTESTS
70 thread_local uint32_t ut_inspection_recursion_counter = 0;
71 #endif
72 
74  struct {
75  uint32_t count;
76  const uint32_t limit;
78 };
79 
80 /**
81  * \brief Run the actual payload match functions
82  *
83  * All keywords are evaluated against the buffer with buffer_len.
84  *
85  * For accounting the last match in relative matching the
86  * det_ctx->buffer_offset int is used.
87  *
88  * \param det_ctx Detection engine thread context
89  * \param s Signature to inspect
90  * \param sm SigMatch to inspect
91  * \param p Packet. Can be NULL.
92  * \param f Flow (for pcre flowvar storage)
93  * \param buffer Ptr to the buffer to inspect
94  * \param buffer_len Length of the payload
95  * \param stream_start_offset Indicates the start of the current buffer in
96  * the whole buffer stream inspected. This
97  * applies if the current buffer is inspected
98  * in chunks.
99  * \param inspection_mode Refers to the engine inspection mode we are currently
100  * inspecting. Can be payload, stream, one of the http
101  * buffer inspection modes or dce inspection mode.
102  * \param flags DETECT_CI_FLAG_*
103  *
104  * \retval -1 no match and give up (discontinue matching)
105  * \retval 0 no match
106  * \retval 1 match
107  */
108 static int DetectEngineContentInspectionInternal(DetectEngineThreadCtx *det_ctx,
109  struct DetectEngineContentInspectionCtx *ctx, const Signature *s, const SigMatchData *smd,
110  Packet *p, Flow *f, const uint8_t *buffer, const uint32_t buffer_len,
111  const uint32_t stream_start_offset, const uint8_t flags,
112  const enum DetectContentInspectionType inspection_mode)
113 {
114  SCEnter();
116 
117  ctx->recursion.count++;
118  if (unlikely(ctx->recursion.count == ctx->recursion.limit)) {
119  KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
120  SCReturnInt(-1);
121  }
122 
123  // we want the ability to match on bsize: 0
124  if (smd == NULL || buffer == NULL) {
125  KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
126  SCReturnInt(0);
127  }
128 
129  if (smd->type == DETECT_CONTENT) {
130  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
131  SCLogDebug("inspecting content %"PRIu32" buffer_len %"PRIu32, cd->id, buffer_len);
132 
133  /* we might have already have this content matched by the mpm.
134  * (if there is any other reason why we'd want to avoid checking
135  * it here, please fill it in) */
136  //if (cd->flags & DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED) {
137  // goto match;
138  //}
139 
140  /* rule parsers should take care of this */
141 #ifdef DEBUG
142  BUG_ON(cd->depth != 0 && cd->depth <= cd->offset);
143 #endif
144 
145  /* search for our pattern, checking the matches recursively.
146  * if we match we look for the next SigMatch as well */
147  uint32_t prev_offset = 0; /**< used in recursive searching */
148  uint32_t prev_buffer_offset = det_ctx->buffer_offset;
149 
150  do {
151  uint32_t depth = buffer_len;
152  uint32_t offset = 0;
153  if ((cd->flags & DETECT_CONTENT_DISTANCE) ||
154  (cd->flags & DETECT_CONTENT_WITHIN)) {
155  SCLogDebug("det_ctx->buffer_offset %" PRIu32, det_ctx->buffer_offset);
156  offset = prev_buffer_offset;
157 
158  int distance = cd->distance;
159  if (cd->flags & DETECT_CONTENT_DISTANCE) {
161  distance = det_ctx->byte_values[cd->distance];
162  }
163  if (distance < 0 && (uint32_t)(abs(distance)) > offset)
164  offset = 0;
165  else
166  offset += distance;
167 
168  SCLogDebug("cd->distance %"PRIi32", offset %"PRIu32", depth %"PRIu32,
169  distance, offset, depth);
170  }
171 
172  if (cd->flags & DETECT_CONTENT_WITHIN) {
173  if (cd->flags & DETECT_CONTENT_WITHIN_VAR) {
174  if ((int32_t)depth > (int32_t)(prev_buffer_offset + det_ctx->byte_values[cd->within] + distance)) {
175  depth = prev_buffer_offset + det_ctx->byte_values[cd->within] + distance;
176  }
177  } else {
178  if ((int32_t)depth > (int32_t)(prev_buffer_offset + cd->within + distance)) {
179  depth = prev_buffer_offset + cd->within + distance;
180  }
181 
182  SCLogDebug("cd->within %"PRIi32", det_ctx->buffer_offset %"PRIu32", depth %"PRIu32,
183  cd->within, prev_buffer_offset, depth);
184  }
185 
186  if (stream_start_offset != 0 && prev_buffer_offset == 0) {
187  if (depth <= stream_start_offset) {
188  goto no_match;
189  } else if (depth >= (stream_start_offset + buffer_len)) {
190  ;
191  } else {
192  depth = depth - stream_start_offset;
193  }
194  }
195  }
196 
197  if (cd->flags & DETECT_CONTENT_DEPTH_VAR) {
198  if ((det_ctx->byte_values[cd->depth] + prev_buffer_offset) < depth) {
199  depth = prev_buffer_offset + det_ctx->byte_values[cd->depth];
200  }
201  } else {
202  if (cd->depth != 0) {
203  if ((cd->depth + prev_buffer_offset) < depth) {
204  depth = prev_buffer_offset + cd->depth;
205  }
206 
207  SCLogDebug("cd->depth %"PRIu32", depth %"PRIu32, cd->depth, depth);
208  }
209  }
210 
211  if (cd->flags & DETECT_CONTENT_OFFSET_VAR) {
212  if (det_ctx->byte_values[cd->offset] > offset)
213  offset = det_ctx->byte_values[cd->offset];
214  } else {
215  if (cd->offset > offset) {
216  offset = cd->offset;
217  SCLogDebug("setting offset %"PRIu32, offset);
218  }
219  }
220  } else { /* implied no relative matches */
221  /* set depth */
222  if (cd->flags & DETECT_CONTENT_DEPTH_VAR) {
223  depth = det_ctx->byte_values[cd->depth];
224  } else {
225  if (cd->depth != 0) {
226  depth = cd->depth;
227  }
228  }
229 
230  if (stream_start_offset != 0 && cd->flags & DETECT_CONTENT_DEPTH) {
231  if (depth <= stream_start_offset) {
232  goto no_match;
233  } else if (depth >= (stream_start_offset + buffer_len)) {
234  ;
235  } else {
236  depth = depth - stream_start_offset;
237  }
238  }
239 
240  /* set offset */
242  offset = det_ctx->byte_values[cd->offset];
243  else
244  offset = cd->offset;
245  prev_buffer_offset = 0;
246  }
247 
248  /* If the value came from a variable, make sure to adjust the depth so it's relative
249  * to the offset value.
250  */
252  depth += offset;
253  }
254 
255  /* update offset with prev_offset if we're searching for
256  * matches after the first occurrence. */
257  SCLogDebug("offset %"PRIu32", prev_offset %"PRIu32, offset, prev_offset);
258  if (prev_offset != 0)
259  offset = prev_offset;
260 
261  SCLogDebug("offset %"PRIu32", depth %"PRIu32, offset, depth);
262 
263  if (depth > buffer_len)
264  depth = buffer_len;
265 
266  /* if offset is bigger than depth we can never match on a pattern.
267  * We can however, "match" on a negated pattern. */
268  if (offset > depth || depth == 0) {
269  if (cd->flags & DETECT_CONTENT_NEGATED) {
270  goto match;
271  } else {
272  goto no_match;
273  }
274  }
275 
276  const uint8_t *sbuffer = buffer + offset;
277  uint32_t sbuffer_len = depth - offset;
278  SCLogDebug("sbuffer_len %" PRIu32 " depth: %" PRIu32 ", buffer_len: %" PRIu32,
279  sbuffer_len, depth, buffer_len);
280 #ifdef DEBUG
281  BUG_ON(sbuffer_len > buffer_len);
282 #endif
283  const uint8_t *found;
284  if (cd->flags & DETECT_CONTENT_ENDS_WITH && depth < buffer_len) {
285  SCLogDebug("depth < buffer_len while DETECT_CONTENT_ENDS_WITH is set. Can't possibly match.");
286  found = NULL;
287  } else if (cd->content_len > sbuffer_len) {
288  found = NULL;
289  } else {
290  /* do the actual search */
291  found = SpmScan(cd->spm_ctx, det_ctx->spm_thread_ctx, sbuffer,
292  sbuffer_len);
293  }
294 
295  /* next we evaluate the result in combination with the
296  * negation flag. */
297  SCLogDebug("found %p cd negated %s", found, cd->flags & DETECT_CONTENT_NEGATED ? "true" : "false");
298 
299  if (found == NULL) {
300  if (!(cd->flags & DETECT_CONTENT_NEGATED)) {
301  if ((cd->flags & (DETECT_CONTENT_DISTANCE | DETECT_CONTENT_WITHIN)) == 0) {
302  /* independent match from previous matches, so failure is fatal */
303  goto no_match_discontinue;
304  }
305 
306  goto no_match;
307  } else {
308  goto match;
309  }
310  }
311 
312  uint32_t match_offset = (uint32_t)((found - buffer) + cd->content_len);
313  if (cd->flags & DETECT_CONTENT_NEGATED) {
314  SCLogDebug("content %" PRIu32 " matched at offset %" PRIu32
315  ", but negated so no match",
316  cd->id, match_offset);
317  /* don't bother carrying recursive matches now, for preceding
318  * relative keywords */
319 
320  /* found a match but not at the end of the buffer */
321  if (cd->flags & DETECT_CONTENT_ENDS_WITH) {
322  if (sbuffer_len != match_offset) {
323  SCLogDebug("content \"%s\" %" PRIu32 " matched at offset %" PRIu32
324  ", but not at end of buffer so match",
325  cd->content, cd->id, match_offset);
326  goto match;
327  }
328  }
329  if (DETECT_CONTENT_IS_SINGLE(cd)) {
330  goto no_match_discontinue;
331  }
332  goto no_match;
333  }
334 
335  SCLogDebug("content %" PRIu32 " matched at offset %" PRIu32 "", cd->id, match_offset);
336  det_ctx->buffer_offset = match_offset;
337 
338  if ((cd->flags & DETECT_CONTENT_ENDS_WITH) == 0 || match_offset == buffer_len) {
339  /* Match branch, add replace to the list if needed */
341  if (inspection_mode == DETECT_ENGINE_CONTENT_INSPECTION_MODE_PAYLOAD) {
342  /* we will need to replace content if match is confirmed
343  * cast to non-const as replace writes to it. */
344  det_ctx->replist =
345  DetectReplaceAddToList(det_ctx->replist, (uint8_t *)found, cd);
346  } else {
347  SCLogWarning("Can't modify payload without packet");
348  }
349  }
350 
351  /* if this is the last match we're done */
352  if (smd->is_last) {
353  goto match;
354  }
355 
356  SCLogDebug("content %" PRIu32, cd->id);
357  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
358 
359  /* see if the next buffer keywords match. If not, we will
360  * search for another occurrence of this content and see
361  * if the others match then until we run out of matches */
362  int r = DetectEngineContentInspectionInternal(det_ctx, ctx, s, smd + 1, p, f,
363  buffer, buffer_len, stream_start_offset, flags, inspection_mode);
364  if (r == 1) {
365  SCReturnInt(1);
366  } else if (r == -1) {
367  SCLogDebug("'next sm' said to discontinue this right now");
368  SCReturnInt(-1);
369  }
370  SCLogDebug("no match for 'next sm'");
371 
372  /* no match and no reason to look for another instance */
373  if ((cd->flags & DETECT_CONTENT_WITHIN_NEXT) == 0) {
374  SCLogDebug("'next sm' does not depend on me, so we can give up");
375  SCReturnInt(-1);
376  }
377 
378  SCLogDebug("'next sm' depends on me %p, lets see what we can do (flags %u)", cd,
379  cd->flags);
380  }
381  /* set the previous match offset to the start of this match + 1 */
382  prev_offset = (match_offset - (cd->content_len - 1));
383  SCLogDebug("trying to see if there is another match after prev_offset %" PRIu32,
384  prev_offset);
385  } while(1);
386 
387  } else if (smd->type == DETECT_ISDATAAT) {
388  SCLogDebug("inspecting isdataat");
389 
390  const DetectIsdataatData *id = (DetectIsdataatData *)smd->ctx;
391  uint32_t dataat = id->dataat;
392  if (id->flags & ISDATAAT_OFFSET_VAR) {
393  uint64_t be_value = det_ctx->byte_values[dataat];
394  if (be_value >= 100000000) {
395  if ((id->flags & ISDATAAT_NEGATED) == 0) {
396  SCLogDebug("extracted value %"PRIu64" very big: no match", be_value);
397  goto no_match;
398  }
399  SCLogDebug("extracted value way %"PRIu64" very big: match", be_value);
400  goto match;
401  }
402  dataat = (uint32_t)be_value;
403  SCLogDebug("isdataat: using value %u from byte_extract local_id %u", dataat, id->dataat);
404  }
405 
406  if (id->flags & ISDATAAT_RELATIVE) {
407  if (det_ctx->buffer_offset + dataat > buffer_len) {
408  SCLogDebug("det_ctx->buffer_offset + dataat %"PRIu32" > %"PRIu32, det_ctx->buffer_offset + dataat, buffer_len);
409  if (id->flags & ISDATAAT_NEGATED)
410  goto match;
411  if ((id->flags & ISDATAAT_OFFSET_VAR) == 0) {
412  goto no_match_discontinue;
413  }
414  goto no_match;
415  } else {
416  SCLogDebug("relative isdataat match");
417  if (id->flags & ISDATAAT_NEGATED) {
418  goto no_match;
419  }
420  goto match;
421  }
422  } else {
423  if (dataat < buffer_len) {
424  SCLogDebug("absolute isdataat match");
425  if (id->flags & ISDATAAT_NEGATED) {
426  if ((id->flags & ISDATAAT_OFFSET_VAR) == 0) {
427  goto no_match_discontinue;
428  }
429  goto no_match;
430  }
431  goto match;
432  } else {
433  SCLogDebug("absolute isdataat mismatch, id->isdataat %"PRIu32", buffer_len %"PRIu32"", dataat, buffer_len);
434  if (id->flags & ISDATAAT_NEGATED)
435  goto match;
436  if ((id->flags & ISDATAAT_OFFSET_VAR) == 0) {
437  goto no_match_discontinue;
438  }
439  goto no_match;
440  }
441  }
442 
443  } else if (smd->type == DETECT_PCRE) {
444  SCLogDebug("inspecting pcre");
445  const DetectPcreData *pe = (const DetectPcreData *)smd->ctx;
446  uint32_t prev_buffer_offset = det_ctx->buffer_offset;
447  uint32_t prev_offset = 0;
448 
449  det_ctx->pcre_match_start_offset = 0;
450  do {
451  int r = DetectPcrePayloadMatch(det_ctx, s, smd, p, f, buffer, buffer_len);
452  if (r == 0) {
453  goto no_match;
454  }
455 
456  if (!(pe->flags & DETECT_PCRE_RELATIVE_NEXT)) {
457  SCLogDebug("no relative match coming up, so this is a match");
458  goto match;
459  }
460  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
461 
462  /* save it, in case we need to do a pcre match once again */
463  prev_offset = det_ctx->pcre_match_start_offset;
464 
465  /* see if the next payload keywords match. If not, we will
466  * search for another occurrence of this pcre and see
467  * if the others match, until we run out of matches */
468  r = DetectEngineContentInspectionInternal(det_ctx, ctx, s, smd + 1, p, f, buffer,
469  buffer_len, stream_start_offset, flags, inspection_mode);
470  if (r == 1) {
471  SCReturnInt(1);
472  } else if (r == -1) {
473  SCReturnInt(-1);
474  }
475 
476  det_ctx->buffer_offset = prev_buffer_offset;
477  det_ctx->pcre_match_start_offset = prev_offset;
478  } while (1);
479 
480  } else if (smd->type == DETECT_BYTETEST) {
481  const DetectBytetestData *btd = (const DetectBytetestData *)smd->ctx;
482  uint16_t btflags = btd->flags;
483  int32_t offset = btd->offset;
484  uint64_t value = btd->value;
485  int32_t nbytes = btd->nbytes;
486  if (btflags & DETECT_BYTETEST_OFFSET_VAR) {
487  offset = det_ctx->byte_values[offset];
488  }
489  if (btflags & DETECT_BYTETEST_VALUE_VAR) {
490  value = det_ctx->byte_values[value];
491  }
492  if (btflags & DETECT_BYTETEST_NBYTES_VAR) {
493  nbytes = det_ctx->byte_values[nbytes];
494  }
495 
496  /* if we have dce enabled we will have to use the endianness
497  * specified by the dce header */
498  if (btflags & DETECT_BYTETEST_DCE) {
499  /* enable the endianness flag temporarily. once we are done
500  * processing we reset the flags to the original value*/
501  btflags |= ((flags & DETECT_CI_FLAGS_DCE_LE) ?
503  }
504 
505  if (DetectBytetestDoMatch(det_ctx, s, smd->ctx, buffer, buffer_len, btflags, offset, nbytes,
506  value) != 1) {
507  goto no_match;
508  }
509 
510  goto match;
511 
512  } else if (smd->type == DETECT_BYTEJUMP) {
513  const DetectBytejumpData *bjd = (const DetectBytejumpData *)smd->ctx;
514  uint16_t bjflags = bjd->flags;
515  int32_t offset = bjd->offset;
516  int32_t nbytes;
517 
518  if (bjflags & DETECT_BYTEJUMP_OFFSET_VAR) {
519  offset = det_ctx->byte_values[offset];
520  }
521 
522  if (bjflags & DETECT_BYTEJUMP_NBYTES_VAR) {
523  nbytes = det_ctx->byte_values[bjd->nbytes];
524  } else {
525  nbytes = bjd->nbytes;
526  }
527 
528  /* if we have dce enabled we will have to use the endianness
529  * specified by the dce header */
530  if (bjflags & DETECT_BYTEJUMP_DCE) {
531  /* enable the endianness flag temporarily. once we are done
532  * processing we reset the flags to the original value*/
533  bjflags |= ((flags & DETECT_CI_FLAGS_DCE_LE) ?
535  }
536 
538  det_ctx, s, smd->ctx, buffer, buffer_len, bjflags, nbytes, offset)) {
539  goto no_match;
540  }
541 
542  goto match;
543 
544  } else if (smd->type == DETECT_BYTE_EXTRACT) {
545 
546  const DetectByteExtractData *bed = (const DetectByteExtractData *)smd->ctx;
547  uint8_t endian = bed->endian;
548 
549  /* if we have dce enabled we will have to use the endianness
550  * specified by the dce header */
551  if ((bed->flags & DETECT_BYTE_EXTRACT_FLAG_ENDIAN) &&
552  endian == DETECT_BYTE_EXTRACT_ENDIAN_DCE &&
554 
555  /* enable the endianness flag temporarily. once we are done
556  * processing we reset the flags to the original value*/
557  endian |= ((flags & DETECT_CI_FLAGS_DCE_LE) ?
559  }
560 
561  if (DetectByteExtractDoMatch(det_ctx, smd, s, buffer, buffer_len,
562  &det_ctx->byte_values[bed->local_id], endian) != 1) {
563  goto no_match;
564  }
565 
566  SCLogDebug("[BE] Fetched value for index %d: %"PRIu64,
567  bed->local_id, det_ctx->byte_values[bed->local_id]);
568  goto match;
569 
570  } else if (smd->type == DETECT_BYTEMATH) {
571 
572  const DetectByteMathData *bmd = (const DetectByteMathData *)smd->ctx;
573  uint8_t endian = bmd->endian;
574 
575  /* if we have dce enabled we will have to use the endianness
576  * specified by the dce header */
577  if ((bmd->flags & DETECT_BYTEMATH_FLAG_ENDIAN) && endian == (int)EndianDCE &&
579  /* enable the endianness flag temporarily. once we are done
580  * processing we reset the flags to the original value*/
581  endian = (uint8_t)((flags & DETECT_CI_FLAGS_DCE_LE) ? LittleEndian : BigEndian);
582  }
583  uint64_t rvalue;
584  if (bmd->flags & DETECT_BYTEMATH_FLAG_RVALUE_VAR) {
585  rvalue = det_ctx->byte_values[bmd->rvalue];
586  } else {
587  rvalue = bmd->rvalue;
588  }
589 
590  uint8_t nbytes;
591  if (bmd->flags & DETECT_BYTEMATH_FLAG_NBYTES_VAR) {
592  nbytes = (uint8_t)det_ctx->byte_values[bmd->nbytes];
593  } else {
594  nbytes = bmd->nbytes;
595  }
596 
597  if (DetectByteMathDoMatch(det_ctx, bmd, s, buffer, buffer_len, nbytes, rvalue,
598  &det_ctx->byte_values[bmd->local_id], endian) != 1) {
599  goto no_match;
600  }
601 
602  SCLogDebug("[BM] Fetched value for index %d: %"PRIu64,
603  bmd->local_id, det_ctx->byte_values[bmd->local_id]);
604  goto match;
605 
606  } else if (smd->type == DETECT_BSIZE) {
607 
608  const bool eof = (flags & DETECT_CI_FLAGS_END);
609  const uint64_t data_size = buffer_len + stream_start_offset;
610  int r = DetectBsizeMatch(smd->ctx, data_size, eof);
611  if (r < 0) {
612  goto no_match_discontinue;
613  } else if (r == 0) {
614  goto no_match;
615  }
616  goto match;
617 
618  } else if (smd->type == DETECT_DATASET) {
619 
620  //PrintRawDataFp(stdout, buffer, buffer_len);
621  const DetectDatasetData *sd = (const DetectDatasetData *) smd->ctx;
622  int r = DetectDatasetBufferMatch(det_ctx, sd, buffer, buffer_len); //TODO buffer offset?
623  if (r == 1) {
624  goto match;
625  }
626  goto no_match_discontinue;
627 
628  } else if (smd->type == DETECT_DATAREP) {
629 
630  //PrintRawDataFp(stdout, buffer, buffer_len);
631  const DetectDatarepData *sd = (const DetectDatarepData *) smd->ctx;
632  int r = DetectDatarepBufferMatch(det_ctx, sd, buffer, buffer_len); //TODO buffer offset?
633  if (r == 1) {
634  goto match;
635  }
636  goto no_match_discontinue;
637 
638  } else if (smd->type == DETECT_AL_URILEN) {
639  SCLogDebug("inspecting uri len");
640 
641  int r;
642  const DetectUrilenData *urilend = (const DetectUrilenData *)smd->ctx;
643  if (buffer_len > UINT16_MAX) {
644  r = DetectU16Match(UINT16_MAX, &urilend->du16);
645  } else {
646  r = DetectU16Match((uint16_t)buffer_len, &urilend->du16);
647  }
648 
649  if (r == 1) {
650  goto match;
651  }
652  goto no_match_discontinue;
653 #ifdef HAVE_LUA
654  }
655  else if (smd->type == DETECT_LUA) {
656  SCLogDebug("lua starting");
657 
658  if (DetectLuaMatchBuffer(det_ctx, s, smd, buffer, buffer_len,
659  det_ctx->buffer_offset, f) != 1)
660  {
661  SCLogDebug("lua no_match");
662  goto no_match;
663  }
664  SCLogDebug("lua match");
665  goto match;
666 #endif /* HAVE_LUA */
667  } else if (smd->type == DETECT_BASE64_DECODE) {
668  if (DetectBase64DecodeDoMatch(det_ctx, s, smd, buffer, buffer_len)) {
669  if (s->sm_arrays[DETECT_SM_LIST_BASE64_DATA] != NULL) {
670  if (det_ctx->base64_decoded_len) {
671  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
672  int r = DetectEngineContentInspectionInternal(det_ctx, ctx, s,
674  det_ctx->base64_decoded, det_ctx->base64_decoded_len, 0,
676  if (r == 1) {
677  /* Base64 is a terminal list. */
678  goto final_match;
679  }
680  }
681  }
682  }
683  } else if (smd->type == DETECT_ASN1) {
684  if (!DetectAsn1Match(smd, buffer, buffer_len, det_ctx->buffer_offset)) {
685  SCLogDebug("asn1 no_match");
686  goto no_match;
687  }
688  SCLogDebug("asn1 match");
689  goto match;
690  } else {
691  SCLogDebug("sm->type %u", smd->type);
692 #ifdef DEBUG
693  BUG_ON(1);
694 #endif
695  }
696 
697 no_match:
698  KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
699  SCReturnInt(0);
700 
701 no_match_discontinue:
702  KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
703  SCReturnInt(-1);
704 
705 match:
706  /* this sigmatch matched, inspect the next one. If it was the last,
707  * the buffer portion of the signature matched. */
708  if (!smd->is_last) {
709  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
710  int r = DetectEngineContentInspectionInternal(det_ctx, ctx, s, smd + 1, p, f, buffer,
711  buffer_len, stream_start_offset, flags, inspection_mode);
712  SCReturnInt(r);
713  }
714 final_match:
715  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
716  SCReturnInt(1);
717 }
718 
719 /** \brief wrapper around DetectEngineContentInspectionInternal to return true/false only
720  *
721  * \param smd sigmatches to evaluate
722  */
724  const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer,
725  const uint32_t buffer_len, const uint32_t stream_start_offset, const uint8_t flags,
726  const enum DetectContentInspectionType inspection_mode)
727 {
729  .recursion.limit = de_ctx->inspection_recursion_limit };
730  det_ctx->buffer_offset = 0;
731 
732  int r = DetectEngineContentInspectionInternal(det_ctx, &ctx, s, smd, p, f, buffer, buffer_len,
733  stream_start_offset, flags, inspection_mode);
734 #ifdef UNITTESTS
736 #endif
737  if (r == 1)
738  return true;
739  else
740  return false;
741 }
742 
743 /** \brief wrapper around DetectEngineContentInspectionInternal to return true/false only
744  *
745  * \param smd sigmatches to evaluate
746  */
748  const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const InspectionBuffer *b,
749  const enum DetectContentInspectionType inspection_mode)
750 {
752  .recursion.limit = de_ctx->inspection_recursion_limit };
753 
754  det_ctx->buffer_offset = 0;
755 
756  int r = DetectEngineContentInspectionInternal(det_ctx, &ctx, s, smd, p, f, b->inspect,
757  b->inspect_len, b->inspect_offset, b->flags, inspection_mode);
758 #ifdef UNITTESTS
760 #endif
761  if (r == 1)
762  return true;
763  else
764  return false;
765 }
766 
767 #ifdef UNITTESTS
769 #endif
DetectEngineContentInspectionCtx
Definition: detect-engine-content-inspection.c:73
DetectEngineThreadCtx_::byte_values
uint64_t * byte_values
Definition: detect.h:1132
DetectAsn1Match
bool DetectAsn1Match(const SigMatchData *smd, const uint8_t *buffer, const uint32_t buffer_len, const uint32_t offset)
Definition: detect-asn1.c:58
DetectContentData_::offset
uint16_t offset
Definition: detect-content.h:107
DetectBytetestData_::flags
uint16_t flags
Definition: detect-bytetest.h:58
DetectDatasetData_
Definition: detect-dataset.h:34
detect-engine-uint.h
DETECT_BYTETEST_VALUE_VAR
#define DETECT_BYTETEST_VALUE_VAR
Definition: detect-bytetest.h:49
detect-content.h
DETECT_BYTE_EXTRACT_ENDIAN_DCE
#define DETECT_BYTE_EXTRACT_ENDIAN_DCE
Definition: detect-byte-extract.h:38
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1121
detect-engine.h
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
DETECT_CONTENT_DISTANCE_VAR
#define DETECT_CONTENT_DISTANCE_VAR
Definition: detect-content.h:47
DetectPcrePayloadMatch
int DetectPcrePayloadMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *payload, uint32_t payload_len)
Match a regex on a single payload.
Definition: detect-pcre.c:175
DETECT_BYTEJUMP
@ DETECT_BYTEJUMP
Definition: detect-engine-register.h:76
ISDATAAT_OFFSET_VAR
#define ISDATAAT_OFFSET_VAR
Definition: detect-isdataat.h:30
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectContentData_::within
int32_t within
Definition: detect-content.h:109
DetectBase64DecodeDoMatch
int DetectBase64DecodeDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, const uint8_t *payload, uint32_t payload_len)
Definition: detect-base64-decode.c:66
DetectBytetestDoMatch
int DetectBytetestDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchCtx *ctx, const uint8_t *payload, uint32_t payload_len, uint16_t flags, int32_t offset, int32_t nbytes, uint64_t value)
Bytetest detection code.
Definition: detect-bytetest.c:154
DetectIsdataatData_::flags
uint8_t flags
Definition: detect-isdataat.h:34
DETECT_BYTEJUMP_LITTLE
#define DETECT_BYTEJUMP_LITTLE
Definition: detect-bytejump.h:35
DetectByteExtractData_::local_id
uint8_t local_id
Definition: detect-byte-extract.h:45
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:62
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
detect-isdataat.h
DETECT_BYTEJUMP_NBYTES_VAR
#define DETECT_BYTEJUMP_NBYTES_VAR
Definition: detect-bytejump.h:43
SigMatchData_::is_last
bool is_last
Definition: detect.h:358
util-lua.h
DetectByteExtractData_
Holds data related to byte_extract keyword.
Definition: detect-byte-extract.h:43
DetectDatarepData_
Definition: detect-datarep.h:36
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:359
detect-bsize.h
InspectionBuffer
Definition: detect.h:371
Flow_
Flow data structure.
Definition: flow.h:350
DetectEngineCtx_::inspection_recursion_limit
int inspection_recursion_limit
Definition: detect.h:881
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
DETECT_DATAREP
@ DETECT_DATAREP
Definition: detect-engine-register.h:102
DETECT_BYTETEST_DCE
#define DETECT_BYTETEST_DCE
Definition: detect-bytetest.h:47
DETECT_CONTENT_DEPTH_VAR
#define DETECT_CONTENT_DEPTH_VAR
Definition: detect-content.h:46
detect-lua.h
rust.h
DETECT_BYTEJUMP_DCE
#define DETECT_BYTEJUMP_DCE
Definition: detect-bytejump.h:40
DetectEngineThreadCtx_::spm_thread_ctx
SpmThreadCtx * spm_thread_ctx
Definition: detect.h:1129
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:376
DetectContentInspectionType
DetectContentInspectionType
Definition: detect-engine-content-inspection.h:31
Signature_::sm_arrays
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:646
DetectIsdataatData_
Definition: detect-isdataat.h:32
DetectContentData_
Definition: detect-content.h:93
DetectPcreData_::flags
uint16_t flags
Definition: detect-pcre.h:46
DetectBytetestData_::nbytes
uint8_t nbytes
Definition: detect-bytetest.h:54
DetectBytetestData_
Definition: detect-bytetest.h:53
SigMatchData_
Data needed for Match()
Definition: detect.h:356
detect-pcre.h
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:50
DETECT_CI_FLAGS_DCE_BE
#define DETECT_CI_FLAGS_DCE_BE
Definition: detect-engine-content-inspection.h:45
SigMatchData_::type
uint16_t type
Definition: detect.h:357
DetectBytejumpData_
Definition: detect-bytejump.h:46
DETECT_ENGINE_CONTENT_INSPECTION_MODE_PAYLOAD
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_PAYLOAD
Definition: detect-engine-content-inspection.h:32
util-unittest.h
DETECT_ASN1
@ DETECT_ASN1
Definition: detect-engine-register.h:208
DetectBytejumpData_::offset
int32_t offset
Definition: detect-bytejump.h:50
util-unittest-helper.h
detect-asn1.h
KEYWORD_PROFILING_END
#define KEYWORD_PROFILING_END(ctx, type, m)
Definition: util-profiling.h:64
detect-base64-data.h
DetectByteExtractDoMatch
int DetectByteExtractDoMatch(DetectEngineThreadCtx *det_ctx, const SigMatchData *smd, const Signature *s, const uint8_t *payload, uint32_t payload_len, uint64_t *value, uint8_t endian)
Definition: detect-byte-extract.c:115
DetectReplaceAddToList
DetectReplaceList * DetectReplaceAddToList(DetectReplaceList *replist, uint8_t *found, const DetectContentData *cd)
Definition: detect-replace.c:179
DetectLuaMatchBuffer
int DetectLuaMatchBuffer(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, const uint8_t *buffer, uint32_t buffer_len, uint32_t offset, Flow *f)
decode.h
util-debug.h
DETECT_CONTENT_ENDS_WITH
#define DETECT_CONTENT_ENDS_WITH
Definition: detect-content.h:42
DETECT_CONTENT_DISTANCE
#define DETECT_CONTENT_DISTANCE
Definition: detect-content.h:30
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectBsizeMatch
int DetectBsizeMatch(const SigMatchCtx *ctx, const uint64_t buffer_size, bool eof)
bsize match function
Definition: detect-bsize.c:119
DetectEngineThreadCtx_
Definition: detect.h:1092
DETECT_BSIZE
@ DETECT_BSIZE
Definition: detect-engine-register.h:108
DETECT_SM_LIST_BASE64_DATA
@ DETECT_SM_LIST_BASE64_DATA
Definition: detect.h:118
DETECT_LUA
@ DETECT_LUA
Definition: detect-engine-register.h:228
DETECT_CONTENT_DEPTH
#define DETECT_CONTENT_DEPTH
Definition: detect-content.h:33
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
DETECT_BYTE_EXTRACT_ENDIAN_BIG
#define DETECT_BYTE_EXTRACT_ENDIAN_BIG
Definition: detect-byte-extract.h:36
detect.h
DETECT_CONTENT_IS_SINGLE
#define DETECT_CONTENT_IS_SINGLE(c)
Definition: detect-content.h:68
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:373
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
DetectContentData_::id
PatIntId id
Definition: detect-content.h:105
DetectByteExtractData_::endian
uint8_t endian
Definition: detect-byte-extract.h:52
DetectBytejumpDoMatch
bool DetectBytejumpDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchCtx *ctx, const uint8_t *payload, uint32_t payload_len, uint16_t flags, int32_t nbytes, int32_t offset)
Byte jump match function.
Definition: detect-bytejump.c:136
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
DetectEngineContentInspectionCtx::recursion
struct DetectEngineContentInspectionCtx::@65 recursion
DetectEngineThreadCtx_::base64_decoded_len
int base64_decoded_len
Definition: detect.h:1135
util-profiling.h
DETECT_BYTETEST_OFFSET_VAR
#define DETECT_BYTETEST_OFFSET_VAR
Definition: detect-bytetest.h:50
DetectContentData_::depth
uint16_t depth
Definition: detect-content.h:106
SpmScan
uint8_t * SpmScan(const SpmCtx *ctx, SpmThreadCtx *thread_ctx, const uint8_t *haystack, uint32_t haystack_len)
Definition: util-spm.c:193
Packet_
Definition: decode.h:436
ISDATAAT_RELATIVE
#define ISDATAAT_RELATIVE
Definition: detect-isdataat.h:27
detect-bytejump.h
DETECT_BYTETEST_NBYTES_VAR
#define DETECT_BYTETEST_NBYTES_VAR
Definition: detect-bytetest.h:51
DETECT_CI_FLAGS_END
#define DETECT_CI_FLAGS_END
Definition: detect-engine-content-inspection.h:42
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
detect-replace.h
DETECT_PCRE
@ DETECT_PCRE
Definition: detect-engine-register.h:64
detect-engine-content-inspection.h
DETECT_CONTENT_WITHIN_VAR
#define DETECT_CONTENT_WITHIN_VAR
Definition: detect-content.h:48
DetectDatasetBufferMatch
int DetectDatasetBufferMatch(DetectEngineThreadCtx *det_ctx, const DetectDatasetData *sd, const uint8_t *data, const uint32_t data_len)
Definition: detect-dataset.c:63
DetectDatarepBufferMatch
int DetectDatarepBufferMatch(DetectEngineThreadCtx *det_ctx, const DetectDatarepData *sd, const uint8_t *data, const uint32_t data_len)
Definition: detect-datarep.c:68
DETECT_BYTETEST
@ DETECT_BYTETEST
Definition: detect-engine-register.h:75
DetectByteMathDoMatch
int DetectByteMathDoMatch(DetectEngineThreadCtx *det_ctx, const DetectByteMathData *data, const Signature *s, const uint8_t *payload, const uint32_t payload_len, uint8_t nbytes, uint64_t rvalue, uint64_t *value, uint8_t endian)
Definition: detect-bytemath.c:87
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:49
DetectU16Match
int DetectU16Match(const uint16_t parg, const DetectUintData_u16 *du16)
Definition: detect-engine-uint.c:107
flags
uint8_t flags
Definition: decode-gre.h:0
DETECT_BYTE_EXTRACT_ENDIAN_LITTLE
#define DETECT_BYTE_EXTRACT_ENDIAN_LITTLE
Definition: detect-byte-extract.h:37
suricata-common.h
detect-byte-extract.h
DetectContentData_::distance
int32_t distance
Definition: detect-content.h:108
DetectBytejumpData_::nbytes
uint8_t nbytes
Definition: detect-bytejump.h:47
ut_inspection_recursion_counter
thread_local uint32_t ut_inspection_recursion_counter
Definition: detect-engine-content-inspection.c:70
DETECT_CONTENT_WITHIN_NEXT
#define DETECT_CONTENT_WITHIN_NEXT
Definition: detect-content.h:57
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
detect-dataset.h
DetectEngineContentInspectionCtx::limit
const uint32_t limit
Definition: detect-engine-content-inspection.c:76
util-validate.h
detect-base64-decode.h
ISDATAAT_NEGATED
#define ISDATAAT_NEGATED
Definition: detect-isdataat.h:29
DetectContentData_::spm_ctx
SpmCtx * spm_ctx
Definition: detect-content.h:111
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:374
DetectEngineThreadCtx_::pcre_match_start_offset
uint32_t pcre_match_start_offset
Definition: detect.h:1125
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:372
detect-datarep.h
DETECT_BYTE_EXTRACT
@ DETECT_BYTE_EXTRACT
Definition: detect-engine-register.h:184
detect-parse.h
Signature_
Signature container.
Definition: detect.h:593
DETECT_ISDATAAT
@ DETECT_ISDATAAT
Definition: detect-engine-register.h:82
DetectByteExtractData_::flags
uint8_t flags
Definition: detect-byte-extract.h:51
DETECT_BYTE_EXTRACT_FLAG_ENDIAN
#define DETECT_BYTE_EXTRACT_FLAG_ENDIAN
Definition: detect-byte-extract.h:32
DetectBytetestData_::offset
int32_t offset
Definition: detect-bytetest.h:60
DetectIsdataatData_::dataat
uint16_t dataat
Definition: detect-isdataat.h:33
DetectEngineThreadCtx_::base64_decoded
uint8_t * base64_decoded
Definition: detect.h:1134
DETECT_PCRE_RELATIVE_NEXT
#define DETECT_PCRE_RELATIVE_NEXT
Definition: detect-pcre.h:34
detect-urilen.h
suricata.h
DetectPcreData_
Definition: detect-pcre.h:42
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DETECT_BYTEMATH
@ DETECT_BYTEMATH
Definition: detect-engine-register.h:77
DETECT_BASE64_DECODE
@ DETECT_BASE64_DECODE
Definition: detect-engine-register.h:262
detect-uricontent.h
DetectEngineContentInspectionBuffer
bool DetectEngineContentInspectionBuffer(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const InspectionBuffer *b, const enum DetectContentInspectionType inspection_mode)
wrapper around DetectEngineContentInspectionInternal to return true/false only
Definition: detect-engine-content-inspection.c:747
DetectEngineThreadCtx_::replist
DetectReplaceList * replist
Definition: detect.h:1204
DetectEngineContentInspectionCtx::count
uint32_t count
Definition: detect-engine-content-inspection.c:75
DETECT_CONTENT_REPLACE
#define DETECT_CONTENT_REPLACE
Definition: detect-content.h:51
DetectEngineContentInspection
bool DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, const uint32_t buffer_len, const uint32_t stream_start_offset, const uint8_t flags, const enum DetectContentInspectionType inspection_mode)
wrapper around DetectEngineContentInspectionInternal to return true/false only
Definition: detect-engine-content-inspection.c:723
detect-engine-content-inspection.c
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DETECT_BYTETEST_LITTLE
#define DETECT_BYTETEST_LITTLE
Definition: detect-bytetest.h:43
DETECT_AL_URILEN
@ DETECT_AL_URILEN
Definition: detect-engine-register.h:137
DETECT_DATASET
@ DETECT_DATASET
Definition: detect-engine-register.h:101
DetectBytetestData_::value
uint64_t value
Definition: detect-bytetest.h:62
DETECT_CONTENT_WITHIN
#define DETECT_CONTENT_WITHIN
Definition: detect-content.h:31
DETECT_CI_FLAGS_DCE_LE
#define DETECT_CI_FLAGS_DCE_LE
Definition: detect-engine-content-inspection.h:44
DETECT_BYTEJUMP_OFFSET_VAR
#define DETECT_BYTEJUMP_OFFSET_VAR
Definition: detect-bytejump.h:44
DetectBytejumpData_::flags
uint16_t flags
Definition: detect-bytejump.h:49
detect-bytemath.h
DETECT_CONTENT_OFFSET_VAR
#define DETECT_CONTENT_OFFSET_VAR
Definition: detect-content.h:45
detect-bytetest.h