suricata
detect-target.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 Eric Leblond <eric@regit.org>
22  *
23  * target keyword allow rules writer to specify information about target of the attack
24  */
25 
26 #include "suricata-common.h"
27 #include "util-unittest.h"
28 
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 
32 #include "detect-target.h"
33 
34 /**
35  * \brief Regex for parsing our keyword options
36  */
37 #define PARSE_REGEX "^\\s*(src_ip|dest_ip)\\s*$"
38 
39 static DetectParseRegex parse_regex;
40 
41 /* Prototypes of functions registered in DetectTargetRegister below */
42 static int DetectTargetSetup (DetectEngineCtx *, Signature *, const char *);
43 static void DetectTargetRegisterTests (void);
44 
45 /**
46  * \brief Registration function for target keyword
47  *
48  */
50  /* keyword name: this is how the keyword is used in a rule */
51  sigmatch_table[DETECT_TARGET].name = "target";
52  /* description: listed in "suricata --list-keywords=all" */
53  sigmatch_table[DETECT_TARGET].desc = "indicate to output module which side is the target of the attack";
54  /* link to further documentation of the keyword. Normally on the Suricata redmine/wiki */
55  sigmatch_table[DETECT_TARGET].url = "/rules/meta.html#target";
56  /* match function is called when the signature is inspected on a packet */
58  /* setup function is called during signature parsing, when the target
59  * keyword is encountered in the rule */
60  sigmatch_table[DETECT_TARGET].Setup = DetectTargetSetup;
61  /* free function is called when the detect engine is freed. Normally at
62  * shutdown, but also during rule reloads. */
64  /* registers unittests into the system */
65  sigmatch_table[DETECT_TARGET].RegisterTests = DetectTargetRegisterTests;
66 
67  /* set up the PCRE for keyword parsing */
68  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
69 }
70 
71 /**
72  * \brief This function is used to parse target options passed via target: keyword
73  *
74  * \param targetstr Pointer to the user provided target options
75  *
76  * \retval targetd pointer to DetectTargetData on success
77  * \retval NULL on failure
78  */
79 static int DetectTargetParse(Signature *s, const char *targetstr)
80 {
81  int ret = 0, res = 0;
82  int ov[MAX_SUBSTRINGS];
83  char value[10];
84 
85  ret = DetectParsePcreExec(&parse_regex, targetstr, 0, 0, ov, MAX_SUBSTRINGS);
86  if (ret < 1) {
87  SCLogError(SC_ERR_PCRE_MATCH, "pcre_exec parse error, ret %" PRId32 ", string %s", ret, targetstr);
88  return -1;
89  }
90 
91  res = pcre_copy_substring(targetstr, ov, MAX_SUBSTRINGS, 1,
92  value, sizeof(value));
93  if (res < 0) {
94  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
95  return -1;
96  }
97 
98  /* now check key value */
99  if (!strcmp(value, "src_ip")) {
100  if (s->flags & SIG_FLAG_DEST_IS_TARGET) {
102  "Conflicting values of target keyword");
103  return -1;
104  }
106  } else if (!strcmp(value, "dest_ip")) {
107  if (s->flags & SIG_FLAG_SRC_IS_TARGET) {
109  "Conflicting values of target keyword");
110  return -1;
111  }
113  } else {
114  SCLogError(SC_ERR_INVALID_VALUE, "only 'src_ip' and 'dest_ip' are supported as target value");
115  return -1;
116  }
117  return 0;
118 }
119 
120 /**
121  * \brief parse the options from the 'target' keyword in the rule into
122  * the Signature data structure.
123  *
124  * \param de_ctx pointer to the Detection Engine Context
125  * \param s pointer to the Current Signature
126  * \param targetstr pointer to the user provided target options
127  *
128  * \retval 0 on Success
129  * \retval -1 on Failure
130  */
131 static int DetectTargetSetup(DetectEngineCtx *de_ctx, Signature *s, const char *targetstr)
132 {
133  int ret = DetectTargetParse(s, targetstr);
134  if (ret < 0)
135  return -1;
136 
137  return 0;
138 }
139 
140 #ifdef UNITTESTS
141 
142 static int DetectTargetSignatureTest01(void)
143 {
146 
147  Signature *sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (target: dest_ip; sid:1; rev:1;)");
148  FAIL_IF_NULL(sig);
149 
151  PASS;
152 }
153 
154 #endif /* UNITTESTS */
155 
156 /**
157  * \brief this function registers unit tests for DetectTarget
158  */
159 void DetectTargetRegisterTests(void) {
160 #ifdef UNITTESTS
161  UtRegisterTest("DetectTargetSignatureTest01",
162  DetectTargetSignatureTest01);
163 #endif /* UNITTESTS */
164 }
SigTableElmt_::url
const char * url
Definition: detect.h:1212
detect-target.h
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
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1200
SigTableElmt_::name
const char * name
Definition: detect.h:1209
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SIG_FLAG_DEST_IS_TARGET
#define SIG_FLAG_DEST_IS_TARGET
Definition: detect.h:251
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2089
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
DetectTargetRegister
void DetectTargetRegister(void)
Registration function for target keyword.
Definition: detect-target.c:49
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2456
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our keyword options.
Definition: detect-target.c:37
Signature_::flags
uint32_t flags
Definition: detect.h:528
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2388
DETECT_TARGET
@ DETECT_TARGET
Definition: detect-engine-register.h:249
SIG_FLAG_SRC_IS_TARGET
#define SIG_FLAG_SRC_IS_TARGET
Definition: detect.h:249
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1178
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
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2044
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1201
SC_ERR_CONFLICTING_RULE_KEYWORDS
@ SC_ERR_CONFLICTING_RULE_KEYWORDS
Definition: util-error.h:171