suricata
detect-xbits.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 xbits keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 #include "action-globals.h"
29 #include "detect.h"
30 #include "threads.h"
31 #include "flow.h"
32 #include "flow-util.h"
33 #include "detect-xbits.h"
34 #include "detect-hostbits.h"
35 #include "util-spm.h"
36 #include "util-byte.h"
37 
38 #include "detect-engine-sigorder.h"
39 
40 #include "app-layer-parser.h"
41 
42 #include "detect-parse.h"
43 #include "detect-engine.h"
44 #include "detect-engine-mpm.h"
45 #include "detect-engine-state.h"
46 #include "detect-engine-build.h"
47 
48 #include "flow-bit.h"
49 #include "host-bit.h"
50 #include "ippair-bit.h"
51 #include "tx-bit.h"
52 
53 #include "util-var-name.h"
54 #include "util-unittest.h"
55 #include "util-debug.h"
56 
57 /*
58  xbits:set,bitname,track ip_pair,expire 60
59  */
60 
61 static int DetectXbitTxMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, void *state,
62  void *txv, const Signature *s, const SigMatchCtx *ctx);
63 static int DetectXbitMatch (DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *);
64 static int DetectXbitSetup (DetectEngineCtx *, Signature *, const char *);
65 #ifdef UNITTESTS
66 static void XBitsRegisterTests(void);
67 #endif
68 static void DetectXbitFree (DetectEngineCtx *, void *);
69 
71 {
72  sigmatch_table[DETECT_XBITS].name = "xbits";
73  sigmatch_table[DETECT_XBITS].desc = "operate on bits";
74  sigmatch_table[DETECT_XBITS].url = "/rules/xbits.html";
75  sigmatch_table[DETECT_XBITS].AppLayerTxMatch = DetectXbitTxMatch;
76  sigmatch_table[DETECT_XBITS].Match = DetectXbitMatch;
77  sigmatch_table[DETECT_XBITS].Setup = DetectXbitSetup;
78  sigmatch_table[DETECT_XBITS].Free = DetectXbitFree;
79 #ifdef UNITTESTS
80  sigmatch_table[DETECT_XBITS].RegisterTests = XBitsRegisterTests;
81 #endif
82  /* this is compatible to ip-only signatures */
84 }
85 
86 static int DetectIPPairbitMatchToggle (Packet *p, const DetectXbitsData *fd)
87 {
88  IPPair *pair = IPPairGetIPPairFromHash(&p->src, &p->dst);
89  if (pair == NULL)
90  return 0;
91 
92  IPPairBitToggle(pair, fd->idx, SCTIME_ADD_SECS(p->ts, fd->expire));
93  IPPairRelease(pair);
94  return 1;
95 }
96 
97 /* return true even if bit not found */
98 static int DetectIPPairbitMatchUnset (Packet *p, const DetectXbitsData *fd)
99 {
100  IPPair *pair = IPPairLookupIPPairFromHash(&p->src, &p->dst);
101  if (pair == NULL)
102  return 1;
103 
104  IPPairBitUnset(pair,fd->idx);
105  IPPairRelease(pair);
106  return 1;
107 }
108 
109 static int DetectIPPairbitMatchSet (Packet *p, const DetectXbitsData *fd)
110 {
111  IPPair *pair = IPPairGetIPPairFromHash(&p->src, &p->dst);
112  if (pair == NULL)
113  return 0;
114 
115  IPPairBitSet(pair, fd->idx, SCTIME_ADD_SECS(p->ts, fd->expire));
116  IPPairRelease(pair);
117  return 1;
118 }
119 
120 static int DetectIPPairbitMatchIsset (Packet *p, const DetectXbitsData *fd)
121 {
122  int r = 0;
123  IPPair *pair = IPPairLookupIPPairFromHash(&p->src, &p->dst);
124  if (pair == NULL)
125  return 0;
126 
127  r = IPPairBitIsset(pair, fd->idx, p->ts);
128  IPPairRelease(pair);
129  return r;
130 }
131 
132 static int DetectIPPairbitMatchIsnotset (Packet *p, const DetectXbitsData *fd)
133 {
134  int r = 0;
135  IPPair *pair = IPPairLookupIPPairFromHash(&p->src, &p->dst);
136  if (pair == NULL)
137  return 1;
138 
139  r = IPPairBitIsnotset(pair, fd->idx, p->ts);
140  IPPairRelease(pair);
141  return r;
142 }
143 
144 static int DetectXbitMatchIPPair(Packet *p, const DetectXbitsData *xd)
145 {
146  switch (xd->cmd) {
148  return DetectIPPairbitMatchIsset(p,xd);
150  return DetectIPPairbitMatchIsnotset(p,xd);
152  return DetectIPPairbitMatchSet(p,xd);
154  return DetectIPPairbitMatchUnset(p,xd);
156  return DetectIPPairbitMatchToggle(p,xd);
157  }
158  return 0;
159 }
160 
161 static int DetectXbitPostMatchTx(
162  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const DetectXbitsData *xd)
163 {
164  if (p->flow == NULL)
165  return 0;
166  if (!det_ctx->tx_id_set)
167  return 0;
168  Flow *f = p->flow;
169  void *txv = AppLayerParserGetTx(f->proto, f->alproto, f->alstate, det_ctx->tx_id);
170  if (txv == NULL)
171  return 0;
173 
174  if (xd->cmd != DETECT_XBITS_CMD_SET)
175  return 0;
176 
177  SCLogDebug("sid %u: post-match SET for bit %u on tx:%" PRIu64 ", txd:%p", s->id, xd->idx,
178  det_ctx->tx_id, txd);
179 
180  return TxBitSet(txd, xd->idx);
181 }
182 
183 /*
184  * returns 0: no match
185  * 1: match
186  * -1: error
187  */
188 
189 static int DetectXbitMatch (DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
190 {
191  const DetectXbitsData *fd = (const DetectXbitsData *)ctx;
192  if (fd == NULL)
193  return 0;
194 
195  switch (fd->type) {
196  case VAR_TYPE_HOST_BIT:
197  return DetectXbitMatchHost(p, (const DetectXbitsData *)fd);
198  break;
199  case VAR_TYPE_IPPAIR_BIT:
200  return DetectXbitMatchIPPair(p, (const DetectXbitsData *)fd);
201  break;
202  case VAR_TYPE_TX_BIT:
203  // TODO this is for PostMatch only. Can we validate somehow?
204  return DetectXbitPostMatchTx(det_ctx, p, s, fd);
205  break;
206  default:
207  break;
208  }
209  return 0;
210 }
211 
212 static int DetectXbitTxMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, void *state,
213  void *txv, const Signature *s, const SigMatchCtx *ctx)
214 {
215  const DetectXbitsData *xd = (const DetectXbitsData *)ctx;
216  DEBUG_VALIDATE_BUG_ON(xd == NULL);
217 
219 
220  SCLogDebug("sid:%u: tx:%" PRIu64 ", txd->txbits:%p", s->id, det_ctx->tx_id, txd->txbits);
221  int r = TxBitIsset(txd, xd->idx);
222  if (r == 1) {
224  }
226 }
227 
228 /** \internal
229  * \brief parse xbits rule options
230  * \retval 0 ok
231  * \retval -1 bad
232  * \param[out] cdout return DetectXbitsData structure or NULL if noalert
233  */
234 static int DetectXbitParse(DetectEngineCtx *de_ctx,
235  const char *rawstr, DetectXbitsData **cdout)
236 {
237  bool cmd_set = false;
238  bool name_set = false;
239  bool track_set = false;
240  bool expire_set = false;
241  uint8_t cmd = 0;
242  uint8_t track = 0;
243  enum VarTypes var_type = VAR_TYPE_NOT_SET;
244  uint32_t expire = DETECT_XBITS_EXPIRE_DEFAULT;
245  DetectXbitsData *cd = NULL;
246  char name[256] = "";
247  char copy[strlen(rawstr) + 1];
248  strlcpy(copy, rawstr, sizeof(copy));
249  char *context = NULL;
250  char *token = strtok_r(copy, ",", &context);
251  while (token != NULL) {
252  while (*token != '\0' && isblank(*token)) {
253  token++;
254  }
255  char *val = strchr(token, ' ');
256  if (val != NULL) {
257  *val++ = '\0';
258  while (*val != '\0' && isblank(*val)) {
259  val++;
260  }
261  } else {
262  SCLogDebug("val %s", token);
263  }
264  if (strlen(token) == 0) {
265  goto next;
266  }
267  if (strcmp(token, "noalert") == 0 && !cmd_set) {
268  if (strtok_r(NULL, ",", &context) != NULL) {
269  return -1;
270  }
271  if (val && strlen(val) != 0) {
272  return -1;
273  }
274  *cdout = NULL;
275  return 0;
276  }
277  if (!cmd_set) {
278  if (val && strlen(val) != 0) {
279  return -1;
280  }
281  if (strcmp(token, "set") == 0) {
282  cmd = DETECT_XBITS_CMD_SET;
283  } else if (strcmp(token, "isset") == 0) {
285  } else if (strcmp(token, "unset") == 0) {
287  } else if (strcmp(token, "isnotset") == 0) {
289  } else if (strcmp(token, "toggle") == 0) {
291  } else {
292  SCLogError("Invalid xbits cmd: %s", token);
293  return -1;
294  }
295  cmd_set = true;
296  } else if (!name_set) {
297  if (val && strlen(val) != 0) {
298  return -1;
299  }
300  strlcpy(name, token, sizeof(name));
301  name_set = true;
302  } else if (!track_set || !expire_set) {
303  if (val == NULL) {
304  return -1;
305  }
306  if (strcmp(token, "track") == 0) {
307  if (track_set) {
308  return -1;
309  }
310  if (strcmp(val, "ip_src") == 0) {
311  track = DETECT_XBITS_TRACK_IPSRC;
312  var_type = VAR_TYPE_HOST_BIT;
313  } else if (strcmp(val, "ip_dst") == 0) {
314  track = DETECT_XBITS_TRACK_IPDST;
315  var_type = VAR_TYPE_HOST_BIT;
316  } else if (strcmp(val, "ip_pair") == 0) {
318  var_type = VAR_TYPE_IPPAIR_BIT;
319  } else if (strcmp(val, "tx") == 0) {
320  track = DETECT_XBITS_TRACK_TX;
321  var_type = VAR_TYPE_TX_BIT;
322  } else {
323  SCLogError("Invalid xbits tracker: %s", val);
324  return -1;
325  }
326  track_set = true;
327  } else if (strcmp(token, "expire") == 0) {
328  if (expire_set) {
329  return -1;
330  }
331  if ((StringParseUint32(&expire, 10, 0, val) < 0) || (expire == 0)) {
332  SCLogError("Invalid expire value: %s", val);
333  return -1;
334  }
335  expire_set = true;
336  }
337  } else {
338  SCLogError("Invalid xbits keyword: %s", token);
339  return -1;
340  }
341  next:
342  token = strtok_r(NULL, ",", &context);
343  }
344 
345  if (track == DETECT_XBITS_TRACK_TX) {
346  if (cmd != DETECT_XBITS_CMD_ISSET && cmd != DETECT_XBITS_CMD_SET) {
347  SCLogError("tx xbits only support set and isset");
348  return -1;
349  }
350  }
351 
352  cd = SCCalloc(1, sizeof(DetectXbitsData));
353  if (unlikely(cd == NULL))
354  return -1;
355 
356  uint32_t varname_id = VarNameStoreRegister(name, var_type);
357  if (unlikely(varname_id == 0)) {
358  SCFree(cd);
359  return -1;
360  }
361  cd->idx = varname_id;
362  cd->cmd = cmd;
363  cd->tracker = track;
364  cd->type = var_type;
365  cd->expire = expire;
366 
367  SCLogDebug("idx %" PRIu32 ", cmd %d, name %s", cd->idx, cmd, strlen(name) ? name : "(none)");
368 
369  *cdout = cd;
370  return 0;
371 }
372 
373 int DetectXbitSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
374 {
375  DetectXbitsData *cd = NULL;
376 
377  int result = DetectXbitParse(de_ctx, rawstr, &cd);
378  if (result < 0) {
379  return -1;
380  } else if (cd == NULL) {
381  /* noalert doesn't use a cd/sm struct. It flags the sig. We're done. */
382  s->action &= ~ACTION_ALERT;
383  return 0;
384  }
385 
386  /* Okay so far so good, lets get this into a SigMatch
387  * and put it in the Signature. */
388  switch (cd->cmd) {
389  /* case DETECT_XBITS_CMD_NOALERT can't happen here */
391  case DETECT_XBITS_CMD_ISSET: {
392  int list = DETECT_SM_LIST_MATCH;
393  if (cd->tracker == DETECT_XBITS_TRACK_TX) {
394  SCLogDebug("tx xbit isset");
396  SCLogError("tx xbits require an explicit rule hook");
397  goto error;
398  }
399  list = s->init_data->hook.sm_list;
400  SCLogDebug("setting list %d", list);
401 
402  if (list == -1) {
403  SCLogError("tx xbits failed to set up"); // TODO how would we get here?
404  goto error;
405  }
406  }
407 
408  SCLogDebug("adding match/txmatch");
409  /* checks, so packet list */
411  NULL) {
412  goto error;
413  }
414  break;
415  }
416  // all other cases
417  // DETECT_XBITS_CMD_SET, DETECT_XBITS_CMD_UNSET, DETECT_XBITS_CMD_TOGGLE:
418  default:
419  SCLogDebug("adding post-match");
420  /* modifiers, only run when entire sig has matched */
422  DETECT_SM_LIST_POSTMATCH) == NULL) {
423  goto error;
424  }
425  break;
426  }
427 
428  return 0;
429 
430 error:
431  DetectXbitFree(de_ctx, cd);
432  return -1;
433 }
434 
435 static void DetectXbitFree (DetectEngineCtx *de_ctx, void *ptr)
436 {
437  if (ptr == NULL)
438  return;
439 
440  DetectXbitsData *fd = (DetectXbitsData *)ptr;
441  VarNameStoreUnregister(fd->idx, fd->type);
442 
443  SCFree(fd);
444 }
445 
446 #ifdef UNITTESTS
447 
448 static void XBitsTestSetup(void)
449 {
450  StorageCleanup();
451  StorageInit();
452  HostBitInitCtx();
454  StorageFinalize();
455  HostInitConfig(true);
456  IPPairInitConfig(true);
457 }
458 
459 static void XBitsTestShutdown(void)
460 {
461  HostShutdown();
462  IPPairShutdown();
463  StorageCleanup();
464 }
465 
466 
467 static int XBitsTestParse01(void)
468 {
469  DetectEngineCtx *de_ctx = NULL;
472  de_ctx->flags |= DE_QUIET;
473  DetectXbitsData *cd = NULL;
474 
475 #define BAD_INPUT(str) \
476  FAIL_IF_NOT(DetectXbitParse(de_ctx, (str), &cd) == -1);
477 
478  BAD_INPUT("alert");
479  BAD_INPUT("n0alert");
480  BAD_INPUT("nOalert");
481  BAD_INPUT("set,abc,track nonsense, expire 3600");
482  BAD_INPUT("set,abc,track ip_source, expire 3600");
483  BAD_INPUT("set,abc,track ip_src, expire -1");
484  BAD_INPUT("set,abc,track ip_src, expire 0");
485 
486 #undef BAD_INPUT
487 
488 #define GOOD_INPUT(str, command, trk, typ, exp) \
489  FAIL_IF_NOT(DetectXbitParse(de_ctx, (str), &cd) == 0); \
490  FAIL_IF_NULL(cd); \
491  FAIL_IF_NOT(cd->cmd == (command)); \
492  FAIL_IF_NOT(cd->tracker == (trk)); \
493  FAIL_IF_NOT(cd->type == (typ)); \
494  FAIL_IF_NOT(cd->expire == (exp)); \
495  DetectXbitFree(NULL, cd); \
496  cd = NULL;
497 
498  GOOD_INPUT("set,abc,track ip_pair",
502  GOOD_INPUT("set,abc,track ip_pair, expire 3600",
505  3600);
506  GOOD_INPUT("set,abc,track ip_src, expire 1234",
509  1234);
510 
511 #undef GOOD_INPUT
512 
514  PASS;
515 }
516 
517 /**
518  * \test
519  */
520 
521 static int XBitsTestSig01(void)
522 {
523  uint8_t *buf = (uint8_t *)
524  "GET /one/ HTTP/1.1\r\n"
525  "Host: one.example.org\r\n"
526  "\r\n";
527  uint16_t buflen = strlen((char *)buf);
528  Packet *p = PacketGetFromAlloc();
529  FAIL_IF_NULL(p);
530  ThreadVars th_v;
531  DetectEngineThreadCtx *det_ctx = NULL;
532  DetectEngineCtx *de_ctx = NULL;
533 
534  memset(&th_v, 0, sizeof(th_v));
535  StatsThreadInit(&th_v.stats);
536  p->src.family = AF_INET;
537  p->dst.family = AF_INET;
538  p->payload = buf;
539  p->payload_len = buflen;
540  p->proto = IPPROTO_TCP;
541 
542  XBitsTestSetup();
543 
546  de_ctx->flags |= DE_QUIET;
547 
549  "alert ip any any -> any any (xbits:set,abc,track ip_pair; content:\"GET \"; sid:1;)");
550  FAIL_IF_NULL(s);
551 
553  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
554  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
555  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
557  XBitsTestShutdown();
558  PacketFree(p);
559  StatsThreadCleanup(&th_v.stats);
561  PASS;
562 }
563 
564 /**
565  * \test various options
566  *
567  * \retval 1 on success
568  * \retval 0 on failure
569  */
570 
571 static int XBitsTestSig02(void)
572 {
573  Signature *s = NULL;
574  DetectEngineCtx *de_ctx = NULL;
577  de_ctx->flags |= DE_QUIET;
578 
580  "alert ip any any -> any any (xbits:isset,abc,track ip_src; content:\"GET \"; sid:1;)");
581  FAIL_IF_NULL(s);
582 
584  "alert ip any any -> any any (xbits:isnotset,abc,track ip_dst; content:\"GET \"; sid:2;)");
585  FAIL_IF_NULL(s);
586 
588  "alert ip any any -> any any (xbits:set,abc,track ip_pair; content:\"GET \"; sid:3;)");
589  FAIL_IF_NULL(s);
590 
592  "alert ip any any -> any any (xbits:unset,abc,track ip_src; content:\"GET \"; sid:4;)");
593  FAIL_IF_NULL(s);
594 
596  "alert ip any any -> any any (xbits:toggle,abc,track ip_dst; content:\"GET \"; sid:5;)");
597  FAIL_IF_NULL(s);
598 
600  "alert ip any any -> any any (xbits:!set,abc,track ip_dst; content:\"GET \"; sid:6;)");
601  FAIL_IF_NOT_NULL(s);
602 
604  PASS;
605 }
606 
607 /* Test to demonstrate redmine bug 4820 */
608 static int XBitsTestSig03(void)
609 {
610  DetectEngineCtx *de_ctx = NULL;
611  XBitsTestSetup();
614  de_ctx->flags |= DE_QUIET;
615 
617  de_ctx, "alert http any any -> any any (msg:\"TEST - No Error\")\";\
618  flow:established,to_server; http.method; content:\"GET\"; \
619  xbits:set,ET.2020_8260.1,track ip_src,expire 10; sid:1;)");
620  FAIL_IF_NULL(s);
621 
623  XBitsTestShutdown();
624  PASS;
625 }
626 
627 /* Test to demonstrate redmine bug 4820 */
628 static int XBitsTestSig04(void)
629 {
630  DetectEngineCtx *de_ctx = NULL;
631  XBitsTestSetup();
634  de_ctx->flags |= DE_QUIET;
635 
636  Signature *s =
637  DetectEngineAppendSig(de_ctx, "alert http any any -> any any (msg:\"TEST - Error\")\"; \
638  flow:established,to_server; http.method; content:\"GET\"; \
639  xbits:set,ET.2020_8260.1,noalert,track ip_src,expire 10; sid:2;)");
640  FAIL_IF_NOT_NULL(s);
641 
643  XBitsTestShutdown();
644  PASS;
645 }
646 
647 static int XBitsTestSig05(void)
648 {
649  DetectEngineCtx *de_ctx = NULL;
650  XBitsTestSetup();
653  de_ctx->flags |= DE_QUIET;
654 
656  "alert http any any -> any any (msg:\"ET EXPLOIT Possible Pulse Secure VPN RCE "
657  "Chain Stage 1 Inbound - Request Config Backup (CVE-2020-8260)\"; "
658  "flow:established,to_server; http.method; content:\"GET\"; http.uri; "
659  "content:\"/dana-admin/cached/config/config.cgi?type=system\"; fast_pattern; "
660  "xbits:set,ET.2020_8260.1,track ip_src,expire 10; xbits:noalert; "
661  "classtype:attempted-admin; sid:2033750; rev:1;");
662  FAIL_IF_NULL(s);
663 
665  XBitsTestShutdown();
666  PASS;
667 }
668 
669 static int XBitsTestSig06(void)
670 {
671  DetectEngineCtx *de_ctx = NULL;
672  XBitsTestSetup();
675  de_ctx->flags |= DE_QUIET;
676 
678  "alert http any any -> any any (msg:\"ET EXPLOIT Possible Pulse Secure VPN RCE "
679  "Chain Stage 2 Inbound - Upload Malicious Config (CVE-2020-8260)\"; "
680  "flow:established,to_server; http.method; content:\"POST\"; http.uri; "
681  "content:\"/dana-admin/cached/config/import.cgi\"; "
682  "xbits:isset,ET.2020_8260.1,track ip_src,expire 10;"
683  "xbits:set,ET.2020_8260.2,track ip_src,expire 10; "
684  "classtype:attempted-admin; sid:2033751; rev:1;");
685  FAIL_IF_NULL(s);
686 
688  XBitsTestShutdown();
689  PASS;
690 }
691 
692 static int DetectXBitsTestBadRules(void)
693 {
696 
697  const char *sigs[] = {
698  "alert http any any -> any any (content:\"abc\"; xbits:set,bit1,noalert,track "
699  "ip_src;sid:1;)",
700  "alert http any any -> any any (content:\"abc\"; xbits:noalert,set,bit1,noalert,track "
701  "ip_src;sid:10;)",
702  "alert http any any -> any any (content:\"abc\"; xbits:isset,bit2,track "
703  "ip_dst,asdf;sid:2;)",
704  "alert http any any -> any any (content:\"abc\"; xbits:isnotset,track ip_pair;sid:3;)",
705  "alert http any any -> any any (content:\"abc\"; xbits:toggle,track ip_pair,bit4;sid:4;)",
706  "alert http any any -> any any (content:\"abc\"; xbits:unset,bit5,track ipsrc;sid:5;)",
707  "alert http any any -> any any (content:\"abc\"; xbits:bit6,set,track ip_src,expire "
708  "10;sid:6;)",
709  "alert http any any -> any any (content:\"abc\"; xbits:set,bit7,track "
710  "ip_pair,expire;sid:7;)",
711  "alert http any any -> any any (content:\"abc\"; xbits:set,bit7,trackk ip_pair,expire "
712  "3600, noalert;sid:8;)",
713  NULL,
714  };
715 
716  const char **sig = sigs;
717  while (*sig) {
718  SCLogDebug("sig %s", *sig);
720  FAIL_IF_NOT_NULL(s);
721  sig++;
722  }
723 
725  PASS;
726 }
727 
728 static int DetectXBitsTestGoodRules(void)
729 {
732 
733  const char *sigs[] = {
734  "alert http any any -> any any (content:\"abc\"; xbits:set,bit1,track ip_src;sid:1;)",
735  "alert http any any -> any any (content:\"abc\"; xbits:isset,bit2,track ip_dst;sid:2;)",
736  "alert http any any -> any any (content:\"abc\"; xbits:isnotset, bit3, track "
737  "ip_pair;sid:3;)",
738  "alert http any any -> any any (content:\"abc\"; xbits:toggle,bit4, track "
739  "ip_pair;sid:4;)",
740  "alert http any any -> any any (content:\"abc\"; xbits: unset ,bit5,track ip_src;sid:5;)",
741  "alert http any any -> any any (content:\"abc\"; xbits:set,bit6 ,track ip_src, expire "
742  "10 ;sid:6;)",
743  "alert http any any -> any any (content:\"abc\"; xbits:set, bit7, track ip_pair, expire "
744  "3600;sid:7;)",
745  "alert http any any -> any any (content:\"abc\"; xbits:set, bit7, track ip_pair, expire "
746  "3600; xbits:noalert; sid:8;)",
747  "alert http any any -> any any (content:\"abc\"; xbits:noalert; xbits:set, bit7, track "
748  "ip_pair, expire "
749  "3600;sid:9;)",
750  NULL,
751  };
752 
753  const char **sig = sigs;
754  while (*sig) {
755  SCLogDebug("sig %s", *sig);
757  FAIL_IF_NULL(s);
758  sig++;
759  }
760 
762  PASS;
763 }
764 
765 /**
766  * \brief this function registers unit tests for XBits
767  */
768 static void XBitsRegisterTests(void)
769 {
770  UtRegisterTest("XBitsTestParse01", XBitsTestParse01);
771  UtRegisterTest("XBitsTestSig01", XBitsTestSig01);
772  UtRegisterTest("XBitsTestSig02", XBitsTestSig02);
773  UtRegisterTest("XBitsTestSig03", XBitsTestSig03);
774  UtRegisterTest("XBitsTestSig04", XBitsTestSig04);
775  UtRegisterTest("XBitsTestSig05", XBitsTestSig05);
776  UtRegisterTest("XBitsTestSig06", XBitsTestSig06);
777  UtRegisterTest("DetectXBitsTestBadRules", DetectXBitsTestBadRules);
778  UtRegisterTest("DetectXBitsTestGoodRules", DetectXBitsTestGoodRules);
779 }
780 #endif /* UNITTESTS */
IPPairBitUnset
void IPPairBitUnset(IPPair *h, uint32_t idx)
Definition: ippair-bit.c:139
util-byte.h
StatsReleaseResources
void StatsReleaseResources(void)
Releases the resources allotted by the Stats API.
Definition: counters.c:1304
tx-bit.h
SigTableElmt_::url
const char * url
Definition: detect.h:1461
Packet_::proto
uint8_t proto
Definition: decode.h:523
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
DetectXbitsRegister
void DetectXbitsRegister(void)
Definition: detect-xbits.c:70
IPPairInitConfig
void IPPairInitConfig(bool quiet)
initialize the configuration
Definition: ippair.c:162
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
DetectXbitsData_::expire
uint32_t expire
Definition: detect-xbits.h:45
StorageInit
void StorageInit(void)
Definition: util-storage.c:70
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1445
SignatureHook_::sm_list
int sm_list
Definition: detect.h:573
flow-util.h
IPPairRelease
void IPPairRelease(IPPair *h)
Definition: ippair.c:502
SigTableElmt_::name
const char * name
Definition: detect.h:1458
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
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
name
const char * name
Definition: detect-engine-proto.c:48
Flow_::proto
uint8_t proto
Definition: flow.h:369
Packet_::payload
uint8_t * payload
Definition: decode.h:605
DetectEngineThreadCtx_::tx_id
uint64_t tx_id
Definition: detect.h:1319
action-globals.h
ippair-bit.h
threads.h
IPPairBitSet
void IPPairBitSet(IPPair *h, uint32_t idx, SCTime_t expire)
Definition: ippair-bit.c:131
Flow_
Flow data structure.
Definition: flow.h:347
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectXbitsData_::cmd
uint8_t cmd
Definition: detect-xbits.h:43
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2634
StorageCleanup
void StorageCleanup(void)
Definition: util-storage.c:78
IPPairBitIsset
int IPPairBitIsset(IPPair *h, uint32_t idx, SCTime_t ts)
Definition: ippair-bit.c:157
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1423
flow-bit.h
util-var-name.h
DETECT_XBITS_TRACK_IPDST
#define DETECT_XBITS_TRACK_IPDST
Definition: detect-xbits.h:35
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: app-layer-parser.h:42
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2418
VarNameStoreRegister
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
Definition: util-var-name.c:155
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect.h:1681
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3447
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
HostBitInitCtx
void HostBitInitCtx(void)
Definition: host-bit.c:49
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:606
DetectXbitsData_
Definition: detect-xbits.h:41
util-unittest.h
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
VAR_TYPE_NOT_SET
@ VAR_TYPE_NOT_SET
Definition: util-var.h:29
GOOD_INPUT
#define GOOD_INPUT(str, command, trk, typ, exp)
IPPairShutdown
void IPPairShutdown(void)
shutdown the flow engine
Definition: ippair.c:290
SIGNATURE_HOOK_TYPE_APP
@ SIGNATURE_HOOK_TYPE_APP
Definition: detect.h:549
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:127
DetectXbitMatchHost
int DetectXbitMatchHost(Packet *p, const DetectXbitsData *xd)
Definition: detect-hostbits.c:248
detect-xbits.h
DETECT_XBITS_CMD_ISNOTSET
#define DETECT_XBITS_CMD_ISNOTSET
Definition: detect-xbits.h:30
DETECT_XBITS_EXPIRE_DEFAULT
#define DETECT_XBITS_EXPIRE_DEFAULT
Definition: detect-xbits.h:39
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1245
Packet_::ts
SCTime_t ts
Definition: decode.h:555
TxBitIsset
int TxBitIsset(AppLayerTxData *txd, uint32_t idx)
Definition: tx-bit.c:69
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:388
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
BAD_INPUT
#define BAD_INPUT(str)
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3364
VarNameStoreUnregister
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:204
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:41
DETECT_XBITS_TRACK_IPPAIR
#define DETECT_XBITS_TRACK_IPPAIR
Definition: detect-xbits.h:36
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:223
StringParseUint32
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:269
IPPairGetIPPairFromHash
IPPair * IPPairGetIPPairFromHash(Address *a, Address *b)
Definition: ippair.c:521
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
app-layer-parser.h
DETECT_XBITS
@ DETECT_XBITS
Definition: detect-engine-register.h:64
SignatureInitData_::hook
SignatureHook hook
Definition: detect.h:590
Signature_::action
uint8_t action
Definition: detect.h:683
DetectXbitsData_::type
enum VarTypes type
Definition: detect-xbits.h:47
ACTION_ALERT
#define ACTION_ALERT
Definition: action-globals.h:29
Packet_
Definition: decode.h:501
detect-engine-build.h
StorageFinalize
int StorageFinalize(void)
Definition: util-storage.c:140
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:747
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:1420
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1095
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2194
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1249
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
DETECT_XBITS_TRACK_TX
#define DETECT_XBITS_TRACK_TX
Definition: detect-xbits.h:37
IPPairBitIsnotset
int IPPairBitIsnotset(IPPair *h, uint32_t idx, SCTime_t ts)
Definition: ippair-bit.c:171
Packet_::flow
struct Flow_ * flow
Definition: decode.h:546
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
IPPair_
Definition: ippair.h:58
SignatureHook_::type
enum SignatureHookType type
Definition: detect.h:572
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3601
util-spm.h
DetectXbitsData_::idx
uint32_t idx
Definition: detect-xbits.h:42
HostShutdown
void HostShutdown(void)
shutdown the flow engine
Definition: host.c:296
AppLayerParserGetTxData
AppLayerTxData * AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx)
Definition: app-layer-parser.c:1169
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:40
detect-hostbits.h
VAR_TYPE_HOST_BIT
@ VAR_TYPE_HOST_BIT
Definition: util-var.h:41
IPPairBitInitCtx
void IPPairBitInitCtx(void)
Definition: ippair-bit.c:49
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:262
detect-engine-sigorder.h
TxBitSet
int TxBitSet(AppLayerTxData *txd, uint32_t idx)
Definition: tx-bit.c:80
DetectXbitsData_::tracker
uint8_t tracker
Definition: detect-xbits.h:44
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Flow_::alstate
void * alstate
Definition: flow.h:470
Signature_::id
uint32_t id
Definition: detect.h:713
DETECT_XBITS_TRACK_IPSRC
#define DETECT_XBITS_TRACK_IPSRC
Definition: detect-xbits.h:34
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
DETECT_XBITS_CMD_ISSET
#define DETECT_XBITS_CMD_ISSET
Definition: detect-xbits.h:31
DetectEngineThreadCtx_::tx_id_set
bool tx_id_set
Definition: detect.h:1317
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2595
IPPairBitToggle
void IPPairBitToggle(IPPair *h, uint32_t idx, SCTime_t expire)
Definition: ippair-bit.c:147
VarTypes
VarTypes
Definition: util-var.h:28
Address_::family
char family
Definition: decode.h:113
Packet_::dst
Address dst
Definition: decode.h:506
IPPairLookupIPPairFromHash
IPPair * IPPairLookupIPPairFromHash(Address *a, Address *b)
look up a ippair in the hash
Definition: ippair.c:620
VAR_TYPE_TX_BIT
@ VAR_TYPE_TX_BIT
Definition: util-var.h:49
HostInitConfig
void HostInitConfig(bool quiet)
initialize the configuration
Definition: host.c:168
DETECT_XBITS_CMD_SET
#define DETECT_XBITS_CMD_SET
Definition: detect-xbits.h:27
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:935
DETECT_XBITS_CMD_TOGGLE
#define DETECT_XBITS_CMD_TOGGLE
Definition: detect-xbits.h:28
flow.h
DETECT_XBITS_CMD_UNSET
#define DETECT_XBITS_CMD_UNSET
Definition: detect-xbits.h:29
VAR_TYPE_IPPAIR_BIT
@ VAR_TYPE_IPPAIR_BIT
Definition: util-var.h:45
SCTIME_ADD_SECS
#define SCTIME_ADD_SECS(ts, s)
Definition: util-time.h:64
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:441
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1345
SIGMATCH_IPONLY_COMPAT
#define SIGMATCH_IPONLY_COMPAT
Definition: detect.h:1652
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
Packet_::src
Address src
Definition: decode.h:505
host-bit.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1447