suricata
detect-config.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 Victor Julien <victor@inliniac.net>
22  *
23  * Implements the config keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "threads.h"
28 #include "debug.h"
29 #include "decode.h"
30 
31 #include "detect.h"
32 #include "detect-parse.h"
33 
34 #include "detect-engine.h"
35 #include "detect-engine-mpm.h"
36 #include "detect-engine-state.h"
37 
38 #include "feature.h"
39 
40 #include "flow.h"
41 #include "flow-var.h"
42 #include "flow-util.h"
43 
44 #include "util-debug.h"
45 #include "util-spm-bm.h"
46 #include "util-unittest.h"
47 #include "util-unittest-helper.h"
48 
49 #include "app-layer.h"
50 #include "app-layer-parser.h"
51 #include "app-layer-htp.h"
52 
53 #include "stream-tcp.h"
54 
55 #include "detect-config.h"
56 
57 #include "output.h"
58 
59 /**
60  * \brief Regex for parsing our flow options
61  */
62 #define PARSE_REGEX "^\\s*([A-z_]+)\\s*\\s*([A-z_]+)\\s*(?:,\\s*([A-z_]+)\\s+([A-z_]+))?\\s*(?:,\\s*([A-z_]+)\\s+([A-z_]+))?$"
63 
64 static DetectParseRegex parse_regex;
65 
66 static int DetectConfigPostMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
67  const Signature *s, const SigMatchCtx *ctx);
68 static int DetectConfigSetup (DetectEngineCtx *, Signature *, const char *);
69 static void DetectConfigFree(DetectEngineCtx *, void *);
70 #ifdef UNITTESTS
71 static void DetectConfigRegisterTests(void);
72 #endif
73 
74 /**
75  * \brief Registration function for keyword: filestore
76  */
78 {
79  sigmatch_table[DETECT_CONFIG].name = "config";
80  sigmatch_table[DETECT_CONFIG].Match = DetectConfigPostMatch;
81  sigmatch_table[DETECT_CONFIG].Setup = DetectConfigSetup;
82  sigmatch_table[DETECT_CONFIG].Free = DetectConfigFree;
83 #ifdef UNITTESTS
84  sigmatch_table[DETECT_CONFIG].RegisterTests = DetectConfigRegisterTests;
85 #endif
86  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
87 }
88 
89 static void ConfigApplyTx(Flow *f,
90  const uint64_t tx_id, const DetectConfigData *config)
91 {
92  void *tx = AppLayerParserGetTx(f->proto, f->alproto, f->alstate, tx_id);
93  if (tx) {
94  AppLayerTxData *txd = AppLayerParserGetTxData(f->proto, f->alproto, tx);
95  if (txd) {
96  SCLogDebug("tx %p txd %p: log_flags %x", tx, txd, txd->config.log_flags);
97  txd->config.log_flags |= BIT_U8(config->type);
98  } else {
99  SCLogDebug("no tx data");
100  }
101 
104  SCLogDebug("handle unidir tx");
105  AppLayerTxConfig req;
106  memset(&req, 0, sizeof(req));
107  req.log_flags = BIT_U8(config->type);
109  CONFIG_ACTION_SET, req);
110  }
111  } else {
112  SCLogDebug("no tx");
113  }
114 }
115 
116 /**
117  * \brief apply the post match filestore with options
118  */
119 static int ConfigApply(DetectEngineThreadCtx *det_ctx,
120  Packet *p, const DetectConfigData *config)
121 {
122  bool this_tx = false;
123  bool this_flow = false;
124 
125  switch (config->scope) {
126  case CONFIG_SCOPE_TX:
127  this_tx = true;
128  break;
129  case CONFIG_SCOPE_FLOW:
130  this_flow = true;
131  break;
132  }
133 
134  if (this_tx) {
135  SCLogDebug("tx logic here: tx_id %"PRIu64, det_ctx->tx_id);
136  ConfigApplyTx(p->flow, det_ctx->tx_id, config);
137  } else if (this_flow) {
138  SCLogDebug("flow logic here");
139  }
140 
141  SCReturnInt(0);
142 }
143 
144 static int DetectConfigPostMatch(DetectEngineThreadCtx *det_ctx,
145  Packet *p, const Signature *s, const SigMatchCtx *ctx)
146 {
147  SCEnter();
148  const DetectConfigData *config = (const DetectConfigData *)ctx;
149  ConfigApply(det_ctx, p, config);
150  SCReturnInt(1);
151 }
152 
153 /**
154  * \brief this function is used to parse filestore options
155  * \brief into the current signature
156  *
157  * \param de_ctx pointer to the Detection Engine Context
158  * \param s pointer to the Current Signature
159  * \param str pointer to the user provided "filestore" option
160  *
161  * \retval 0 on Success
162  * \retval -1 on Failure
163  */
164 static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
165 {
166  SCEnter();
167 
168  DetectConfigData *fd = NULL;
169  SigMatch *sm = NULL;
170  int ret = 0, res = 0;
171  size_t pcre2len;
172 #if 0
173  /* filestore and bypass keywords can't work together */
174  if (s->flags & SIG_FLAG_BYPASS) {
176  "filestore can't work with bypass keyword");
177  return -1;
178  }
179 #endif
180  sm = SigMatchAlloc();
181  if (sm == NULL)
182  goto error;
183  sm->type = DETECT_CONFIG;
184 
185  if (str == NULL || strlen(str) == 0) {
186  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "config keywords need arguments");
187  goto error;
188  }
189  char subsys[32];
190  char state[32];
191  char type[32];
192  char typeval[32];
193  char scope[32];
194  char scopeval[32];
195  SCLogDebug("str %s", str);
196 
197  ret = DetectParsePcreExec(&parse_regex, str, 0, 0);
198  if (ret != 7) {
199  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "config is rather picky at this time");
200  goto error;
201  }
202  pcre2len = sizeof(subsys);
203  res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)subsys, &pcre2len);
204  if (res < 0) {
205  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
206  goto error;
207  }
208 
209  if (strcmp(subsys, "logging") != 0) {
210  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'logging' supported at this time");
211  goto error;
212  }
213  SCLogDebug("subsys %s", subsys);
214 
215  pcre2len = sizeof(state);
216  res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)state, &pcre2len);
217  if (res < 0) {
218  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
219  goto error;
220  }
221 
222  if (strcmp(state, "disable") != 0) {
223  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'disable' supported at this time");
224  goto error;
225  }
226  SCLogDebug("state %s", state);
227 
228  pcre2len = sizeof(type);
229  res = pcre2_substring_copy_bynumber(parse_regex.match, 3, (PCRE2_UCHAR8 *)type, &pcre2len);
230  if (res < 0) {
231  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
232  goto error;
233  }
234 
235  if (strcmp(type, "type") != 0) {
236  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'type' supported at this time");
237  goto error;
238  }
239  SCLogDebug("type %s", type);
240 
241  pcre2len = sizeof(typeval);
242  res = pcre2_substring_copy_bynumber(parse_regex.match, 4, (PCRE2_UCHAR8 *)typeval, &pcre2len);
243  if (res < 0) {
244  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
245  goto error;
246  }
247 
248  if (!(strcmp(typeval, "tx") == 0 ||strcmp(typeval, "flow") == 0)) {
249  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'tx' and 'flow' supported at this time");
250  goto error;
251  }
252  SCLogDebug("typeval %s", typeval);
253 
254  pcre2len = sizeof(scope);
255  res = pcre2_substring_copy_bynumber(parse_regex.match, 5, (PCRE2_UCHAR8 *)scope, &pcre2len);
256  if (res < 0) {
257  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
258  goto error;
259  }
260 
261  if (strcmp(scope, "scope") != 0) {
262  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'scope' supported at this time");
263  goto error;
264  }
265  SCLogDebug("scope %s", scope);
266 
267  pcre2len = sizeof(scopeval);
268  res = pcre2_substring_copy_bynumber(parse_regex.match, 6, (PCRE2_UCHAR8 *)scopeval, &pcre2len);
269  if (res < 0) {
270  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
271  goto error;
272  }
273 
274  if (!(strcmp(scopeval, "tx") == 0 ||strcmp(scopeval, "flow") == 0)) {
275  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'tx' and 'flow' supported at this time");
276  goto error;
277  }
278  SCLogDebug("scopeval %s", scopeval);
279 
280  fd = SCCalloc(1, sizeof(DetectConfigData));
281  if (unlikely(fd == NULL))
282  goto error;
283 
284  if (strcmp(typeval, "tx") == 0) {
285  fd->type = CONFIG_TYPE_TX;
286  }
287  if (strcmp(scopeval, "tx") == 0) {
288  fd->scope = CONFIG_SCOPE_TX;
289  }
290 
291  sm->ctx = (SigMatchCtx*)fd;
293 
294  return 0;
295 
296 error:
297  if (sm != NULL)
298  SCFree(sm);
299  return -1;
300 }
301 
302 static void DetectConfigFree(DetectEngineCtx *de_ctx, void *ptr)
303 {
304  if (ptr != NULL) {
305  SCFree(ptr);
306  }
307 }
308 
309 #ifdef UNITTESTS
310 /*
311  * The purpose of this test is to confirm that
312  * filestore and bypass keywords can't
313  * can't work together
314  */
315 static int DetectConfigTest01(void)
316 {
318  FAIL_IF(de_ctx == NULL);
319  de_ctx->flags |= DE_QUIET;
321  "config dns any any -> any any ("
322  "dns.query; content:\"common.domain.com\"; "
323  "config:logging disable, type tx, scope tx; "
324  "sid:1;)");
325  FAIL_IF_NULL(s);
327  PASS;
328 }
329 
330 void DetectConfigRegisterTests(void)
331 {
332  UtRegisterTest("DetectConfigTest01", DetectConfigTest01);
333 }
334 #endif /* UNITTESTS */
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2435
DetectConfigRegister
void DetectConfigRegister(void)
Registration function for keyword: filestore.
Definition: detect-config.c:77
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1191
flow-util.h
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1201
CONFIG_TYPE_TX
@ CONFIG_TYPE_TX
Definition: util-config.h:37
stream-tcp.h
AppLayerParserApplyTxConfig
void AppLayerParserApplyTxConfig(uint8_t ipproto, AppProto alproto, void *state, void *tx, enum ConfigAction mode, AppLayerTxConfig config)
Definition: app-layer-parser.c:1141
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
SC_ERR_INVALID_RULE_ARGUMENT
@ SC_ERR_INVALID_RULE_ARGUMENT
Definition: util-error.h:302
CONFIG_ACTION_SET
@ CONFIG_ACTION_SET
Definition: util-config.h:29
Flow_::proto
uint8_t proto
Definition: flow.h:375
DetectEngineThreadCtx_::tx_id
uint64_t tx_id
Definition: detect.h:1072
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our flow options.
Definition: detect-config.c:62
Flow_::protomap
uint8_t protomap
Definition: flow.h:455
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2069
CONFIG_SCOPE_TX
@ CONFIG_SCOPE_TX
Definition: util-config.h:48
DE_QUIET
#define DE_QUIET
Definition: detect.h:294
AppLayerParserGetOptionFlags
uint32_t AppLayerParserGetOptionFlags(uint8_t protomap, AppProto alproto)
Definition: app-layer-parser.c:387
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
util-unittest.h
util-unittest-helper.h
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:98
app-layer-htp.h
feature.h
decode.h
util-debug.h
type
uint8_t type
Definition: decode-icmpv4.h:0
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
DetectConfigData_::scope
enum ConfigScope scope
Definition: detect-config.h:32
res
PoolThreadReserved res
Definition: stream-tcp-private.h:0
SIG_FLAG_BYPASS
#define SIG_FLAG_BYPASS
Definition: detect.h:241
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2558
SC_ERR_PCRE_COPY_SUBSTRING
@ SC_ERR_PCRE_COPY_SUBSTRING
Definition: util-error.h:358
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
detect.h
APP_LAYER_PARSER_OPT_UNIDIR_TXS
#define APP_LAYER_PARSER_OPT_UNIDIR_TXS
Definition: app-layer-parser.h:46
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
Signature_::flags
uint32_t flags
Definition: detect.h:518
Packet_
Definition: decode.h:414
DetectConfigData_::type
enum ConfigType type
Definition: detect-config.h:31
DetectConfigData_
Definition: detect-config.h:29
DETECT_CONFIG
@ DETECT_CONFIG
Definition: detect-engine-register.h:196
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1169
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1062
SigMatch_::type
uint8_t type
Definition: detect.h:321
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:315
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:2381
Packet_::flow
struct Flow_ * flow
Definition: decode.h:451
BIT_U8
#define BIT_U8(n)
Definition: suricata-common.h:379
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:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
AppLayerParserGetTxData
AppLayerTxData * AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx)
Definition: app-layer-parser.c:1134
util-spm-bm.h
str
#define str(s)
Definition: suricata-common.h:272
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Flow_::alstate
void * alstate
Definition: flow.h:486
detect-parse.h
Signature_
Signature container.
Definition: detect.h:517
SigMatch_
a single match condition for a signature
Definition: detect.h:320
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2024
CONFIG_SCOPE_FLOW
@ CONFIG_SCOPE_FLOW
Definition: util-config.h:49
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:759
flow.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:460
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
detect-config.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
flow-var.h
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
debug.h
output.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
app-layer.h
SC_ERR_CONFLICTING_RULE_KEYWORDS
@ SC_ERR_CONFLICTING_RULE_KEYWORDS
Definition: util-error.h:171