suricata
detect-reference.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 Breno Silva <breno.silva@gmail.com>
22  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23  *
24  * Implements the reference keyword support
25  */
26 
27 #include "suricata-common.h"
28 #include "suricata.h"
29 #include "detect.h"
30 #include "detect-parse.h"
31 #include "detect-engine.h"
32 #include "detect-engine-mpm.h"
33 
34 #include "decode.h"
35 #include "flow-var.h"
36 #include "decode-events.h"
37 #include "stream-tcp.h"
38 
39 #include "util-reference-config.h"
40 #include "detect-reference.h"
41 
42 #include "util-unittest.h"
43 #include "util-byte.h"
44 #include "util-debug.h"
45 
46 /* Breakout key and scheme (optional) and domain/path (mandatory) */
47 #define PARSE_REGEX \
48  "^\\s*([A-Za-z0-9]+)\\s*,\"?\\s*\"?\\s*([a-zA-Z]+:\\/\\/)?([a-zA-Z0-9\\-_\\.\\/" \
49  "\\?\\=]+)\"?\\s*\"?"
50 
51 static DetectParseRegex parse_regex;
52 
53 #ifdef UNITTESTS
54 static void ReferenceRegisterTests(void);
55 #endif
56 static int DetectReferenceSetup(DetectEngineCtx *, Signature *s, const char *str);
57 
58 /**
59  * \brief Registration function for the reference: keyword
60  */
62 {
63  sigmatch_table[DETECT_REFERENCE].name = "reference";
64  sigmatch_table[DETECT_REFERENCE].desc = "direct to places where information about the rule can be found";
65  sigmatch_table[DETECT_REFERENCE].url = "/rules/meta.html#reference";
66  sigmatch_table[DETECT_REFERENCE].Setup = DetectReferenceSetup;
67 #ifdef UNITTESTS
68  sigmatch_table[DETECT_REFERENCE].RegisterTests = ReferenceRegisterTests;
69 #endif
70  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
71 }
72 
73 /**
74  * \brief Free a Reference object
75  */
77 {
78  SCEnter();
79 
80  if (ref->key)
81  SCFree(ref->key);
82 
83  if (ref->reference != NULL) {
84  SCFree(ref->reference);
85  }
86  SCFree(ref);
87 
88  SCReturn;
89 }
90 
91 /**
92  * \internal
93  * \brief This function is used to parse reference options passed via reference: keyword
94  *
95  * \param rawstr Pointer to the user provided reference options.
96  *
97  * \retval ref Pointer to signature reference on success.
98  * \retval NULL On failure.
99  */
100 static DetectReference *DetectReferenceParse(const char *rawstr, DetectEngineCtx *de_ctx)
101 {
102  SCEnter();
103 
104  int res = 0;
105  size_t pcre2len;
106  char key[REFERENCE_SYSTEM_NAME_MAX] = "";
107  char scheme[REFERENCE_SYSTEM_NAME_MAX] = "";
108  char uri[REFERENCE_CONTENT_NAME_MAX] = "";
109 
110  pcre2_match_data *match = NULL;
111  int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
112  if (ret != 4) {
113  SCLogError("Unable to parse \"reference\" "
114  "keyword argument - \"%s\". Invalid argument.",
115  rawstr);
116  if (match) {
117  pcre2_match_data_free(match);
118  }
119  return NULL;
120  }
121 
122  DetectReference *ref = SCCalloc(1, sizeof(DetectReference));
123  if (unlikely(ref == NULL)) {
124  pcre2_match_data_free(match);
125  return NULL;
126  }
127 
128  /* Position 1 = key (mandatory) */
129  pcre2len = sizeof(key);
130  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)key, &pcre2len);
131  if (res < 0) {
132  SCLogError("pcre2_substring_copy_bynumber key failed");
133  goto error;
134  }
135 
136  /* Position 2 = scheme (optional) */
137  pcre2len = sizeof(scheme);
138  (void)pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)scheme, &pcre2len);
139 
140  /* Position 3 = domain-path (mandatory) */
141  pcre2len = sizeof(uri);
142  res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)uri, &pcre2len);
143  if (res < 0) {
144  SCLogError("pcre2_substring_copy_bynumber domain-path failed");
145  goto error;
146  }
147 
148  size_t ref_len = strlen(uri);
149  /* no key, reference -- return an error */
150  if (strlen(key) == 0 || ref_len == 0)
151  goto error;
152 
153  if (strlen(scheme)) {
154  SCLogConfig("scheme value %s overrides key %s", scheme, key);
155  ref->key = SCStrdup(scheme);
156  if (ref->key == NULL) {
157  goto error;
158  }
159  /* already bound checked to be REFERENCE_SYSTEM_NAME_MAX or less */
160  ref->key_len = (uint16_t)strlen(scheme);
161  } else {
162 
163  SCRConfReference *lookup_ref_conf = SCRConfGetReference(key, de_ctx);
164  if (lookup_ref_conf != NULL) {
165  ref->key = SCStrdup(lookup_ref_conf->url);
166  if (ref->key == NULL) {
167  goto error;
168  }
169  /* already bound checked to be REFERENCE_SYSTEM_NAME_MAX or less */
170  ref->key_len = (uint16_t)strlen(ref->key);
171  } else {
173  SCLogError("unknown reference key \"%s\"", key);
174  goto error;
175  }
176 
177  SCLogWarning("unknown reference key \"%s\"", key);
178 
179  char str[2048];
180  snprintf(str, sizeof(str), "config reference: %s undefined\n", key);
181 
182  if (SCRConfAddReference(de_ctx, str) < 0)
183  goto error;
184  lookup_ref_conf = SCRConfGetReference(key, de_ctx);
185  if (lookup_ref_conf == NULL)
186  goto error;
187  }
188  }
189 
190  /* make a copy so we can free pcre's substring */
191  ref->reference = SCStrdup(uri);
192  if (ref->reference == NULL) {
193  SCLogError("strdup failed: %s", strerror(errno));
194  goto error;
195  }
196 
197  /* already bound checked to be REFERENCE_CONTENT_NAME_MAX or less */
198  ref->reference_len = (uint16_t)ref_len;
199 
200  pcre2_match_data_free(match);
201  /* free the substrings */
202  SCReturnPtr(ref, "Reference");
203 
204 error:
205  if (match) {
206  pcre2_match_data_free(match);
207  }
208  DetectReferenceFree(ref);
209  SCReturnPtr(NULL, "Reference");
210 }
211 
212 /**
213  * \internal
214  * \brief Used to add the parsed reference into the current signature.
215  *
216  * \param de_ctx Pointer to the Detection Engine Context.
217  * \param s Pointer to the Current Signature.
218  * \param m Pointer to the Current SigMatch.
219  * \param rawstr Pointer to the user provided reference options.
220  *
221  * \retval 0 On Success.
222  * \retval -1 On Failure.
223  */
224 static int DetectReferenceSetup(DetectEngineCtx *de_ctx, Signature *s,
225  const char *rawstr)
226 {
227  SCEnter();
228 
229  DetectReference *sig_refs = NULL;
230 
231  DetectReference *ref = DetectReferenceParse(rawstr, de_ctx);
232  if (ref == NULL)
233  SCReturnInt(-1);
234 
235  SCLogDebug("ref %s %s", ref->key, ref->reference);
236 
237  if (s->references == NULL) {
238  s->references = ref;
239  } else {
240  sig_refs = s->references;
241  while (sig_refs->next != NULL) {
242  sig_refs = sig_refs->next;
243  }
244  sig_refs->next = ref;
245  ref->next = NULL;
246  }
247 
248  SCReturnInt(0);
249 }
250 
251 /***************************************Unittests******************************/
252 
253 #ifdef UNITTESTS
254 
255 /**
256  * \test one valid reference.
257  *
258  * \retval 1 on success.
259  * \retval 0 on failure.
260  */
261 static int DetectReferenceParseTest01(void)
262 {
265  de_ctx->flags |= DE_QUIET;
266 
269  FAIL_IF_NULL(fd);
271 
272  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
273  "(msg:\"One reference\"; reference:one,001-2010; sid:2;)");
274  FAIL_IF_NULL(s);
276 
277  DetectReference *ref = s->references;
278  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
279  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
280 
282  PASS;
283 }
284 
285 /**
286  * \test for two valid references.
287  *
288  * \retval 1 on success.
289  * \retval 0 on failure.
290  */
291 static int DetectReferenceParseTest02(void)
292 {
295  de_ctx->flags |= DE_QUIET;
296 
299  FAIL_IF_NULL(fd);
301 
302  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
303  "(msg:\"Two references\"; "
304  "reference:one,openinfosecdoundation.txt; "
305  "reference:two,001-2010; sid:2;)");
306  FAIL_IF_NULL(s);
309 
310  DetectReference *ref = s->references;
311  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
312  FAIL_IF (strcmp(ref->reference, "openinfosecdoundation.txt") != 0);
313 
314  ref = s->references->next;
315  FAIL_IF (strcmp(ref->key, "http://www.two.com") != 0);
316  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
317 
319  PASS;
320 }
321 
322 /**
323  * \test parsing: invalid reference.
324  *
325  * \retval 1 on success.
326  * \retval 0 on failure.
327  */
328 static int DetectReferenceParseTest03(void)
329 {
332  de_ctx->flags |= DE_QUIET;
333 
336  FAIL_IF_NULL(fd);
338 
339  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
340  "(msg:\"invalid ref\"; "
341  "reference:unknownkey,001-2010; sid:2;)");
342  FAIL_IF_NULL(s);
344  PASS;
345 }
346 
347 static void ReferenceRegisterTests(void)
348 {
349  UtRegisterTest("DetectReferenceParseTest01", DetectReferenceParseTest01);
350  UtRegisterTest("DetectReferenceParseTest02", DetectReferenceParseTest02);
351  UtRegisterTest("DetectReferenceParseTest03", DetectReferenceParseTest03);
352 }
353 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1512
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:1511
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
DetectParseRegex
Definition: detect-parse.h:94
SigTableElmt_::name
const char * name
Definition: detect.h:1509
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DetectReferenceFree
void DetectReferenceFree(DetectReference *ref)
Free a Reference object.
Definition: detect-reference.c:76
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2759
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:3649
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3595
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1491
util-unittest.h
SCRConfDeInitContext
void SCRConfDeInitContext(DetectEngineCtx *de_ctx)
Releases de_ctx resources related to Reference Config API.
Definition: util-reference-config.c:180
detect-reference.h
decode.h
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectReference_::key_len
uint16_t key_len
Definition: detect-reference.h:40
DETECT_REFERENCE
@ DETECT_REFERENCE
Definition: detect-engine-register.h:107
util-reference-config.h
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:3775
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
Signature_::references
DetectReference * references
Definition: detect.h:748
detect.h
SigMatchStrictEnabled
bool SigMatchStrictEnabled(const enum DetectKeywordId id)
Definition: detect-parse.c:335
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
DetectReference_
Signature reference list.
Definition: detect-reference.h:30
REFERENCE_SYSTEM_NAME_MAX
#define REFERENCE_SYSTEM_NAME_MAX
Definition: util-reference-config.h:29
SCReturn
#define SCReturn
Definition: util-debug.h:286
DetectReference_::reference_len
uint16_t reference_len
Definition: detect-reference.h:41
SCRConfLoadReferenceConfigFile
int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Reference info from the reference.config file.
Definition: util-reference-config.c:481
DetectReference_::reference
char * reference
Definition: detect-reference.h:34
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:300
DetectReferenceRegister
void DetectReferenceRegister(void)
Registration function for the reference: keyword.
Definition: detect-reference.c:61
SCRConfAddReference
int SCRConfAddReference(DetectEngineCtx *de_ctx, const char *line)
Parses a line from the reference config file and adds it to Reference Config hash table DetectEngineC...
Definition: util-reference-config.c:221
DetectReference_::next
struct DetectReference_ * next
Definition: detect-reference.h:43
SCRConfGetReference
SCRConfReference * SCRConfGetReference(const char *rconf_name, DetectEngineCtx *de_ctx)
Gets the reference config from the corresponding hash table stored in the Detection Engine Context's ...
Definition: util-reference-config.c:512
decode-events.h
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
SCRConfReference_
Holds a reference from the file - reference.config.
Definition: util-reference-config.h:35
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-reference.c:47
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
str
#define str(s)
Definition: suricata-common.h:316
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectReference_::key
char * key
Definition: detect-reference.h:32
detect-parse.h
Signature_
Signature container.
Definition: detect.h:675
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2720
SCRConfReference_::url
char * url
Definition: util-reference-config.h:39
suricata.h
SCRConfGenerateValidDummyReferenceConfigFD01
FILE * SCRConfGenerateValidDummyReferenceConfigFD01(void)
Creates a dummy reference config, with all valid references, for testing purposes.
Definition: util-reference-config.c:534
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:975
REFERENCE_CONTENT_NAME_MAX
#define REFERENCE_CONTENT_NAME_MAX
Definition: util-reference-config.h:30
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1498