suricata
detect-priority.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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";
53  sigmatch_table[DETECT_PRIORITY].Setup = DetectPrioritySetup;
55 
56  DetectSetupParseRegexes(PARSE_REGEX, &regex, &regex_study);
57 }
58 
59 static int DetectPrioritySetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
60 {
61  char copy_str[128] = "";
62 
63 #define MAX_SUBSTRINGS 30
64  int ret = 0;
65  int ov[MAX_SUBSTRINGS];
66 
67  ret = pcre_exec(regex, regex_study, rawstr, strlen(rawstr), 0, 0, ov, 30);
68  if (ret < 0) {
69  SCLogError(SC_ERR_PCRE_MATCH, "Invalid Priority in Signature "
70  "- %s", rawstr);
71  return -1;
72  }
73 
74  ret = pcre_copy_substring((char *)rawstr, ov, 30, 1, copy_str, sizeof(copy_str));
75  if (ret < 0) {
76  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
77  return -1;
78  }
79 
80  long prio = 0;
81  char *endptr = NULL;
82  prio = strtol(copy_str, &endptr, 10);
83  if (endptr == NULL || *endptr != '\0') {
84  SCLogError(SC_ERR_INVALID_SIGNATURE, "Saw an invalid character as arg "
85  "to priority keyword");
86  return -1;
87  }
88 
90  SCLogWarning(SC_ERR_CONFLICTING_RULE_KEYWORDS, "duplicate priority "
91  "keyword. Using highest priority in the rule");
92  s->prio = MIN(s->prio, prio);
93  } else {
94  s->prio = prio;
96  }
97  return 0;
98 }
99 
100 /*------------------------------Unittests-------------------------------------*/
101 
102 #ifdef UNITTESTS
103 
104 static int DetectPriorityTest01(void)
105 {
107  FAIL_IF_NULL(de_ctx);
108 
109  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
110  "(msg:\"Priority test\"; priority:2; sid:1;)");
111  FAIL_IF_NULL(de_ctx->sig_list);
112 
113  FAIL_IF_NOT(de_ctx->sig_list->prio == 2);
114 
115  DetectEngineCtxFree(de_ctx);
116  PASS;
117 }
118 
119 static int DetectPriorityTest02(void)
120 {
122  FAIL_IF_NULL(de_ctx);
123 
124  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
125  "(msg:\"Priority test\"; priority:1; sid:1;)");
126  FAIL_IF_NULL(sig);
127  FAIL_IF_NOT(sig->prio == 1);
128 
129  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
130  "(msg:\"Priority test\"; priority:boo; sid:2;)");
131  FAIL_IF_NOT_NULL(sig);
132 
133  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
134  "(msg:\"Priority test\"; priority:10boo; sid:3;)");
135  FAIL_IF_NOT_NULL(sig);
136 
137  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
138  "(msg:\"Priority test\"; priority:b10oo; sid:4;)");
139  FAIL_IF_NOT_NULL(sig);
140 
141  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
142  "(msg:\"Priority test\"; priority:boo10; sid:5;)");
143  FAIL_IF_NOT_NULL(sig);
144 
145  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
146  "(msg:\"Priority test\"; priority:-1; sid:6;)");
147  FAIL_IF_NOT_NULL(sig);
148 
149  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
150  "(msg:\"Priority test\"; sid:7;)");
151  FAIL_IF_NULL(sig);
152  FAIL_IF_NOT(sig->prio == 3);
153 
154  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
155  "(msg:\"Priority test\"; priority:5; priority:4; sid:8;)");
156  FAIL_IF_NULL(sig);
157  FAIL_IF_NOT(sig->prio == 4);
158 
159  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
160  "(msg:\"Priority test\"; priority:5; priority:4; "
161  "priority:1; sid:9;)");
162  FAIL_IF_NULL(sig);
163  FAIL_IF_NOT(sig->prio == 1);
164 
165  DetectEngineCtxFree(de_ctx);
166  PASS;
167 }
168 #endif /* UNITTESTS */
169 
170 /**
171  * \brief This function registers unit tests for Classification Config API.
172  */
174 {
175 #ifdef UNITTESTS
176  UtRegisterTest("DetectPriorityTest01", DetectPriorityTest01);
177  UtRegisterTest("DetectPriorityTest02", DetectPriorityTest02);
178 #endif /* UNITTESTS */
179 }
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
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 PASS
Pass the test.
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
int prio
Definition: detect.h:558
Signature * sig_list
Definition: detect.h:767
#define MIN(x, y)
#define PARSE_REGEX
const char * name
Definition: detect.h:1200
Signature container.
Definition: detect.h:522
main detection engine ctx
Definition: detect.h:761
#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.
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
const char * desc
Definition: detect.h:1202
uint32_t init_flags
Definition: detect.h:486
const char * url
Definition: detect.h:1203
#define MAX_SUBSTRINGS
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
#define DOC_URL
Definition: suricata.h:86
#define SIG_FLAG_INIT_PRIO_EXPLICT
Definition: detect.h:264
#define DOC_VERSION
Definition: suricata.h:91
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
void(* RegisterTests)(void)
Definition: detect.h:1192
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
Definition: util-unittest.h:82
DetectEngineCtx * DetectEngineCtxInit(void)