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 static void DetectFilemagicRegisterTests(void);
85 static void DetectFilemagicFree(DetectEngineCtx *, 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(
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 = "/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 = "/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 FilemagicThreadLookup(magic_t *ctx, File *file)
168 {
169  if (ctx == NULL || 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 = MagicThreadLookup(ctx, data, data_len);
182  } else if (file->state >= FILE_STATE_CLOSED) {
183  file->magic = MagicThreadLookup(ctx, data, data_len);
184  }
185  }
186  SCReturnInt(0);
187 }
188 
189 /**
190  * \brief match the specified filemagic
191  *
192  * \param t thread local vars
193  * \param det_ctx pattern matcher thread local data
194  * \param f *LOCKED* flow
195  * \param flags direction flags
196  * \param file file being inspected
197  * \param s signature being inspected
198  * \param m sigmatch that we will cast into DetectFilemagicData
199  *
200  * \retval 0 no match
201  * \retval 1 match
202  */
203 static int DetectFilemagicMatch (DetectEngineThreadCtx *det_ctx,
204  Flow *f, uint8_t flags, File *file, const Signature *s, const SigMatchCtx *m)
205 {
206  SCEnter();
207  int ret = 0;
208  DetectFilemagicData *filemagic = (DetectFilemagicData *)m;
209 
210  DetectFilemagicThreadData *tfilemagic =
211  (DetectFilemagicThreadData *)DetectThreadCtxGetKeywordThreadCtx(det_ctx, g_magic_thread_ctx_id);
212  if (tfilemagic == NULL) {
213  SCReturnInt(0);
214  }
215 
216  if (file->magic == NULL) {
217  FilemagicThreadLookup(&tfilemagic->ctx, file);
218  }
219 
220  if (file->magic != NULL) {
221  SCLogDebug("magic %s", file->magic);
222 
223  /* we include the \0 in the inspection, so patterns can match on the
224  * end of the string. */
225  if (BoyerMooreNocase(filemagic->name, filemagic->len, (uint8_t *)file->magic,
226  strlen(file->magic) + 1, filemagic->bm_ctx) != NULL)
227  {
228 #ifdef DEBUG
229  if (SCLogDebugEnabled()) {
230  char *name = SCMalloc(filemagic->len + 1);
231  if (name != NULL) {
232  memcpy(name, filemagic->name, filemagic->len);
233  name[filemagic->len] = '\0';
234  SCLogDebug("will look for filemagic %s", name);
235  SCFree(name);
236  }
237  }
238 #endif
239 
240  if (!(filemagic->flags & DETECT_CONTENT_NEGATED)) {
241  ret = 1;
242  }
243  } else if (filemagic->flags & DETECT_CONTENT_NEGATED) {
244  SCLogDebug("negated match");
245  ret = 1;
246  }
247  }
248 
249  SCReturnInt(ret);
250 }
251 
252 /**
253  * \brief Parse the filemagic keyword
254  *
255  * \param de_ctx Pointer to the detection engine context
256  * \param idstr Pointer to the user provided option
257  *
258  * \retval filemagic pointer to DetectFilemagicData on success
259  * \retval NULL on failure
260  */
261 static DetectFilemagicData *DetectFilemagicParse (DetectEngineCtx *de_ctx, const char *str, bool negate)
262 {
263  DetectFilemagicData *filemagic = NULL;
264 
265  /* We have a correct filemagic option */
266  filemagic = SCMalloc(sizeof(DetectFilemagicData));
267  if (unlikely(filemagic == NULL))
268  goto error;
269 
270  memset(filemagic, 0x00, sizeof(DetectFilemagicData));
271 
272  if (DetectContentDataParse ("filemagic", str, &filemagic->name, &filemagic->len) == -1) {
273  goto error;
274  }
275 
276  filemagic->bm_ctx = BoyerMooreNocaseCtxInit(filemagic->name, filemagic->len);
277  if (filemagic->bm_ctx == NULL) {
278  goto error;
279  }
280 
281  if (negate) {
282  filemagic->flags |= DETECT_CONTENT_NEGATED;
283  }
284 
285  SCLogDebug("flags %02X", filemagic->flags);
286  if (filemagic->flags & DETECT_CONTENT_NEGATED) {
287  SCLogDebug("negated filemagic");
288  }
289 
290 #ifdef DEBUG
291  if (SCLogDebugEnabled()) {
292  char *name = SCMalloc(filemagic->len + 1);
293  if (name != NULL) {
294  memcpy(name, filemagic->name, filemagic->len);
295  name[filemagic->len] = '\0';
296  SCLogDebug("will look for filemagic %s", name);
297  SCFree(name);
298  }
299  }
300 #endif
301 
302  return filemagic;
303 
304 error:
305  if (filemagic != NULL)
306  DetectFilemagicFree(de_ctx, filemagic);
307  return NULL;
308 }
309 
310 static void *DetectFilemagicThreadInit(void *data /*@unused@*/)
311 {
312  DetectFilemagicThreadData *t = SCCalloc(1, sizeof(DetectFilemagicThreadData));
313  if (unlikely(t == NULL)) {
314  SCLogError(SC_ERR_MEM_ALLOC, "couldn't alloc ctx memory");
315  return NULL;
316  }
317 
318  t->ctx = MagicInitContext();
319  if (t->ctx == NULL)
320  goto error;
321 
322  return (void *)t;
323 
324 error:
325  if (t->ctx)
326  magic_close(t->ctx);
327  SCFree(t);
328  return NULL;
329 }
330 
331 static void DetectFilemagicThreadFree(void *ctx)
332 {
333  if (ctx != NULL) {
334  DetectFilemagicThreadData *t = (DetectFilemagicThreadData *)ctx;
335  if (t->ctx)
336  magic_close(t->ctx);
337  SCFree(t);
338  }
339 }
340 
341 /**
342  * \brief this function is used to parse filemagic options
343  * \brief into the current signature
344  *
345  * \param de_ctx pointer to the Detection Engine Context
346  * \param s pointer to the Current Signature
347  * \param str pointer to the user provided "filemagic" option
348  *
349  * \retval 0 on Success
350  * \retval -1 on Failure
351  */
352 static int DetectFilemagicSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
353 {
354  SigMatch *sm = NULL;
355 
356  DetectFilemagicData *filemagic = DetectFilemagicParse(de_ctx, str, s->init_data->negated);
357  if (filemagic == NULL)
358  return -1;
359 
360  if (g_magic_thread_ctx_id == -1) {
361  g_magic_thread_ctx_id = DetectRegisterThreadCtxFuncs(de_ctx, "filemagic",
362  DetectFilemagicThreadInit, (void *)filemagic,
363  DetectFilemagicThreadFree, 1);
364  if (g_magic_thread_ctx_id == -1)
365  goto error;
366  }
367 
368  /* Okay so far so good, lets get this into a SigMatch
369  * and put it in the Signature. */
370  sm = SigMatchAlloc();
371  if (sm == NULL)
372  goto error;
373 
374  sm->type = DETECT_FILEMAGIC;
375  sm->ctx = (void *)filemagic;
376 
377  SigMatchAppendSMToList(s, sm, g_file_match_list_id);
378 
380  return 0;
381 
382 error:
383  DetectFilemagicFree(de_ctx, filemagic);
384  if (sm != NULL)
385  SCFree(sm);
386  return -1;
387 }
388 
389 /**
390  * \brief this function will free memory associated with DetectFilemagicData
391  *
392  * \param filemagic pointer to DetectFilemagicData
393  */
394 static void DetectFilemagicFree(DetectEngineCtx *de_ctx, void *ptr)
395 {
396  if (ptr != NULL) {
397  DetectFilemagicData *filemagic = (DetectFilemagicData *)ptr;
398  if (filemagic->bm_ctx != NULL) {
399  BoyerMooreCtxDeInit(filemagic->bm_ctx);
400  }
401  if (filemagic->name != NULL)
402  SCFree(filemagic->name);
403  SCFree(filemagic);
404  }
405 }
406 
407 /* file.magic implementation */
408 
409 /**
410  * \brief this function setup the file.magic keyword used in the rule
411  *
412  * \param de_ctx Pointer to the Detection Engine Context
413  * \param s Pointer to the Signature to which the current keyword belongs
414  * \param str Should hold an empty string always
415  *
416  * \retval 0 On success
417  */
418 static int DetectFilemagicSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
419 {
420  if (DetectBufferSetActiveList(s, g_file_magic_buffer_id) < 0)
421  return -1;
422 
423  if (g_magic_thread_ctx_id == -1) {
424  g_magic_thread_ctx_id = DetectRegisterThreadCtxFuncs(de_ctx, "filemagic",
425  DetectFilemagicThreadInit, NULL,
426  DetectFilemagicThreadFree, 1);
427  if (g_magic_thread_ctx_id == -1)
428  return -1;
429  }
430  return 0;
431 }
432 
433 static InspectionBuffer *FilemagicGetDataCallback(DetectEngineThreadCtx *det_ctx,
434  const DetectEngineTransforms *transforms,
435  Flow *f, uint8_t flow_flags, File *cur_file,
436  int list_id, int local_file_id, bool first)
437 {
438  SCEnter();
439 
441  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(fb, 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  InspectionBufferSetup(buffer, data, data_len);
464  InspectionBufferApplyTransforms(buffer, transforms);
465 
466  SCReturnPtr(buffer, "InspectionBuffer");
467 }
468 
469 static int DetectEngineInspectFilemagic(
471  const DetectEngineAppInspectionEngine *engine,
472  const Signature *s,
473  Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
474 {
475  const DetectEngineTransforms *transforms = NULL;
476  if (!engine->mpm) {
477  transforms = engine->v2.transforms;
478  }
479 
481  if (ffc == NULL) {
483  }
484 
486  int local_file_id = 0;
487  for (File *file = ffc->head; file != NULL; file = file->next) {
488  if (file->txid != tx_id)
489  continue;
490 
491  InspectionBuffer *buffer = FilemagicGetDataCallback(det_ctx,
492  transforms, f, flags, file, engine->sm_list, local_file_id, false);
493  if (buffer == NULL)
494  continue;
495 
496  det_ctx->buffer_offset = 0;
497  det_ctx->discontinue_matching = 0;
498  det_ctx->inspection_recursion_counter = 0;
499  int match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd,
500  NULL, f,
501  (uint8_t *)buffer->inspect,
502  buffer->inspect_len,
505  if (match == 1) {
507  } else {
509  }
510  local_file_id++;
511  }
512  return r;
513 }
514 
515 typedef struct PrefilterMpmFilemagic {
516  int list_id;
517  const MpmCtx *mpm_ctx;
518  const DetectEngineTransforms *transforms;
519 } PrefilterMpmFilemagic;
520 
521 /** \brief Filedata Filedata Mpm prefilter callback
522  *
523  * \param det_ctx detection engine thread ctx
524  * \param p packet to inspect
525  * \param f flow to inspect
526  * \param txv tx to inspect
527  * \param pectx inspection context
528  */
529 static void PrefilterTxFilemagic(DetectEngineThreadCtx *det_ctx,
530  const void *pectx,
531  Packet *p, Flow *f, void *txv,
532  const uint64_t idx, const uint8_t flags)
533 {
534  SCEnter();
535 
536  const PrefilterMpmFilemagic *ctx = (const PrefilterMpmFilemagic *)pectx;
537  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
538  const int list_id = ctx->list_id;
539 
541  int local_file_id = 0;
542  if (ffc != NULL) {
543  File *file = ffc->head;
544  for (; file != NULL; file = file->next) {
545  if (file->txid != idx)
546  continue;
547 
548  InspectionBuffer *buffer = FilemagicGetDataCallback(det_ctx,
549  ctx->transforms, f, flags, file, list_id, local_file_id, true);
550  if (buffer == NULL)
551  continue;
552 
553  if (buffer->inspect_len >= mpm_ctx->minlen) {
554  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
555  &det_ctx->mtcu, &det_ctx->pmq,
556  buffer->inspect, buffer->inspect_len);
557  }
558  }
559  }
560 }
561 
562 static void PrefilterMpmFilemagicFree(void *ptr)
563 {
564  SCFree(ptr);
565 }
566 
567 static int PrefilterMpmFilemagicRegister(DetectEngineCtx *de_ctx,
568  SigGroupHead *sgh, MpmCtx *mpm_ctx,
569  const DetectBufferMpmRegistery *mpm_reg, int list_id)
570 {
571  PrefilterMpmFilemagic *pectx = SCCalloc(1, sizeof(*pectx));
572  if (pectx == NULL)
573  return -1;
574  pectx->list_id = list_id;
575  pectx->mpm_ctx = mpm_ctx;
576  pectx->transforms = &mpm_reg->transforms;
577 
578  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxFilemagic,
579  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
580  pectx, PrefilterMpmFilemagicFree, mpm_reg->pname);
581 }
582 #ifdef UNITTESTS /* UNITTESTS */
583 
584 /**
585  * \test DetectFilemagicTestParse01
586  */
587 static int DetectFilemagicTestParse01 (void)
588 {
589  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "secret.pdf", false);
590  if (dnd != NULL) {
591  DetectFilemagicFree(NULL, dnd);
592  return 1;
593  }
594  return 0;
595 }
596 
597 /**
598  * \test DetectFilemagicTestParse02
599  */
600 static int DetectFilemagicTestParse02 (void)
601 {
602  int result = 0;
603 
604  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "backup.tar.gz", false);
605  if (dnd != NULL) {
606  if (dnd->len == 13 && memcmp(dnd->name, "backup.tar.gz", 13) == 0) {
607  result = 1;
608  }
609 
610  DetectFilemagicFree(NULL, dnd);
611  return result;
612  }
613  return 0;
614 }
615 
616 /**
617  * \test DetectFilemagicTestParse03
618  */
619 static int DetectFilemagicTestParse03 (void)
620 {
621  int result = 0;
622 
623  DetectFilemagicData *dnd = DetectFilemagicParse(NULL, "cmd.exe", false);
624  if (dnd != NULL) {
625  if (dnd->len == 7 && memcmp(dnd->name, "cmd.exe", 7) == 0) {
626  result = 1;
627  }
628 
629  DetectFilemagicFree(NULL, dnd);
630  return result;
631  }
632  return 0;
633 }
634 
635 #endif /* UNITTESTS */
636 
637 /**
638  * \brief this function registers unit tests for DetectFilemagic
639  */
640 void DetectFilemagicRegisterTests(void)
641 {
642 #ifdef UNITTESTS /* UNITTESTS */
643  UtRegisterTest("DetectFilemagicTestParse01", DetectFilemagicTestParse01);
644  UtRegisterTest("DetectFilemagicTestParse02", DetectFilemagicTestParse02);
645  UtRegisterTest("DetectFilemagicTestParse03", DetectFilemagicTestParse03);
646 #endif /* UNITTESTS */
647 }
648 
649 #endif /* HAVE_MAGIC */
650 
DetectEngineAppInspectionEngine_
Definition: detect.h:401
SigTableElmt_::url
const char * url
Definition: detect.h:1212
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:1040
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1402
SigTableElmt_::desc
const char * desc
Definition: detect.h:1211
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1200
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1209
stream-tcp.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1345
DetectThreadCtxGetKeywordThreadCtx
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3119
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:375
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:71
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:623
InspectionBuffer
Definition: detect.h:343
threads.h
Flow_
Flow data structure.
Definition: flow.h:343
SigTableElmt_::FileMatch
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1186
File_::state
FileState state
Definition: util-file.h:66
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1110
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1203
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
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:864
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:31
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:610
m
SCMutex m
Definition: flow-hash.h:3
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:103
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1195
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:1108
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:876
File_::sb
StreamingBuffer * sb
Definition: util-file.h:67
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:407
SIGMATCH_QUOTES_MANDATORY
#define SIGMATCH_QUOTES_MANDATORY
Definition: detect.h:1394
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:363
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:187
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1009
FILE_SIG_NEED_MAGIC
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:285
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:405
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:186
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect.h:345
SigTableElmt_::alternative
uint16_t alternative
Definition: detect.h:1207
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:322
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:411
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:231
conf.h
util-magic.h
SIGMATCH_HANDLE_NEGATION
#define SIGMATCH_HANDLE_NEGATION
Definition: detect.h:1398
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:596
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
SignatureInitData_::negated
bool negated
Definition: detect.h:484
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:320
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:314
DetectEngineThreadCtx_::discontinue_matching
uint16_t discontinue_matching
Definition: detect.h:1075
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:425
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:78
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:832
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:612
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:3042
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:1116
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:541
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1207
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectEngineThreadCtx_::inspection_recursion_counter
int inspection_recursion_counter
Definition: detect.h:1087
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:346
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@93::@95 app_v2
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:344
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:527
SigMatch_
a single match condition for a signature
Definition: detect.h:319
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:422
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:1096
FILE_SIG_NEED_FILE
#define FILE_SIG_NEED_FILE
Definition: detect.h:283
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:1378
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:988
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:904
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:1070
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1201
app-layer.h
BoyerMooreCtxDeInit
void BoyerMooreCtxDeInit(BmCtx *bmctx)
Free the memory allocated to Booyer Moore context.
Definition: util-spm-bm.c:120