suricata
detect-prefilter.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 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  * Implements the prefilter keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 
29 #include "detect.h"
30 #include "detect-parse.h"
31 #include "detect-content.h"
32 #include "detect-prefilter.h"
33 #include "util-debug.h"
34 
35 static int DetectPrefilterSetup (DetectEngineCtx *, Signature *, const char *);
36 
38 {
39  sigmatch_table[DETECT_PREFILTER].name = "prefilter";
40  sigmatch_table[DETECT_PREFILTER].desc = "force a condition to be used as prefilter";
41  sigmatch_table[DETECT_PREFILTER].url = DOC_URL DOC_VERSION "/rules/prefilter-keywords.html#prefilter";
43  sigmatch_table[DETECT_PREFILTER].Setup = DetectPrefilterSetup;
46 
48 }
49 
50 /**
51  * \internal
52  * \brief Apply the prefilter keyword to the last match
53  * \param det_ctx detection engine ctx
54  * \param s signature
55  * \param nullstr should be null
56  * \retval 0 ok
57  * \retval -1 failure
58  */
59 static int DetectPrefilterSetup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
60 {
61  SCEnter();
62 
63  if (nullstr != NULL) {
64  SCLogError(SC_ERR_INVALID_VALUE, "prefilter has value");
65  SCReturnInt(-1);
66  }
67 
68  if (s->flags & SIG_FLAG_PREFILTER) {
69  SCLogError(SC_ERR_INVALID_SIGNATURE, "prefilter already set");
70  SCReturnInt(-1);
71  }
72 
73  SigMatch *sm = DetectGetLastSM(s);
74  if (sm == NULL) {
75  SCLogError(SC_ERR_INVALID_SIGNATURE, "prefilter needs preceding match");
76  SCReturnInt(-1);
77  }
78 
79  s->init_data->prefilter_sm = sm;
80 
81  /* if the sig match is content, prefilter should act like
82  * 'fast_pattern' w/o options. */
83  if (sm->type == DETECT_CONTENT) {
85  if ((cd->flags & DETECT_CONTENT_NEGATED) &&
86  ((cd->flags & DETECT_CONTENT_DISTANCE) ||
87  (cd->flags & DETECT_CONTENT_WITHIN) ||
88  (cd->flags & DETECT_CONTENT_OFFSET) ||
89  (cd->flags & DETECT_CONTENT_DEPTH)))
90  {
91  SCLogError(SC_ERR_INVALID_SIGNATURE, "prefilter; cannot be "
92  "used with negated content, along with relative modifiers");
93  SCReturnInt(-1);
94  }
96  } else {
98 
99  /* make sure setup function runs for this type. */
100  de_ctx->sm_types_prefilter[sm->type] = true;
101  }
102 
103  SCReturnInt(0);
104 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
SignatureInitData * init_data
Definition: detect.h:591
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
#define SIG_FLAG_PREFILTER
Definition: detect.h:243
SigMatch * prefilter_sm
Definition: detect.h:495
uint32_t flags
Definition: detect.h:523
#define DETECT_CONTENT_FAST_PATTERN
SigMatch * DetectGetLastSM(const Signature *s)
Returns the sm with the largest index (added latest) from this sig.
Definition: detect-parse.c:569
#define DETECT_CONTENT_DISTANCE
const char * name
Definition: detect.h:1200
Signature container.
Definition: detect.h:522
#define DETECT_CONTENT_DEPTH
void DetectPrefilterRegister(void)
main detection engine ctx
Definition: detect.h:761
void(* Free)(void *)
Definition: detect.h:1191
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define SCEnter(...)
Definition: util-debug.h:337
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1170
uint8_t type
Definition: detect.h:319
#define SCReturnInt(x)
Definition: util-debug.h:341
const char * desc
Definition: detect.h:1202
SigMatchCtx * ctx
Definition: detect.h:321
#define DETECT_CONTENT_NEGATED
#define SIGMATCH_NOOPT
Definition: detect.h:1369
const char * url
Definition: detect.h:1203
#define DETECT_CONTENT_WITHIN
#define DOC_URL
Definition: suricata.h:86
#define DOC_VERSION
Definition: suricata.h:91
bool sm_types_prefilter[DETECT_TBLSIZE]
Definition: detect.h:946
uint16_t flags
Definition: detect.h:1194
#define DETECT_CONTENT_OFFSET
void(* RegisterTests)(void)
Definition: detect.h:1192
a single match condition for a signature
Definition: detect.h:318