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 ret = 0, res = 0;
99  int ov[MAX_SUBSTRINGS];
100  char key[REFERENCE_SYSTEM_NAME_MAX] = "";
101  char content[REFERENCE_CONTENT_NAME_MAX] = "";
102 
103  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, MAX_SUBSTRINGS);
104  if (ret < 2) {
105  SCLogError(SC_ERR_INVALID_SIGNATURE, "Unable to parse \"reference\" "
106  "keyword argument - \"%s\". Invalid argument.", rawstr);
107  return NULL;
108  }
109 
110  DetectReference *ref = SCCalloc(1, sizeof(DetectReference));
111  if (unlikely(ref == NULL)) {
112  return NULL;
113  }
114 
115  res = pcre_copy_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 1, key, sizeof(key));
116  if (res < 0) {
117  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
118  goto error;
119  }
120 
121  res = pcre_copy_substring((char *)rawstr, ov, MAX_SUBSTRINGS, 2, content, sizeof(content));
122  if (res < 0) {
123  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
124  goto error;
125  }
126 
127  if (strlen(key) == 0 || strlen(content) == 0)
128  goto error;
129 
130  SCRConfReference *lookup_ref_conf = SCRConfGetReference(key, de_ctx);
131  if (lookup_ref_conf != NULL) {
132  ref->key = lookup_ref_conf->url;
133  } else {
136  "unknown reference key \"%s\"", key);
137  goto error;
138  }
139 
141  "unknown reference key \"%s\"", key);
142 
143  char str[2048];
144  snprintf(str, sizeof(str), "config reference: %s undefined\n", key);
145 
146  if (SCRConfAddReference(de_ctx, str) < 0)
147  goto error;
148  lookup_ref_conf = SCRConfGetReference(key, de_ctx);
149  if (lookup_ref_conf == NULL)
150  goto error;
151  }
152 
153  /* make a copy so we can free pcre's substring */
154  ref->reference = SCStrdup(content);
155  if (ref->reference == NULL) {
156  SCLogError(SC_ERR_MEM_ALLOC, "strdup failed: %s", strerror(errno));
157  goto error;
158  }
159 
160  /* free the substrings */
161  SCReturnPtr(ref, "Reference");
162 
163 error:
164  DetectReferenceFree(ref);
165  SCReturnPtr(NULL, "Reference");
166 }
167 
168 /**
169  * \internal
170  * \brief Used to add the parsed reference into the current signature.
171  *
172  * \param de_ctx Pointer to the Detection Engine Context.
173  * \param s Pointer to the Current Signature.
174  * \param m Pointer to the Current SigMatch.
175  * \param rawstr Pointer to the user provided reference options.
176  *
177  * \retval 0 On Success.
178  * \retval -1 On Failure.
179  */
180 static int DetectReferenceSetup(DetectEngineCtx *de_ctx, Signature *s,
181  const char *rawstr)
182 {
183  SCEnter();
184 
185  DetectReference *sig_refs = NULL;
186 
187  DetectReference *ref = DetectReferenceParse(rawstr, de_ctx);
188  if (ref == NULL)
189  SCReturnInt(-1);
190 
191  SCLogDebug("ref %s %s", ref->key, ref->reference);
192 
193  if (s->references == NULL) {
194  s->references = ref;
195  } else {
196  sig_refs = s->references;
197  while (sig_refs->next != NULL) {
198  sig_refs = sig_refs->next;
199  }
200  sig_refs->next = ref;
201  ref->next = NULL;
202  }
203 
204  SCReturnInt(0);
205 }
206 
207 /***************************************Unittests******************************/
208 
209 #ifdef UNITTESTS
210 
211 /**
212  * \test one valid reference.
213  *
214  * \retval 1 on succces.
215  * \retval 0 on failure.
216  */
217 static int DetectReferenceParseTest01(void)
218 {
221  de_ctx->flags |= DE_QUIET;
222 
224  FAIL_IF_NULL(fd);
226 
227  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
228  "(msg:\"One reference\"; reference:one,001-2010; sid:2;)");
229  FAIL_IF_NULL(s);
231 
232  DetectReference *ref = s->references;
233  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
234  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
235 
237  PASS;
238 }
239 
240 /**
241  * \test for two valid references.
242  *
243  * \retval 1 on succces.
244  * \retval 0 on failure.
245  */
246 static int DetectReferenceParseTest02(void)
247 {
250  de_ctx->flags |= DE_QUIET;
251 
253  FAIL_IF_NULL(fd);
255 
256  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
257  "(msg:\"Two references\"; "
258  "reference:one,openinfosecdoundation.txt; "
259  "reference:two,001-2010; sid:2;)");
260  FAIL_IF_NULL(s);
263 
264  DetectReference *ref = s->references;
265  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
266  FAIL_IF (strcmp(ref->reference, "openinfosecdoundation.txt") != 0);
267 
268  ref = s->references->next;
269  FAIL_IF (strcmp(ref->key, "http://www.two.com") != 0);
270  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
271 
273  PASS;
274 }
275 
276 /**
277  * \test parsing: invalid reference.
278  *
279  * \retval 1 on succces.
280  * \retval 0 on failure.
281  */
282 static int DetectReferenceParseTest03(void)
283 {
286  de_ctx->flags |= DE_QUIET;
287 
289  FAIL_IF_NULL(fd);
291 
292  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
293  "(msg:\"invalid ref\"; "
294  "reference:unknownkey,001-2010; sid:2;)");
295  FAIL_IF_NULL(s);
297  PASS;
298 }
299 
300 static void ReferenceRegisterTests(void)
301 {
302  UtRegisterTest("DetectReferenceParseTest01", DetectReferenceParseTest01);
303  UtRegisterTest("DetectReferenceParseTest02", DetectReferenceParseTest02);
304  UtRegisterTest("DetectReferenceParseTest03", DetectReferenceParseTest03);
305 }
306 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1212
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:1211
SigTableElmt_::name
const char * name
Definition: detect.h:1209
stream-tcp.h
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
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:298
DetectReferenceFree
void DetectReferenceFree(DetectReference *ref)
Free a Reference object.
Definition: detect-reference.c:73
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2089
DE_QUIET
#define DE_QUIET
Definition: detect.h:293
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:1195
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:56
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
util-reference-config.h
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2456
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
Signature_::references
DetectReference * references
Definition: detect.h:590
detect.h
SigMatchStrictEnabled
bool SigMatchStrictEnabled(const enum DetectKeywordId id)
Definition: detect-parse.c:298
DetectReference_
Signature reference list.
Definition: detect-reference.h:34
REFERENCE_SYSTEM_NAME_MAX
#define REFERENCE_SYSTEM_NAME_MAX
Definition: util-reference-config.h:27
SCReturn
#define SCReturn
Definition: util-debug.h:302
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2388
SCRConfLoadReferenceConfigFile
int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Reference info from the reference.config file.
Definition: util-reference-config.c:493
DetectReference_::reference
char * reference
Definition: detect-reference.h:38
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
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:233
DetectReference_::next
struct DetectReference_ * next
Definition: detect-reference.h:40
SCRConfGetReference
SCRConfReference * SCRConfGetReference(const char *rconf_name, DetectEngineCtx *de_ctx)
Gets the refernce config from the corresponding hash table stored in the Detection Engine Context's r...
Definition: util-reference-config.c:524
decode-events.h
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:2326
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SC_ERR_REFERENCE_UNKNOWN
@ SC_ERR_REFERENCE_UNKNOWN
Definition: util-error.h:182
DetectParseRegex_
Definition: detect-parse.h:42
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
SCRConfReference_
Holds a reference from the file - reference.config.
Definition: util-reference-config.h:33
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-reference.c:46
str
#define str(s)
Definition: suricata-common.h:273
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectReference_::key
char * key
Definition: detect-reference.h:36
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2044
SCRConfReference_::url
char * url
Definition: util-reference-config.h:37
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
suricata.h
SCRConfGenerateValidDummyReferenceConfigFD01
FILE * SCRConfGenerateValidDummyReferenceConfigFD01(void)
Creates a dummy reference config, with all valid references, for testing purposes.
Definition: util-reference-config.c:546
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:767
REFERENCE_CONTENT_NAME_MAX
#define REFERENCE_CONTENT_NAME_MAX
Definition: util-reference-config.h:28
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1201