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  size_t pcre2len;
100  char key[REFERENCE_SYSTEM_NAME_MAX] = "";
101  char content[REFERENCE_CONTENT_NAME_MAX] = "";
102 
103  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
104  if (ret < 2) {
105  SCLogError("Unable to parse \"reference\" "
106  "keyword argument - \"%s\". Invalid argument.",
107  rawstr);
108  return NULL;
109  }
110 
111  DetectReference *ref = SCCalloc(1, sizeof(DetectReference));
112  if (unlikely(ref == NULL)) {
113  return NULL;
114  }
115 
116  pcre2len = sizeof(key);
117  res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)key, &pcre2len);
118  if (res < 0) {
119  SCLogError("pcre2_substring_copy_bynumber failed");
120  goto error;
121  }
122 
123  pcre2len = sizeof(content);
124  res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)content, &pcre2len);
125  if (res < 0) {
126  SCLogError("pcre2_substring_copy_bynumber failed");
127  goto error;
128  }
129 
130  if (strlen(key) == 0 || strlen(content) == 0)
131  goto error;
132 
133  SCRConfReference *lookup_ref_conf = SCRConfGetReference(key, de_ctx);
134  if (lookup_ref_conf != NULL) {
135  ref->key = lookup_ref_conf->url;
136  } else {
138  SCLogError("unknown reference key \"%s\"", key);
139  goto error;
140  }
141 
142  SCLogWarning("unknown reference key \"%s\"", key);
143 
144  char str[2048];
145  snprintf(str, sizeof(str), "config reference: %s undefined\n", key);
146 
147  if (SCRConfAddReference(de_ctx, str) < 0)
148  goto error;
149  lookup_ref_conf = SCRConfGetReference(key, de_ctx);
150  if (lookup_ref_conf == NULL)
151  goto error;
152  }
153 
154  /* make a copy so we can free pcre's substring */
155  ref->reference = SCStrdup(content);
156  if (ref->reference == NULL) {
157  SCLogError("strdup failed: %s", strerror(errno));
158  goto error;
159  }
160 
161  /* free the substrings */
162  SCReturnPtr(ref, "Reference");
163 
164 error:
165  DetectReferenceFree(ref);
166  SCReturnPtr(NULL, "Reference");
167 }
168 
169 /**
170  * \internal
171  * \brief Used to add the parsed reference into the current signature.
172  *
173  * \param de_ctx Pointer to the Detection Engine Context.
174  * \param s Pointer to the Current Signature.
175  * \param m Pointer to the Current SigMatch.
176  * \param rawstr Pointer to the user provided reference options.
177  *
178  * \retval 0 On Success.
179  * \retval -1 On Failure.
180  */
181 static int DetectReferenceSetup(DetectEngineCtx *de_ctx, Signature *s,
182  const char *rawstr)
183 {
184  SCEnter();
185 
186  DetectReference *sig_refs = NULL;
187 
188  DetectReference *ref = DetectReferenceParse(rawstr, de_ctx);
189  if (ref == NULL)
190  SCReturnInt(-1);
191 
192  SCLogDebug("ref %s %s", ref->key, ref->reference);
193 
194  if (s->references == NULL) {
195  s->references = ref;
196  } else {
197  sig_refs = s->references;
198  while (sig_refs->next != NULL) {
199  sig_refs = sig_refs->next;
200  }
201  sig_refs->next = ref;
202  ref->next = NULL;
203  }
204 
205  SCReturnInt(0);
206 }
207 
208 /***************************************Unittests******************************/
209 
210 #ifdef UNITTESTS
211 
212 /**
213  * \test one valid reference.
214  *
215  * \retval 1 on succces.
216  * \retval 0 on failure.
217  */
218 static int DetectReferenceParseTest01(void)
219 {
222  de_ctx->flags |= DE_QUIET;
223 
225  FAIL_IF_NULL(fd);
227 
228  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
229  "(msg:\"One reference\"; reference:one,001-2010; sid:2;)");
230  FAIL_IF_NULL(s);
232 
233  DetectReference *ref = s->references;
234  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
235  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
236 
238  PASS;
239 }
240 
241 /**
242  * \test for two valid references.
243  *
244  * \retval 1 on succces.
245  * \retval 0 on failure.
246  */
247 static int DetectReferenceParseTest02(void)
248 {
251  de_ctx->flags |= DE_QUIET;
252 
254  FAIL_IF_NULL(fd);
256 
257  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
258  "(msg:\"Two references\"; "
259  "reference:one,openinfosecdoundation.txt; "
260  "reference:two,001-2010; sid:2;)");
261  FAIL_IF_NULL(s);
264 
265  DetectReference *ref = s->references;
266  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
267  FAIL_IF (strcmp(ref->reference, "openinfosecdoundation.txt") != 0);
268 
269  ref = s->references->next;
270  FAIL_IF (strcmp(ref->key, "http://www.two.com") != 0);
271  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
272 
274  PASS;
275 }
276 
277 /**
278  * \test parsing: invalid reference.
279  *
280  * \retval 1 on succces.
281  * \retval 0 on failure.
282  */
283 static int DetectReferenceParseTest03(void)
284 {
287  de_ctx->flags |= DE_QUIET;
288 
290  FAIL_IF_NULL(fd);
292 
293  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
294  "(msg:\"invalid ref\"; "
295  "reference:unknownkey,001-2010; sid:2;)");
296  FAIL_IF_NULL(s);
298  PASS;
299 }
300 
301 static void ReferenceRegisterTests(void)
302 {
303  UtRegisterTest("DetectReferenceParseTest01", DetectReferenceParseTest01);
304  UtRegisterTest("DetectReferenceParseTest02", DetectReferenceParseTest02);
305  UtRegisterTest("DetectReferenceParseTest03", DetectReferenceParseTest03);
306 }
307 #endif /* UNITTESTS */
util-byte.h
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
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:787
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2455
DE_QUIET
#define DE_QUIET
Definition: detect.h:289
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
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:2598
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
Signature_::references
DetectReference * references
Definition: detect.h:607
detect.h
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
DetectReference_
Signature reference list.
Definition: detect-reference.h:31
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:491
DetectReference_::reference
char * reference
Definition: detect-reference.h:35
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:228
DetectReference_::next
struct DetectReference_ * next
Definition: detect-reference.h:37
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:522
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:76
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:280
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:33
detect-parse.h
Signature_
Signature container.
Definition: detect.h:542
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2416
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:544
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:788
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:1232