suricata
detect-priority.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23  *
24  * Implements the priority keyword
25  */
26 
27 #include "suricata-common.h"
28 #include "detect.h"
29 #include "detect-parse.h"
30 #include "detect-priority.h"
31 #include "detect-engine.h"
32 #include "detect-engine-mpm.h"
33 #include "util-error.h"
34 #include "util-debug.h"
35 #include "util-unittest.h"
36 
37 #define PARSE_REGEX "^\\s*(\\d+|\"\\d+\")\\s*$"
38 
39 static pcre *regex = NULL;
40 static pcre_extra *regex_study = NULL;
41 
42 static int DetectPrioritySetup (DetectEngineCtx *, Signature *, const char *);
43 void SCPriorityRegisterTests(void);
44 
45 /**
46  * \brief Registers the handler functions for the "priority" keyword
47  */
49 {
50  sigmatch_table[DETECT_PRIORITY].name = "priority";
51  sigmatch_table[DETECT_PRIORITY].desc = "rules with a higher priority will be examined first";
52  sigmatch_table[DETECT_PRIORITY].url = DOC_URL DOC_VERSION "/rules/meta.html#priority";
54  sigmatch_table[DETECT_PRIORITY].Setup = DetectPrioritySetup;
57 
58  DetectSetupParseRegexes(PARSE_REGEX, &regex, &regex_study);
59 }
60 
61 static int DetectPrioritySetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
62 {
63  char copy_str[128] = "";
64 
65 #define MAX_SUBSTRINGS 30
66  int ret = 0;
67  int ov[MAX_SUBSTRINGS];
68 
69  ret = pcre_exec(regex, regex_study, rawstr, strlen(rawstr), 0, 0, ov, 30);
70  if (ret < 0) {
71  SCLogError(SC_ERR_PCRE_MATCH, "Invalid Priority in Signature "
72  "- %s", rawstr);
73  return -1;
74  }
75 
76  ret = pcre_copy_substring((char *)rawstr, ov, 30, 1, copy_str, sizeof(copy_str));
77  if (ret < 0) {
78  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
79  return -1;
80  }
81 
82  long prio = 0;
83  char *endptr = NULL;
84  prio = strtol(copy_str, &endptr, 10);
85  if (endptr == NULL || *endptr != '\0') {
86  SCLogError(SC_ERR_INVALID_SIGNATURE, "Saw an invalid character as arg "
87  "to priority keyword");
88  return -1;
89  }
90  /* if we have reached here, we have had a valid priority. Assign it */
91  s->prio = prio;
92 
93  return 0;
94 }
95 
96 /*------------------------------Unittests-------------------------------------*/
97 
98 #ifdef UNITTESTS
99 
100 static int DetectPriorityTest01(void)
101 {
102  int result = 0;
103 
105  if (de_ctx == NULL) {
106  goto end;
107  }
108 
109  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
110  "(msg:\"Priority test\"; priority:2; sid:1;)");
111  if (de_ctx->sig_list != NULL)
112  result = 1;
113 
114  DetectEngineCtxFree(de_ctx);
115 
116 end:
117  return result;
118 }
119 
120 static int DetectPriorityTest02(void)
121 {
122  int result = 0;
123  Signature *last = NULL;
124  Signature *sig = NULL;
125 
127  if (de_ctx == NULL) {
128  goto end;
129  }
130 
131  sig = SigInit(de_ctx, "alert tcp any any -> any any "
132  "(msg:\"Priority test\"; priority:1; sid:1;)");
133  de_ctx->sig_list = last = sig;
134  if (sig == NULL) {
135  result = 0;
136  } else {
137  result = 1;
138  result &= (sig->prio == 1);
139  }
140 
141  sig = SigInit(de_ctx, "alert tcp any any -> any any "
142  "(msg:\"Priority test\"; priority:boo; sid:1;)");
143  if (last != NULL)
144  last->next = sig;
145  result &= (sig == NULL);
146 
147  sig = SigInit(de_ctx, "alert tcp any any -> any any "
148  "(msg:\"Priority test\"; priority:10boo; sid:1;)");
149  if (last != NULL)
150  last->next = sig;
151  result &= (sig == NULL);
152 
153  sig = SigInit(de_ctx, "alert tcp any any -> any any "
154  "(msg:\"Priority test\"; priority:b10oo; sid:1;)");
155  if (last != NULL)
156  last->next = sig;
157  result &= (sig == NULL);
158 
159  sig = SigInit(de_ctx, "alert tcp any any -> any any "
160  "(msg:\"Priority test\"; priority:boo10; sid:1;)");
161  if (last != NULL)
162  last->next = sig;
163  result &= (sig == NULL);
164 
165  sig = SigInit(de_ctx, "alert tcp any any -> any any "
166  "(msg:\"Priority test\"; priority:-1; sid:1;)");
167  if (last != NULL)
168  last->next = sig;
169  result &= (sig == NULL);
170 
171  sig = SigInit(de_ctx, "alert tcp any any -> any any "
172  "(msg:\"Priority test\"; sid:1;)");
173  if (last != NULL)
174  last->next = sig;
175  if (sig == NULL) {
176  result &= 0;
177  } else {
178  result &= (sig->prio == 3);
179  }
180 
181  SigCleanSignatures(de_ctx);
182  DetectEngineCtxFree(de_ctx);
183 
184 end:
185  return result;
186 }
187 
188 
189 #endif /* UNITTESTS */
190 
191 /**
192  * \brief This function registers unit tests for Classification Config API.
193  */
195 {
196 
197 #ifdef UNITTESTS
198 
199  UtRegisterTest("DetectPriorityTest01", DetectPriorityTest01);
200  UtRegisterTest("DetectPriorityTest02", DetectPriorityTest02);
201 
202 #endif /* UNITTESTS */
203 
204 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
int prio
Definition: detect.h:528
Signature * sig_list
Definition: detect.h:726
void SigCleanSignatures(DetectEngineCtx *de_ctx)
#define PARSE_REGEX
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
main detection engine ctx
Definition: detect.h:720
void(* Free)(void *)
Definition: detect.h:1151
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void DetectPriorityRegister(void)
Registers the handler functions for the "priority" keyword.
void DetectSetupParseRegexes(const char *parse_str, pcre **parse_regex, pcre_extra **parse_regex_study)
void SCPriorityRegisterTests(void)
This function registers unit tests for Classification Config API.
struct Signature_ * next
Definition: detect.h:563
const char * desc
Definition: detect.h:1162
int(* Match)(ThreadVars *, DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1129
const char * url
Definition: detect.h:1163
#define MAX_SUBSTRINGS
#define DOC_URL
Definition: suricata.h:86
#define DOC_VERSION
Definition: suricata.h:91
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
void(* RegisterTests)(void)
Definition: detect.h:1152
DetectEngineCtx * DetectEngineCtxInit(void)