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("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("pcre2_substring_copy_bynumber failed");
87  return -1;
88  }
89 
90  if (strlen(e) >= CLASSTYPE_NAME_MAX_LEN) {
91  SCLogError("classtype '%s' is too big: max %d", rawstr, CLASSTYPE_NAME_MAX_LEN - 1);
92  return -1;
93  }
94  (void)strlcpy(out, e, outsize);
95 
96  return 0;
97 }
98 
99 /**
100  * \brief The setup function that would be called when the Signature parsing
101  * module encounters the "Classtype" keyword.
102  *
103  * \param de_ctx Pointer to the Detection Engine Context.
104  * \param s Pointer the current Signature instance that is being parsed.
105  * \param rawstr Pointer to the argument supplied to the classtype keyword.
106  *
107  * \retval 0 On success
108  * \retval -1 On failure
109  */
110 static int DetectClasstypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
111 {
112  char parsed_ct_name[CLASSTYPE_NAME_MAX_LEN] = "";
113 
114  if ((s->class_id > 0) || (s->class_msg != NULL)) {
116  SCLogError("duplicated 'classtype' "
117  "keyword detected.");
118  return -1;
119  } else {
120  SCLogWarning("duplicated 'classtype' "
121  "keyword detected. Using instance with highest priority");
122  }
123  }
124 
125  if (DetectClasstypeParseRawString(rawstr, parsed_ct_name, sizeof(parsed_ct_name)) < 0) {
126  SCLogError("invalid value for classtype keyword: "
127  "\"%s\"",
128  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("unknown classtype '%s'", parsed_ct_name);
137  return -1;
138  }
139 
140  if (s->id > 0) {
141  SCLogWarning("signature sid:%u uses "
142  "unknown classtype: \"%s\", using default priority %d. "
143  "This message won't be shown again for this classtype",
144  s->id, parsed_ct_name, DETECT_DEFAULT_PRIO);
145  } else if (de_ctx->rule_file != NULL) {
146  SCLogWarning("signature at %s:%u uses "
147  "unknown classtype: \"%s\", using default priority %d. "
148  "This message won't be shown again for this classtype",
149  de_ctx->rule_file, de_ctx->rule_line, parsed_ct_name, DETECT_DEFAULT_PRIO);
150  } else {
151  SCLogWarning("unknown classtype: \"%s\", "
152  "using default priority %d. "
153  "This message won't be shown again for this classtype",
154  parsed_ct_name, DETECT_DEFAULT_PRIO);
155  }
156 
157  char str[256];
158  snprintf(str, sizeof(str),
159  "config classification: %s,Unknown Classtype,%d\n",
160  parsed_ct_name, DETECT_DEFAULT_PRIO);
161 
162  if (SCClassConfAddClasstype(de_ctx, str, 0) < 0)
163  return -1;
164  ct = SCClassConfGetClasstype(parsed_ct_name, de_ctx);
165  if (ct == NULL)
166  return -1;
167  real_ct = false;
168  }
169 
170  /* set prio only if not already explicitly set by 'priority' keyword.
171  * update classtype in sig, but only if it is 'real' (not undefined)
172  * update sigs classtype if its prio is lower (but not undefined)
173  */
174 
175  bool update_ct = false;
177  /* don't touch Signature::prio */
178  update_ct = true;
179  } else if (s->prio == -1) {
180  s->prio = ct->priority;
181  update_ct = true;
182  } else {
183  if (ct->priority < s->prio) {
184  s->prio = ct->priority;
185  update_ct = true;
186  }
187  }
188 
189  if (real_ct && update_ct) {
190  s->class_id = ct->classtype_id;
191  s->class_msg = ct->classtype_desc;
192  }
193  return 0;
194 }
195 
196 #ifdef UNITTESTS
197 
198 /**
199  * \test undefined classtype
200  */
201 static int DetectClasstypeTest01(void)
202 {
205 
207  FAIL_IF_NULL(fd);
209  Signature *s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
210  "(msg:\"Classtype test\"; "
211  "Classtype:not_available; sid:1;)");
212  FAIL_IF_NULL(s);
213  FAIL_IF_NOT(s->prio == 3);
214 
216  PASS;
217 }
218 
219 /**
220  * \test Check that both valid and invalid classtypes in a rule are handled
221  * properly, with rules containing invalid classtypes being rejected
222  * and the ones containing valid classtypes parsed and returned.
223  */
224 static int DetectClasstypeTest02(void)
225 {
228 
230  FAIL_IF_NULL(fd);
232 
233  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
234  "(Classtype:bad-unknown; sid:1;)");
235  FAIL_IF_NULL(sig);
236 
237  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
238  "(Classtype:not-there; sid:2;)");
239  FAIL_IF_NULL(sig);
240 
241  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
242  "(Classtype:Bad-UnkNown; sid:3;)");
243  FAIL_IF_NULL(sig);
244 
245  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
246  "(Classtype:nothing-wrong; sid:4;)");
247  FAIL_IF_NULL(sig);
248 
249  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
250  "(Classtype:attempted_dos; Classtype:bad-unknown; sid:5;)");
251  FAIL_IF_NULL(sig);
252  FAIL_IF_NOT(sig->prio == 2);
253 
254  /* duplicate test */
255  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
256  "(Classtype:nothing-wrong; Classtype:Bad-UnkNown; sid:6;)");
257  FAIL_IF_NULL(sig);
258  FAIL_IF_NOT(sig->prio == 2);
259 
261  PASS;
262 }
263 
264 /**
265  * \test Check that the signatures are assigned priority based on classtype they
266  * are given.
267  */
268 static int DetectClasstypeTest03(void)
269 {
272 
274  FAIL_IF_NULL(fd);
276 
277  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
278  "(msg:\"Classtype test\"; Classtype:bad-unknown; priority:1; sid:1;)");
279  FAIL_IF_NULL(sig);
280  FAIL_IF_NOT(sig->prio == 1);
281 
282  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
283  "(msg:\"Classtype test\"; Classtype:unKnoWn; "
284  "priority:3; sid:2;)");
285  FAIL_IF_NULL(sig);
286  FAIL_IF_NOT(sig->prio == 3);
287 
288  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"Classtype test\"; "
289  "Classtype:nothing-wrong; priority:1; sid:3;)");
290  FAIL_IF_NOT(sig->prio == 1);
291 
292  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
293  "(msg:\"Classtype test\"; Classtype:bad-unknown; Classtype:undefined; "
294  "priority:5; sid:4;)");
295  FAIL_IF_NULL(sig);
296  FAIL_IF_NOT(sig->prio == 5);
297 
299  PASS;
300 }
301 
302 /**
303  * \brief This function registers unit tests for Classification Config API.
304  */
305 static void DetectClasstypeRegisterTests(void)
306 {
307  UtRegisterTest("DetectClasstypeTest01", DetectClasstypeTest01);
308  UtRegisterTest("DetectClasstypeTest02", DetectClasstypeTest02);
309  UtRegisterTest("DetectClasstypeTest03", DetectClasstypeTest03);
310 }
311 #endif /* UNITTESTS */
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:47
SigTableElmt_::url
const char * url
Definition: detect.h:1243
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:1242
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2477
DetectParseRegex
Definition: detect-parse.h:44
SigTableElmt_::name
const char * name
Definition: detect.h:1240
DetectEngineCtx_::rule_file
char * rule_file
Definition: detect.h:892
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:787
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2455
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:505
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2423
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1225
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
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:563
util-debug.h
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
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2598
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:238
SigMatchStrictEnabled
bool SigMatchStrictEnabled(const enum DetectKeywordId id)
Definition: detect-parse.c:304
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
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:563
SIG_FLAG_INIT_PRIO_EXPLICT
#define SIG_FLAG_INIT_PRIO_EXPLICT
Definition: detect.h:258
SCClassConfLoadClassficationConfigFile
bool SCClassConfLoadClassficationConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Classtype info from the classification.config file.
Definition: util-classification-config.c:526
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:613
Signature_::class_msg
char * class_msg
Definition: detect.h:605
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
util-classification-config.h
Signature_::prio
int prio
Definition: detect.h:579
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
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-classtype.c:39
Signature_::id
uint32_t id
Definition: detect.h:576
detect-parse.h
Signature_
Signature container.
Definition: detect.h:542
CLASSTYPE_NAME_MAX_LEN
#define CLASSTYPE_NAME_MAX_LEN
Definition: util-classification-config.h:27
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2416
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:589
DETECT_DEFAULT_PRIO
#define DETECT_DEFAULT_PRIO
Definition: detect.h:51
DetectEngineCtx_::rule_line
int rule_line
Definition: detect.h:893
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:1232