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  if (txd) {
98  SCLogDebug("tx %p txd %p: log_flags %x", tx, txd, txd->config.log_flags);
99  txd->config.log_flags |= BIT_U8(config->type);
100 
101  uint64_t unidir = ((AppLayerParserGetTxDetectFlags(txd, STREAM_TOSERVER) |
102  AppLayerParserGetTxDetectFlags(txd, STREAM_TOCLIENT)) &
104  if (unidir) {
105  SCLogDebug("handle unidir tx");
106  AppLayerTxConfig req;
107  memset(&req, 0, sizeof(req));
108  req.log_flags = BIT_U8(config->type);
110  f->proto, f->alproto, f->alstate, tx, CONFIG_ACTION_SET, req);
111  }
112  } else {
113  SCLogDebug("no tx data");
114  }
115  } else {
116  SCLogDebug("no tx");
117  }
118 }
119 
120 /**
121  * \brief apply the post match filestore with options
122  */
123 static int ConfigApply(DetectEngineThreadCtx *det_ctx,
124  Packet *p, const DetectConfigData *config)
125 {
126  bool this_tx = false;
127  bool this_flow = false;
128 
129  switch (config->scope) {
130  case CONFIG_SCOPE_TX:
131  this_tx = true;
132  break;
133  case CONFIG_SCOPE_FLOW:
134  this_flow = true;
135  break;
136  }
137 
138  if (this_tx) {
139  SCLogDebug("tx logic here: tx_id %"PRIu64, det_ctx->tx_id);
140  ConfigApplyTx(p->flow, det_ctx->tx_id, config);
141  } else if (this_flow) {
142  SCLogDebug("flow logic here");
143  }
144 
145  SCReturnInt(0);
146 }
147 
148 static int DetectConfigPostMatch(DetectEngineThreadCtx *det_ctx,
149  Packet *p, const Signature *s, const SigMatchCtx *ctx)
150 {
151  SCEnter();
152  const DetectConfigData *config = (const DetectConfigData *)ctx;
153  ConfigApply(det_ctx, p, config);
154  SCReturnInt(1);
155 }
156 
157 /**
158  * \brief this function is used to parse filestore options
159  * \brief into the current signature
160  *
161  * \param de_ctx pointer to the Detection Engine Context
162  * \param s pointer to the Current Signature
163  * \param str pointer to the user provided "filestore" option
164  *
165  * \retval 0 on Success
166  * \retval -1 on Failure
167  */
168 static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
169 {
170  SCEnter();
171 
172  DetectConfigData *fd = NULL;
173  int 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) {
178  SCLogError(
179  "filestore can't work with bypass keyword");
180  return -1;
181  }
182 #endif
183  pcre2_match_data *match = NULL;
184 
185  if (str == NULL || strlen(str) == 0) {
186  SCLogError("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  int ret = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
198  if (ret != 7) {
199  SCLogError("config is rather picky at this time");
200  goto error;
201  }
202  pcre2len = sizeof(subsys);
203  res = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)subsys, &pcre2len);
204  if (res < 0) {
205  SCLogError("pcre2_substring_copy_bynumber failed");
206  goto error;
207  }
208 
209  if (strcmp(subsys, "logging") != 0) {
210  SCLogError("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(match, 2, (PCRE2_UCHAR8 *)state, &pcre2len);
217  if (res < 0) {
218  SCLogError("pcre2_substring_copy_bynumber failed");
219  goto error;
220  }
221 
222  if (strcmp(state, "disable") != 0) {
223  SCLogError("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(match, 3, (PCRE2_UCHAR8 *)type, &pcre2len);
230  if (res < 0) {
231  SCLogError("pcre2_substring_copy_bynumber failed");
232  goto error;
233  }
234 
235  if (strcmp(type, "type") != 0) {
236  SCLogError("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(match, 4, (PCRE2_UCHAR8 *)typeval, &pcre2len);
243  if (res < 0) {
244  SCLogError("pcre2_substring_copy_bynumber failed");
245  goto error;
246  }
247 
248  if (!(strcmp(typeval, "tx") == 0 ||strcmp(typeval, "flow") == 0)) {
249  SCLogError("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(match, 5, (PCRE2_UCHAR8 *)scope, &pcre2len);
256  if (res < 0) {
257  SCLogError("pcre2_substring_copy_bynumber failed");
258  goto error;
259  }
260 
261  if (strcmp(scope, "scope") != 0) {
262  SCLogError("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(match, 6, (PCRE2_UCHAR8 *)scopeval, &pcre2len);
269  if (res < 0) {
270  SCLogError("pcre2_substring_copy_bynumber failed");
271  goto error;
272  }
273 
274  if (!(strcmp(scopeval, "tx") == 0 ||strcmp(scopeval, "flow") == 0)) {
275  SCLogError("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  if (fd->scope == CONFIG_SCOPE_TX) {
292  s->flags |= SIG_FLAG_APPLAYER;
293  }
294 
297  goto error;
298  }
299 
300  pcre2_match_data_free(match);
301  return 0;
302 
303 error:
304  if (match) {
305  pcre2_match_data_free(match);
306  }
307  return -1;
308 }
309 
310 static void DetectConfigFree(DetectEngineCtx *de_ctx, void *ptr)
311 {
312  if (ptr != NULL) {
313  SCFree(ptr);
314  }
315 }
316 
317 #ifdef UNITTESTS
318 /*
319  * The purpose of this test is to confirm that
320  * filestore and bypass keywords can't
321  * can't work together
322  */
323 static int DetectConfigTest01(void)
324 {
326  FAIL_IF(de_ctx == NULL);
327  de_ctx->flags |= DE_QUIET;
329  "config dns any any -> any any ("
330  "dns.query; content:\"common.domain.com\"; "
331  "config:logging disable, type tx, scope tx; "
332  "sid:1;)");
333  FAIL_IF_NULL(s);
335  PASS;
336 }
337 
338 void DetectConfigRegisterTests(void)
339 {
340  UtRegisterTest("DetectConfigTest01", DetectConfigTest01);
341 }
342 #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:128
DetectConfigRegister
void DetectConfigRegister(void)
Registration function for keyword: filestore.
Definition: detect-config.c:76
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1299
flow-util.h
DetectParseRegex
Definition: detect-parse.h:62
SigTableElmt_::name
const char * name
Definition: detect.h:1309
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:1182
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:1179
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:843
APP_LAYER_TX_SKIP_INSPECT_FLAG
#define APP_LAYER_TX_SKIP_INSPECT_FLAG
Definition: app-layer-parser.h:78
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2623
CONFIG_SCOPE_TX
@ CONFIG_SCOPE_TX
Definition: util-config.h:47
DE_QUIET
#define DE_QUIET
Definition: detect.h:323
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:2645
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2591
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1294
util-unittest.h
util-unittest-helper.h
SIG_FLAG_APPLAYER
#define SIG_FLAG_APPLAYER
Definition: detect.h:246
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:124
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:17
DetectEngineThreadCtx_
Definition: detect.h:1098
AppLayerParserGetTxDetectFlags
uint64_t AppLayerParserGetTxDetectFlags(AppLayerTxData *txd, const uint8_t dir)
Definition: app-layer-parser.c:709
DetectConfigData_::scope
enum ConfigScope scope
Definition: detect-config.h:32
SIG_FLAG_BYPASS
#define SIG_FLAG_BYPASS
Definition: detect.h:272
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2771
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:604
Packet_
Definition: decode.h:476
type
uint16_t type
Definition: decode-vlan.c:107
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:227
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:1277
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1094
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:344
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1372
Packet_::flow
struct Flow_ * flow
Definition: decode.h:515
BIT_U8
#define BIT_U8(n)
Definition: suricata-common.h:398
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:1164
util-spm-bm.h
str
#define str(s)
Definition: suricata-common.h:291
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:481
detect-parse.h
Signature_
Signature container.
Definition: detect.h:603
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2584
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:437
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:845
flow.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:455
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:1301
app-layer.h