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 = DOC_URL DOC_VERSION "/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 static void DetectFilemagicRegisterTests(void);
85 static void DetectFilemagicFree(void *);
86 static int g_file_match_list_id = 0;
87 
88 static int DetectFilemagicSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
89 static int g_file_magic_buffer_id = 0;
90 
91 static int PrefilterMpmFilemagicRegister(DetectEngineCtx *de_ctx,
92  SigGroupHead *sgh, MpmCtx *mpm_ctx,
93  const DetectBufferMpmRegistery *mpm_reg, int list_id);
94 static int DetectEngineInspectFilemagic(
95  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
96  const DetectEngineAppInspectionEngine *engine,
97  const Signature *s,
98  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
99 
100 static int g_magic_thread_ctx_id = -1;
101 
102 /**
103  * \brief Registration function for keyword: filemagic
104  */
105 void DetectFilemagicRegister(void)
106 {
107  sigmatch_table[DETECT_FILEMAGIC].name = "filemagic";
108  sigmatch_table[DETECT_FILEMAGIC].desc = "match on the information libmagic returns about a file";
109  sigmatch_table[DETECT_FILEMAGIC].url = DOC_URL DOC_VERSION "/rules/file-keywords.html#filemagic";
110  sigmatch_table[DETECT_FILEMAGIC].FileMatch = DetectFilemagicMatch;
111  sigmatch_table[DETECT_FILEMAGIC].Setup = DetectFilemagicSetup;
112  sigmatch_table[DETECT_FILEMAGIC].Free = DetectFilemagicFree;
113  sigmatch_table[DETECT_FILEMAGIC].RegisterTests = DetectFilemagicRegisterTests;
116 
117  sigmatch_table[DETECT_FILE_MAGIC].name = "file.magic";
118  sigmatch_table[DETECT_FILE_MAGIC].desc = "sticky buffer to match on the file magic";
119  sigmatch_table[DETECT_FILE_MAGIC].url = DOC_URL DOC_VERSION "/rules/file-keywords.html#filemagic";
120  sigmatch_table[DETECT_FILE_MAGIC].Setup = DetectFilemagicSetupSticky;
122 
123  g_file_match_list_id = DetectBufferTypeRegister("files");
124 
125  AppProto protos_ts[] = {
127  AppProto protos_tc[] = {
129 
130  for (int i = 0; protos_ts[i] != 0; i++) {
131  DetectAppLayerInspectEngineRegister2("file.magic", protos_ts[i],
133  DetectEngineInspectFilemagic, NULL);
134 
136  PrefilterMpmFilemagicRegister, NULL, protos_ts[i],
137  0);
138  }
139  for (int i = 0; protos_tc[i] != 0; i++) {
140  DetectAppLayerInspectEngineRegister2("file.magic", protos_tc[i],
142  DetectEngineInspectFilemagic, NULL);
143 
145  PrefilterMpmFilemagicRegister, NULL, protos_tc[i],
146  0);
147  }
148 
150  "file magic");
151 
152  g_file_magic_buffer_id = DetectBufferTypeGetByName("file.magic");
153  SCLogDebug("registering filemagic rule option");
154  return;
155 }
156 
157 #define FILEMAGIC_MIN_SIZE 512
158 
159 /**
160  * \brief run the magic check
161  *
162  * \param file the file
163  *
164  * \retval -1 error
165  * \retval 0 ok
166  */
167 int FilemagicGlobalLookup(File *file)
168 {
169  if (file == NULL || FileDataSize(file) == 0) {
170  SCReturnInt(-1);
171  }
172 
173  const uint8_t *data = NULL;
174  uint32_t data_len = 0;
175  uint64_t offset = 0;
176 
178  &data, &data_len, &offset);
179  if (offset == 0) {
180  if (FileDataSize(file) >= FILEMAGIC_MIN_SIZE) {
181  file->magic = MagicGlobalLookup(data, data_len);
182  } else if (file->state >= FILE_STATE_CLOSED) {
183  file->magic = MagicGlobalLookup(data, data_len);
184  }
185  }
186 
187  SCReturnInt(0);
188 }
189 
190 /**
191  * \brief run the magic check
192  *
193  * \param file the file
194  *
195  * \retval -1 error
196  * \retval 0 ok
197  */
198 static int FilemagicThreadLookup(magic_t *ctx, File *file)
199 {
200  if (ctx == NULL || file == NULL || FileDataSize(file) == 0) {
201  SCReturnInt(-1);
202  }
203 
204  const uint8_t *data = NULL;
205  uint32_t data_len = 0;
206  uint64_t offset = 0;
207 
209  &data, &data_len, &offset);
210  if (offset == 0) {
211  if (FileDataSize(file) >= FILEMAGIC_MIN_SIZE) {
212  file->magic = MagicThreadLookup(ctx, data, data_len);
213  } else if (file->state >= FILE_STATE_CLOSED) {
214  file->magic = MagicThreadLookup(ctx, data, data_len);
215  }
216  }
217  SCReturnInt(0);
218 }
219 
220 /**
221  * \brief match the specified filemagic
222  *
223  * \param t thread local vars
224  * \param det_ctx pattern matcher thread local data
225  * \param f *LOCKED* flow
226  * \param flags direction flags
227  * \param file file being inspected
228  * \param s signature being inspected
229  * \param m sigmatch that we will cast into DetectFilemagicData
230  *
231  * \retval 0 no match
232  * \retval 1 match
233  */
234 static int DetectFilemagicMatch (DetectEngineThreadCtx *det_ctx,
235  Flow *f, uint8_t flags, File *file, const Signature *s, const SigMatchCtx *m)
236 {
237  SCEnter();
238  int ret = 0;
239  DetectFilemagicData *filemagic = (DetectFilemagicData *)m;
240 
241  DetectFilemagicThreadData *tfilemagic =
242  (DetectFilemagicThreadData *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, g_magic_thread_ctx_id);
243  if (tfilemagic == NULL) {
244  SCReturnInt(0);
245  }
246 
247  if (file->magic == NULL) {
248  FilemagicThreadLookup(&tfilemagic->ctx, file);
249  }
250 
251  if (file->magic != NULL) {
252  SCLogDebug("magic %s", file->magic);
253 
254  /* we include the \0 in the inspection, so patterns can match on the
255  * end of the string. */
256  if (BoyerMooreNocase(filemagic->name, filemagic->len, (uint8_t *)file->magic,
257  strlen(file->magic) + 1, filemagic->bm_ctx) != NULL)
258  {
259 #ifdef DEBUG
260  if (SCLogDebugEnabled()) {
261  char *name = SCMalloc(filemagic->len + 1);
262  if (name != NULL) {
263  memcpy(name, filemagic->name, filemagic->len);
264  name[filemagic->len] = '\0';
265  SCLogDebug("will look for filemagic %s", name);
266  SCFree(name);
267  }
268  }
269 #endif
270 
271  if (!(filemagic->flags & DETECT_CONTENT_NEGATED)) {
272  ret = 1;
273  }
274  } else if (filemagic->flags & DETECT_CONTENT_NEGATED) {
275  SCLogDebug("negated match");
276  ret = 1;
277  }
278  }
279 
280  SCReturnInt(ret);
281 }
282 
283 /**
284  * \brief Parse the filemagic keyword
285  *
286  * \param idstr Pointer to the user provided option
287  *
288  * \retval filemagic pointer to DetectFilemagicData on success
289  * \retval NULL on failure
290  */
291 static DetectFilemagicData *DetectFilemagicParse (const char *str, bool negate)
292 {
293  DetectFilemagicData *filemagic = NULL;
294 
295  /* We have a correct filemagic option */
296  filemagic = SCMalloc(sizeof(DetectFilemagicData));
297  if (unlikely(filemagic == NULL))
298  goto error;
299 
300  memset(filemagic, 0x00, sizeof(DetectFilemagicData));
301 
302  if (DetectContentDataParse ("filemagic", str, &filemagic->name, &filemagic->len) == -1) {
303  goto error;
304  }
305 
306  filemagic->bm_ctx = BoyerMooreNocaseCtxInit(filemagic->name, filemagic->len);
307  if (filemagic->bm_ctx == NULL) {
308  goto error;
309  }
310 
311  if (negate) {
312  filemagic->flags |= DETECT_CONTENT_NEGATED;
313  }
314 
315  SCLogDebug("flags %02X", filemagic->flags);
316  if (filemagic->flags & DETECT_CONTENT_NEGATED) {
317  SCLogDebug("negated filemagic");
318  }
319 
320 #ifdef DEBUG
321  if (SCLogDebugEnabled()) {
322  char *name = SCMalloc(filemagic->len + 1);
323  if (name != NULL) {
324  memcpy(name, filemagic->name, filemagic->len);
325  name[filemagic->len] = '\0';
326  SCLogDebug("will look for filemagic %s", name);
327  SCFree(name);
328  }
329  }
330 #endif
331 
332  return filemagic;
333 
334 error:
335  if (filemagic != NULL)
336  DetectFilemagicFree(filemagic);
337  return NULL;
338 }
339 
340 static void *DetectFilemagicThreadInit(void *data /*@unused@*/)
341 {
342  const char *filename = NULL;
343  FILE *fd = NULL;
344 
345  DetectFilemagicThreadData *t = SCCalloc(1, sizeof(DetectFilemagicThreadData));
346  if (unlikely(t == NULL)) {
347  SCLogError(SC_ERR_MEM_ALLOC, "couldn't alloc ctx memory");
348  return NULL;
349  }
350 
351  t->ctx = magic_open(0);
352  if (t->ctx == NULL) {
353  SCLogError(SC_ERR_MAGIC_OPEN, "magic_open failed: %s", magic_error(t->ctx));
354  goto error;
355  }
356 
357  (void)ConfGet("magic-file", &filename);
358  if (filename != NULL) {
359  if (strlen(filename) == 0) {
360  /* set filename to NULL on *nix systems so magic_load uses system default path (see man libmagic) */
361  SCLogInfo("using system default magic-file");
362  filename = NULL;
363  }
364  else {
365  SCLogInfo("using magic-file %s", filename);
366 
367  if ( (fd = fopen(filename, "r")) == NULL) {
368  SCLogWarning(SC_ERR_FOPEN, "Error opening file: \"%s\": %s", filename, strerror(errno));
369  goto error;
370  }
371  fclose(fd);
372  }
373  }
374 
375  if (magic_load(t->ctx, filename) != 0) {
376  SCLogError(SC_ERR_MAGIC_LOAD, "magic_load failed: %s", magic_error(t->ctx));
377  goto error;
378  }
379 
380  return (void *)t;
381 
382 error:
383  if (t->ctx)
384  magic_close(t->ctx);
385  SCFree(t);
386  return NULL;
387 }
388 
389 static void DetectFilemagicThreadFree(void *ctx)
390 {
391  if (ctx != NULL) {
392  DetectFilemagicThreadData *t = (DetectFilemagicThreadData *)ctx;
393  if (t->ctx)
394  magic_close(t->ctx);
395  SCFree(t);
396  }
397 }
398 
399 /**
400  * \brief this function is used to parse filemagic options
401  * \brief into the current signature
402  *
403  * \param de_ctx pointer to the Detection Engine Context
404  * \param s pointer to the Current Signature
405  * \param str pointer to the user provided "filemagic" option
406  *
407  * \retval 0 on Success
408  * \retval -1 on Failure
409  */
410 static int DetectFilemagicSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
411 {
412  SigMatch *sm = NULL;
413 
414  DetectFilemagicData *filemagic = DetectFilemagicParse(str, s->init_data->negated);
415  if (filemagic == NULL)
416  return -1;
417 
418  if (g_magic_thread_ctx_id == -1) {
419  g_magic_thread_ctx_id = DetectRegisterThreadCtxFuncs(de_ctx, "filemagic",
420  DetectFilemagicThreadInit, (void *)filemagic,
421  DetectFilemagicThreadFree, 1);
422  if (g_magic_thread_ctx_id == -1)
423  goto error;
424  }
425 
426  /* Okay so far so good, lets get this into a SigMatch
427  * and put it in the Signature. */
428  sm = SigMatchAlloc();
429  if (sm == NULL)
430  goto error;
431 
432  sm->type = DETECT_FILEMAGIC;
433  sm->ctx = (void *)filemagic;
434 
435  SigMatchAppendSMToList(s, sm, g_file_match_list_id);
436 
438  return 0;
439 
440 error:
441  DetectFilemagicFree(filemagic);
442  if (sm != NULL)
443  SCFree(sm);
444  return -1;
445 }
446 
447 /**
448  * \brief this function will free memory associated with DetectFilemagicData
449  *
450  * \param filemagic pointer to DetectFilemagicData
451  */
452 static void DetectFilemagicFree(void *ptr)
453 {
454  if (ptr != NULL) {
455  DetectFilemagicData *filemagic = (DetectFilemagicData *)ptr;
456  if (filemagic->bm_ctx != NULL) {
457  BoyerMooreCtxDeInit(filemagic->bm_ctx);
458  }
459  if (filemagic->name != NULL)
460  SCFree(filemagic->name);
461  SCFree(filemagic);
462  }
463 }
464 
465 /* file.magic implementation */
466 
467 /**
468  * \brief this function setup the file.magic keyword used in the rule
469  *
470  * \param de_ctx Pointer to the Detection Engine Context
471  * \param s Pointer to the Signature to which the current keyword belongs
472  * \param str Should hold an empty string always
473  *
474  * \retval 0 On success
475  */
476 static int DetectFilemagicSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
477 {
478  if (DetectBufferSetActiveList(s, g_file_magic_buffer_id) < 0)
479  return -1;
480 
481  if (g_magic_thread_ctx_id == -1) {
482  g_magic_thread_ctx_id = DetectRegisterThreadCtxFuncs(de_ctx, "filemagic",
483  DetectFilemagicThreadInit, NULL,
484  DetectFilemagicThreadFree, 1);
485  if (g_magic_thread_ctx_id == -1)
486  return -1;
487  }
488  return 0;
489 }
490 
491 static InspectionBuffer *FilemagicGetDataCallback(DetectEngineThreadCtx *det_ctx,
492  const DetectEngineTransforms *transforms,
493  Flow *f, uint8_t flow_flags, File *cur_file,
494  int list_id, int local_file_id, bool first)
495 {
496  SCEnter();
497 
499  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(fb, local_file_id);
500  if (buffer == NULL)
501  return NULL;
502  if (!first && buffer->inspect != NULL)
503  return buffer;
504 
505  if (cur_file->magic == NULL) {
506  DetectFilemagicThreadData *tfilemagic =
507  (DetectFilemagicThreadData *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, g_magic_thread_ctx_id);
508  if (tfilemagic == NULL) {
509  return NULL;
510  }
511 
512  FilemagicThreadLookup(&tfilemagic->ctx, cur_file);
513  }
514  if (cur_file->magic == NULL) {
515  return NULL;
516  }
517 
518  const uint8_t *data = (const uint8_t *)cur_file->magic;
519  uint32_t data_len = (uint32_t)strlen(cur_file->magic);
520 
521  InspectionBufferSetup(buffer, data, data_len);
522  InspectionBufferApplyTransforms(buffer, transforms);
523 
524  SCReturnPtr(buffer, "InspectionBuffer");
525 }
526 
527 static int DetectEngineInspectFilemagic(
528  DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
529  const DetectEngineAppInspectionEngine *engine,
530  const Signature *s,
531  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
532 {
533  const DetectEngineTransforms *transforms = NULL;
534  if (!engine->mpm) {
535  transforms = engine->v2.transforms;
536  }
537 
539  f->alstate, flags);
540  if (ffc == NULL) {
542  }
543 
545  int local_file_id = 0;
546  for (File *file = ffc->head; file != NULL; file = file->next) {
547  if (file->txid != tx_id)
548  continue;
549 
550  InspectionBuffer *buffer = FilemagicGetDataCallback(det_ctx,
551  transforms, f, flags, file, engine->sm_list, local_file_id, false);
552  if (buffer == NULL)
553  continue;
554 
555  det_ctx->buffer_offset = 0;
556  det_ctx->discontinue_matching = 0;
557  det_ctx->inspection_recursion_counter = 0;
558  int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
559  NULL, f,
560  (uint8_t *)buffer->inspect,
561  buffer->inspect_len,
564  if (match == 1) {
566  } else {
568  }
569  local_file_id++;
570  }
571  return r;
572 }
573 
574 typedef struct PrefilterMpmFilemagic {
575  int list_id;
576  const MpmCtx *mpm_ctx;
577  const DetectEngineTransforms *transforms;
578 } PrefilterMpmFilemagic;
579 
580 /** \brief Filedata Filedata Mpm prefilter callback
581  *
582  * \param det_ctx detection engine thread ctx
583  * \param p packet to inspect
584  * \param f flow to inspect
585  * \param txv tx to inspect
586  * \param pectx inspection context
587  */
588 static void PrefilterTxFilemagic(DetectEngineThreadCtx *det_ctx,
589  const void *pectx,
590  Packet *p, Flow *f, void *txv,
591  const uint64_t idx, const uint8_t flags)
592 {
593  SCEnter();
594 
595  const PrefilterMpmFilemagic *ctx = (const PrefilterMpmFilemagic *)pectx;
596  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
597  const int list_id = ctx->list_id;
598 
600  f->alstate, flags);
601  int local_file_id = 0;
602  if (ffc != NULL) {
603  File *file = ffc->head;
604  for (; file != NULL; file = file->next) {
605  if (file->txid != idx)
606  continue;
607 
608  InspectionBuffer *buffer = FilemagicGetDataCallback(det_ctx,
609  ctx->transforms, f, flags, file, list_id, local_file_id, true);
610  if (buffer == NULL)
611  continue;
612 
613  if (buffer->inspect_len >= mpm_ctx->minlen) {
614  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
615  &det_ctx->mtcu, &det_ctx->pmq,
616  buffer->inspect, buffer->inspect_len);
617  }
618  }
619  }
620 }
621 
622 static void PrefilterMpmFilemagicFree(void *ptr)
623 {
624  SCFree(ptr);
625 }
626 
627 static int PrefilterMpmFilemagicRegister(DetectEngineCtx *de_ctx,
628  SigGroupHead *sgh, MpmCtx *mpm_ctx,
629  const DetectBufferMpmRegistery *mpm_reg, int list_id)
630 {
631  PrefilterMpmFilemagic *pectx = SCCalloc(1, sizeof(*pectx));
632  if (pectx == NULL)
633  return -1;
634  pectx->list_id = list_id;
635  pectx->mpm_ctx = mpm_ctx;
636  pectx->transforms = &mpm_reg->transforms;
637 
638  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxFilemagic,
639  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
640  pectx, PrefilterMpmFilemagicFree, mpm_reg->pname);
641 }
642 #ifdef UNITTESTS /* UNITTESTS */
643 
644 /**
645  * \test DetectFilemagicTestParse01
646  */
647 static int DetectFilemagicTestParse01 (void)
648 {
649  DetectFilemagicData *dnd = DetectFilemagicParse("secret.pdf", false);
650  if (dnd != NULL) {
651  DetectFilemagicFree(dnd);
652  return 1;
653  }
654  return 0;
655 }
656 
657 /**
658  * \test DetectFilemagicTestParse02
659  */
660 static int DetectFilemagicTestParse02 (void)
661 {
662  int result = 0;
663 
664  DetectFilemagicData *dnd = DetectFilemagicParse("backup.tar.gz", false);
665  if (dnd != NULL) {
666  if (dnd->len == 13 && memcmp(dnd->name, "backup.tar.gz", 13) == 0) {
667  result = 1;
668  }
669 
670  DetectFilemagicFree(dnd);
671  return result;
672  }
673  return 0;
674 }
675 
676 /**
677  * \test DetectFilemagicTestParse03
678  */
679 static int DetectFilemagicTestParse03 (void)
680 {
681  int result = 0;
682 
683  DetectFilemagicData *dnd = DetectFilemagicParse("cmd.exe", false);
684  if (dnd != NULL) {
685  if (dnd->len == 7 && memcmp(dnd->name, "cmd.exe", 7) == 0) {
686  result = 1;
687  }
688 
689  DetectFilemagicFree(dnd);
690  return result;
691  }
692  return 0;
693 }
694 
695 #endif /* UNITTESTS */
696 
697 /**
698  * \brief this function registers unit tests for DetectFilemagic
699  */
700 void DetectFilemagicRegisterTests(void)
701 {
702 #ifdef UNITTESTS /* UNITTESTS */
703  UtRegisterTest("DetectFilemagicTestParse01", DetectFilemagicTestParse01);
704  UtRegisterTest("DetectFilemagicTestParse02", DetectFilemagicTestParse02);
705  UtRegisterTest("DetectFilemagicTestParse03", DetectFilemagicTestParse03);
706 #endif /* UNITTESTS */
707 }
708 
709 #endif /* HAVE_MAGIC */
710 
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
SignatureInitData * init_data
Definition: detect.h:591
uint16_t flags
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
#define SCLogDebug(...)
Definition: util-debug.h:335
uint16_t minlen
Definition: util-mpm.h:99
struct DetectEngineAppInspectionEngine_::@93 v2
uint16_t discontinue_matching
Definition: detect.h:1067
uint8_t proto
Definition: flow.h:344
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms transforms
Definition: detect.h:618
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
struct DetectBufferMpmRegistery_::@95::@97 app_v2
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:277
int DetectContentDataParse(const char *keyword, const char *contentstr, uint8_t **pstr, uint16_t *plen)
Parse a content string, ie "abc|DE|fgh".
uint64_t offset
Container for matching data for a signature group.
Definition: detect.h:1336
uint32_t buffer_offset
Definition: detect.h:1032
const char * name
Definition: detect.h:1200
uint16_t AppProto
Signature container.
Definition: detect.h:522
struct File_ * next
Definition: util-file.h:79
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
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:313
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:331
StreamingBuffer * sb
Definition: util-file.h:68
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
main detection engine ctx
Definition: detect.h:761
int SCLogDebugEnabled(void)
Returns whether debug messages are enabled to be logged or not.
Definition: util-debug.c:624
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:350
void * alstate
Definition: flow.h:438
void BoyerMooreCtxDeInit(BmCtx *bmctx)
Free the memory allocated to Booyer Moore context.
Definition: util-spm-bm.c:120
int DetectBufferTypeGetByName(const char *name)
#define str(s)
#define SCCalloc(nm, a)
Definition: util-mem.h:253
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1393
#define DETECT_ENGINE_INSPECT_SIG_MATCH
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1178
uint64_t inspect_offset
Definition: detect.h:344
void(* Free)(void *)
Definition: detect.h:1191
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
uint8_t mpm_type
Definition: util-mpm.h:90
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:284
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
one time registration of keywords at start up
Definition: detect.h:605
uint64_t txid
Definition: util-file.h:69
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
#define SIGMATCH_QUOTES_MANDATORY
Definition: detect.h:1385
#define SCEnter(...)
Definition: util-debug.h:337
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
PrefilterRuleStore pmq
Definition: detect.h:1102
uint16_t alternative
Definition: detect.h:1198
int StreamingBufferGetData(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t *stream_offset)
uint8_t type
Definition: detect.h:319
#define SCReturnInt(x)
Definition: util-debug.h:341
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
#define FILE_SIG_NEED_FILE
Definition: detect.h:282
const char * desc
Definition: detect.h:1202
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:346
int DetectBufferTypeRegister(const char *name)
int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *), int mode)
Register Thread keyword context Funcs.
SigMatchCtx * ctx
Definition: detect.h:321
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
#define DETECT_CONTENT_NEGATED
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.h:169
#define SCFree(a)
Definition: util-mem.h:322
uint16_t tx_id
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
MpmThreadCtx mtcu
Definition: detect.h:1100
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
#define SIGMATCH_NOOPT
Definition: detect.h:1369
const char * url
Definition: detect.h:1203
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.
void DetectFilemagicRegister(void)
Registration function for keyword: filemagic.
#define SCReturnPtr(x, type)
Definition: util-debug.h:353
FileState state
Definition: util-file.h:67
SCMutex m
Definition: flow-hash.h:105
BmCtx * BoyerMooreNocaseCtxInit(uint8_t *needle, uint16_t needle_len)
Setup a Booyer Moore context for nocase search.
Definition: util-spm-bm.c:106
InspectionBufferMultipleForList * InspectionBufferGetMulti(DetectEngineThreadCtx *det_ctx, const int list_id)
int inspection_recursion_counter
Definition: detect.h:1079
#define DETECT_CI_FLAGS_SINGLE
uint32_t inspect_len
Definition: detect.h:345
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:343
#define DOC_URL
Definition: suricata.h:86
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:162
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:232
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)
AppProto alproto
application level protocol
Definition: flow.h:409
uint8_t file_flags
Definition: detect.h:536
FileContainer * AppLayerParserGetFiles(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t direction)
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1389
#define DOC_VERSION
Definition: suricata.h:91
InspectionBuffer * InspectionBufferMultipleForListGet(InspectionBufferMultipleForList *fb, uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
uint16_t flags
Definition: detect.h:1194
Flow data structure.
Definition: flow.h:325
void(* RegisterTests)(void)
Definition: detect.h:1192
a single match condition for a signature
Definition: detect.h:318
const DetectEngineTransforms * transforms
Definition: detect.h:416