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