suricata
detect-iprep.c
Go to the documentation of this file.
1 /* Copyright (C) 2012-2021 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 iprep keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 #include "detect.h"
29 #include "threads.h"
30 #include "flow.h"
31 #include "flow-bit.h"
32 #include "flow-util.h"
33 #include "detect-iprep.h"
34 #include "util-spm.h"
35 
36 #include "app-layer-parser.h"
37 
38 #include "detect-parse.h"
39 #include "detect-engine.h"
40 #include "detect-engine-mpm.h"
41 #include "detect-engine-state.h"
42 #include "detect-engine-uint.h"
43 #include "detect-engine-build.h"
44 
45 #include "util-debug.h"
46 #include "util-byte.h"
47 #include "util-unittest-helper.h"
48 #include "util-fmemopen.h"
49 #include "util-validate.h"
50 
51 #include "reputation.h"
52 #include "host.h"
53 
54 static int DetectIPRepMatch (DetectEngineThreadCtx *, Packet *,
55  const Signature *, const SigMatchCtx *);
56 static int DetectIPRepSetup (DetectEngineCtx *, Signature *, const char *);
57 void DetectIPRepFree (DetectEngineCtx *, void *);
58 #ifdef UNITTESTS
59 static void IPRepRegisterTests(void);
60 #endif
61 
63 {
64  sigmatch_table[DETECT_IPREP].name = "iprep";
65  sigmatch_table[DETECT_IPREP].desc = "match on the IP reputation information for a host";
66  sigmatch_table[DETECT_IPREP].url = "/rules/ip-reputation-rules.html#iprep";
67  sigmatch_table[DETECT_IPREP].Match = DetectIPRepMatch;
68  sigmatch_table[DETECT_IPREP].Setup = DetectIPRepSetup;
70 #ifdef UNITTESTS
71  sigmatch_table[DETECT_IPREP].RegisterTests = IPRepRegisterTests;
72 #endif
73  /* this is compatible to ip-only signatures */
75 }
76 
77 static inline int8_t GetRep(const SReputation *r, const uint8_t cat, const uint32_t version)
78 {
79  /* allow higher versions as this happens during
80  * rule reload */
81  if (r != NULL && r->version >= version) {
82  return r->rep[cat];
83  }
84  return -1;
85 }
86 
87 /** \returns: -2 no host, -1 no rep entry, 0-127 rep values */
88 static int8_t GetHostRepSrc(Packet *p, uint8_t cat, uint32_t version)
89 {
90  if (p->flags & PKT_HOST_SRC_LOOKED_UP && p->host_src == NULL) {
91  return -2;
92  } else if (p->host_src != NULL) {
93  Host *h = (Host *)p->host_src;
94  HostLock(h);
95  /* use_cnt: 1 for having iprep, 1 for packet ref */
96  DEBUG_VALIDATE_BUG_ON(h->iprep != NULL && SC_ATOMIC_GET(h->use_cnt) < 2);
97  int8_t val = GetRep(h->iprep, cat, version);
98  HostUnlock(h);
99  return val;
100  } else {
101  Host *h = HostLookupHostFromHash(&(p->src));
103  if (h == NULL)
104  return -2;
105  HostReference(&p->host_src, h);
106  /* use_cnt: 1 for having iprep, 1 for HostLookupHostFromHash,
107  * 1 for HostReference to packet */
108  DEBUG_VALIDATE_BUG_ON(h->iprep != NULL && SC_ATOMIC_GET(h->use_cnt) < 3);
109  int8_t val = GetRep(h->iprep, cat, version);
110  HostRelease(h); /* use_cnt >= 2: 1 for iprep, 1 for packet ref */
111  return val;
112  }
113 }
114 
115 static int8_t GetHostRepDst(Packet *p, uint8_t cat, uint32_t version)
116 {
117  if (p->flags & PKT_HOST_DST_LOOKED_UP && p->host_dst == NULL) {
118  return -2;
119  } else if (p->host_dst != NULL) {
120  Host *h = (Host *)p->host_dst;
121  HostLock(h);
122  /* use_cnt: 1 for having iprep, 1 for packet ref */
123  DEBUG_VALIDATE_BUG_ON(h->iprep != NULL && SC_ATOMIC_GET(h->use_cnt) < 2);
124  int8_t val = GetRep(h->iprep, cat, version);
125  HostUnlock(h);
126  return val;
127  } else {
128  Host *h = HostLookupHostFromHash(&(p->dst));
130  if (h == NULL)
131  return -2;
132  HostReference(&p->host_dst, h);
133  /* use_cnt: 1 for having iprep, 1 for HostLookupHostFromHash,
134  * 1 for HostReference to packet */
135  DEBUG_VALIDATE_BUG_ON(h->iprep != NULL && SC_ATOMIC_GET(h->use_cnt) < 3);
136  int8_t val = GetRep(h->iprep, cat, version);
137  HostRelease(h); /* use_cnt >= 2: 1 for iprep, 1 for packet ref */
138  return val;
139  }
140 }
141 
142 /*
143  * returns 0: no match
144  * 1: match
145  * -1: error
146  */
147 static int DetectIPRepMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
148  const Signature *s, const SigMatchCtx *ctx)
149 {
150  const DetectIPRepData *rd = (const DetectIPRepData *)ctx;
151  if (rd == NULL)
152  return 0;
153 
154  uint32_t version = det_ctx->de_ctx->srep_version;
155  int8_t val = 0;
156 
157  SCLogDebug("rd->cmd %u", rd->cmd);
158  switch (rd->cmd) {
159  case IPRepCmdAny:
160  if (!rd->isnotset) {
161  val = GetHostRepSrc(p, rd->cat, version);
162  if (val < 0)
163  val = SRepCIDRGetIPRepSrc(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
164  if (val >= 0) {
165  if (DetectU8Match((uint8_t)val, &rd->du8))
166  return 1;
167  }
168  val = GetHostRepDst(p, rd->cat, version);
169  if (val < 0)
170  val = SRepCIDRGetIPRepDst(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
171  if (val >= 0) {
172  return DetectU8Match((uint8_t)val, &rd->du8);
173  }
174  } else {
175  /* isnotset for any */
176 
177  val = GetHostRepSrc(p, rd->cat, version);
178  if (val < 0)
179  val = SRepCIDRGetIPRepSrc(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
180  if (val < 0) {
181  return 1;
182  }
183  val = GetHostRepDst(p, rd->cat, version);
184  if (val < 0)
185  val = SRepCIDRGetIPRepDst(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
186  if (val < 0) {
187  return 1;
188  }
189  /* both have a value, so none 'isnotset' */
190  return 0;
191  }
192  break;
193 
194  case IPRepCmdSrc:
195  val = GetHostRepSrc(p, rd->cat, version);
196  SCLogDebug("checking src -- val %d (looking for cat %u, val %u)", val, rd->cat,
197  rd->du8.arg1);
198  if (val < 0)
199  val = SRepCIDRGetIPRepSrc(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
200  if (val >= 0) {
201  return DetectU8Match((uint8_t)val, &rd->du8);
202  }
203  /* implied: no value found */
204  if (rd->isnotset) {
205  return 1;
206  }
207  break;
208 
209  case IPRepCmdDst:
210  SCLogDebug("checking dst");
211  val = GetHostRepDst(p, rd->cat, version);
212  if (val < 0)
213  val = SRepCIDRGetIPRepDst(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
214  if (val >= 0) {
215  return DetectU8Match((uint8_t)val, &rd->du8);
216  }
217  /* implied: no value found */
218  if (rd->isnotset) {
219  return 1;
220  }
221  break;
222 
223  case IPRepCmdBoth:
224  if (!rd->isnotset) {
225  val = GetHostRepSrc(p, rd->cat, version);
226  if (val < 0)
227  val = SRepCIDRGetIPRepSrc(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
228  if (val < 0 || DetectU8Match((uint8_t)val, &rd->du8) == 0)
229  return 0;
230  val = GetHostRepDst(p, rd->cat, version);
231  if (val < 0)
232  val = SRepCIDRGetIPRepDst(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
233  if (val >= 0) {
234  return DetectU8Match((uint8_t)val, &rd->du8);
235  }
236  } else {
237  val = GetHostRepSrc(p, rd->cat, version);
238  if (val >= 0)
239  return 0;
240  val = SRepCIDRGetIPRepSrc(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
241  if (val >= 0)
242  return 0;
243  val = GetHostRepDst(p, rd->cat, version);
244  if (val >= 0)
245  return 0;
246  val = SRepCIDRGetIPRepDst(det_ctx->de_ctx->srepCIDR_ctx, p, rd->cat, version);
247  if (val >= 0)
248  return 0;
249  return 1;
250  }
251  break;
252  }
253 
254  return 0;
255 }
256 
257 int DetectIPRepSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
258 {
259  DetectIPRepData *cd = SCDetectIPRepParse(rawstr);
260  if (cd == NULL) {
261  SCLogError("\"%s\" is not a valid setting for iprep", rawstr);
262  goto error;
263  }
264 
265  SCLogDebug("cmd %u, cat %u, op %u, val %u", cd->cmd, cd->cat, cd->du8.mode, cd->du8.arg1);
266 
267  /* Okay so far so good, lets get this into a SigMatch
268  * and put it in the Signature. */
269 
271  de_ctx, s, DETECT_IPREP, (SigMatchCtx *)cd, DETECT_SM_LIST_MATCH) == NULL) {
272  goto error;
273  }
274 
275  return 0;
276 
277 error:
278  if (cd != NULL)
279  DetectIPRepFree(de_ctx, cd);
280  return -1;
281 }
282 
284 {
285  DetectIPRepData *fd = (DetectIPRepData *)ptr;
286  if (fd == NULL)
287  return;
288 
289  SCDetectIPRepFree(fd);
290 }
291 
292 #ifdef UNITTESTS
293 #include "packet.h"
294 #include "action-globals.h"
295 
296 static FILE *DetectIPRepGenerateCategoriesDummy(void)
297 {
298  FILE *fd = NULL;
299  const char *buffer = "1,BadHosts,Know bad hosts";
300 
301  fd = SCFmemopen((void *)buffer, strlen(buffer), "r");
302  if (fd == NULL)
303  SCLogDebug("Error with SCFmemopen()");
304 
305  return fd;
306 }
307 
308 static FILE *DetectIPRepGenerateCategoriesDummy2(void)
309 {
310  FILE *fd = NULL;
311  const char *buffer =
312  "1,BadHosts,Know bad hosts\n"
313  "2,GoodHosts,Know good hosts\n";
314 
315  fd = SCFmemopen((void *)buffer, strlen(buffer), "r");
316  if (fd == NULL)
317  SCLogDebug("Error with SCFmemopen()");
318 
319  return fd;
320 }
321 
322 static FILE *DetectIPRepGenerateNetworksDummy(void)
323 {
324  FILE *fd = NULL;
325  const char *buffer = "10.0.0.0/24,1,20";
326 
327  fd = SCFmemopen((void *)buffer, strlen(buffer), "r");
328  if (fd == NULL)
329  SCLogDebug("Error with SCFmemopen()");
330 
331  return fd;
332 }
333 
334 static FILE *DetectIPRepGenerateNetworksDummy2(void)
335 {
336  FILE *fd = NULL;
337  const char *buffer =
338  "0.0.0.0/0,1,10\n"
339  "192.168.0.0/16,2,127";
340 
341  fd = SCFmemopen((void *)buffer, strlen(buffer), "r");
342  if (fd == NULL)
343  SCLogDebug("Error with SCFmemopen()");
344 
345  return fd;
346 }
347 
348 static int DetectIPRepTest01(void)
349 {
351  DetectEngineThreadCtx *det_ctx = NULL;
352  Signature *sig = NULL;
353  FILE *fd = NULL;
354  int r = 0;
355  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
357 
359  memset(&th_v, 0, sizeof(th_v));
361 
363  FAIL_IF_NULL(p);
364 
365  p->src.addr_data32[0] = UTHSetIPv4Address("10.0.0.1");
366  de_ctx->flags |= DE_QUIET;
368 
369  fd = DetectIPRepGenerateCategoriesDummy();
370  r = SRepLoadCatFileFromFD(fd);
371  FAIL_IF(r < 0);
372 
373  fd = DetectIPRepGenerateNetworksDummy();
375  FAIL_IF(r < 0);
376 
377  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"IPREP High value "
378  "badhost\"; iprep:any,BadHosts,>,1; sid:1;rev:1;)");
379  FAIL_IF_NULL(sig);
380 
382  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
383 
384  p->alerts.cnt = 0;
385  p->action = 0;
386  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
387 
388  FAIL_IF(p->alerts.cnt != 1);
389  FAIL_IF(PacketTestAction(p, ACTION_DROP));
390 
391  UTHFreePacket(p);
392 
393  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
395  HostShutdown();
397  PASS;
398 }
399 
400 static int DetectIPRepTest02(void)
401 {
403  DetectEngineThreadCtx *det_ctx = NULL;
404  Signature *sig = NULL;
405  FILE *fd = NULL;
406  int r = 0;
407  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
409 
411  memset(&th_v, 0, sizeof(th_v));
413 
415  FAIL_IF_NULL(p);
416 
417  p->src.addr_data32[0] = UTHSetIPv4Address("10.0.0.1");
418  de_ctx->flags |= DE_QUIET;
420 
421  fd = DetectIPRepGenerateCategoriesDummy();
422  r = SRepLoadCatFileFromFD(fd);
423  FAIL_IF(r < 0);
424 
425  fd = DetectIPRepGenerateNetworksDummy();
427  FAIL_IF(r < 0);
428 
429  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"IPREP High value "
430  "badhost\"; iprep:src,BadHosts,>,1; sid:1; rev:1;)");
431  FAIL_IF_NULL(sig);
432 
434  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
435 
436  p->alerts.cnt = 0;
437  p->action = 0;
438  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
439  FAIL_IF(p->alerts.cnt != 1);
440  FAIL_IF(PacketTestAction(p, ACTION_DROP));
441 
442  UTHFreePacket(p);
443 
444  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
446 
447  HostShutdown();
449  PASS;
450 }
451 
452 static int DetectIPRepTest03(void)
453 {
455  DetectEngineThreadCtx *det_ctx = NULL;
456  Signature *sig = NULL;
457  FILE *fd = NULL;
458  int r = 0;
459  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
461 
463  memset(&th_v, 0, sizeof(th_v));
465 
467  FAIL_IF_NULL(p);
468 
469  p->dst.addr_data32[0] = UTHSetIPv4Address("10.0.0.2");
470  de_ctx->flags |= DE_QUIET;
472 
473  fd = DetectIPRepGenerateCategoriesDummy();
474  r = SRepLoadCatFileFromFD(fd);
475  FAIL_IF(r < 0);
476 
477  fd = DetectIPRepGenerateNetworksDummy();
479  FAIL_IF(r < 0);
480 
481  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"IPREP High value "
482  "badhost\"; iprep:dst,BadHosts,>,1; sid:1; rev:1;)");
483  FAIL_IF_NULL(sig);
484 
486  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
487 
488  p->alerts.cnt = 0;
489  p->action = 0;
490  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
491  FAIL_IF(p->alerts.cnt != 1);
492  FAIL_IF(PacketTestAction(p, ACTION_DROP));
493 
494  UTHFreePacket(p);
495 
496  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
498 
499  HostShutdown();
501  PASS;
502 }
503 
504 static int DetectIPRepTest04(void)
505 {
507  DetectEngineThreadCtx *det_ctx = NULL;
508  Signature *sig = NULL;
509  FILE *fd = NULL;
510  int r = 0;
511  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
513 
515  memset(&th_v, 0, sizeof(th_v));
517 
519  FAIL_IF_NULL(p);
520 
521  p->src.addr_data32[0] = UTHSetIPv4Address("10.0.0.1");
522  p->dst.addr_data32[0] = UTHSetIPv4Address("10.0.0.2");
523  de_ctx->flags |= DE_QUIET;
525 
526  fd = DetectIPRepGenerateCategoriesDummy();
527  r = SRepLoadCatFileFromFD(fd);
528  FAIL_IF(r < 0);
529 
530  fd = DetectIPRepGenerateNetworksDummy();
532  FAIL_IF(r < 0);
533 
534  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"IPREP High value "
535  "badhost\"; iprep:both,BadHosts,>,1; sid:1; rev:1;)");
536  FAIL_IF_NULL(sig);
537 
539  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
540 
541  p->alerts.cnt = 0;
542  p->action = 0;
543  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
544  FAIL_IF(p->alerts.cnt != 1);
545  FAIL_IF(PacketTestAction(p, ACTION_DROP));
546 
547  UTHFreePacket(p);
548 
549  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
551 
552  HostShutdown();
554  PASS;
555 }
556 
557 static int DetectIPRepTest05(void)
558 {
560  DetectEngineThreadCtx *det_ctx = NULL;
561  Signature *sig = NULL;
562  FILE *fd = NULL;
563  int r = 0;
564  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
566 
568  memset(&th_v, 0, sizeof(th_v));
570 
572  FAIL_IF_NULL(p);
573 
574  p->src.addr_data32[0] = UTHSetIPv4Address("1.0.0.1");
575  de_ctx->flags |= DE_QUIET;
577 
578  fd = DetectIPRepGenerateCategoriesDummy();
579  r = SRepLoadCatFileFromFD(fd);
580  FAIL_IF(r < 0);
581 
582  fd = DetectIPRepGenerateNetworksDummy();
584  FAIL_IF(r < 0);
585 
586  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"IPREP High value "
587  "badhost\"; iprep:any,BadHosts,>,1; sid:1; rev:1;)");
588  FAIL_IF_NULL(sig);
589 
591  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
592 
593  p->alerts.cnt = 0;
594  p->action = 0;
595  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
596  FAIL_IF(p->alerts.cnt != 0);
597  FAIL_IF(PacketTestAction(p, ACTION_DROP));
598 
599  UTHFreePacket(p);
600 
601  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
603 
604  HostShutdown();
606  PASS;
607 }
608 
609 static int DetectIPRepTest06(void)
610 {
612  DetectEngineThreadCtx *det_ctx = NULL;
613  Signature *sig = NULL;
614  FILE *fd = NULL;
615  int r = 0;
616  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
618 
620  memset(&th_v, 0, sizeof(th_v));
622 
624  FAIL_IF_NULL(p);
625 
626  p->src.addr_data32[0] = UTHSetIPv4Address("1.0.0.1");
627  de_ctx->flags |= DE_QUIET;
629 
630  fd = DetectIPRepGenerateCategoriesDummy();
631  r = SRepLoadCatFileFromFD(fd);
632  FAIL_IF(r < 0);
633 
634  fd = DetectIPRepGenerateNetworksDummy2();
636  FAIL_IF(r < 0);
637 
638  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"IPREP High value "
639  "badhost\"; iprep:any,BadHosts,>,1; sid:1; rev:1;)");
640  FAIL_IF_NULL(sig);
641 
643  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
644 
645  p->alerts.cnt = 0;
646  p->action = 0;
647  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
648  FAIL_IF(p->alerts.cnt != 1);
649  FAIL_IF(PacketTestAction(p, ACTION_DROP));
650 
651  UTHFreePacket(p);
652 
653  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
655 
656  HostShutdown();
658  PASS;
659 }
660 
661 static int DetectIPRepTest07(void)
662 {
664  DetectEngineThreadCtx *det_ctx = NULL;
665  Signature *sig = NULL;
666  FILE *fd = NULL;
667  int r = 0;
668  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
670 
672  memset(&th_v, 0, sizeof(th_v));
674 
676  FAIL_IF_NULL(p);
677 
678  p->dst.addr_data32[0] = UTHSetIPv4Address("1.0.0.2");
679  de_ctx->flags |= DE_QUIET;
681 
682  fd = DetectIPRepGenerateCategoriesDummy();
683  r = SRepLoadCatFileFromFD(fd);
684  FAIL_IF(r < 0);
685 
686  fd = DetectIPRepGenerateNetworksDummy2();
688  FAIL_IF(r < 0);
689 
690  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"IPREP High value "
691  "badhost\"; iprep:any,BadHosts,>,1; sid:1; rev:1;)");
692  FAIL_IF_NULL(sig);
693 
695  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
696 
697  p->alerts.cnt = 0;
698  p->action = 0;
699  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
700  FAIL_IF(p->alerts.cnt != 1);
701  FAIL_IF(PacketTestAction(p, ACTION_DROP));
702 
703  UTHFreePacket(p);
704 
705  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
707 
708  HostShutdown();
710  PASS;
711 }
712 
713 static int DetectIPRepTest08(void)
714 {
716  DetectEngineThreadCtx *det_ctx = NULL;
717  Signature *sig = NULL;
718  FILE *fd = NULL;
719  int r = 0;
720  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
722 
724  memset(&th_v, 0, sizeof(th_v));
726 
728  FAIL_IF_NULL(p);
729 
730  p->src.addr_data32[0] = UTHSetIPv4Address("1.0.0.1");
731  p->dst.addr_data32[0] = UTHSetIPv4Address("1.0.0.2");
732  de_ctx->flags |= DE_QUIET;
734 
735  fd = DetectIPRepGenerateCategoriesDummy();
736  r = SRepLoadCatFileFromFD(fd);
737  FAIL_IF(r < 0);
738 
739  fd = DetectIPRepGenerateNetworksDummy();
741  FAIL_IF(r < 0);
742 
743  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"IPREP High value "
744  "badhost\"; iprep:any,BadHosts,>,1; sid:1; rev:1;)");
745  FAIL_IF_NULL(sig);
746 
748  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
749 
750  p->alerts.cnt = 0;
751  p->action = 0;
752  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
753  FAIL_IF(p->alerts.cnt != 0);
754  FAIL_IF(PacketTestAction(p, ACTION_DROP));
755 
756  UTHFreePacket(p);
757 
758  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
760 
761  HostShutdown();
763  PASS;
764 }
765 
766 static int DetectIPRepTest09(void)
767 {
769  DetectEngineThreadCtx *det_ctx = NULL;
770  Signature *sig = NULL;
771  FILE *fd = NULL;
772  int r = 0;
773  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
775 
777  memset(&th_v, 0, sizeof(th_v));
779 
781  FAIL_IF_NULL(p);
782 
783  p->src.addr_data32[0] = UTHSetIPv4Address("192.168.0.1");
784  p->dst.addr_data32[0] = UTHSetIPv4Address("192.168.0.2");
785  de_ctx->flags |= DE_QUIET;
787 
788  fd = DetectIPRepGenerateCategoriesDummy2();
789  r = SRepLoadCatFileFromFD(fd);
790  FAIL_IF(r < 0);
791 
792  fd = DetectIPRepGenerateNetworksDummy2();
794  FAIL_IF(r < 0);
795 
797  "alert tcp any any -> any any (msg:\"test\"; iprep:src,BadHosts,>,9; sid:1; rev:1;)");
798  FAIL_IF_NULL(sig);
799 
801  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
802 
803  p->alerts.cnt = 0;
804  p->action = 0;
805  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
806  FAIL_IF(p->alerts.cnt != 1);
807  FAIL_IF(PacketTestAction(p, ACTION_DROP));
808 
809  UTHFreePacket(p);
810 
811  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
813 
814  HostShutdown();
816  PASS;
817 }
818 
819 static FILE *DetectIPRepGenerateNetworksDummy3(void)
820 {
821  FILE *fd = NULL;
822  const char *buffer = "192.168.0.0/16,1,127"; // BadHosts
823 
824  fd = SCFmemopen((void *)buffer, strlen(buffer), "r");
825  if (fd == NULL)
826  SCLogDebug("Error with SCFmemopen()");
827 
828  return fd;
829 }
830 
831 static int DetectIPRepTest10(void)
832 {
834  DetectEngineThreadCtx *det_ctx = NULL;
835  Signature *sig = NULL;
836  FILE *fd = NULL;
837  int r = 0;
838  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
840 
842  memset(&th_v, 0, sizeof(th_v));
844 
846  FAIL_IF_NULL(p);
847 
848  p->src.addr_data32[0] = UTHSetIPv4Address("192.168.0.1");
849  p->dst.addr_data32[0] = UTHSetIPv4Address("192.168.0.2");
850  de_ctx->flags |= DE_QUIET;
852 
853  fd = DetectIPRepGenerateCategoriesDummy2();
854  r = SRepLoadCatFileFromFD(fd);
855  FAIL_IF(r < 0);
856 
857  fd = DetectIPRepGenerateNetworksDummy3();
859  FAIL_IF(r < 0);
860 
862  "alert tcp any any -> any any (msg:\"test\"; iprep:src,BadHosts,isset; sid:1; rev:1;)");
863  FAIL_IF_NULL(sig);
864 
866  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
867 
868  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
869  FAIL_IF_NOT(p->alerts.cnt == 1);
870 
871  UTHFreePacket(p);
872 
873  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
875 
876  HostShutdown();
878  PASS;
879 }
880 
881 static int DetectIPRepTest11(void)
882 {
884  DetectEngineThreadCtx *det_ctx = NULL;
885  Signature *sig = NULL;
886  FILE *fd = NULL;
887  int r = 0;
888  Packet *p = UTHBuildPacket((uint8_t *)"lalala", 6, IPPROTO_TCP);
890 
892  memset(&th_v, 0, sizeof(th_v));
894 
896  FAIL_IF_NULL(p);
897 
898  p->src.addr_data32[0] = UTHSetIPv4Address("10.0.0.1");
899  p->dst.addr_data32[0] = UTHSetIPv4Address("10.0.0.2");
900  de_ctx->flags |= DE_QUIET;
902 
903  fd = DetectIPRepGenerateCategoriesDummy2();
904  r = SRepLoadCatFileFromFD(fd);
905  FAIL_IF(r < 0);
906 
907  fd = DetectIPRepGenerateNetworksDummy3();
909  FAIL_IF(r < 0);
910 
911  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (msg:\"test\"; "
912  "iprep:src,BadHosts,isnotset; sid:1; rev:1;)");
913  FAIL_IF_NULL(sig);
914 
916  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
917 
918  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
919  FAIL_IF_NOT(p->alerts.cnt == 1);
920 
921  UTHFreePacket(p);
922 
923  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
925 
926  HostShutdown();
928  PASS;
929 }
930 
931 /**
932  * \brief this function registers unit tests for IPRep
933  */
934 void IPRepRegisterTests(void)
935 {
936  UtRegisterTest("DetectIPRepTest01", DetectIPRepTest01);
937  UtRegisterTest("DetectIPRepTest02", DetectIPRepTest02);
938  UtRegisterTest("DetectIPRepTest03", DetectIPRepTest03);
939  UtRegisterTest("DetectIPRepTest04", DetectIPRepTest04);
940  UtRegisterTest("DetectIPRepTest05", DetectIPRepTest05);
941  UtRegisterTest("DetectIPRepTest06", DetectIPRepTest06);
942  UtRegisterTest("DetectIPRepTest07", DetectIPRepTest07);
943  UtRegisterTest("DetectIPRepTest08", DetectIPRepTest08);
944  UtRegisterTest("DetectIPRepTest09", DetectIPRepTest09);
945  UtRegisterTest("DetectIPRepTest10 -- isset", DetectIPRepTest10);
946  UtRegisterTest("DetectIPRepTest11 -- isnotset", DetectIPRepTest11);
947 }
948 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
host.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
HostUnlock
void HostUnlock(Host *h)
Definition: host.c:472
SReputation_
Definition: reputation.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
PKT_HOST_DST_LOOKED_UP
#define PKT_HOST_DST_LOOKED_UP
Definition: decode.h:1332
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
util-fmemopen.h
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
SReputation_::rep
uint8_t rep[SREP_MAX_CATS]
Definition: reputation.h:47
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1542
Packet_::host_src
struct Host_ * host_src
Definition: decode.h:639
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:289
action-globals.h
Packet_::flags
uint32_t flags
Definition: decode.h:562
Packet_::action
uint8_t action
Definition: decode.h:624
threads.h
HostRelease
void HostRelease(Host *h)
Definition: host.c:461
UTHSetIPv4Address
uint32_t UTHSetIPv4Address(const char *str)
return the uint32_t for a ipv4 address string
Definition: util-unittest-helper.c:273
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2912
flow-bit.h
DetectEngineCtx_::srep_version
uint32_t srep_version
Definition: detect.h:1009
DE_QUIET
#define DE_QUIET
Definition: detect.h:334
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:241
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:3305
p
Packet * p
Definition: fuzz_dataset.c:30
SCFmemopen
#define SCFmemopen
Definition: util-fmemopen.h:52
Packet_::host_dst
struct Host_ * host_dst
Definition: decode.h:640
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3965
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:637
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
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:22
detect-iprep.h
DetectEngineThreadCtx_
Definition: detect.h:1316
DetectEngineCtx_::srepCIDR_ctx
SRepCIDRTree * srepCIDR_ctx
Definition: detect.h:1012
SRepLoadCatFileFromFD
int SRepLoadCatFileFromFD(FILE *fp)
Definition: reputation.c:367
PKT_HOST_SRC_LOOKED_UP
#define PKT_HOST_SRC_LOOKED_UP
Definition: decode.h:1331
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:424
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3660
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:118
app-layer-parser.h
HostLock
void HostLock(Host *h)
Definition: host.c:467
Packet_
Definition: decode.h:516
SIGMATCH_IPONLY_COMPAT
#define SIGMATCH_IPONLY_COMPAT
Definition: detect-engine-register.h:310
detect-engine-build.h
HostReference
#define HostReference(dst_h_ptr, h)
Definition: host.h:117
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:1504
reputation.h
SRepLoadFileFromFD
int SRepLoadFileFromFD(SRepCIDRTree *cidr_ctx, FILE *fp)
Definition: reputation.c:423
DetectIPRepFree
void DetectIPRepFree(DetectEngineCtx *, void *)
Definition: detect-iprep.c:283
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2300
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1332
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:355
th_v
ThreadVars * th_v
Definition: fuzz_dataset.c:29
Host_::iprep
void * iprep
Definition: host.h:69
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
SRepCIDRGetIPRepSrc
int8_t SRepCIDRGetIPRepSrc(SRepCIDRTree *cidr_ctx, Packet *p, uint8_t cat, uint32_t version)
Definition: reputation.c:134
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
packet.h
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3905
util-spm.h
HOST_QUIET
#define HOST_QUIET
Definition: host.h:93
version
uint8_t version
Definition: decode-gre.h:1
HostShutdown
void HostShutdown(void)
shutdown the flow engine
Definition: host.c:296
SReputation_::version
uint32_t version
Definition: reputation.h:46
util-validate.h
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:470
SRepCIDRGetIPRepDst
int8_t SRepCIDRGetIPRepDst(SRepCIDRTree *cidr_ctx, Packet *p, uint8_t cat, uint32_t version)
Definition: reputation.c:146
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
SRepResetVersion
void SRepResetVersion(void)
Definition: reputation.c:63
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2873
HostLookupHostFromHash
Host * HostLookupHostFromHash(Address *a)
look up a host in the hash
Definition: host.c:585
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect-engine-register.h:336
DetectEngineThreadCtx_::de_ctx
DetectEngineCtx * de_ctx
Definition: detect.h:1444
Packet_::dst
Address dst
Definition: decode.h:521
HostInitConfig
void HostInitConfig(bool quiet)
initialize the configuration
Definition: host.c:168
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:997
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
flow.h
DetectIPRepRegister
void DetectIPRepRegister(void)
Definition: detect-iprep.c:62
Host_
Definition: host.h:58
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1428
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
Packet_::src
Address src
Definition: decode.h:520
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
DETECT_IPREP
@ DETECT_IPREP
Definition: detect-engine-register.h:237