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  } else {
101  SCLogDebug("no tx data");
102  }
103 
106  SCLogDebug("handle unidir tx");
107  AppLayerTxConfig req;
108  memset(&req, 0, sizeof(req));
109  req.log_flags = BIT_U8(config->type);
111  CONFIG_ACTION_SET, req);
112  }
113  } else {
114  SCLogDebug("no tx");
115  }
116 }
117 
118 /**
119  * \brief apply the post match filestore with options
120  */
121 static int ConfigApply(DetectEngineThreadCtx *det_ctx,
122  Packet *p, const DetectConfigData *config)
123 {
124  bool this_tx = false;
125  bool this_flow = false;
126 
127  switch (config->scope) {
128  case CONFIG_SCOPE_TX:
129  this_tx = true;
130  break;
131  case CONFIG_SCOPE_FLOW:
132  this_flow = true;
133  break;
134  }
135 
136  if (this_tx) {
137  SCLogDebug("tx logic here: tx_id %"PRIu64, det_ctx->tx_id);
138  ConfigApplyTx(p->flow, det_ctx->tx_id, config);
139  } else if (this_flow) {
140  SCLogDebug("flow logic here");
141  }
142 
143  SCReturnInt(0);
144 }
145 
146 static int DetectConfigPostMatch(DetectEngineThreadCtx *det_ctx,
147  Packet *p, const Signature *s, const SigMatchCtx *ctx)
148 {
149  SCEnter();
150  const DetectConfigData *config = (const DetectConfigData *)ctx;
151  ConfigApply(det_ctx, p, config);
152  SCReturnInt(1);
153 }
154 
155 /**
156  * \brief this function is used to parse filestore options
157  * \brief into the current signature
158  *
159  * \param de_ctx pointer to the Detection Engine Context
160  * \param s pointer to the Current Signature
161  * \param str pointer to the user provided "filestore" option
162  *
163  * \retval 0 on Success
164  * \retval -1 on Failure
165  */
166 static int DetectConfigSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
167 {
168  SCEnter();
169 
170  DetectConfigData *fd = NULL;
171  SigMatch *sm = NULL;
172  int ret = 0, res = 0;
173  size_t pcre2len;
174 #if 0
175  /* filestore and bypass keywords can't work together */
176  if (s->flags & SIG_FLAG_BYPASS) {
177  SCLogError(
178  "filestore can't work with bypass keyword");
179  return -1;
180  }
181 #endif
182  sm = SigMatchAlloc();
183  if (sm == NULL)
184  goto error;
185  sm->type = DETECT_CONFIG;
186 
187  if (str == NULL || strlen(str) == 0) {
188  SCLogError("config keywords need arguments");
189  goto error;
190  }
191  char subsys[32];
192  char state[32];
193  char type[32];
194  char typeval[32];
195  char scope[32];
196  char scopeval[32];
197  SCLogDebug("str %s", str);
198 
199  ret = DetectParsePcreExec(&parse_regex, str, 0, 0);
200  if (ret != 7) {
201  SCLogError("config is rather picky at this time");
202  goto error;
203  }
204  pcre2len = sizeof(subsys);
205  res = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)subsys, &pcre2len);
206  if (res < 0) {
207  SCLogError("pcre2_substring_copy_bynumber failed");
208  goto error;
209  }
210 
211  if (strcmp(subsys, "logging") != 0) {
212  SCLogError("only 'logging' supported at this time");
213  goto error;
214  }
215  SCLogDebug("subsys %s", subsys);
216 
217  pcre2len = sizeof(state);
218  res = pcre2_substring_copy_bynumber(parse_regex.match, 2, (PCRE2_UCHAR8 *)state, &pcre2len);
219  if (res < 0) {
220  SCLogError("pcre2_substring_copy_bynumber failed");
221  goto error;
222  }
223 
224  if (strcmp(state, "disable") != 0) {
225  SCLogError("only 'disable' supported at this time");
226  goto error;
227  }
228  SCLogDebug("state %s", state);
229 
230  pcre2len = sizeof(type);
231  res = pcre2_substring_copy_bynumber(parse_regex.match, 3, (PCRE2_UCHAR8 *)type, &pcre2len);
232  if (res < 0) {
233  SCLogError("pcre2_substring_copy_bynumber failed");
234  goto error;
235  }
236 
237  if (strcmp(type, "type") != 0) {
238  SCLogError("only 'type' supported at this time");
239  goto error;
240  }
241  SCLogDebug("type %s", type);
242 
243  pcre2len = sizeof(typeval);
244  res = pcre2_substring_copy_bynumber(parse_regex.match, 4, (PCRE2_UCHAR8 *)typeval, &pcre2len);
245  if (res < 0) {
246  SCLogError("pcre2_substring_copy_bynumber failed");
247  goto error;
248  }
249 
250  if (!(strcmp(typeval, "tx") == 0 ||strcmp(typeval, "flow") == 0)) {
251  SCLogError("only 'tx' and 'flow' supported at this time");
252  goto error;
253  }
254  SCLogDebug("typeval %s", typeval);
255 
256  pcre2len = sizeof(scope);
257  res = pcre2_substring_copy_bynumber(parse_regex.match, 5, (PCRE2_UCHAR8 *)scope, &pcre2len);
258  if (res < 0) {
259  SCLogError("pcre2_substring_copy_bynumber failed");
260  goto error;
261  }
262 
263  if (strcmp(scope, "scope") != 0) {
264  SCLogError("only 'scope' supported at this time");
265  goto error;
266  }
267  SCLogDebug("scope %s", scope);
268 
269  pcre2len = sizeof(scopeval);
270  res = pcre2_substring_copy_bynumber(parse_regex.match, 6, (PCRE2_UCHAR8 *)scopeval, &pcre2len);
271  if (res < 0) {
272  SCLogError("pcre2_substring_copy_bynumber failed");
273  goto error;
274  }
275 
276  if (!(strcmp(scopeval, "tx") == 0 ||strcmp(scopeval, "flow") == 0)) {
277  SCLogError("only 'tx' and 'flow' supported at this time");
278  goto error;
279  }
280  SCLogDebug("scopeval %s", scopeval);
281 
282  fd = SCCalloc(1, sizeof(DetectConfigData));
283  if (unlikely(fd == NULL))
284  goto error;
285 
286  if (strcmp(typeval, "tx") == 0) {
287  fd->type = CONFIG_TYPE_TX;
288  }
289  if (strcmp(scopeval, "tx") == 0) {
290  fd->scope = CONFIG_SCOPE_TX;
291  }
292 
293  if (fd->scope == CONFIG_SCOPE_TX) {
294  s->flags |= SIG_FLAG_APPLAYER;
295  }
296 
297  sm->ctx = (SigMatchCtx*)fd;
299 
300  return 0;
301 
302 error:
303  if (sm != NULL)
304  SCFree(sm);
305  return -1;
306 }
307 
308 static void DetectConfigFree(DetectEngineCtx *de_ctx, void *ptr)
309 {
310  if (ptr != NULL) {
311  SCFree(ptr);
312  }
313 }
314 
315 #ifdef UNITTESTS
316 /*
317  * The purpose of this test is to confirm that
318  * filestore and bypass keywords can't
319  * can't work together
320  */
321 static int DetectConfigTest01(void)
322 {
324  FAIL_IF(de_ctx == NULL);
325  de_ctx->flags |= DE_QUIET;
327  "config dns any any -> any any ("
328  "dns.query; content:\"common.domain.com\"; "
329  "config:logging disable, type tx, scope tx; "
330  "sid:1;)");
331  FAIL_IF_NULL(s);
333  PASS;
334 }
335 
336 void DetectConfigRegisterTests(void)
337 {
338  UtRegisterTest("DetectConfigTest01", DetectConfigTest01);
339 }
340 #endif /* UNITTESTS */
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:47
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:2477
DetectConfigRegister
void DetectConfigRegister(void)
Registration function for keyword: filestore.
Definition: detect-config.c:76
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1230
flow-util.h
DetectParseRegex
Definition: detect-parse.h:44
SigTableElmt_::name
const char * name
Definition: detect.h:1240
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:1233
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:29
Flow_::proto
uint8_t proto
Definition: flow.h:379
DetectEngineThreadCtx_::tx_id
uint64_t tx_id
Definition: detect.h:1100
threads.h
Flow_
Flow data structure.
Definition: flow.h:357
PARSE_REGEX
#define PARSE_REGEX
Regex for parsing our flow options.
Definition: detect-config.c:61
Flow_::protomap
uint8_t protomap
Definition: flow.h:451
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:787
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2455
CONFIG_SCOPE_TX
@ CONFIG_SCOPE_TX
Definition: util-config.h:48
DE_QUIET
#define DE_QUIET
Definition: detect.h:289
AppLayerParserGetOptionFlags
uint32_t AppLayerParserGetOptionFlags(uint8_t protomap, AppProto alproto)
Definition: app-layer-parser.c:425
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2423
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1225
util-unittest.h
util-unittest-helper.h
SIG_FLAG_APPLAYER
#define SIG_FLAG_APPLAYER
Definition: detect.h:209
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:89
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:1027
DetectConfigData_::scope
enum ConfigScope scope
Definition: detect-config.h:32
SIG_FLAG_BYPASS
#define SIG_FLAG_BYPASS
Definition: detect.h:235
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2598
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
APP_LAYER_PARSER_OPT_UNIDIR_TXS
#define APP_LAYER_PARSER_OPT_UNIDIR_TXS
Definition: app-layer-parser.h:48
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:318
Signature_::flags
uint32_t flags
Definition: detect.h:543
Packet_
Definition: decode.h:428
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:205
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:1208
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:241
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1143
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:310
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1305
Packet_::flow
struct Flow_ * flow
Definition: decode.h:465
BIT_U8
#define BIT_U8(n)
Definition: suricata-common.h:387
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:316
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
AppLayerParserGetTxData
AppLayerTxData * AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx)
Definition: app-layer-parser.c:1215
util-spm-bm.h
str
#define str(s)
Definition: suricata-common.h:280
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:482
detect-parse.h
Signature_
Signature container.
Definition: detect.h:542
SigMatch_
a single match condition for a signature
Definition: detect.h:315
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2416
CONFIG_SCOPE_FLOW
@ CONFIG_SCOPE_FLOW
Definition: util-config.h:49
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:788
flow.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:456
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
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:356
output.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1232
app-layer.h