suricata
detect-reference.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2026 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-byte.h"
43 #include "util-debug.h"
44 
45 /* Breakout key and scheme (optional) and domain/path (mandatory) */
46 #define PARSE_REGEX \
47  "^\\s*([A-Za-z0-9]+)\\s*,\"?\\s*\"?\\s*([a-zA-Z]+:\\/\\/)?([a-zA-Z0-9\\-_\\.\\/" \
48  "\\?\\=]+)\"?\\s*\"?"
49 
50 static DetectParseRegex parse_regex;
51 
52 #ifdef UNITTESTS
53 static void ReferenceRegisterTests(void);
54 #endif
55 static int DetectReferenceSetup(DetectEngineCtx *, Signature *s, const char *str);
56 
57 /**
58  * \brief Registration function for the reference: keyword
59  */
61 {
62  sigmatch_table[DETECT_REFERENCE].name = "reference";
63  sigmatch_table[DETECT_REFERENCE].desc = "direct to places where information about the rule can be found";
64  sigmatch_table[DETECT_REFERENCE].url = "/rules/meta.html#reference";
65  sigmatch_table[DETECT_REFERENCE].Setup = DetectReferenceSetup;
66 #ifdef UNITTESTS
67  sigmatch_table[DETECT_REFERENCE].RegisterTests = ReferenceRegisterTests;
68 #endif
69  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
70 }
71 
72 /**
73  * \brief Free a Reference object
74  */
76 {
77  SCEnter();
78 
79  if (ref->key)
80  SCFree(ref->key);
81 
82  if (ref->reference != NULL) {
83  SCFree(ref->reference);
84  }
85  SCFree(ref);
86 
87  SCReturn;
88 }
89 
90 /**
91  * \internal
92  * \brief This function is used to parse reference options passed via reference: keyword
93  *
94  * \param rawstr Pointer to the user provided reference options.
95  *
96  * \retval ref Pointer to signature reference on success.
97  * \retval NULL On failure.
98  */
99 static DetectReference *DetectReferenceParse(const char *rawstr, DetectEngineCtx *de_ctx)
100 {
101  SCEnter();
102 
103  int res = 0;
104  size_t pcre2len;
105  char key[REFERENCE_SYSTEM_NAME_MAX] = "";
106  char scheme[REFERENCE_SYSTEM_NAME_MAX] = "";
107  char uri[REFERENCE_CONTENT_NAME_MAX] = "";
108 
109  pcre2_match_data *match = NULL;
110  int ret = DetectParsePcreExec(&parse_regex, &match, rawstr, 0, 0);
111  if (ret != 4) {
112  SCLogError("Unable to parse \"reference\" "
113  "keyword argument - \"%s\". Invalid argument.",
114  rawstr);
115  if (match) {
116  pcre2_match_data_free(match);
117  }
118  return NULL;
119  }
120 
121  DetectReference *ref = SCCalloc(1, sizeof(DetectReference));
122  if (unlikely(ref == NULL)) {
123  pcre2_match_data_free(match);
124  return NULL;
125  }
126 
127  /* Position 1 = key (mandatory) */
128  pcre2len = sizeof(key);
129  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)key, &pcre2len);
130  if (res < 0) {
131  SCLogError("pcre2_substring_copy_bynumber key failed");
132  goto error;
133  }
134 
135  /* Position 2 = scheme (optional) */
136  pcre2len = sizeof(scheme);
137  (void)pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)scheme, &pcre2len);
138 
139  /* Position 3 = domain-path (mandatory) */
140  pcre2len = sizeof(uri);
141  res = pcre2_substring_copy_bynumber(match, 3, (PCRE2_UCHAR8 *)uri, &pcre2len);
142  if (res < 0) {
143  SCLogError("pcre2_substring_copy_bynumber domain-path failed");
144  goto error;
145  }
146 
147  size_t ref_len = strlen(uri);
148  /* no key, reference -- return an error */
149  if (strlen(key) == 0 || ref_len == 0)
150  goto error;
151 
152  if (strlen(scheme)) {
153  SCLogConfig("scheme value %s overrides key %s", scheme, key);
154  ref->key = SCStrdup(scheme);
155  if (ref->key == NULL) {
156  goto error;
157  }
158  /* already bound checked to be REFERENCE_SYSTEM_NAME_MAX or less */
159  ref->key_len = (uint16_t)strlen(scheme);
160  } else {
161 
162  SCRConfReference *lookup_ref_conf = SCRConfGetReference(key, de_ctx);
163  if (lookup_ref_conf == NULL) {
165  SCLogError("unknown reference key \"%s\"", key);
166  goto error;
167  }
168 
169  SCLogWarning("unknown reference key \"%s\"", key);
170 
171  char str[2048];
172  snprintf(str, sizeof(str), "config reference: %s undefined\n", key);
173 
174  if (SCRConfAddReference(de_ctx, str) < 0)
175  goto error;
176  lookup_ref_conf = SCRConfGetReference(key, de_ctx);
177  if (lookup_ref_conf == NULL)
178  goto error;
179  }
180 
181  ref->key = SCStrdup(lookup_ref_conf->url);
182  if (ref->key == NULL) {
183  goto error;
184  }
185  /* already bound checked to be REFERENCE_CONTENT_NAME_MAX or less */
186  ref->key_len = (uint16_t)strlen(ref->key);
187  }
188 
189  /* make a copy so we can free pcre's substring */
190  ref->reference = SCStrdup(uri);
191  if (ref->reference == NULL) {
192  SCLogError("strdup failed: %s", strerror(errno));
193  goto error;
194  }
195 
196  /* already bound checked to be REFERENCE_CONTENT_NAME_MAX or less */
197  ref->reference_len = (uint16_t)ref_len;
198 
199  pcre2_match_data_free(match);
200  /* free the substrings */
201  SCReturnPtr(ref, "Reference");
202 
203 error:
204  if (match) {
205  pcre2_match_data_free(match);
206  }
207  DetectReferenceFree(ref);
208  SCReturnPtr(NULL, "Reference");
209 }
210 
211 /**
212  * \internal
213  * \brief Used to add the parsed reference into the current signature.
214  *
215  * \param de_ctx Pointer to the Detection Engine Context.
216  * \param s Pointer to the Current Signature.
217  * \param m Pointer to the Current SigMatch.
218  * \param rawstr Pointer to the user provided reference options.
219  *
220  * \retval 0 On Success.
221  * \retval -1 On Failure.
222  */
223 static int DetectReferenceSetup(DetectEngineCtx *de_ctx, Signature *s,
224  const char *rawstr)
225 {
226  SCEnter();
227 
228  DetectReference *sig_refs = NULL;
229 
230  DetectReference *ref = DetectReferenceParse(rawstr, de_ctx);
231  if (ref == NULL)
232  SCReturnInt(-1);
233 
234  SCLogDebug("ref %s %s", ref->key, ref->reference);
235 
236  if (s->references == NULL) {
237  s->references = ref;
238  } else {
239  sig_refs = s->references;
240  while (sig_refs->next != NULL) {
241  sig_refs = sig_refs->next;
242  }
243  sig_refs->next = ref;
244  ref->next = NULL;
245  }
246 
247  SCReturnInt(0);
248 }
249 
250 /***************************************Unittests******************************/
251 
252 #ifdef UNITTESTS
253 
254 /**
255  * \test one valid reference.
256  *
257  * \retval 1 on success.
258  * \retval 0 on failure.
259  */
260 static int DetectReferenceParseTest01(void)
261 {
264  de_ctx->flags |= DE_QUIET;
265 
268  FAIL_IF_NULL(fd);
270 
271  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
272  "(msg:\"One reference\"; reference:one,001-2010; sid:2;)");
273  FAIL_IF_NULL(s);
275 
276  DetectReference *ref = s->references;
277  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
278  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
279 
281  PASS;
282 }
283 
284 /**
285  * \test for two valid references.
286  *
287  * \retval 1 on success.
288  * \retval 0 on failure.
289  */
290 static int DetectReferenceParseTest02(void)
291 {
294  de_ctx->flags |= DE_QUIET;
295 
298  FAIL_IF_NULL(fd);
300 
301  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
302  "(msg:\"Two references\"; "
303  "reference:one,openinfosecdoundation.txt; "
304  "reference:two,001-2010; sid:2;)");
305  FAIL_IF_NULL(s);
308 
309  DetectReference *ref = s->references;
310  FAIL_IF (strcmp(ref->key, "http://www.one.com") != 0);
311  FAIL_IF (strcmp(ref->reference, "openinfosecdoundation.txt") != 0);
312 
313  ref = s->references->next;
314  FAIL_IF (strcmp(ref->key, "http://www.two.com") != 0);
315  FAIL_IF (strcmp(ref->reference, "001-2010") != 0);
316 
318  PASS;
319 }
320 
321 /**
322  * \test parsing: invalid reference.
323  *
324  * \retval 1 on success.
325  * \retval 0 on failure.
326  */
327 static int DetectReferenceParseTest03(void)
328 {
331  de_ctx->flags |= DE_QUIET;
332 
335  FAIL_IF_NULL(fd);
337 
338  Signature *s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any "
339  "(msg:\"invalid ref\"; "
340  "reference:unknownkey,001-2010; sid:2;)");
341  FAIL_IF_NULL(s);
343  /* an unregistered key resolves through the synthetic config entry */
345  FAIL_IF(strcmp(s->references->key, "undefined") != 0);
346  FAIL_IF(s->references->key_len != strlen("undefined"));
348  FAIL_IF(strcmp(s->references->reference, "001-2010") != 0);
350  PASS;
351 }
352 
353 static void ReferenceRegisterTests(void)
354 {
355  UtRegisterTest("DetectReferenceParseTest01", DetectReferenceParseTest01);
356  UtRegisterTest("DetectReferenceParseTest02", DetectReferenceParseTest02);
357  UtRegisterTest("DetectReferenceParseTest03", DetectReferenceParseTest03);
358 }
359 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
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:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
DetectParseRegex
Definition: detect-parse.h:94
SigTableElmt_::name
const char * name
Definition: detect.h:1542
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:101
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DetectReferenceFree
void DetectReferenceFree(DetectReference *ref)
Free a Reference object.
Definition: detect-reference.c:75
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2912
DE_QUIET
#define DE_QUIET
Definition: detect.h:334
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:4025
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3971
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
SCRConfDeInitContext
void SCRConfDeInitContext(DetectEngineCtx *de_ctx)
Releases de_ctx resources related to Reference Config API.
Definition: util-reference-config.c:179
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:109
util-reference-config.h
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:4151
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
Signature_::references
DetectReference * references
Definition: detect.h:769
detect.h
SigMatchStrictEnabled
bool SigMatchStrictEnabled(const enum DetectKeywordId id)
Definition: detect-parse.c:372
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:480
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:60
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:220
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:511
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:46
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
str
#define str(s)
Definition: suricata-common.h:313
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:692
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2873
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:533
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:997
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:1531