suricata
detect-filemagic.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2014 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 Victor Julien <victor@inliniac.net>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "threads.h"
27 #include "debug.h"
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 
33 #include "detect-engine.h"
34 #include "detect-engine-mpm.h"
37 
38 #include "flow.h"
39 #include "flow-var.h"
40 #include "flow-util.h"
41 
42 #include "util-debug.h"
43 #include "util-spm-bm.h"
44 #include "util-magic.h"
45 #include "util-print.h"
46 
47 #include "util-unittest.h"
48 #include "util-unittest-helper.h"
49 
50 #include "app-layer.h"
51 #include "app-layer-parser.h"
52 
53 #include "stream-tcp.h"
54 
55 #include "detect-filemagic.h"
56 
57 #include "conf.h"
58 
59 #ifndef HAVE_MAGIC
60 
61 static int DetectFilemagicSetupNoSupport (DetectEngineCtx *de_ctx, Signature *s, const char *str)
62 {
63  SCLogError(SC_ERR_NO_MAGIC_SUPPORT, "no libmagic support built in, needed for filemagic keyword");
64  return -1;
65 }
66 
67 /**
68  * \brief Registration function for keyword: filemagic
69  */
71 {
72  sigmatch_table[DETECT_FILEMAGIC].name = "filemagic";
73  sigmatch_table[DETECT_FILEMAGIC].desc = "match on the information libmagic returns about a file";
74  sigmatch_table[DETECT_FILEMAGIC].url = "/rules/file-keywords.html#filemagic";
75  sigmatch_table[DETECT_FILEMAGIC].Setup = DetectFilemagicSetupNoSupport;
77 }
78 
79 #else /* HAVE_MAGIC */
80 
81 static int DetectFilemagicMatch (DetectEngineThreadCtx *, Flow *,
82  uint8_t, File *, const Signature *, const SigMatchCtx *);
83 static int DetectFilemagicSetup (DetectEngineCtx *, Signature *, const char *);
84 #ifdef UNITTESTS
85 static void DetectFilemagicRegisterTests(void);
86 #endif
87 static void DetectFilemagicFree(DetectEngineCtx *, void *);
88 static int g_file_match_list_id = 0;
89 
90 static int DetectFilemagicSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
91 static int g_file_magic_buffer_id = 0;
92 
93 static int PrefilterMpmFilemagicRegister(DetectEngineCtx *de_ctx,
94  SigGroupHead *sgh, MpmCtx *mpm_ctx,
95  const DetectBufferMpmRegistery *mpm_reg, int list_id);
96 static int DetectEngineInspectFilemagic(
98  const DetectEngineAppInspectionEngine *engine,
99  const Signature *s,
100  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
101 
102 static int g_magic_thread_ctx_id = -1;
103 
104 /**
105  * \brief Registration function for keyword: filemagic
106  */
107 void DetectFilemagicRegister(void)
108 {
109  sigmatch_table[DETECT_FILEMAGIC].name = "filemagic";
110  sigmatch_table[DETECT_FILEMAGIC].desc = "match on the information libmagic returns about a file";
111  sigmatch_table[DETECT_FILEMAGIC].url = "/rules/file-keywords.html#filemagic";
112  sigmatch_table[DETECT_FILEMAGIC].FileMatch = DetectFilemagicMatch;
113  sigmatch_table[DETECT_FILEMAGIC].Setup = DetectFilemagicSetup;
114  sigmatch_table[DETECT_FILEMAGIC].Free = DetectFilemagicFree;
115 #ifdef UNITTESTS
116  sigmatch_table[DETECT_FILEMAGIC].RegisterTests = DetectFilemagicRegisterTests;
117 #endif
120 
121  sigmatch_table[DETECT_FILE_MAGIC].name = "file.magic";
122  sigmatch_table[DETECT_FILE_MAGIC].desc = "sticky buffer to match on the file magic";
123  sigmatch_table[DETECT_FILE_MAGIC].url = "/rules/file-keywords.html#filemagic";
124  sigmatch_table[DETECT_FILE_MAGIC].Setup = DetectFilemagicSetupSticky;
126 
127  g_file_match_list_id = DetectBufferTypeRegister("files");
128 
130  ALPROTO_HTTP2, 0 };
132  0 };
133 
134  for (int i = 0; protos_ts[i] != 0; i++) {
135  DetectAppLayerInspectEngineRegister2("file.magic", protos_ts[i],
137  DetectEngineInspectFilemagic, NULL);
138 
140  PrefilterMpmFilemagicRegister, NULL, protos_ts[i],
141  0);
142  }
143  for (int i = 0; protos_tc[i] != 0; i++) {
144  DetectAppLayerInspectEngineRegister2("file.magic", protos_tc[i],
146  DetectEngineInspectFilemagic, NULL);
147 
149  PrefilterMpmFilemagicRegister, NULL, protos_tc[i],
150  0);
151  }
152 
154  "file magic");
155 
156  g_file_magic_buffer_id = DetectBufferTypeGetByName("file.magic");
157  SCLogDebug("registering filemagic rule option");
158  return;
159 }
160 
161 #define FILEMAGIC_MIN_SIZE 512
162 
163 /**
164  * \brief run the magic check
165  *
166  * \param file the file
167  *
168  * \retval -1 error
169  * \retval 0 ok
170  */
171 int FilemagicThreadLookup(magic_t *ctx, File *file)
172 {
173  if (ctx == NULL || file == NULL || FileDataSize(file) == 0) {
174  SCReturnInt(-1);
175  }
176 
177  const uint8_t *data = NULL;
178  uint32_t data_len = 0;
179  uint64_t offset = 0;
180 
182  &data, &data_len, &offset);
183  if (offset == 0) {
184  if (FileDataSize(file) >= FILEMAGIC_MIN_SIZE) {
185  file->magic = MagicThreadLookup(ctx, data, data_len);
186  } else if (file->state >= FILE_STATE_CLOSED) {
187  file->magic = MagicThreadLookup(ctx, data, data_len);
188  }
189  }
190  SCReturnInt(0);
191 }
192 
193 /**
194  * \brief match the specified filemagic
195  *
196  * \param t thread local vars
197  * \param det_ctx pattern matcher thread local data
198  * \param f *LOCKED* flow
199  * \param flags direction flags
200  * \param file file being inspected
201  * \param s signature being inspected
202  * \param m sigmatch that we will cast into DetectFilemagicData
203  *
204  * \retval 0 no match
205  * \retval 1 match
206  */
207 static int DetectFilemagicMatch (DetectEngineThreadCtx *det_ctx,
208  Flow *f, uint8_t flags, File *file, const Signature *s, const SigMatchCtx *m)
209 {
210  SCEnter();
211  int ret = 0;
212  DetectFilemagicData *filemagic = (DetectFilemagicData *)m;
213 
214  DetectFilemagicThreadData *tfilemagic =
215  (DetectFilemagicThreadData *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, g_magic_thread_ctx_id);
216  if (tfilemagic == NULL) {
217  SCReturnInt(0);
218  }
219 
220  if (file->magic == NULL) {
221  FilemagicThreadLookup(&tfilemagic->ctx, file);
222  }
223 
224  if (file->magic != NULL) {
225  SCLogDebug("magic %s", file->magic);
226 
227  /* we include the \0 in the inspection, so patterns can match on the
228  * end of the string. */
229  if (BoyerMooreNocase(filemagic->name, filemagic->len, (uint8_t *)file->magic,
230  strlen(file->magic) + 1, filemagic->bm_ctx) != NULL)
231  {
232 #ifdef DEBUG
233  if (SCLogDebugEnabled()) {
234  char *name = SCMalloc(filemagic->len + 1);
235  if (name != NULL) {
236  memcpy(name, filemagic->name, filemagic->len);
237  name[filemagic->len] = '\0';
238  SCLogDebug("will look for filemagic %s", name);
239  SCFree(name);
240  }
241  }
242 #endif
243 
244  if (!(filemagic->flags & DETECT_CONTENT_NEGATED)) {
245  ret = 1;
246  }
247  } else if (filemagic->flags & DETECT_CONTENT_NEGATED) {
248  SCLogDebug("negated match");
249  ret = 1;
250  }
251  }
252 
253  SCReturnInt(ret);
254 }
255 
256 /**
257  * \brief Parse the filemagic keyword
258  *
259  * \param de_ctx Pointer to the detection engine context
260  * \param idstr Pointer to the user provided option
261  *
262  * \retval filemagic pointer to DetectFilemagicData on success
263  * \retval NULL on failure
264  */
265 static DetectFilemagicData *DetectFilemagicParse (DetectEngineCtx *de_ctx, const char *str, bool negate)
266 {
267  DetectFilemagicData *filemagic = NULL;
268 
269  /* We have a correct filemagic option */
270  filemagic = SCMalloc(sizeof(DetectFilemagicData));
271  if (unlikely(filemagic == NULL))
272  goto error;
273 
274  memset(filemagic, 0x00, sizeof(DetectFilemagicData));
275 
276  if (DetectContentDataParse ("filemagic", str, &filemagic->name, &filemagic->len) == -1) {
277  goto error;
278  }
279 
280  filemagic->bm_ctx = BoyerMooreNocaseCtxInit(filemagic->name, filemagic->len);
281  if (filemagic->bm_ctx == NULL) {
282  goto error;
283  }
284 
285  if (negate) {
286  filemagic->flags |= DETECT_CONTENT_NEGATED;
287  }
288 
289  SCLogDebug("flags %02X", filemagic->flags);
290  if (filemagic->flags & DETECT_CONTENT_NEGATED) {
291  SCLogDebug("negated filemagic");
292  }
293 
294 #ifdef DEBUG
295  if (SCLogDebugEnabled()) {
296  char *name = SCMalloc(filemagic->len + 1);
297  if (name != NULL) {
298  memcpy(name, filemagic->name, filemagic->len);
299  name[filemagic->len] = '\0';
300  SCLogDebug("will look for filemagic %s", name);
301  SCFree(name);
302  }
303  }
304 #endif
305 
306  return filemagic;
307 
308 error:
309  if (filemagic != NULL)
310  DetectFilemagicFree(de_ctx, filemagic);
311  return NULL;
312 }
313 
314 static void *DetectFilemagicThreadInit(void *data /*@unused@*/)
315 {
316  DetectFilemagicThreadData *t = SCCalloc(1, sizeof(DetectFilemagicThreadData));
317  if (unlikely(t == NULL)) {
318  SCLogError(SC_ERR_MEM_ALLOC, "couldn't alloc ctx memory");
319  return NULL;
320  }
321 
322  t->ctx = MagicInitContext();
323  if (t->ctx == NULL)
324  goto error;
325 
326  return (void *)t;
327 
328 error:
329  if (t->ctx)
330  magic_close(t->ctx);
331  SCFree(t);
332  return NULL;
333 }
334 
335 static void DetectFilemagicThreadFree(void *ctx)
336 {
337  if (ctx != NULL) {
338  DetectFilemagicThreadData *t = (DetectFilemagicThreadData *)ctx;
339  if (t->ctx)
340  magic_close(t->ctx);
341  SCFree(t);
342  }
343 }
344 
345 /**
346  * \brief this function is used to parse filemagic options
347  * \brief into the current signature
348  *
349  * \param de_ctx pointer to the Detection Engine Context
350  * \param s pointer to the Current Signature
351  * \param str pointer to the user provided "filemagic" option
352  *
353  * \retval 0 on Success
354  * \retval -1 on Failure
355  */
356 static int DetectFilemagicSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
357 {
358  SigMatch *sm = NULL;
359 
360  DetectFilemagicData *filemagic = DetectFilemagicParse(de_ctx, str, s->init_data->negated);
361  if (filemagic == NULL)
362  return -1;
363 
364  g_magic_thread_ctx_id = DetectRegisterThreadCtxFuncs(
365  de_ctx, "filemagic", DetectFilemagicThreadInit, NULL, DetectFilemagicThreadFree, 1);
366  if (g_magic_thread_ctx_id == -1)
367  goto error;
368 
369  /* Okay so far so good, lets get this into a SigMatch
370  * and put it in the Signature. */
371  sm = SigMatchAlloc();
372  if (sm == NULL)
373  goto error;
374 
375  sm->type = DETECT_FILEMAGIC;
376  sm->ctx = (void *)filemagic;
377 
378  SigMatchAppendSMToList(s, sm, g_file_match_list_id);
379 
381  return 0;
382 
383 error:
384  DetectFilemagicFree(de_ctx, filemagic);
385  if (sm != NULL)
386  SCFree(sm);
387  return -1;
388 }
389 
390 /**
391  * \brief this function will free memory associated with DetectFilemagicData
392  *
393  * \param filemagic pointer to DetectFilemagicData
394  */
395 static void DetectFilemagicFree(DetectEngineCtx *de_ctx, void *ptr)
396 {
397  if (ptr != NULL) {
398  DetectFilemagicData *filemagic = (DetectFilemagicData *)ptr;
399  if (filemagic->bm_ctx != NULL) {
400  BoyerMooreCtxDeInit(filemagic->bm_ctx);
401  }
402  if (filemagic->name != NULL)
403  SCFree(filemagic->name);
404  SCFree(filemagic);
405  }
406 }
407 
408 /* file.magic implementation */
409 
410 /**
411  * \brief this function setup the file.magic keyword used in the rule
412  *
413  * \param de_ctx Pointer to the Detection Engine Context
414  * \param s Pointer to the Signature to which the current keyword belongs
415  * \param str Should hold an empty string always
416  *
417  * \retval 0 On success
418  */
419 static int DetectFilemagicSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
420 {
421  if (DetectBufferSetActiveList(s, g_file_magic_buffer_id) < 0)
422  return -1;
423 
424  if (g_magic_thread_ctx_id == -1) {
425  g_magic_thread_ctx_id = DetectRegisterThreadCtxFuncs(de_ctx, "filemagic",
426  DetectFilemagicThreadInit, NULL,
427  DetectFilemagicThreadFree, 1);
428  if (g_magic_thread_ctx_id == -1)
429  return -1;
430  }
431  return 0;
432 }
433 
434 static InspectionBuffer *FilemagicGetDataCallback(DetectEngineThreadCtx *det_ctx,
435  const DetectEngineTransforms *transforms,
436  Flow *f, uint8_t flow_flags, File *cur_file,
437  int list_id, int local_file_id, bool first)
438 {
439  SCEnter();
440 
441  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_file_id);
442  if (buffer == NULL)
443  return NULL;
444  if (!first && buffer->inspect != NULL)
445  return buffer;
446 
447  if (cur_file->magic == NULL) {
448  DetectFilemagicThreadData *tfilemagic =
449  (DetectFilemagicThreadData *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, g_magic_thread_ctx_id);
450  if (tfilemagic == NULL) {
451  return NULL;
452  }
453 
454  FilemagicThreadLookup(&tfilemagic->ctx, cur_file);
455  }
456  if (cur_file->magic == NULL) {
457  return NULL;
458  }
459 
460  const uint8_t *data = (const uint8_t *)cur_file->magic;
461  uint32_t data_len = (uint32_t)strlen(cur_file->magic);
462 
463  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
464 
465  SCReturnPtr(buffer, "InspectionBuffer");
466 }
467 
468 static int DetectEngineInspectFilemagic(
470  const DetectEngineAppInspectionEngine *engine,
471  const Signature *s,
472  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
473 {
474  const DetectEngineTransforms *transforms = NULL;
475  if (!engine->mpm) {
476  transforms = engine->v2.transforms;
477  }
478 
480  if (ffc == NULL) {
482  }
483 
485  int local_file_id = 0;
486  for (File *file = ffc->head; file != NULL; file = file->next) {
487  if (file->txid != tx_id)
488  continue;
489 
490  InspectionBuffer *buffer = FilemagicGetDataCallback(det_ctx,
491  transforms, f, flags, file, engine->sm_list, local_file_id, false);
492  if (buffer == NULL)
493  continue;
494 
495  det_ctx->buffer_offset = 0;
496  det_ctx->discontinue_matching = 0;
497  det_ctx->inspection_recursion_counter = 0;
498  int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
499  NULL, f,
500  (uint8_t *)buffer->inspect,
501  buffer->inspect_len,
504  if (match == 1) {
506  } else {
508  }
509  local_file_id++;
510  }
511  return r;
512 }
513 
514 typedef struct PrefilterMpmFilemagic {
515  int list_id;
516  const MpmCtx *mpm_ctx;
517  const DetectEngineTransforms *transforms;
518 } PrefilterMpmFilemagic;
519 
520 /** \brief Filedata Filedata Mpm prefilter callback
521  *
522  * \param det_ctx detection engine thread ctx
523  * \param p packet to inspect
524  * \param f flow to inspect
525  * \param txv tx to inspect
526  * \param pectx inspection context
527  */
528 static void PrefilterTxFilemagic(DetectEngineThreadCtx *det_ctx,
529  const void *pectx,
530  Packet *p, Flow *f, void *txv,
531  const uint64_t idx, const uint8_t flags)
532 {
533  SCEnter();
534 
535  const PrefilterMpmFilemagic *ctx = (const PrefilterMpmFilemagic *)pectx;
536  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
537  const int list_id = ctx->list_id;
538 
540  if (ffc != NULL) {
541  int local_file_id = 0;
542  for (File *file = ffc->head; file != NULL; file = file->next) {
543  if (file->txid != idx)
544  continue;
545 
546  InspectionBuffer *buffer = FilemagicGetDataCallback(det_ctx,
547  ctx->transforms, f, flags, file, list_id, local_file_id, true);
548  if (buffer == NULL)
549  continue;
550 
551  if (buffer->inspect_len >= mpm_ctx->minlen) {
552  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
553  &det_ctx->mtcu, &det_ctx->pmq,
554  buffer->inspect, buffer->inspect_len);
555  }
556  local_file_id++;
557  }
558  }
559 }
560 
561 static void PrefilterMpmFilemagicFree(void *ptr)
562 {
563  SCFree(ptr);
564 }
565 
566 static int PrefilterMpmFilemagicRegister(DetectEngineCtx *de_ctx,
567  SigGroupHead *sgh, MpmCtx *mpm_ctx,
568  const DetectBufferMpmRegistery *mpm_reg, int list_id)
569 {
570  PrefilterMpmFilemagic *pectx = SCCalloc(1, sizeof(*pectx));
571  if (pectx == NULL)
572  return -1;
573  pectx->list_id = list_id;
574  pectx->mpm_ctx = mpm_ctx;
575  pectx->transforms = &mpm_reg->transforms;
576 
577  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxFilemagic,
578  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
579  pectx, PrefilterMpmFilemagicFree, mpm_reg->pname);
580 }
581 #ifdef UNITTESTS /* UNITTESTS */
582 
583 /**
584  * \test DetectFilemagicTestParse01
585  */
586 static int DetectFilemagicTestParse01 (void)
587 {
588  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "secret.pdf", false);
589  if (dnd != NULL) {
590  DetectFilemagicFree(NULL, dnd);
591  return 1;
592  }
593  return 0;
594 }
595 
596 /**
597  * \test DetectFilemagicTestParse02
598  */
599 static int DetectFilemagicTestParse02 (void)
600 {
601  int result = 0;
602 
603  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "backup.tar.gz", false);
604  if (dnd != NULL) {
605  if (dnd->len == 13 && memcmp(dnd->name, "backup.tar.gz", 13) == 0) {
606  result = 1;
607  }
608 
609  DetectFilemagicFree(NULL, dnd);
610  return result;
611  }
612  return 0;
613 }
614 
615 /**
616  * \test DetectFilemagicTestParse03
617  */
618 static int DetectFilemagicTestParse03 (void)
619 {
620  int result = 0;
621 
622  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "cmd.exe", false);
623  if (dnd != NULL) {
624  if (dnd->len == 7 && memcmp(dnd->name, "cmd.exe", 7) == 0) {
625  result = 1;
626  }
627 
628  DetectFilemagicFree(NULL, dnd);
629  return result;
630  }
631  return 0;
632 }
633 
634 /**
635  * \brief this function registers unit tests for DetectFilemagic
636  */
637 void DetectFilemagicRegisterTests(void)
638 {
639  UtRegisterTest("DetectFilemagicTestParse01", DetectFilemagicTestParse01);
640  UtRegisterTest("DetectFilemagicTestParse02", DetectFilemagicTestParse02);
641  UtRegisterTest("DetectFilemagicTestParse03", DetectFilemagicTestParse03);
642 }
643 #endif /* UNITTESTS */
644 #endif /* HAVE_MAGIC */
645 
DetectEngineAppInspectionEngine_
Definition: detect.h:398
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:402
FileContainer_
Definition: util-file.h:110
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1088
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1477
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1267
stream-tcp.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1425
DetectThreadCtxGetKeywordThreadCtx
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3488
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:379
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:80
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:648
InspectionBuffer
Definition: detect.h:345
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
SigTableElmt_::FileMatch
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1243
File_::state
FileState state
Definition: util-file.h:78
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1167
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1261
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@87::@89 app_v2
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
StreamingBufferGetData
int StreamingBufferGetData(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t *stream_offset)
Definition: util-streaming-buffer.c:832
AppLayerParserGetFiles
FileContainer * AppLayerParserGetFiles(const Flow *f, const uint8_t direction)
Definition: app-layer-parser.c:887
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:31
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:634
m
SCMutex m
Definition: flow-hash.h:6
DetectEngineContentInspection
int 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:102
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@84 v2
detect-engine-prefilter.h
DetectFilemagicRegister
void DetectFilemagicRegister(void)
Registration function for keyword: filemagic.
Definition: detect-filemagic.c:70
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1165
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1077
File_::sb
StreamingBuffer * sb
Definition: util-file.h:79
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:404
SIGMATCH_QUOTES_MANDATORY
#define SIGMATCH_QUOTES_MANDATORY
Definition: detect.h:1469
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
Definition: detect-engine-state.h:44
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
decode.h
util-debug.h
DETECT_FILE_MAGIC
@ DETECT_FILE_MAGIC
Definition: detect-engine-register.h:206
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
FILE_SIG_NEED_MAGIC
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:287
ALPROTO_SMTP
@ ALPROTO_SMTP
Definition: app-layer-protos.h:32
util-print.h
detect-filemagic.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
FileContainer_::head
File * head
Definition: util-file.h:111
detect.h
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:39
DETECT_FILEMAGIC
@ DETECT_FILEMAGIC
Definition: detect-engine-register.h:205
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:347
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1265
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
BoyerMooreNocase
uint8_t * BoyerMooreNocase(const uint8_t *x, uint16_t m, const uint8_t *y, uint32_t n, BmCtx *bm_ctx)
Boyer Moore search algorithm Is better as the pattern length increases and for big buffers to search ...
Definition: util-spm-bm.c:353
Packet_
Definition: decode.h:427
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:225
conf.h
util-magic.h
SIGMATCH_HANDLE_NEGATION
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1473
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:619
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
SignatureInitData_::negated
bool negated
Definition: detect.h:503
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:62
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:165
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
FileDataSize
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:294
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:316
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1127
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:415
DetectContentDataParse
int DetectContentDataParse(const char *keyword, const char *contentstr, uint8_t **pstr, uint16_t *plen)
Parse a content string, ie "abc|DE|fgh".
Definition: detect-content.c:82
FILE_STATE_CLOSED
@ FILE_STATE_CLOSED
Definition: util-file.h:67
File_
Definition: util-file.h:75
DetectAppLayerMpmRegister2
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
Definition: detect-engine-mpm.c:89
SC_ERR_NO_MAGIC_SUPPORT
@ SC_ERR_NO_MAGIC_SUPPORT
Definition: util-error.h:326
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:47
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1023
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:636
flags
uint8_t flags
Definition: decode-gre.h:0
DetectRegisterThreadCtxFuncs
int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *), int mode)
Register Thread keyword context Funcs.
Definition: detect-engine.c:3418
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:322
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:563
ALPROTO_HTTP1
@ ALPROTO_HTTP1
Definition: app-layer-protos.h:30
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1144
File_::next
struct File_ * next
Definition: util-file.h:89
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
util-spm-bm.h
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1427
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:38
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:348
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:346
str
#define str(s)
Definition: suricata-common.h:272
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags), AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:270
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:412
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1380
FILE_SIG_NEED_FILE
#define FILE_SIG_NEED_FILE
Definition: detect.h:285
File_::txid
uint64_t txid
Definition: util-file.h:80
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1453
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1291
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1174
MpmCtx_
Definition: util-mpm.h:88
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
flow-var.h
BoyerMooreNocaseCtxInit
BmCtx * BoyerMooreNocaseCtxInit(uint8_t *needle, uint16_t needle_len)
Setup a Booyer Moore context for nocase search.
Definition: util-spm-bm.c:107
SCLogDebugEnabled
int SCLogDebugEnabled(void)
Returns whether debug messages are enabled to be logged or not.
Definition: util-debug.c:647
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
app-layer.h
BoyerMooreCtxDeInit
void BoyerMooreCtxDeInit(BmCtx *bmctx)
Free the memory allocated to Booyer Moore context.
Definition: util-spm-bm.c:121