suricata
detect-classtype.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 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 Anoop Saldanha <anoopsaldanha@gmail.com>
22  * \author Victor Julien <victor@inliniac.net>
23  *
24  * Implements classtype keyword.
25  */
26 
27 #include "suricata-common.h"
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 #include "detect-engine.h"
33 #include "detect-classtype.h"
35 #include "util-error.h"
36 #include "util-debug.h"
37 #include "util-unittest.h"
38 
39 #define PARSE_REGEX "^\\s*([a-zA-Z][a-zA-Z0-9-_]*)\\s*$"
40 
41 static DetectParseRegex parse_regex;
42 
43 static int DetectClasstypeSetup(DetectEngineCtx *, Signature *, const char *);
44 #ifdef UNITTESTS
45 static void DetectClasstypeRegisterTests(void);
46 #endif
47 
48 /**
49  * \brief Registers the handler functions for the "Classtype" keyword.
50  */
52 {
53  sigmatch_table[DETECT_CLASSTYPE].name = "classtype";
54  sigmatch_table[DETECT_CLASSTYPE].desc = "information about the classification of rules and alerts";
55  sigmatch_table[DETECT_CLASSTYPE].url = "/rules/meta.html#classtype";
56  sigmatch_table[DETECT_CLASSTYPE].Setup = DetectClasstypeSetup;
57 #ifdef UNITTESTS
58  sigmatch_table[DETECT_CLASSTYPE].RegisterTests = DetectClasstypeRegisterTests;
59 #endif
60  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
61 }
62 
63 /**
64  * \brief Parses the raw string supplied with the "Classtype" keyword.
65  *
66  * \param Pointer to the string to be parsed.
67  *
68  * \retval bool success or failure.
69  */
70 static int DetectClasstypeParseRawString(const char *rawstr, char *out, size_t outsize)
71 {
72  size_t pcre2len;
73 
74  const size_t esize = CLASSTYPE_NAME_MAX_LEN + 8;
75  char e[esize];
76 
77  int ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
78  if (ret < 0) {
79  SCLogError(SC_ERR_PCRE_MATCH, "Invalid Classtype in Signature");
80  return -1;
81  }
82 
83  pcre2len = esize;
84  ret = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)e, &pcre2len);
85  if (ret < 0) {
86  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_copy_bynumber failed");
87  return -1;
88  }
89 
90  if (strlen(e) >= CLASSTYPE_NAME_MAX_LEN) {
91  SCLogError(SC_ERR_INVALID_VALUE, "classtype '%s' is too big: max %d",
92  rawstr, CLASSTYPE_NAME_MAX_LEN - 1);
93  return -1;
94  }
95  (void)strlcpy(out, e, outsize);
96 
97  return 0;
98 }
99 
100 /**
101  * \brief The setup function that would be called when the Signature parsing
102  * module encounters the "Classtype" keyword.
103  *
104  * \param de_ctx Pointer to the Detection Engine Context.
105  * \param s Pointer the current Signature instance that is being parsed.
106  * \param rawstr Pointer to the argument supplied to the classtype keyword.
107  *
108  * \retval 0 On success
109  * \retval -1 On failure
110  */
111 static int DetectClasstypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
112 {
113  char parsed_ct_name[CLASSTYPE_NAME_MAX_LEN] = "";
114 
115  if ((s->class_id > 0) || (s->class_msg != NULL)) {
117  SCLogError(SC_ERR_CONFLICTING_RULE_KEYWORDS, "duplicated 'classtype' "
118  "keyword detected.");
119  return -1;
120  } else {
121  SCLogWarning(SC_ERR_CONFLICTING_RULE_KEYWORDS, "duplicated 'classtype' "
122  "keyword detected. Using instance with highest priority");
123  }
124  }
125 
126  if (DetectClasstypeParseRawString(rawstr, parsed_ct_name, sizeof(parsed_ct_name)) < 0) {
127  SCLogError(SC_ERR_PCRE_PARSE, "invalid value for classtype keyword: "
128  "\"%s\"", rawstr);
129  return -1;
130  }
131 
132  bool real_ct = true;
133  SCClassConfClasstype *ct = SCClassConfGetClasstype(parsed_ct_name, de_ctx);
134  if (ct == NULL) {
136  SCLogError(SC_ERR_UNKNOWN_VALUE, "unknown classtype '%s'",
137  parsed_ct_name);
138  return -1;
139  }
140 
141  if (s->id > 0) {
142  SCLogWarning(SC_ERR_UNKNOWN_VALUE, "signature sid:%u uses "
143  "unknown classtype: \"%s\", using default priority %d. "
144  "This message won't be shown again for this classtype",
145  s->id, parsed_ct_name, DETECT_DEFAULT_PRIO);
146  } else if (de_ctx->rule_file != NULL) {
147  SCLogWarning(SC_ERR_UNKNOWN_VALUE, "signature at %s:%u uses "
148  "unknown classtype: \"%s\", using default priority %d. "
149  "This message won't be shown again for this classtype",
151  parsed_ct_name, DETECT_DEFAULT_PRIO);
152  } else {
153  SCLogWarning(SC_ERR_UNKNOWN_VALUE, "unknown classtype: \"%s\", "
154  "using default priority %d. "
155  "This message won't be shown again for this classtype",
156  parsed_ct_name, DETECT_DEFAULT_PRIO);
157  }
158 
159  char str[256];
160  snprintf(str, sizeof(str),
161  "config classification: %s,Unknown Classtype,%d\n",
162  parsed_ct_name, DETECT_DEFAULT_PRIO);
163 
164  if (SCClassConfAddClasstype(de_ctx, str, 0) < 0)
165  return -1;
166  ct = SCClassConfGetClasstype(parsed_ct_name, de_ctx);
167  if (ct == NULL)
168  return -1;
169  real_ct = false;
170  }
171 
172  /* set prio only if not already explicitly set by 'priority' keyword.
173  * update classtype in sig, but only if it is 'real' (not undefined)
174  * update sigs classtype if its prio is lower (but not undefined)
175  */
176 
177  bool update_ct = false;
179  /* don't touch Signature::prio */
180  update_ct = true;
181  } else if (s->prio == -1) {
182  s->prio = ct->priority;
183  update_ct = true;
184  } else {
185  if (ct->priority < s->prio) {
186  s->prio = ct->priority;
187  update_ct = true;
188  }
189  }
190 
191  if (real_ct && update_ct) {
192  s->class_id = ct->classtype_id;
193  s->class_msg = ct->classtype_desc;
194  }
195  return 0;
196 }
197 
198 #ifdef UNITTESTS
199 
200 /**
201  * \test undefined classtype
202  */
203 static int DetectClasstypeTest01(void)
204 {
207 
209  FAIL_IF_NULL(fd);
211  Signature *s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
212  "(msg:\"Classtype test\"; "
213  "Classtype:not_available; sid:1;)");
214  FAIL_IF_NULL(s);
215  FAIL_IF_NOT(s->prio == 3);
216 
218  PASS;
219 }
220 
221 /**
222  * \test Check that both valid and invalid classtypes in a rule are handled
223  * properly, with rules containing invalid classtypes being rejected
224  * and the ones containing valid classtypes parsed and returned.
225  */
226 static int DetectClasstypeTest02(void)
227 {
230 
232  FAIL_IF_NULL(fd);
234 
235  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
236  "(Classtype:bad-unknown; sid:1;)");
237  FAIL_IF_NULL(sig);
238 
239  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
240  "(Classtype:not-there; sid:2;)");
241  FAIL_IF_NULL(sig);
242 
243  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
244  "(Classtype:Bad-UnkNown; sid:3;)");
245  FAIL_IF_NULL(sig);
246 
247  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
248  "(Classtype:nothing-wrong; sid:4;)");
249  FAIL_IF_NULL(sig);
250 
251  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
252  "(Classtype:attempted_dos; Classtype:bad-unknown; sid:5;)");
253  FAIL_IF_NULL(sig);
254  FAIL_IF_NOT(sig->prio == 2);
255 
256  /* duplicate test */
257  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
258  "(Classtype:nothing-wrong; Classtype:Bad-UnkNown; sid:6;)");
259  FAIL_IF_NULL(sig);
260  FAIL_IF_NOT(sig->prio == 2);
261 
263  PASS;
264 }
265 
266 /**
267  * \test Check that the signatures are assigned priority based on classtype they
268  * are given.
269  */
270 static int DetectClasstypeTest03(void)
271 {
274 
276  FAIL_IF_NULL(fd);
278 
279  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
280  "(msg:\"Classtype test\"; Classtype:bad-unknown; priority:1; sid:1;)");
281  FAIL_IF_NULL(sig);
282  FAIL_IF_NOT(sig->prio == 1);
283 
284  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
285  "(msg:\"Classtype test\"; Classtype:unKnoWn; "
286  "priority:3; sid:2;)");
287  FAIL_IF_NULL(sig);
288  FAIL_IF_NOT(sig->prio == 3);
289 
290  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"Classtype test\"; "
291  "Classtype:nothing-wrong; priority:1; sid:3;)");
292  FAIL_IF_NOT(sig->prio == 1);
293 
294  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
295  "(msg:\"Classtype test\"; Classtype:bad-unknown; Classtype:undefined; "
296  "priority:5; sid:4;)");
297  FAIL_IF_NULL(sig);
298  FAIL_IF_NOT(sig->prio == 5);
299 
301  PASS;
302 }
303 
304 /**
305  * \brief This function registers unit tests for Classification Config API.
306  */
307 static void DetectClasstypeRegisterTests(void)
308 {
309  UtRegisterTest("DetectClasstypeTest01", DetectClasstypeTest01);
310  UtRegisterTest("DetectClasstypeTest02", DetectClasstypeTest02);
311  UtRegisterTest("DetectClasstypeTest03", DetectClasstypeTest03);
312 }
313 #endif /* UNITTESTS */
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:47
SigTableElmt_::url
const char * url
Definition: detect.h:1238
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1237
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2488
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
DetectParseRegex
Definition: detect-parse.h:44
SigTableElmt_::name
const char * name
Definition: detect.h:1235
DetectEngineCtx_::rule_file
char * rule_file
Definition: detect.h:889
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
detect-classtype.h
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:784
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2444
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:502
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1220
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
SC_ERR_PCRE_PARSE
@ SC_ERR_PCRE_PARSE
Definition: util-error.h:37
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
decode.h
Signature_::class_id
uint16_t class_id
Definition: detect.h:560
util-debug.h
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
SCClassConfClasstype_
Container for a Classtype from the Classification.config file.
Definition: util-classification-config.h:33
SC_ERR_UNKNOWN_VALUE
@ SC_ERR_UNKNOWN_VALUE
Definition: util-error.h:159
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2611
detect.h
SCClassConfClasstype_::classtype_id
uint16_t classtype_id
Definition: util-classification-config.h:35
SCClassConfAddClasstype
int SCClassConfAddClasstype(DetectEngineCtx *de_ctx, char *rawstr, uint16_t index)
Parses a line from the classification file and adds it to Classtype hash table in DetectEngineCtx,...
Definition: util-classification-config.c:240
SigMatchStrictEnabled
bool SigMatchStrictEnabled(const enum DetectKeywordId id)
Definition: detect-parse.c:301
SCClassConfGetClasstype
SCClassConfClasstype * SCClassConfGetClasstype(const char *ct_name, DetectEngineCtx *de_ctx)
Gets the classtype from the corresponding hash table stored in the Detection Engine Context's class c...
Definition: util-classification-config.c:569
SIG_FLAG_INIT_PRIO_EXPLICT
#define SIG_FLAG_INIT_PRIO_EXPLICT
Definition: detect.h:256
SCClassConfLoadClassficationConfigFile
bool SCClassConfLoadClassficationConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Classtype info from the classification.config file.
Definition: util-classification-config.c:531
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:610
Signature_::class_msg
char * class_msg
Definition: detect.h:602
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2434
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
util-classification-config.h
Signature_::prio
int prio
Definition: detect.h:576
SCClassConfClasstype_::priority
int priority
Definition: util-classification-config.h:38
DETECT_CLASSTYPE
@ DETECT_CLASSTYPE
Definition: detect-engine-register.h:31
str
#define str(s)
Definition: suricata-common.h:280
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:242
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-classtype.c:39
Signature_::id
uint32_t id
Definition: detect.h:573
detect-parse.h
Signature_
Signature container.
Definition: detect.h:539
CLASSTYPE_NAME_MAX_LEN
#define CLASSTYPE_NAME_MAX_LEN
Definition: util-classification-config.h:27
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2405
SCClassConfClasstype_::classtype_desc
char * classtype_desc
Definition: util-classification-config.h:45
SCClassConfGenerateValidDummyClassConfigFD01
FILE * SCClassConfGenerateValidDummyClassConfigFD01(void)
Creates a dummy classification file, with all valid Classtypes, for testing purposes.
Definition: util-classification-config.c:595
DETECT_DEFAULT_PRIO
#define DETECT_DEFAULT_PRIO
Definition: detect.h:51
DetectEngineCtx_::rule_line
int rule_line
Definition: detect.h:890
DetectClasstypeRegister
void DetectClasstypeRegister(void)
Registers the handler functions for the "Classtype" keyword.
Definition: detect-classtype.c:51
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1227
SC_ERR_CONFLICTING_RULE_KEYWORDS
@ SC_ERR_CONFLICTING_RULE_KEYWORDS
Definition: util-error.h:171