suricata
detect-engine-content-inspection.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 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 
29 #include "decode.h"
30 
31 #include "detect.h"
32 #include "detect-engine.h"
33 #include "detect-parse.h"
34 #include "detect-content.h"
35 #include "detect-pcre.h"
36 #include "detect-isdataat.h"
37 #include "detect-bytetest.h"
38 #include "detect-bytemath.h"
39 #include "detect-bytejump.h"
40 #include "detect-byte-extract.h"
41 #include "detect-replace.h"
43 #include "detect-uricontent.h"
44 #include "detect-urilen.h"
45 #include "detect-engine-uint.h"
46 #include "detect-bsize.h"
47 #include "detect-lua.h"
48 #include "detect-base64-decode.h"
49 #include "detect-base64-data.h"
50 #include "detect-dataset.h"
51 #include "detect-datarep.h"
52 
53 #include "util-spm.h"
54 #include "util-debug.h"
55 #include "util-print.h"
56 #include "util-validate.h"
57 
58 #include "util-unittest.h"
59 #include "util-unittest-helper.h"
60 #include "util-profiling.h"
61 
62 #include "rust.h"
63 
64 #ifdef HAVE_LUA
65 #include "util-lua.h"
66 #endif
67 
68 /**
69  * \brief Run the actual payload match functions
70  *
71  * The following keywords are inspected:
72  * - content, including all the http and dce modified contents
73  * - isdaatat
74  * - pcre
75  * - bytejump
76  * - bytetest
77  * - byte_extract
78  * - urilen
79  * -
80  *
81  * All keywords are evaluated against the buffer with buffer_len.
82  *
83  * For accounting the last match in relative matching the
84  * det_ctx->buffer_offset int is used.
85  *
86  * \param de_ctx Detection engine context
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 0 no match
104  * \retval 1 match
105  */
107  const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer,
108  uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode)
109 {
110  SCEnter();
112 
113  det_ctx->inspection_recursion_counter++;
114 
116  det_ctx->discontinue_matching = 1;
117  KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
118  SCReturnInt(0);
119  }
120 
121  if (smd == NULL || buffer_len == 0) {
122  KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
123  SCReturnInt(0);
124  }
125 
126  /* \todo unify this which is phase 2 of payload inspection unification */
127  if (smd->type == DETECT_CONTENT) {
128 
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  const uint8_t *found = NULL;
147  uint32_t offset = 0;
148  uint32_t depth = buffer_len;
149  uint32_t prev_offset = 0; /**< used in recursive searching */
150  uint32_t prev_buffer_offset = det_ctx->buffer_offset;
151 
152  do {
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 
157  offset = prev_buffer_offset;
158  depth = buffer_len;
159 
160  int distance = cd->distance;
161  if (cd->flags & DETECT_CONTENT_DISTANCE) {
163  distance = det_ctx->byte_values[cd->distance];
164  }
165  if (distance < 0 && (uint32_t)(abs(distance)) > offset)
166  offset = 0;
167  else
168  offset += distance;
169 
170  SCLogDebug("cd->distance %"PRIi32", offset %"PRIu32", depth %"PRIu32,
171  distance, offset, depth);
172  }
173 
174  if (cd->flags & DETECT_CONTENT_WITHIN) {
175  if (cd->flags & DETECT_CONTENT_WITHIN_VAR) {
176  if ((int32_t)depth > (int32_t)(prev_buffer_offset + det_ctx->byte_values[cd->within] + distance)) {
177  depth = prev_buffer_offset + det_ctx->byte_values[cd->within] + distance;
178  }
179  } else {
180  if ((int32_t)depth > (int32_t)(prev_buffer_offset + cd->within + distance)) {
181  depth = prev_buffer_offset + cd->within + distance;
182  }
183 
184  SCLogDebug("cd->within %"PRIi32", det_ctx->buffer_offset %"PRIu32", depth %"PRIu32,
185  cd->within, prev_buffer_offset, depth);
186  }
187 
188  if (stream_start_offset != 0 && prev_buffer_offset == 0) {
189  if (depth <= stream_start_offset) {
190  goto no_match;
191  } else if (depth >= (stream_start_offset + buffer_len)) {
192  ;
193  } else {
194  depth = depth - stream_start_offset;
195  }
196  }
197  }
198 
199  if (cd->flags & DETECT_CONTENT_DEPTH_VAR) {
200  if ((det_ctx->byte_values[cd->depth] + prev_buffer_offset) < depth) {
201  depth = prev_buffer_offset + det_ctx->byte_values[cd->depth];
202  }
203  } else {
204  if (cd->depth != 0) {
205  if ((cd->depth + prev_buffer_offset) < depth) {
206  depth = prev_buffer_offset + cd->depth;
207  }
208 
209  SCLogDebug("cd->depth %"PRIu32", depth %"PRIu32, cd->depth, depth);
210  }
211  }
212 
213  if (cd->flags & DETECT_CONTENT_OFFSET_VAR) {
214  if (det_ctx->byte_values[cd->offset] > offset)
215  offset = det_ctx->byte_values[cd->offset];
216  } else {
217  if (cd->offset > offset) {
218  offset = cd->offset;
219  SCLogDebug("setting offset %"PRIu32, offset);
220  }
221  }
222  } else { /* implied no relative matches */
223  /* set depth */
224  if (cd->flags & DETECT_CONTENT_DEPTH_VAR) {
225  depth = det_ctx->byte_values[cd->depth];
226  } else {
227  if (cd->depth != 0) {
228  depth = cd->depth;
229  }
230  }
231 
232  if (stream_start_offset != 0 && cd->flags & DETECT_CONTENT_DEPTH) {
233  if (depth <= stream_start_offset) {
234  goto no_match;
235  } else if (depth >= (stream_start_offset + buffer_len)) {
236  ;
237  } else {
238  depth = depth - stream_start_offset;
239  }
240  }
241 
242  /* set offset */
244  offset = det_ctx->byte_values[cd->offset];
245  else
246  offset = cd->offset;
247  prev_buffer_offset = 0;
248  }
249 
250  /* If the value came from a variable, make sure to adjust the depth so it's relative
251  * to the offset value.
252  */
254  depth += offset;
255  }
256 
257  /* update offset with prev_offset if we're searching for
258  * matches after the first occurrence. */
259  SCLogDebug("offset %"PRIu32", prev_offset %"PRIu32, offset, prev_offset);
260  if (prev_offset != 0)
261  offset = prev_offset;
262 
263  SCLogDebug("offset %"PRIu32", depth %"PRIu32, offset, depth);
264 
265  if (depth > buffer_len)
266  depth = buffer_len;
267 
268  /* if offset is bigger than depth we can never match on a pattern.
269  * We can however, "match" on a negated pattern. */
270  if (offset > depth || depth == 0) {
271  if (cd->flags & DETECT_CONTENT_NEGATED) {
272  goto match;
273  } else {
274  goto no_match;
275  }
276  }
277 
278  const uint8_t *sbuffer = buffer + offset;
279  uint32_t sbuffer_len = depth - offset;
280  uint32_t match_offset = 0;
281  SCLogDebug("sbuffer_len %"PRIu32, sbuffer_len);
282 #ifdef DEBUG
283  BUG_ON(sbuffer_len > buffer_len);
284 #endif
285  if (cd->flags & DETECT_CONTENT_ENDS_WITH && depth < buffer_len) {
286  SCLogDebug("depth < buffer_len while DETECT_CONTENT_ENDS_WITH is set. Can't possibly match.");
287  found = NULL;
288  } else if (cd->content_len > sbuffer_len) {
289  found = NULL;
290  } else {
291  /* do the actual search */
292  found = SpmScan(cd->spm_ctx, det_ctx->spm_thread_ctx, sbuffer,
293  sbuffer_len);
294  }
295 
296  /* next we evaluate the result in combination with the
297  * negation flag. */
298  SCLogDebug("found %p cd negated %s", found, cd->flags & DETECT_CONTENT_NEGATED ? "true" : "false");
299 
300  if (found == NULL) {
301  if (!(cd->flags & DETECT_CONTENT_NEGATED)) {
302  if ((cd->flags & (DETECT_CONTENT_DISTANCE | DETECT_CONTENT_WITHIN)) == 0) {
303  /* independent match from previous matches, so failure is fatal */
304  det_ctx->discontinue_matching = 1;
305  }
306 
307  goto no_match;
308  } else {
309  goto match;
310  }
311  } else if (cd->flags & DETECT_CONTENT_NEGATED) {
312  SCLogDebug("content %"PRIu32" matched at offset %"PRIu32", but negated so no match", cd->id, match_offset);
313  /* don't bother carrying recursive matches now, for preceding
314  * relative keywords */
315  if (DETECT_CONTENT_IS_SINGLE(cd))
316  det_ctx->discontinue_matching = 1;
317  goto no_match;
318  } else {
319  match_offset = (uint32_t)((found - buffer) + cd->content_len);
320  SCLogDebug("content %"PRIu32" matched at offset %"PRIu32"", cd->id, match_offset);
321  det_ctx->buffer_offset = match_offset;
322 
323  if ((cd->flags & DETECT_CONTENT_ENDS_WITH) == 0 || match_offset == buffer_len) {
324  /* Match branch, add replace to the list if needed */
325  if (cd->flags & DETECT_CONTENT_REPLACE) {
326  if (inspection_mode == DETECT_ENGINE_CONTENT_INSPECTION_MODE_PAYLOAD) {
327  /* we will need to replace content if match is confirmed
328  * cast to non-const as replace writes to it. */
329  det_ctx->replist = DetectReplaceAddToList(det_ctx->replist, (uint8_t *)found, cd);
330  } else {
331  SCLogWarning("Can't modify payload without packet");
332  }
333  }
334 
335  /* if this is the last match we're done */
336  if (smd->is_last) {
337  goto match;
338  }
339 
340  SCLogDebug("content %"PRIu32, cd->id);
341  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
342 
343  /* see if the next buffer keywords match. If not, we will
344  * search for another occurrence of this content and see
345  * if the others match then until we run out of matches */
346  uint8_t r = DetectEngineContentInspection(de_ctx, det_ctx, s, smd + 1, p, f,
347  buffer, buffer_len, stream_start_offset, flags, inspection_mode);
348  if (r == 1) {
349  SCReturnInt(1);
350  }
351  SCLogDebug("no match for 'next sm'");
352 
353  if (det_ctx->discontinue_matching) {
354  SCLogDebug("'next sm' said to discontinue this right now");
355  goto no_match;
356  }
357 
358  /* no match and no reason to look for another instance */
359  if ((cd->flags & DETECT_CONTENT_WITHIN_NEXT) == 0) {
360  SCLogDebug("'next sm' does not depend on me, so we can give up");
361  det_ctx->discontinue_matching = 1;
362  goto no_match;
363  }
364 
365  SCLogDebug("'next sm' depends on me %p, lets see what we can do (flags %u)", cd, cd->flags);
366  }
367  /* set the previous match offset to the start of this match + 1 */
368  prev_offset = (match_offset - (cd->content_len - 1));
369  SCLogDebug("trying to see if there is another match after prev_offset %"PRIu32, prev_offset);
370  }
371 
372  } while(1);
373 
374  } else if (smd->type == DETECT_ISDATAAT) {
375  SCLogDebug("inspecting isdataat");
376 
377  const DetectIsdataatData *id = (DetectIsdataatData *)smd->ctx;
378  uint32_t dataat = id->dataat;
379  if (id->flags & ISDATAAT_OFFSET_VAR) {
380  uint64_t be_value = det_ctx->byte_values[dataat];
381  if (be_value >= 100000000) {
382  if ((id->flags & ISDATAAT_NEGATED) == 0) {
383  SCLogDebug("extracted value %"PRIu64" very big: no match", be_value);
384  goto no_match;
385  }
386  SCLogDebug("extracted value way %"PRIu64" very big: match", be_value);
387  goto match;
388  }
389  dataat = (uint32_t)be_value;
390  SCLogDebug("isdataat: using value %u from byte_extract local_id %u", dataat, id->dataat);
391  }
392 
393  if (id->flags & ISDATAAT_RELATIVE) {
394  if (det_ctx->buffer_offset + dataat > buffer_len) {
395  SCLogDebug("det_ctx->buffer_offset + dataat %"PRIu32" > %"PRIu32, det_ctx->buffer_offset + dataat, buffer_len);
396  if (id->flags & ISDATAAT_NEGATED)
397  goto match;
398  goto no_match;
399  } else {
400  SCLogDebug("relative isdataat match");
401  if (id->flags & ISDATAAT_NEGATED)
402  goto no_match;
403  goto match;
404  }
405  } else {
406  if (dataat < buffer_len) {
407  SCLogDebug("absolute isdataat match");
408  if (id->flags & ISDATAAT_NEGATED)
409  goto no_match;
410  goto match;
411  } else {
412  SCLogDebug("absolute isdataat mismatch, id->isdataat %"PRIu32", buffer_len %"PRIu32"", dataat, buffer_len);
413  if (id->flags & ISDATAAT_NEGATED)
414  goto match;
415  goto no_match;
416  }
417  }
418 
419  } else if (smd->type == DETECT_PCRE) {
420  SCLogDebug("inspecting pcre");
421  DetectPcreData *pe = (DetectPcreData *)smd->ctx;
422  uint32_t prev_buffer_offset = det_ctx->buffer_offset;
423  uint32_t prev_offset = 0;
424  int r = 0;
425 
426  det_ctx->pcre_match_start_offset = 0;
427  do {
428  r = DetectPcrePayloadMatch(det_ctx, s, smd, p, f,
429  buffer, buffer_len);
430  if (r == 0) {
431  goto no_match;
432  }
433 
434  if (!(pe->flags & DETECT_PCRE_RELATIVE_NEXT)) {
435  SCLogDebug("no relative match coming up, so this is a match");
436  goto match;
437  }
438  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
439 
440  /* save it, in case we need to do a pcre match once again */
441  prev_offset = det_ctx->pcre_match_start_offset;
442 
443  /* see if the next payload keywords match. If not, we will
444  * search for another occurrence of this pcre and see
445  * if the others match, until we run out of matches */
446  r = DetectEngineContentInspection(de_ctx, det_ctx, s, smd+1,
447  p, f, buffer, buffer_len, stream_start_offset, flags,
448  inspection_mode);
449  if (r == 1) {
450  SCReturnInt(1);
451  }
452 
453  if (det_ctx->discontinue_matching)
454  goto no_match;
455 
456  det_ctx->buffer_offset = prev_buffer_offset;
457  det_ctx->pcre_match_start_offset = prev_offset;
458  } while (1);
459 
460  } else if (smd->type == DETECT_BYTETEST) {
461  DetectBytetestData *btd = (DetectBytetestData *)smd->ctx;
462  uint8_t btflags = btd->flags;
463  int32_t offset = btd->offset;
464  uint64_t value = btd->value;
465  if (btflags & DETECT_BYTETEST_OFFSET_VAR) {
466  offset = det_ctx->byte_values[offset];
467  }
468  if (btflags & DETECT_BYTETEST_VALUE_VAR) {
469  value = det_ctx->byte_values[value];
470  }
471 
472  /* if we have dce enabled we will have to use the endianness
473  * specified by the dce header */
474  if (btflags & DETECT_BYTETEST_DCE) {
475  /* enable the endianness flag temporarily. once we are done
476  * processing we reset the flags to the original value*/
477  btflags |= ((flags & DETECT_CI_FLAGS_DCE_LE) ?
479  }
480 
481  if (DetectBytetestDoMatch(det_ctx, s, smd->ctx, buffer, buffer_len, btflags,
482  offset, value) != 1) {
483  goto no_match;
484  }
485 
486  goto match;
487 
488  } else if (smd->type == DETECT_BYTEJUMP) {
489  DetectBytejumpData *bjd = (DetectBytejumpData *)smd->ctx;
490  uint16_t bjflags = bjd->flags;
491  int32_t offset = bjd->offset;
492 
493  if (bjflags & DETECT_CONTENT_OFFSET_VAR) {
494  offset = det_ctx->byte_values[offset];
495  }
496 
497  /* if we have dce enabled we will have to use the endianness
498  * specified by the dce header */
499  if (bjflags & DETECT_BYTEJUMP_DCE) {
500  /* enable the endianness flag temporarily. once we are done
501  * processing we reset the flags to the original value*/
502  bjflags |= ((flags & DETECT_CI_FLAGS_DCE_LE) ?
504  }
505 
506  if (DetectBytejumpDoMatch(det_ctx, s, smd->ctx, buffer, buffer_len,
507  bjflags, offset) != 1) {
508  goto no_match;
509  }
510 
511  goto match;
512 
513  } else if (smd->type == DETECT_BYTE_EXTRACT) {
514 
515  DetectByteExtractData *bed = (DetectByteExtractData *)smd->ctx;
516  uint8_t endian = bed->endian;
517 
518  /* if we have dce enabled we will have to use the endianness
519  * specified by the dce header */
520  if ((bed->flags & DETECT_BYTE_EXTRACT_FLAG_ENDIAN) &&
521  endian == DETECT_BYTE_EXTRACT_ENDIAN_DCE &&
523 
524  /* enable the endianness flag temporarily. once we are done
525  * processing we reset the flags to the original value*/
526  endian |= ((flags & DETECT_CI_FLAGS_DCE_LE) ?
528  }
529 
530  if (DetectByteExtractDoMatch(det_ctx, smd, s, buffer, buffer_len,
531  &det_ctx->byte_values[bed->local_id], endian) != 1) {
532  goto no_match;
533  }
534 
535  SCLogDebug("[BE] Fetched value for index %d: %"PRIu64,
536  bed->local_id, det_ctx->byte_values[bed->local_id]);
537  goto match;
538 
539  } else if (smd->type == DETECT_BYTEMATH) {
540 
541  DetectByteMathData *bmd = (DetectByteMathData *)smd->ctx;
542  uint8_t endian = bmd->endian;
543 
544  /* if we have dce enabled we will have to use the endianness
545  * specified by the dce header */
546  if ((bmd->flags & DETECT_BYTEMATH_FLAG_ENDIAN) && endian == (int)EndianDCE &&
548 
549  /* enable the endianness flag temporarily. once we are done
550  * processing we reset the flags to the original value*/
551  endian |= (uint8_t)((flags & DETECT_CI_FLAGS_DCE_LE) ? LittleEndian : BigEndian);
552  }
553  uint64_t rvalue;
554  if (bmd->flags & DETECT_BYTEMATH_FLAG_RVALUE_VAR) {
555  rvalue = det_ctx->byte_values[bmd->local_id];
556  } else {
557  rvalue = bmd->rvalue;
558  }
559 
560  DEBUG_VALIDATE_BUG_ON(buffer_len > UINT16_MAX);
561  if (DetectByteMathDoMatch(det_ctx, smd, s, buffer, (uint16_t)buffer_len, rvalue,
562  &det_ctx->byte_values[bmd->local_id], endian) != 1) {
563  goto no_match;
564  }
565 
566  SCLogDebug("[BM] Fetched value for index %d: %"PRIu64,
567  bmd->local_id, det_ctx->byte_values[bmd->local_id]);
568  goto match;
569 
570  } else if (smd->type == DETECT_BSIZE) {
571 
572  bool eof = (flags & DETECT_CI_FLAGS_END);
573  const uint64_t data_size = buffer_len + stream_start_offset;
574  int r = DetectBsizeMatch(smd->ctx, data_size, eof);
575  if (r < 0) {
576  det_ctx->discontinue_matching = 1;
577  goto no_match;
578 
579  } else if (r == 0) {
580  goto no_match;
581  }
582  goto match;
583 
584  } else if (smd->type == DETECT_DATASET) {
585 
586  //PrintRawDataFp(stdout, buffer, buffer_len);
587  const DetectDatasetData *sd = (const DetectDatasetData *) smd->ctx;
588  int r = DetectDatasetBufferMatch(det_ctx, sd, buffer, buffer_len); //TODO buffer offset?
589  if (r == 1) {
590  goto match;
591  }
592  det_ctx->discontinue_matching = 1;
593  goto no_match;
594 
595  } else if (smd->type == DETECT_DATAREP) {
596 
597  //PrintRawDataFp(stdout, buffer, buffer_len);
598  const DetectDatarepData *sd = (const DetectDatarepData *) smd->ctx;
599  int r = DetectDatarepBufferMatch(det_ctx, sd, buffer, buffer_len); //TODO buffer offset?
600  if (r == 1) {
601  goto match;
602  }
603  det_ctx->discontinue_matching = 1;
604  goto no_match;
605 
606  } else if (smd->type == DETECT_AL_URILEN) {
607  SCLogDebug("inspecting uri len");
608 
609  int r = 0;
610  DetectUrilenData *urilend = (DetectUrilenData *) smd->ctx;
611  if (buffer_len > UINT16_MAX) {
612  r = DetectU16Match(UINT16_MAX, &urilend->du16);
613  } else {
614  r = DetectU16Match((uint16_t)buffer_len, &urilend->du16);
615  }
616 
617  if (r == 1) {
618  goto match;
619  }
620 
621  det_ctx->discontinue_matching = 0;
622 
623  goto no_match;
624 #ifdef HAVE_LUA
625  }
626  else if (smd->type == DETECT_LUA) {
627  SCLogDebug("lua starting");
628 
629  if (DetectLuaMatchBuffer(det_ctx, s, smd, buffer, buffer_len,
630  det_ctx->buffer_offset, f) != 1)
631  {
632  SCLogDebug("lua no_match");
633  goto no_match;
634  }
635  SCLogDebug("lua match");
636  goto match;
637 #endif /* HAVE_LUA */
638  } else if (smd->type == DETECT_BASE64_DECODE) {
639  if (DetectBase64DecodeDoMatch(det_ctx, s, smd, buffer, buffer_len)) {
640  if (s->sm_arrays[DETECT_SM_LIST_BASE64_DATA] != NULL) {
641  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
642  if (DetectBase64DataDoMatch(de_ctx, det_ctx, s, f)) {
643  /* Base64 is a terminal list. */
644  goto final_match;
645  }
646  }
647  }
648  } else {
649  SCLogDebug("sm->type %u", smd->type);
650 #ifdef DEBUG
651  BUG_ON(1);
652 #endif
653  }
654 
655 no_match:
656  KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
657  SCReturnInt(0);
658 
659 match:
660  /* this sigmatch matched, inspect the next one. If it was the last,
661  * the buffer portion of the signature matched. */
662  if (!smd->is_last) {
663  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
664  uint8_t r = DetectEngineContentInspection(de_ctx, det_ctx, s, smd + 1, p, f, buffer,
665  buffer_len, stream_start_offset, flags, inspection_mode);
666  SCReturnInt(r);
667  }
668 final_match:
669  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
670  SCReturnInt(1);
671 }
672 
673 #ifdef UNITTESTS
675 #endif
DetectEngineThreadCtx_::byte_values
uint64_t * byte_values
Definition: detect.h:1146
DetectContentData_::offset
uint16_t offset
Definition: detect-content.h:102
DetectDatasetData_
Definition: detect-dataset.h:36
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:1055
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
DetectContentData_::within
int32_t within
Definition: detect-content.h:104
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
DetectIsdataatData_::flags
uint8_t flags
Definition: detect-isdataat.h:37
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
DetectBytetestData_::flags
uint8_t flags
Definition: detect-bytetest.h:57
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:327
detect-bsize.h
Flow_
Flow data structure.
Definition: flow.h:357
DetectEngineCtx_::inspection_recursion_limit
int inspection_recursion_limit
Definition: detect.h:860
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:787
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:1140
DetectIsdataatData_
Definition: detect-isdataat.h:35
DetectContentData_
Definition: detect-content.h:88
DetectPcreData_::flags
uint16_t flags
Definition: detect-pcre.h:47
DetectBytetestData_
Definition: detect-bytetest.h:52
SigMatchData_
Data needed for Match()
Definition: detect.h:324
detect-pcre.h
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:70
DETECT_CI_FLAGS_DCE_BE
#define DETECT_CI_FLAGS_DCE_BE
Definition: detect-engine-content-inspection.h:43
SigMatchData_::type
uint16_t type
Definition: detect.h:325
DetectBytejumpData_
Definition: detect-bytejump.h:44
util-unittest.h
DetectBytejumpData_::offset
int32_t offset
Definition: detect-bytejump.h:49
util-unittest-helper.h
KEYWORD_PROFILING_END
#define KEYWORD_PROFILING_END(ctx, type, m)
Definition: util-profiling.h:84
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
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:1027
DETECT_BSIZE
@ DETECT_BSIZE
Definition: detect-engine-register.h:108
DETECT_SM_LIST_BASE64_DATA
@ DETECT_SM_LIST_BASE64_DATA
Definition: detect.h:86
DETECT_LUA
@ DETECT_LUA
Definition: detect-engine-register.h:220
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
DetectBytejumpDoMatch
int DetectBytejumpDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchCtx *ctx, const uint8_t *payload, uint32_t payload_len, uint16_t flags, int32_t offset)
Byte jump match function.
Definition: detect-bytejump.c:96
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
DetectBase64DataDoMatch
int DetectBase64DataDoMatch(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, Flow *f)
Definition: detect-base64-data.c:64
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
DetectContentData_::id
PatIntId id
Definition: detect-content.h:100
DetectByteExtractData_::endian
uint8_t endian
Definition: detect-byte-extract.h:52
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:289
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:101
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:428
ISDATAAT_RELATIVE
#define ISDATAAT_RELATIVE
Definition: detect-isdataat.h:27
detect-bytejump.h
DETECT_CI_FLAGS_END
#define DETECT_CI_FLAGS_END
Definition: detect-engine-content-inspection.h:40
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:99
detect-replace.h
DetectReplaceAddToList
DetectReplaceList * DetectReplaceAddToList(DetectReplaceList *replist, uint8_t *found, DetectContentData *cd)
Definition: detect-replace.c:185
DETECT_PCRE
@ DETECT_PCRE
Definition: detect-engine-register.h:64
detect-engine-content-inspection.h
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1094
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
DetectEngineContentInspection
uint8_t DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode)
Run the actual payload match functions.
Definition: detect-engine-content-inspection.c:106
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
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
SigMatchData_::is_last
uint8_t is_last
Definition: detect.h:326
suricata-common.h
detect-byte-extract.h
DetectContentData_::distance
int32_t distance
Definition: detect-content.h:103
DETECT_CONTENT_WITHIN_NEXT
#define DETECT_CONTENT_WITHIN_NEXT
Definition: detect-content.h:57
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1113
DetectBytetestDoMatch
int DetectBytetestDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchCtx *ctx, const uint8_t *payload, uint32_t payload_len, uint8_t flags, int32_t offset, uint64_t value)
Bytetest detection code.
Definition: detect-bytetest.c:105
util-spm.h
detect-dataset.h
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:106
DetectEngineThreadCtx_::pcre_match_start_offset
uint32_t pcre_match_start_offset
Definition: detect.h:1057
detect-datarep.h
DETECT_BYTE_EXTRACT
@ DETECT_BYTE_EXTRACT
Definition: detect-engine-register.h:178
detect-parse.h
Signature_
Signature container.
Definition: detect.h:542
DETECT_ISDATAAT
@ DETECT_ISDATAAT
Definition: detect-engine-register.h:82
DETECT_ENGINE_CONTENT_INSPECTION_MODE_PAYLOAD
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_PAYLOAD
Definition: detect-engine-content-inspection.h:32
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:59
DetectIsdataatData_::dataat
uint16_t dataat
Definition: detect-isdataat.h:36
DetectByteMathDoMatch
int DetectByteMathDoMatch(DetectEngineThreadCtx *det_ctx, const SigMatchData *smd, const Signature *s, const uint8_t *payload, uint16_t payload_len, uint64_t rvalue, uint64_t *value, uint8_t endian)
Definition: detect-bytemath.c:79
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:90
DETECT_BYTEMATH
@ DETECT_BYTEMATH
Definition: detect-engine-register.h:77
DETECT_BASE64_DECODE
@ DETECT_BASE64_DECODE
Definition: detect-engine-register.h:250
detect-uricontent.h
DetectEngineThreadCtx_::replist
DetectReplaceList * replist
Definition: detect.h:1149
DETECT_CONTENT_REPLACE
#define DETECT_CONTENT_REPLACE
Definition: detect-content.h:51
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:131
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
DETECT_DATASET
@ DETECT_DATASET
Definition: detect-engine-register.h:101
DetectBytetestData_::value
uint64_t value
Definition: detect-bytetest.h:61
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:42
DetectBytejumpData_::flags
uint16_t flags
Definition: detect-bytejump.h:47
detect-bytemath.h
DETECT_CONTENT_OFFSET_VAR
#define DETECT_CONTENT_OFFSET_VAR
Definition: detect-content.h:45
detect-bytetest.h