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 #ifdef UNITTESTS
44 static void DetectTargetRegisterTests (void);
45 #endif
46 
47 /**
48  * \brief Registration function for target keyword
49  *
50  */
52  /* keyword name: this is how the keyword is used in a rule */
53  sigmatch_table[DETECT_TARGET].name = "target";
54  /* description: listed in "suricata --list-keywords=all" */
55  sigmatch_table[DETECT_TARGET].desc = "indicate to output module which side is the target of the attack";
56  /* link to further documentation of the keyword. Normally on the Suricata redmine/wiki */
57  sigmatch_table[DETECT_TARGET].url = "/rules/meta.html#target";
58  /* match function is called when the signature is inspected on a packet */
60  /* setup function is called during signature parsing, when the target
61  * keyword is encountered in the rule */
62  sigmatch_table[DETECT_TARGET].Setup = DetectTargetSetup;
63  /* free function is called when the detect engine is freed. Normally at
64  * shutdown, but also during rule reloads. */
66  /* registers unittests into the system */
67 #ifdef UNITTESTS
68  sigmatch_table[DETECT_TARGET].RegisterTests = DetectTargetRegisterTests;
69 #endif
70  /* set up the PCRE for keyword parsing */
71  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
72 }
73 
74 /**
75  * \brief This function is used to parse target options passed via target: keyword
76  *
77  * \param targetstr Pointer to the user provided target options
78  *
79  * \retval targetd pointer to DetectTargetData on success
80  * \retval NULL on failure
81  */
82 static int DetectTargetParse(Signature *s, const char *targetstr)
83 {
84  int ret = 0, res = 0;
85  int ov[MAX_SUBSTRINGS];
86  char value[10];
87 
88  ret = DetectParsePcreExec(&parse_regex, targetstr, 0, 0, ov, MAX_SUBSTRINGS);
89  if (ret < 1) {
90  SCLogError(SC_ERR_PCRE_MATCH, "pcre_exec parse error, ret %" PRId32 ", string %s", ret, targetstr);
91  return -1;
92  }
93 
94  res = pcre_copy_substring(targetstr, ov, MAX_SUBSTRINGS, 1,
95  value, sizeof(value));
96  if (res < 0) {
97  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
98  return -1;
99  }
100 
101  /* now check key value */
102  if (!strcmp(value, "src_ip")) {
103  if (s->flags & SIG_FLAG_DEST_IS_TARGET) {
105  "Conflicting values of target keyword");
106  return -1;
107  }
109  } else if (!strcmp(value, "dest_ip")) {
110  if (s->flags & SIG_FLAG_SRC_IS_TARGET) {
112  "Conflicting values of target keyword");
113  return -1;
114  }
116  } else {
117  SCLogError(SC_ERR_INVALID_VALUE, "only 'src_ip' and 'dest_ip' are supported as target value");
118  return -1;
119  }
120  return 0;
121 }
122 
123 /**
124  * \brief parse the options from the 'target' keyword in the rule into
125  * the Signature data structure.
126  *
127  * \param de_ctx pointer to the Detection Engine Context
128  * \param s pointer to the Current Signature
129  * \param targetstr pointer to the user provided target options
130  *
131  * \retval 0 on Success
132  * \retval -1 on Failure
133  */
134 static int DetectTargetSetup(DetectEngineCtx *de_ctx, Signature *s, const char *targetstr)
135 {
136  int ret = DetectTargetParse(s, targetstr);
137  if (ret < 0)
138  return -1;
139 
140  return 0;
141 }
142 
143 #ifdef UNITTESTS
144 
145 static int DetectTargetSignatureTest01(void)
146 {
149 
150  Signature *sig = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any (target: dest_ip; sid:1; rev:1;)");
151  FAIL_IF_NULL(sig);
152 
154  PASS;
155 }
156 
157 /**
158  * \brief this function registers unit tests for DetectTarget
159  */
160 static void DetectTargetRegisterTests(void)
161 {
162  UtRegisterTest("DetectTargetSignatureTest01",
163  DetectTargetSignatureTest01);
164 }
165 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1214
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:1213
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
SigTableElmt_::name
const char * name
Definition: detect.h:1211
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:767
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
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:1196
util-unittest.h
DetectTargetRegister
void DetectTargetRegister(void)
Registration function for target keyword.
Definition: detect-target.c:51
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:2493
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our keyword options.
Definition: detect-target.c:37
Signature_::flags
uint32_t flags
Definition: detect.h:529
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2423
DETECT_TARGET
@ DETECT_TARGET
Definition: detect-engine-register.h:265
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:1179
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:2361
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:528
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203
SC_ERR_CONFLICTING_RULE_KEYWORDS
@ SC_ERR_CONFLICTING_RULE_KEYWORDS
Definition: util-error.h:171