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  if (f->alstate == NULL) {
93  return;
94  }
95  void *tx = AppLayerParserGetTx(f->proto, f->alproto, f->alstate, tx_id);
96  if (tx) {
97  AppLayerTxData *txd = AppLayerParserGetTxData(f->proto, f->alproto, tx);
98  if (txd) {
99  SCLogDebug("tx %p txd %p: log_flags %x", tx, txd, txd->config.log_flags);
100  txd->config.log_flags |= BIT_U8(config->type);
101  } else {
102  SCLogDebug("no tx data");
103  }
104 
107  SCLogDebug("handle unidir tx");
108  AppLayerTxConfig req;
109  memset(&req, 0, sizeof(req));
110  req.log_flags = BIT_U8(config->type);
112  CONFIG_ACTION_SET, req);
113  }
114  } else {
115  SCLogDebug("no tx");
116  }
117 }
118 
119 /**
120  * \brief apply the post match filestore with options
121  */
122 static int ConfigApply(DetectEngineThreadCtx *det_ctx,
123  Packet *p, const DetectConfigData *config)
124 {
125  bool this_tx = false;
126  bool this_flow = false;
127 
128  switch (config->scope) {
129  case CONFIG_SCOPE_TX:
130  this_tx = true;
131  break;
132  case CONFIG_SCOPE_FLOW:
133  this_flow = true;
134  break;
135  }
136 
137  if (this_tx) {
138  SCLogDebug("tx logic here: tx_id %"PRIu64, det_ctx->tx_id);
139  ConfigApplyTx(p->flow, det_ctx->tx_id, config);
140  } else if (this_flow) {
141  SCLogDebug("flow logic here");
142  }
143 
144  SCReturnInt(0);
145 }
146 
147 static int DetectConfigPostMatch(DetectEngineThreadCtx *det_ctx,
148  Packet *p, const Signature *s, const SigMatchCtx *ctx)
149 {
150  SCEnter();
151  const DetectConfigData *config = (const DetectConfigData *)ctx;
152  ConfigApply(det_ctx, p, config);
153  SCReturnInt(1);
154 }
155 
156 /**
157  * \brief this function is used to parse filestore options
158  * \brief into the current signature
159  *
160  * \param de_ctx pointer to the Detection Engine Context
161  * \param s pointer to the Current Signature
162  * \param str pointer to the user provided "filestore" option
163  *
164  * \retval 0 on Success
165  * \retval -1 on Failure
166  */
167 static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
168 {
169  SCEnter();
170 
171  DetectConfigData *fd = NULL;
172  SigMatch *sm = NULL;
173  int ret = 0, res = 0;
174  size_t pcre2len;
175 #if 0
176  /* filestore and bypass keywords can't work together */
177  if (s->flags & SIG_FLAG_BYPASS) {
179  "filestore can't work with bypass keyword");
180  return -1;
181  }
182 #endif
183  sm = SigMatchAlloc();
184  if (sm == NULL)
185  goto error;
186  sm->type = DETECT_CONFIG;
187 
188  if (str == NULL || strlen(str) == 0) {
189  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "config keywords need arguments");
190  goto error;
191  }
192  char subsys[32];
193  char state[32];
194  char type[32];
195  char typeval[32];
196  char scope[32];
197  char scopeval[32];
198  SCLogDebug("str %s", str);
199 
200  ret = DetectParsePcreExec(&parse_regex, str, 0, 0);
201  if (ret != 7) {
202  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "config is rather picky at this time");
203  goto error;
204  }
205  pcre2len = sizeof(subsys);
206  res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)subsys, &pcre2len);
207  if (res < 0) {
208  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
209  goto error;
210  }
211 
212  if (strcmp(subsys, "logging") != 0) {
213  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'logging' supported at this time");
214  goto error;
215  }
216  SCLogDebug("subsys %s", subsys);
217 
218  pcre2len = sizeof(state);
219  res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)state, &pcre2len);
220  if (res < 0) {
221  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
222  goto error;
223  }
224 
225  if (strcmp(state, "disable") != 0) {
226  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'disable' supported at this time");
227  goto error;
228  }
229  SCLogDebug("state %s", state);
230 
231  pcre2len = sizeof(type);
232  res = pcre2_substring_copy_bynumber(parse_regex.match, 3, (PCRE2_UCHAR8 *)type, &pcre2len);
233  if (res < 0) {
234  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
235  goto error;
236  }
237 
238  if (strcmp(type, "type") != 0) {
239  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'type' supported at this time");
240  goto error;
241  }
242  SCLogDebug("type %s", type);
243 
244  pcre2len = sizeof(typeval);
245  res = pcre2_substring_copy_bynumber(parse_regex.match, 4, (PCRE2_UCHAR8 *)typeval, &pcre2len);
246  if (res < 0) {
247  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
248  goto error;
249  }
250 
251  if (!(strcmp(typeval, "tx") == 0 ||strcmp(typeval, "flow") == 0)) {
252  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'tx' and 'flow' supported at this time");
253  goto error;
254  }
255  SCLogDebug("typeval %s", typeval);
256 
257  pcre2len = sizeof(scope);
258  res = pcre2_substring_copy_bynumber(parse_regex.match, 5, (PCRE2_UCHAR8 *)scope, &pcre2len);
259  if (res < 0) {
260  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
261  goto error;
262  }
263 
264  if (strcmp(scope, "scope") != 0) {
265  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'scope' supported at this time");
266  goto error;
267  }
268  SCLogDebug("scope %s", scope);
269 
270  pcre2len = sizeof(scopeval);
271  res = pcre2_substring_copy_bynumber(parse_regex.match, 6, (PCRE2_UCHAR8 *)scopeval, &pcre2len);
272  if (res < 0) {
273  SCLogError(SC_ERR_PCRE_COPY_SUBSTRING, "pcre2_substring_copy_bynumber failed");
274  goto error;
275  }
276 
277  if (!(strcmp(scopeval, "tx") == 0 ||strcmp(scopeval, "flow") == 0)) {
278  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "only 'tx' and 'flow' supported at this time");
279  goto error;
280  }
281  SCLogDebug("scopeval %s", scopeval);
282 
283  fd = SCCalloc(1, sizeof(DetectConfigData));
284  if (unlikely(fd == NULL))
285  goto error;
286 
287  if (strcmp(typeval, "tx") == 0) {
288  fd->type = CONFIG_TYPE_TX;
289  }
290  if (strcmp(scopeval, "tx") == 0) {
291  fd->scope = CONFIG_SCOPE_TX;
292  }
293 
294  if (fd->scope == CONFIG_SCOPE_TX) {
295  s->flags |= SIG_FLAG_APPLAYER;
296  }
297 
298  sm->ctx = (SigMatchCtx*)fd;
300 
301  return 0;
302 
303 error:
304  if (sm != NULL)
305  SCFree(sm);
306  return -1;
307 }
308 
309 static void DetectConfigFree(DetectEngineCtx *de_ctx, void *ptr)
310 {
311  if (ptr != NULL) {
312  SCFree(ptr);
313  }
314 }
315 
316 #ifdef UNITTESTS
317 /*
318  * The purpose of this test is to confirm that
319  * filestore and bypass keywords can't
320  * can't work together
321  */
322 static int DetectConfigTest01(void)
323 {
325  FAIL_IF(de_ctx == NULL);
326  de_ctx->flags |= DE_QUIET;
328  "config dns any any -> any any ("
329  "dns.query; content:\"common.domain.com\"; "
330  "config:logging disable, type tx, scope tx; "
331  "sid:1;)");
332  FAIL_IF_NULL(s);
334  PASS;
335 }
336 
337 void DetectConfigRegisterTests(void)
338 {
339  UtRegisterTest("DetectConfigTest01", DetectConfigTest01);
340 }
341 #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:2474
DetectConfigRegister
void DetectConfigRegister(void)
Registration function for keyword: filestore.
Definition: detect-config.c:77
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
flow-util.h
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1267
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:1195
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:1133
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:811
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2433
CONFIG_SCOPE_TX
@ CONFIG_SCOPE_TX
Definition: util-config.h:48
DE_QUIET
#define DE_QUIET
Definition: detect.h:295
AppLayerParserGetOptionFlags
uint32_t AppLayerParserGetOptionFlags(uint8_t protomap, AppProto alproto)
Definition: app-layer-parser.c:431
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
util-unittest.h
util-unittest-helper.h
SIG_FLAG_APPLAYER
#define SIG_FLAG_APPLAYER
Definition: detect.h:218
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:1060
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:2597
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:47
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
Signature_::flags
uint32_t flags
Definition: detect.h:549
Packet_
Definition: decode.h:427
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:198
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:1235
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:1116
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:316
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:2420
Packet_::flow
struct Flow_ * flow
Definition: decode.h:464
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_::type
uint16_t type
Definition: detect.h:322
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:1188
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:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2394
CONFIG_SCOPE_FLOW
@ CONFIG_SCOPE_FLOW
Definition: util-config.h:49
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:812
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:1259
app-layer.h
SC_ERR_CONFLICTING_RULE_KEYWORDS
@ SC_ERR_CONFLICTING_RULE_KEYWORDS
Definition: util-error.h:171