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 
129  AppProto protos_ts[] = {
131  AppProto protos_tc[] = {
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  if (g_magic_thread_ctx_id == -1) {
365  g_magic_thread_ctx_id = DetectRegisterThreadCtxFuncs(de_ctx, "filemagic",
366  DetectFilemagicThreadInit, (void *)filemagic,
367  DetectFilemagicThreadFree, 1);
368  if (g_magic_thread_ctx_id == -1)
369  goto error;
370  }
371 
372  /* Okay so far so good, lets get this into a SigMatch
373  * and put it in the Signature. */
374  sm = SigMatchAlloc();
375  if (sm == NULL)
376  goto error;
377 
378  sm->type = DETECT_FILEMAGIC;
379  sm->ctx = (void *)filemagic;
380 
381  SigMatchAppendSMToList(s, sm, g_file_match_list_id);
382 
384  return 0;
385 
386 error:
387  DetectFilemagicFree(de_ctx, filemagic);
388  if (sm != NULL)
389  SCFree(sm);
390  return -1;
391 }
392 
393 /**
394  * \brief this function will free memory associated with DetectFilemagicData
395  *
396  * \param filemagic pointer to DetectFilemagicData
397  */
398 static void DetectFilemagicFree(DetectEngineCtx *de_ctx, void *ptr)
399 {
400  if (ptr != NULL) {
401  DetectFilemagicData *filemagic = (DetectFilemagicData *)ptr;
402  if (filemagic->bm_ctx != NULL) {
403  BoyerMooreCtxDeInit(filemagic->bm_ctx);
404  }
405  if (filemagic->name != NULL)
406  SCFree(filemagic->name);
407  SCFree(filemagic);
408  }
409 }
410 
411 /* file.magic implementation */
412 
413 /**
414  * \brief this function setup the file.magic keyword used in the rule
415  *
416  * \param de_ctx Pointer to the Detection Engine Context
417  * \param s Pointer to the Signature to which the current keyword belongs
418  * \param str Should hold an empty string always
419  *
420  * \retval 0 On success
421  */
422 static int DetectFilemagicSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
423 {
424  if (DetectBufferSetActiveList(s, g_file_magic_buffer_id) < 0)
425  return -1;
426 
427  if (g_magic_thread_ctx_id == -1) {
428  g_magic_thread_ctx_id = DetectRegisterThreadCtxFuncs(de_ctx, "filemagic",
429  DetectFilemagicThreadInit, NULL,
430  DetectFilemagicThreadFree, 1);
431  if (g_magic_thread_ctx_id == -1)
432  return -1;
433  }
434  return 0;
435 }
436 
437 static InspectionBuffer *FilemagicGetDataCallback(DetectEngineThreadCtx *det_ctx,
438  const DetectEngineTransforms *transforms,
439  Flow *f, uint8_t flow_flags, File *cur_file,
440  int list_id, int local_file_id, bool first)
441 {
442  SCEnter();
443 
445  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(fb, local_file_id);
446  if (buffer == NULL)
447  return NULL;
448  if (!first && buffer->inspect != NULL)
449  return buffer;
450 
451  if (cur_file->magic == NULL) {
452  DetectFilemagicThreadData *tfilemagic =
453  (DetectFilemagicThreadData *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, g_magic_thread_ctx_id);
454  if (tfilemagic == NULL) {
455  return NULL;
456  }
457 
458  FilemagicThreadLookup(&tfilemagic->ctx, cur_file);
459  }
460  if (cur_file->magic == NULL) {
461  return NULL;
462  }
463 
464  const uint8_t *data = (const uint8_t *)cur_file->magic;
465  uint32_t data_len = (uint32_t)strlen(cur_file->magic);
466 
467  InspectionBufferSetup(buffer, data, data_len);
468  InspectionBufferApplyTransforms(buffer, transforms);
469 
470  SCReturnPtr(buffer, "InspectionBuffer");
471 }
472 
473 static int DetectEngineInspectFilemagic(
475  const DetectEngineAppInspectionEngine *engine,
476  const Signature *s,
477  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
478 {
479  const DetectEngineTransforms *transforms = NULL;
480  if (!engine->mpm) {
481  transforms = engine->v2.transforms;
482  }
483 
485  if (ffc == NULL) {
487  }
488 
490  int local_file_id = 0;
491  for (File *file = ffc->head; file != NULL; file = file->next) {
492  if (file->txid != tx_id)
493  continue;
494 
495  InspectionBuffer *buffer = FilemagicGetDataCallback(det_ctx,
496  transforms, f, flags, file, engine->sm_list, local_file_id, false);
497  if (buffer == NULL)
498  continue;
499 
500  det_ctx->buffer_offset = 0;
501  det_ctx->discontinue_matching = 0;
502  det_ctx->inspection_recursion_counter = 0;
503  int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
504  NULL, f,
505  (uint8_t *)buffer->inspect,
506  buffer->inspect_len,
509  if (match == 1) {
511  } else {
513  }
514  local_file_id++;
515  }
516  return r;
517 }
518 
519 typedef struct PrefilterMpmFilemagic {
520  int list_id;
521  const MpmCtx *mpm_ctx;
522  const DetectEngineTransforms *transforms;
523 } PrefilterMpmFilemagic;
524 
525 /** \brief Filedata Filedata Mpm prefilter callback
526  *
527  * \param det_ctx detection engine thread ctx
528  * \param p packet to inspect
529  * \param f flow to inspect
530  * \param txv tx to inspect
531  * \param pectx inspection context
532  */
533 static void PrefilterTxFilemagic(DetectEngineThreadCtx *det_ctx,
534  const void *pectx,
535  Packet *p, Flow *f, void *txv,
536  const uint64_t idx, const uint8_t flags)
537 {
538  SCEnter();
539 
540  const PrefilterMpmFilemagic *ctx = (const PrefilterMpmFilemagic *)pectx;
541  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
542  const int list_id = ctx->list_id;
543 
545  int local_file_id = 0;
546  if (ffc != NULL) {
547  File *file = ffc->head;
548  for (; file != NULL; file = file->next) {
549  if (file->txid != idx)
550  continue;
551 
552  InspectionBuffer *buffer = FilemagicGetDataCallback(det_ctx,
553  ctx->transforms, f, flags, file, list_id, local_file_id, true);
554  if (buffer == NULL)
555  continue;
556 
557  if (buffer->inspect_len >= mpm_ctx->minlen) {
558  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
559  &det_ctx->mtcu, &det_ctx->pmq,
560  buffer->inspect, buffer->inspect_len);
561  }
562  }
563  }
564 }
565 
566 static void PrefilterMpmFilemagicFree(void *ptr)
567 {
568  SCFree(ptr);
569 }
570 
571 static int PrefilterMpmFilemagicRegister(DetectEngineCtx *de_ctx,
572  SigGroupHead *sgh, MpmCtx *mpm_ctx,
573  const DetectBufferMpmRegistery *mpm_reg, int list_id)
574 {
575  PrefilterMpmFilemagic *pectx = SCCalloc(1, sizeof(*pectx));
576  if (pectx == NULL)
577  return -1;
578  pectx->list_id = list_id;
579  pectx->mpm_ctx = mpm_ctx;
580  pectx->transforms = &mpm_reg->transforms;
581 
582  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxFilemagic,
583  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
584  pectx, PrefilterMpmFilemagicFree, mpm_reg->pname);
585 }
586 #ifdef UNITTESTS /* UNITTESTS */
587 
588 /**
589  * \test DetectFilemagicTestParse01
590  */
591 static int DetectFilemagicTestParse01 (void)
592 {
593  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "secret.pdf", false);
594  if (dnd != NULL) {
595  DetectFilemagicFree(NULL, dnd);
596  return 1;
597  }
598  return 0;
599 }
600 
601 /**
602  * \test DetectFilemagicTestParse02
603  */
604 static int DetectFilemagicTestParse02 (void)
605 {
606  int result = 0;
607 
608  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "backup.tar.gz", false);
609  if (dnd != NULL) {
610  if (dnd->len == 13 && memcmp(dnd->name, "backup.tar.gz", 13) == 0) {
611  result = 1;
612  }
613 
614  DetectFilemagicFree(NULL, dnd);
615  return result;
616  }
617  return 0;
618 }
619 
620 /**
621  * \test DetectFilemagicTestParse03
622  */
623 static int DetectFilemagicTestParse03 (void)
624 {
625  int result = 0;
626 
627  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "cmd.exe", false);
628  if (dnd != NULL) {
629  if (dnd->len == 7 && memcmp(dnd->name, "cmd.exe", 7) == 0) {
630  result = 1;
631  }
632 
633  DetectFilemagicFree(NULL, dnd);
634  return result;
635  }
636  return 0;
637 }
638 
639 /**
640  * \brief this function registers unit tests for DetectFilemagic
641  */
642 void DetectFilemagicRegisterTests(void)
643 {
644  UtRegisterTest("DetectFilemagicTestParse01", DetectFilemagicTestParse01);
645  UtRegisterTest("DetectFilemagicTestParse02", DetectFilemagicTestParse02);
646  UtRegisterTest("DetectFilemagicTestParse03", DetectFilemagicTestParse03);
647 }
648 #endif /* UNITTESTS */
649 #endif /* HAVE_MAGIC */
650 
DetectEngineAppInspectionEngine_
Definition: detect.h:402
SigTableElmt_::url
const char * url
Definition: detect.h:1214
FileContainer_
Definition: util-file.h:100
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
DetectEngineThreadCtx_::buffer_offset
uint32_t buffer_offset
Definition: detect.h:1041
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1404
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1211
stream-tcp.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1347
DetectThreadCtxGetKeywordThreadCtx
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3123
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:376
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:73
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:624
InspectionBuffer
Definition: detect.h:344
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
SigTableElmt_::FileMatch
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1187
File_::state
FileState state
Definition: util-file.h:66
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1111
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1205
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
StreamingBufferGetData
int StreamingBufferGetData(const StreamingBuffer *sb, const uint8_t **data, uint32_t *data_len, uint64_t *stream_offset)
Definition: util-streaming-buffer.c:849
AppLayerParserGetFiles
FileContainer * AppLayerParserGetFiles(const Flow *f, const uint8_t direction)
Definition: app-layer-parser.c:858
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:31
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:611
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:104
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
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:1109
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:880
File_::sb
StreamingBuffer * sb
Definition: util-file.h:67
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:408
SIGMATCH_QUOTES_MANDATORY
#define SIGMATCH_QUOTES_MANDATORY
Definition: detect.h:1396
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
Definition: detect-engine-state.h:44
InspectionBufferMultipleForList
Definition: detect.h:364
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:203
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1010
FILE_SIG_NEED_MAGIC
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:286
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:101
detect.h
DetectEngineAppInspectionEngine_::mpm
uint16_t mpm
Definition: detect.h:406
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:202
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:346
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1209
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
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:350
Packet_
Definition: decode.h:414
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:232
conf.h
util-magic.h
SIGMATCH_HANDLE_NEGATION
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1400
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:597
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
SignatureInitData_::negated
bool negated
Definition: detect.h:485
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:59
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:162
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:291
SigMatch_::type
uint8_t type
Definition: detect.h:321
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:315
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1076
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:426
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:55
File_
Definition: util-file.h:63
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:46
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:836
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:613
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:3046
suricata-common.h
InspectionBufferSetup
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1120
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:542
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1211
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1088
File_::next
struct File_ * next
Definition: util-file.h:77
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
util-spm-bm.h
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:347
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@93::@95 app_v2
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
str
#define str(s)
Definition: suricata-common.h:273
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
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:257
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:423
ALPROTO_HTTP
@ ALPROTO_HTTP
Definition: app-layer-protos.h:30
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
InspectionBufferGetMulti
InspectionBufferMultipleForList * InspectionBufferGetMulti(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1100
FILE_SIG_NEED_FILE
#define FILE_SIG_NEED_FILE
Definition: detect.h:284
File_::txid
uint64_t txid
Definition: util-file.h:68
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1380
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:992
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:908
MpmCtx_
Definition: util-mpm.h:88
flow.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@91 v2
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
flow-var.h
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:35
BoyerMooreNocaseCtxInit
BmCtx * BoyerMooreNocaseCtxInit(uint8_t *needle, uint16_t needle_len)
Setup a Booyer Moore context for nocase search.
Definition: util-spm-bm.c:106
SCLogDebugEnabled
int SCLogDebugEnabled(void)
Returns whether debug messages are enabled to be logged or not.
Definition: util-debug.c:662
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
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(InspectionBufferMultipleForList *fb, uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1074
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203
app-layer.h
BoyerMooreCtxDeInit
void BoyerMooreCtxDeInit(BmCtx *bmctx)
Free the memory allocated to Booyer Moore context.
Definition: util-spm-bm.c:120