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 #define PARSE_REGEX "^\\s*([A-Za-z0-9]+)\\s*,\"?\\s*\"?\\s*([a-zA-Z0-9\\-_\\.\\/\\?\\=]+)\"?\\s*\"?"
47 
48 static DetectParseRegex parse_regex;
49 
50 #ifdef UNITTESTS
51 static void ReferenceRegisterTests(void);
52 #endif
53 static int DetectReferenceSetup(DetectEngineCtx *, Signature *s, const char *str);
54 
55 /**
56  * \brief Registration function for the reference: keyword
57  */
59 {
60  sigmatch_table[DETECT_REFERENCE].name = "reference";
61  sigmatch_table[DETECT_REFERENCE].desc = "direct to places where information about the rule can be found";
62  sigmatch_table[DETECT_REFERENCE].url = "/rules/meta.html#reference";
63  sigmatch_table[DETECT_REFERENCE].Setup = DetectReferenceSetup;
64 #ifdef UNITTESTS
65  sigmatch_table[DETECT_REFERENCE].RegisterTests = ReferenceRegisterTests;
66 #endif
67  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
68 }
69 
70 /**
71  * \brief Free a Reference object
72  */
74 {
75  SCEnter();
76 
77  if (ref->reference != NULL) {
78  SCFree(ref->reference);
79  }
80  SCFree(ref);
81 
82  SCReturn;
83 }
84 
85 /**
86  * \internal
87  * \brief This function is used to parse reference options passed via reference: keyword
88  *
89  * \param rawstr Pointer to the user provided reference options.
90  *
91  * \retval ref Pointer to signature reference on success.
92  * \retval NULL On failure.
93  */
94 static DetectReference *DetectReferenceParse(const char *rawstr, DetectEngineCtx *de_ctx)
95 {
96  SCEnter();
97 
98  int res = 0;
99  size_t pcre2len;
100  char key[REFERENCE_SYSTEM_NAME_MAX] = "";
101  char content[REFERENCE_CONTENT_NAME_MAX] = "";
102 
103  pcre2_match_data *match = NULL;
104  int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
105  if (ret < 2) {
106  SCLogError("Unable to parse \"reference\" "
107  "keyword argument - \"%s\". Invalid argument.",
108  rawstr);
109  if (match) {
110  pcre2_match_data_free(match);
111  }
112  return NULL;
113  }
114 
115  DetectReference *ref = SCCalloc(1, sizeof(DetectReference));
116  if (unlikely(ref == NULL)) {
117  pcre2_match_data_free(match);
118  return NULL;
119  }
120 
121  pcre2len = sizeof(key);
122  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)key, &pcre2len);
123  if (res < 0) {
124  SCLogError("pcre2_substring_copy_bynumber failed");
125  goto error;
126  }
127 
128  pcre2len = sizeof(content);
129  res = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)content, &pcre2len);
130  if (res < 0) {
131  SCLogError("pcre2_substring_copy_bynumber failed");
132  goto error;
133  }
134 
135  if (strlen(key) == 0 || strlen(content) == 0)
136  goto error;
137 
138  SCRConfReference *lookup_ref_conf = SCRConfGetReference(key, de_ctx);
139  if (lookup_ref_conf != NULL) {
140  ref->key = lookup_ref_conf->url;
141  } else {
143  SCLogError("unknown reference key \"%s\"", key);
144  goto error;
145  }
146 
147  SCLogWarning("unknown reference key \"%s\"", key);
148 
149  char str[2048];
150  snprintf(str, sizeof(str), "config reference: %s undefined\n", key);
151 
152  if (SCRConfAddReference(de_ctx, str) < 0)
153  goto error;
154  lookup_ref_conf = SCRConfGetReference(key, de_ctx);
155  if (lookup_ref_conf == NULL)
156  goto error;
157  }
158 
159  /* make a copy so we can free pcre's substring */
160  ref->reference = SCStrdup(content);
161  if (ref->reference == NULL) {
162  SCLogError("strdup failed: %s", strerror(errno));
163  goto error;
164  }
165 
166  pcre2_match_data_free(match);
167  /* free the substrings */
168  SCReturnPtr(ref, "Reference");
169 
170 error:
171  if (match) {
172  pcre2_match_data_free(match);
173  }
174  DetectReferenceFree(ref);
175  SCReturnPtr(NULL, "Reference");
176 }
177 
178 /**
179  * \internal
180  * \brief Used to add the parsed reference into the current signature.
181  *
182  * \param de_ctx Pointer to the Detection Engine Context.
183  * \param s Pointer to the Current Signature.
184  * \param m Pointer to the Current SigMatch.
185  * \param rawstr Pointer to the user provided reference options.
186  *
187  * \retval 0 On Success.
188  * \retval -1 On Failure.
189  */
190 static int DetectReferenceSetup(DetectEngineCtx *de_ctx, Signature *s,
191  const char *rawstr)
192 {
193  SCEnter();
194 
195  DetectReference *sig_refs = NULL;
196 
197  DetectReference *ref = DetectReferenceParse(rawstr, de_ctx);
198  if (ref == NULL)
199  SCReturnInt(-1);
200 
201  SCLogDebug("ref %s %s", ref->key, ref->reference);
202 
203  if (s->references == NULL) {
204  s->references = ref;
205  } else {
206  sig_refs = s->references;
207  while (sig_refs->next != NULL) {
208  sig_refs = sig_refs->next;
209  }
210  sig_refs->next = ref;
211  ref->next = NULL;
212  }
213 
214  SCReturnInt(0);
215 }
216 
217 /***************************************Unittests******************************/
218 
219 #ifdef UNITTESTS
220 
221 /**
222  * \test one valid reference.
223  *
224  * \retval 1 on success.
225  * \retval 0 on failure.
226  */
227 static int DetectReferenceParseTest01(void)
228 {
231  de_ctx->flags |= DE_QUIET;
232 
234  FAIL_IF_NULL(fd);
236 
237  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
238  "(msg:\"One reference\"; reference:one,001-2010; sid:2;)");
239  FAIL_IF_NULL(s);
241 
242  DetectReference *ref = s->references;
243  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
244  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
245 
247  PASS;
248 }
249 
250 /**
251  * \test for two valid references.
252  *
253  * \retval 1 on success.
254  * \retval 0 on failure.
255  */
256 static int DetectReferenceParseTest02(void)
257 {
260  de_ctx->flags |= DE_QUIET;
261 
263  FAIL_IF_NULL(fd);
265 
266  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
267  "(msg:\"Two references\"; "
268  "reference:one,openinfosecdoundation.txt; "
269  "reference:two,001-2010; sid:2;)");
270  FAIL_IF_NULL(s);
273 
274  DetectReference *ref = s->references;
275  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
276  FAIL_IF (strcmp(ref->reference, "openinfosecdoundation.txt") != 0);
277 
278  ref = s->references->next;
279  FAIL_IF (strcmp(ref->key, "http://www.two.com") != 0);
280  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
281 
283  PASS;
284 }
285 
286 /**
287  * \test parsing: invalid reference.
288  *
289  * \retval 1 on success.
290  * \retval 0 on failure.
291  */
292 static int DetectReferenceParseTest03(void)
293 {
296  de_ctx->flags |= DE_QUIET;
297 
299  FAIL_IF_NULL(fd);
301 
302  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
303  "(msg:\"invalid ref\"; "
304  "reference:unknownkey,001-2010; sid:2;)");
305  FAIL_IF_NULL(s);
307  PASS;
308 }
309 
310 static void ReferenceRegisterTests(void)
311 {
312  UtRegisterTest("DetectReferenceParseTest01", DetectReferenceParseTest01);
313  UtRegisterTest("DetectReferenceParseTest02", DetectReferenceParseTest02);
314  UtRegisterTest("DetectReferenceParseTest03", DetectReferenceParseTest03);
315 }
316 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1296
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:1295
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1293
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:269
DetectReferenceFree
void DetectReferenceFree(DetectReference *ref)
Free a Reference object.
Definition: detect-reference.c:73
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2580
DE_QUIET
#define DE_QUIET
Definition: detect.h:321
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2674
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2620
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
util-unittest.h
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:17
DETECT_REFERENCE
@ DETECT_REFERENCE
Definition: detect-engine-register.h:59
util-reference-config.h
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2791
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
Signature_::references
DetectReference * references
Definition: detect.h:656
detect.h
SigMatchStrictEnabled
bool SigMatchStrictEnabled(const enum DetectKeywordId id)
Definition: detect-parse.c:395
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
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:273
SCRConfLoadReferenceConfigFile
int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Reference info from the reference.config file.
Definition: util-reference-config.c:495
DetectReference_::reference
char * reference
Definition: detect-reference.h:34
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
DetectReferenceRegister
void DetectReferenceRegister(void)
Registration function for the reference: keyword.
Definition: detect-reference.c:58
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:226
DetectReference_::next
struct DetectReference_ * next
Definition: detect-reference.h:36
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:526
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
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
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:46
str
#define str(s)
Definition: suricata-common.h:291
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
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:593
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2541
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:548
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:838
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:275
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1285