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