1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
|
package processing
// DCSO FEVER
// Copyright (c) 2017, 2020, DCSO GmbH
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"regexp"
"sync"
"testing"
"time"
"github.com/DCSO/fever/types"
"github.com/DCSO/fever/util"
"github.com/DCSO/bloom"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
)
var (
reHTTPURL = regexp.MustCompile(`Possibly bad HTTP URL: [^ ]+ . ([^ ]+) . ([^" ]+)`)
reHTTPHost = regexp.MustCompile(`Possibly bad HTTP host: ([^" ]+)`)
reDNSReq = regexp.MustCompile("Possibly bad DNS lookup to ([^\" ]+)")
reDNSRep = regexp.MustCompile("Possibly bad DNS response for ([^\" ]+)")
reSNI = regexp.MustCompile("Possibly bad TLS SNI: ([^\" ]+)")
reFingerprint = regexp.MustCompile("Possibly bad TLS Fingerprint: ([^\" ]+)")
)
func makeBloomDNSEvent(rrname string) types.Entry {
e := types.Entry{
SrcIP: fmt.Sprintf("10.0.0.%d", rand.Intn(5)+1),
SrcPort: 53,
DestIP: fmt.Sprintf("10.0.0.%d", rand.Intn(50)),
DestPort: []int64{11, 12, 13, 14, 15}[rand.Intn(5)],
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "dns",
Proto: "TCP",
DNSRCode: []string{"NOERROR", "NXDOMAIN"}[rand.Intn(2)],
DNSRData: fmt.Sprintf("10.%d.0.%d", rand.Intn(50), rand.Intn(50)+100),
DNSRRName: rrname,
DNSRRType: "A",
DNSType: []string{"answer", "query"}[rand.Intn(2)],
}
eve := types.EveEvent{
Timestamp: &types.SuriTime{
Time: time.Now().UTC(),
},
EventType: e.EventType,
SrcIP: e.SrcIP,
SrcPort: int(e.SrcPort),
DestIP: e.DestIP,
DestPort: int(e.DestPort),
Proto: e.Proto,
DNS: &types.DNSEvent{
Rcode: e.DNSRCode,
Rrname: e.DNSRRName,
Rdata: e.DNSRData,
Rrtype: e.DNSRRType,
Type: e.DNSType,
},
}
json, err := json.Marshal(eve)
if err != nil {
log.Warn(err)
} else {
e.JSONLine = string(json)
}
return e
}
func makeBloomHTTPEvent(host string, url string) types.Entry {
e := types.Entry{
SrcIP: fmt.Sprintf("10.0.0.%d", rand.Intn(5)+1),
SrcPort: int64(rand.Intn(60000) + 1025),
DestIP: fmt.Sprintf("10.0.0.%d", rand.Intn(50)),
DestPort: 80,
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "http",
Proto: "TCP",
HTTPHost: host,
HTTPUrl: url,
HTTPMethod: "GET",
}
eve := types.EveEvent{
Timestamp: &types.SuriTime{
Time: time.Now().UTC(),
},
EventType: e.EventType,
SrcIP: e.SrcIP,
SrcPort: int(e.SrcPort),
DestIP: e.DestIP,
DestPort: int(e.DestPort),
Proto: e.Proto,
HTTP: &types.HTTPEvent{
Hostname: e.HTTPHost,
URL: e.HTTPUrl,
},
}
json, err := json.Marshal(eve)
if err != nil {
log.Warn(err)
} else {
e.JSONLine = string(json)
}
return e
}
func makeBloomTLSEvent(host string, fingerprint string) types.Entry {
e := types.Entry{
SrcIP: fmt.Sprintf("10.0.0.%d", rand.Intn(5)+1),
SrcPort: int64(rand.Intn(60000) + 1025),
DestIP: fmt.Sprintf("10.0.0.%d", rand.Intn(50)),
DestPort: 443,
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "tls",
Proto: "TCP",
TLSSNI: host,
TLSFingerprint: fingerprint,
}
eve := types.EveEvent{
Timestamp: &types.SuriTime{
Time: time.Now().UTC(),
},
EventType: e.EventType,
SrcIP: e.SrcIP,
SrcPort: int(e.SrcPort),
DestIP: e.DestIP,
DestPort: int(e.DestPort),
Proto: e.Proto,
TLS: &types.TLSEvent{
Sni: e.TLSSNI,
Fingerprint: e.TLSFingerprint,
},
}
json, err := json.Marshal(eve)
if err != nil {
log.Warn(err)
} else {
e.JSONLine = string(json)
}
return e
}
var testURLs []string
var testHosts []string
var testTLSHosts []string
var testTLSFingerprints []string
const numOfTestBloomItems = 1000
// fill Bloom filter with disjunct set of values
func fillBloom(b *bloom.BloomFilter) {
testURLs = make([]string, 0)
testHosts = make([]string, 0)
testTLSHosts = make([]string, 0)
i := 0
for i < numOfTestBloomItems {
val := fmt.Sprintf("%s.com", util.RndStringFromAlpha(6))
for b.Check([]byte(val)) {
val = fmt.Sprintf("%s.com", util.RndStringFromAlpha(6))
}
i++
testHosts = append(testHosts, val)
b.Add([]byte(val))
}
i = 0
for i < numOfTestBloomItems {
val := fmt.Sprintf("%s.com", util.RndStringFromAlpha(6))
for b.Check([]byte(val)) {
val = fmt.Sprintf("%s.com", util.RndStringFromAlpha(6))
}
i++
testTLSHosts = append(testTLSHosts, val)
b.Add([]byte(val))
}
i = 0
for i < numOfTestBloomItems {
val := fmt.Sprintf("http://foo.com/%s.html", util.RndStringFromAlpha(6))
for b.Check([]byte(val)) {
val = fmt.Sprintf("http://foo.com/%s.html", util.RndStringFromAlpha(6))
}
i++
testURLs = append(testURLs, val)
b.Add([]byte(val))
}
i = 0
for i < numOfTestBloomItems {
i++
fingp := util.RndTLSFingerprint()
testTLSFingerprints = append(testTLSFingerprints, fingp)
b.Add([]byte(fingp))
}
}
// CollectorHandler simply gathers consumed events in a list
type CollectorHandler struct {
EntriesLock sync.Mutex
Entries map[string]bool
}
func (h *CollectorHandler) GetName() string {
return "Collector handler"
}
func (h *CollectorHandler) GetEventTypes() []string {
return []string{"alert"}
}
func (h *CollectorHandler) Consume(e *types.Entry) error {
h.EntriesLock.Lock()
defer h.EntriesLock.Unlock()
match := reHTTPURL.FindStringSubmatch(e.JSONLine)
if match != nil {
url := match[2]
h.Entries[url] = true
return nil
}
match = reHTTPHost.FindStringSubmatch(e.JSONLine)
if match != nil {
host := match[1]
h.Entries[host] = true
return nil
}
match = reDNSReq.FindStringSubmatch(e.JSONLine)
if match != nil {
var eve types.EveEvent
var err = json.Unmarshal([]byte(e.JSONLine), &eve)
if err != nil {
log.Fatal(err)
}
if eve.DNS.Type != "query" {
log.Fatalf("request alert for type (%s) != query", eve.DNS.Type)
}
h.Entries[match[1]] = true
return nil
}
match = reDNSRep.FindStringSubmatch(e.JSONLine)
if match != nil {
var eve types.EveEvent
var err = json.Unmarshal([]byte(e.JSONLine), &eve)
if err != nil {
log.Fatal(err)
}
if eve.DNS.Type != "answer" {
log.Fatalf("request alert for type (%s) != answer", eve.DNS.Type)
}
h.Entries[match[1]] = true
return nil
}
match = reSNI.FindStringSubmatch(e.JSONLine)
if match != nil {
h.Entries[match[1]] = true
return nil
}
match = reFingerprint.FindStringSubmatch(e.JSONLine)
if match != nil {
h.Entries[match[1]] = true
return nil
}
return nil
}
func (h *CollectorHandler) Reset() {
h.EntriesLock.Lock()
defer h.EntriesLock.Unlock()
h.Entries = make(map[string]bool)
}
func (h *CollectorHandler) GetEntries() map[string]bool {
h.EntriesLock.Lock()
defer h.EntriesLock.Unlock()
return h.Entries
}
func TestBloomHandler(t *testing.T) {
// initalize Bloom filter and fill with 'interesting' values
bf := bloom.Initialize(100000, 0.0000001)
fillBloom(&bf)
// channel to receive events to be saved to database
dbChan := make(chan types.Entry)
// handler to receive forwarded events
fwhandler := &CollectorHandler{
Entries: make(map[string]bool),
}
// concurrently gather entries to be written to DB
dbWritten := make([]types.Entry, 0)
consumeWaitChan := make(chan bool)
go func() {
for e := range dbChan {
dbWritten = append(dbWritten, e)
}
close(consumeWaitChan)
}()
bh := MakeBloomHandler(&bf, dbChan, fwhandler, "FOO BAR")
err := bh.Reload()
if err == nil {
t.Fatal("reloading without file should fail")
}
bhTypes := bh.GetEventTypes()
if len(bhTypes) != 3 {
t.Fatal("Bloom handler should claim three types")
}
if bhTypes[0] != "http" {
t.Fatal("Bloom handler should claim 'http' type")
}
if bhTypes[1] != "dns" {
t.Fatal("Bloom handler should claim 'dns' type")
}
if bhTypes[2] != "tls" {
t.Fatal("Bloom handler should claim 'tls' type")
}
if bh.GetName() != "Bloom filter handler" {
t.Fatal("Bloom handler has wrong name")
}
i := 0
j := 0
k := 0
l := 0
for {
var e types.Entry
// emit Bloom filter TP event with 20% individual probability, at most
// <numOfTestBloomItems> each
if 2 < rand.Intn(10) {
if i == numOfTestBloomItems && j == numOfTestBloomItems && k == numOfTestBloomItems {
break
}
// uniformly distribute hits over HTTP URL/Host and DNS lookups
switch rnd := rand.Intn(4); rnd {
case 0:
if i < numOfTestBloomItems {
e = makeBloomDNSEvent(testHosts[i])
bh.Consume(&e)
i++
}
case 1:
if j < numOfTestBloomItems {
e = makeBloomHTTPEvent("foo.com", testURLs[j])
bh.Consume(&e)
j++
}
case 2:
if k < numOfTestBloomItems {
e = makeBloomTLSEvent(testTLSHosts[k], ":::")
bh.Consume(&e)
k++
}
case 3:
if l < numOfTestBloomItems {
e = makeBloomTLSEvent("foo.com", testTLSFingerprints[l])
bh.Consume(&e)
l++
}
}
} else {
// uniformly distribute non-matching hits over HTTP URL/Host and DNS lookups
switch rnd := rand.Intn(4); rnd {
case 0:
s := fmt.Sprintf("%s.com", util.RndStringFromAlpha(6))
for bf.Check([]byte(s)) {
s = fmt.Sprintf("%s.%s", util.RndStringFromAlpha(6),
util.RndStringFromAlpha(2))
}
e = makeBloomDNSEvent(s)
bh.Consume(&e)
case 1:
s := fmt.Sprintf("/%s.html", util.RndStringFromAlpha(6))
for bf.Check([]byte(s)) {
s = fmt.Sprintf("/%s.%s.html", util.RndStringFromAlpha(6),
util.RndStringFromAlpha(6))
}
e = makeBloomHTTPEvent("foo.com", s)
bh.Consume(&e)
case 2:
s := fmt.Sprintf("%s.com", util.RndStringFromAlpha(6))
for bf.Check([]byte(s)) {
s = fmt.Sprintf("%s.%s", util.RndStringFromAlpha(6),
util.RndStringFromAlpha(2))
}
e = makeBloomTLSEvent(s, ":::")
bh.Consume(&e)
case 3:
f := util.RndStringFromAlpha(6)
for bf.Check([]byte(f)) {
f = util.RndStringFromAlpha(6)
}
e = makeBloomTLSEvent("foo.com", f)
bh.Consume(&e)
}
}
}
// wait until all values have been collected
close(dbChan)
<-consumeWaitChan
// check that we haven't missed anything
if len(fwhandler.Entries) < 4*numOfTestBloomItems {
t.Fatalf("expected %d forwarded BLF alerts, seen less (%d)", 4*numOfTestBloomItems,
len(fwhandler.Entries))
}
// we want _at least_ to have the test values forwarded as alerts
// (as FP are possible)
for _, v := range testHosts {
if _, ok := fwhandler.Entries[v]; !ok {
t.Fatalf("testhost %s not forwarded", v)
}
}
for _, v := range testURLs {
if _, ok := fwhandler.Entries[v]; !ok {
t.Fatalf("testurl %s not forwarded", v)
}
}
}
func TestBloomHandlerFromFile(t *testing.T) {
b1 := bloom.Initialize(1000, 0.0001)
b2 := bloom.Initialize(1000, 0.0001)
b1.Add([]byte("foobar"))
b2.Add([]byte("baz"))
b1File, err := ioutil.TempFile("", "example")
if err != nil {
t.Fatal(err)
}
defer os.Remove(b1File.Name())
b1.Write(b1File)
b1File.Close()
// handler to receive forwarded events
fwhandler := &CollectorHandler{
Entries: make(map[string]bool),
}
dbChan := make(chan types.Entry, 10)
defer close(dbChan)
bh, err := MakeBloomHandlerFromFile(b1File.Name(), false, dbChan, fwhandler, "FOO BAR", []string{"/"})
if err != nil {
t.Fatal(err)
}
e := makeBloomDNSEvent("foobar")
bh.Consume(&e)
if len(fwhandler.Entries) != 1 {
t.Fatalf("Unexpected number of entries: %d != 1 ", len(fwhandler.Entries))
}
if !fwhandler.Entries["foobar"] {
t.Fatalf("expected entry is missing")
}
e = makeBloomDNSEvent("baz")
bh.Consume(&e)
if len(fwhandler.Entries) != 1 {
t.Fatalf("Unexpected number of entries: %d != 1 ", len(fwhandler.Entries))
}
if !fwhandler.Entries["foobar"] {
t.Fatalf("expected entry is missing")
}
b2File, err := os.OpenFile(b1File.Name(), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
t.Fatal(err)
}
b2.Write(b2File)
b2File.Close()
bh.Reload()
fwhandler.Entries = make(map[string]bool)
e = makeBloomDNSEvent("baz")
bh.Consume(&e)
if len(fwhandler.Entries) != 1 {
t.Fatalf("Unexpected number of entries: %d != 1 ", len(fwhandler.Entries))
}
if !fwhandler.Entries["baz"] {
t.Fatalf("expected entry is missing")
}
if fwhandler.Entries["foobar"] {
t.Fatalf("unexpected entry")
}
e = makeBloomDNSEvent("foobar")
bh.Consume(&e)
if len(fwhandler.Entries) != 1 {
t.Fatalf("Unexpected number of entries: %d != 1 ", len(fwhandler.Entries))
}
if !fwhandler.Entries["baz"] {
t.Fatalf("expected entry is missing")
}
if fwhandler.Entries["foobar"] {
t.Fatalf("unexpected entry")
}
}
func TestBloomHandlerEmptyInput(t *testing.T) {
blFile, err := ioutil.TempFile("", "empty")
if err != nil {
t.Fatal(err)
}
defer os.Remove(blFile.Name())
blFile.Close()
dbChan := make(chan types.Entry, 10)
defer close(dbChan)
bf, err := MakeBloomHandlerFromFile(blFile.Name(), false, dbChan, nil, "FOO BAR", []string{"/"})
if err != nil {
t.Fatal(err)
}
if bf == nil {
t.Fatal("bloom filter should not be nil for empty file")
}
}
func TestBloomHandlerBlacklistedInputFromFile(t *testing.T) {
b1 := bloom.Initialize(1000, 0.0001)
b1.Add([]byte("/"))
b1File, err := ioutil.TempFile("", "blist")
if err != nil {
t.Fatal(err)
}
defer os.Remove(b1File.Name())
b1.Write(b1File)
b1File.Close()
b2 := bloom.Initialize(1000, 0.0001)
b2.Add([]byte("/foobarbaz"))
dbChan := make(chan types.Entry, 10)
defer close(dbChan)
hook := test.NewGlobal()
_, err = MakeBloomHandlerFromFile(b1File.Name(), false, nil, nil, "FOO BAR", []string{"/"})
if err != nil {
t.Fatal(err)
}
entries := hook.AllEntries()
if len(entries) != 4 {
t.Fatal("missing log entries")
}
if entries[2].Message != "filter contains blacklisted indicator '/'" {
t.Fatal("wrong log entry for invalid IP range")
}
b2File, err := os.OpenFile(b1File.Name(), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
t.Fatal(err)
}
b2.Write(b2File)
b2File.Close()
bf, err := MakeBloomHandlerFromFile(b1File.Name(), false, nil, nil, "FOO BAR", []string{"/"})
if err != nil {
t.Fatal(err)
}
b2File, err = os.OpenFile(b1File.Name(), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
t.Fatal(err)
}
b1.Write(b2File)
b2File.Close()
hook.Reset()
err = bf.Reload()
if err != nil {
t.Fatal(err)
}
entries = hook.AllEntries()
if len(entries) != 2 {
t.Fatal("missing log entries")
}
if entries[0].Message != "filter contains blacklisted indicator '/'" {
t.Fatal("wrong log entry for invalid IP range")
}
}
func TestBloomHandlerURL(t *testing.T) {
e1 := types.Entry{
SrcIP: "10.0.0.1",
SrcPort: 23545,
DestIP: "10.0.0.2",
DestPort: 80,
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "http",
Proto: "TCP",
HTTPHost: "foo.bar.de",
HTTPUrl: "http://foo.bar.de/oddlyspecific",
HTTPMethod: "GET",
}
eve1 := types.EveEvent{
EventType: e1.EventType,
SrcIP: e1.SrcIP,
SrcPort: int(e1.SrcPort),
DestIP: e1.DestIP,
DestPort: int(e1.DestPort),
Proto: e1.Proto,
HTTP: &types.HTTPEvent{
Hostname: e1.HTTPHost,
URL: e1.HTTPUrl,
},
}
json1, err := json.Marshal(eve1)
if err != nil {
log.Warn(err)
} else {
e1.JSONLine = string(json1)
}
e2 := types.Entry{
SrcIP: "10.0.0.1",
SrcPort: 23545,
DestIP: "10.0.0.2",
DestPort: 80,
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "http",
Proto: "TCP",
HTTPHost: "foo.bar.de",
HTTPUrl: "/oddlyspecific",
HTTPMethod: "GET",
}
eve2 := types.EveEvent{
EventType: e2.EventType,
SrcIP: e2.SrcIP,
SrcPort: int(e2.SrcPort),
DestIP: e2.DestIP,
DestPort: int(e2.DestPort),
Proto: e2.Proto,
HTTP: &types.HTTPEvent{
Hostname: e2.HTTPHost,
URL: e2.HTTPUrl,
},
}
json2, err := json.Marshal(eve2)
if err != nil {
log.Warn(err)
} else {
e2.JSONLine = string(json2)
}
e3 := types.Entry{
SrcIP: "10.0.0.1",
SrcPort: 23545,
DestIP: "10.0.0.2",
DestPort: 80,
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "http",
Proto: "TCP",
HTTPHost: "foo.bar.com",
HTTPUrl: "/oddlyspecific",
HTTPMethod: "GET",
}
eve3 := types.EveEvent{
EventType: e3.EventType,
SrcIP: e3.SrcIP,
SrcPort: int(e3.SrcPort),
DestIP: e3.DestIP,
DestPort: int(e3.DestPort),
Proto: e3.Proto,
HTTP: &types.HTTPEvent{
Hostname: e3.HTTPHost,
URL: e3.HTTPUrl,
},
}
json3, err := json.Marshal(eve3)
if err != nil {
log.Warn(err)
} else {
e3.JSONLine = string(json3)
}
dbChan := make(chan types.Entry)
dbWritten := make([]types.Entry, 0)
consumeWaitChan := make(chan bool)
go func() {
for e := range dbChan {
dbWritten = append(dbWritten, e)
}
close(consumeWaitChan)
}()
// initalize Bloom filter and fill with 'interesting' values
bf := bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("/oddlyspecific"))
// handler to receive forwarded events
fwhandler := &CollectorHandler{
Entries: make(map[string]bool),
}
bh := MakeBloomHandler(&bf, dbChan, fwhandler, "FOO BAR")
bh.Consume(&e1)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e1)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("http://foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e1)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("https://foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e1)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("https://foo.bar.com/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e1)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("/"))
fwhandler.Reset()
bh.Consume(&e1)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e2)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e2)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("http://foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e2)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("https://foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e2)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("https://foo.bar.com/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e2)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("/"))
fwhandler.Reset()
bh.Consume(&e2)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e3)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e3)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("http://foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e3)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("https://foo.bar.de/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e3)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("https://foo.bar.com/oddlyspecific"))
fwhandler.Reset()
bh.Consume(&e3)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
bf = bloom.Initialize(100000, 0.0000001)
bf.Add([]byte("/"))
fwhandler.Reset()
bh.Consume(&e3)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("too many alerts: %d", len(fwhandler.GetEntries()))
}
}
func TestBloomHandlerBlacklistedSkip(t *testing.T) {
e1 := types.Entry{
SrcIP: "10.0.0.1",
SrcPort: 23545,
DestIP: "10.0.0.2",
DestPort: 80,
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "http",
Proto: "TCP",
HTTPHost: "foo.bar.de",
HTTPUrl: "http://foo.bar.de/oddlyspecific",
HTTPMethod: "GET",
}
eve1 := types.EveEvent{
EventType: e1.EventType,
SrcIP: e1.SrcIP,
SrcPort: int(e1.SrcPort),
DestIP: e1.DestIP,
DestPort: int(e1.DestPort),
Proto: e1.Proto,
HTTP: &types.HTTPEvent{
Hostname: e1.HTTPHost,
URL: e1.HTTPUrl,
},
}
json1, err := json.Marshal(eve1)
if err != nil {
log.Warn(err)
} else {
e1.JSONLine = string(json1)
}
e2 := types.Entry{
SrcIP: "10.0.0.1",
SrcPort: 23545,
DestIP: "10.0.0.2",
DestPort: 80,
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "http",
Proto: "TCP",
HTTPHost: "foo.bar.de",
HTTPUrl: "/",
HTTPMethod: "GET",
}
eve2 := types.EveEvent{
EventType: e2.EventType,
SrcIP: e2.SrcIP,
SrcPort: int(e2.SrcPort),
DestIP: e2.DestIP,
DestPort: int(e2.DestPort),
Proto: e2.Proto,
HTTP: &types.HTTPEvent{
Hostname: e2.HTTPHost,
URL: e2.HTTPUrl,
},
}
json2, err := json.Marshal(eve2)
if err != nil {
log.Warn(err)
} else {
e2.JSONLine = string(json2)
}
b1 := bloom.Initialize(1000, 0.0001)
b1.Add([]byte("/oddlyspecific"))
b1.Add([]byte("/"))
b1File, err := ioutil.TempFile("", "blist")
if err != nil {
t.Fatal(err)
}
defer os.Remove(b1File.Name())
b1.Write(b1File)
b1File.Close()
dbChan := make(chan types.Entry, 5)
dbWritten := make([]types.Entry, 0)
consumeWaitChan := make(chan bool)
go func() {
for e := range dbChan {
dbWritten = append(dbWritten, e)
}
close(consumeWaitChan)
}()
// handler to receive forwarded events
fwhandler := &CollectorHandler{
Entries: make(map[string]bool),
}
bh, err := MakeBloomHandlerFromFile(b1File.Name(), false, dbChan, fwhandler, "FOO BAR", []string{"/"})
if err != nil {
t.Fatal(err)
}
bh.Consume(&e1)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
fwhandler.Reset()
bh.Consume(&e2)
if len(fwhandler.GetEntries()) != 0 {
t.Fatalf("should not create alert but got %d", len(fwhandler.GetEntries()))
}
bh.Consume(&e1)
if len(fwhandler.GetEntries()) != 1 {
t.Fatalf("not enough alerts: %d", len(fwhandler.GetEntries()))
}
}
func TestBloomHandlerInvalidDNS(t *testing.T) {
// initalize Bloom filter and fill with 'interesting' values
bf := bloom.Initialize(100000, 0.0000001)
// channel to receive events to be saved to database
dbChan := make(chan types.Entry)
// handler to receive forwarded events
fwhandler := &CollectorHandler{
Entries: make(map[string]bool),
}
// concurrently gather entries to be written to DB
dbWritten := make([]types.Entry, 0)
consumeWaitChan := make(chan bool)
go func() {
for e := range dbChan {
dbWritten = append(dbWritten, e)
}
close(consumeWaitChan)
}()
bh := MakeBloomHandler(&bf, dbChan, fwhandler, "FOO BAR")
e := makeBloomDNSEvent("foobar")
e.DNSType = "foobar"
bf.Add([]byte(e.DNSRRName))
hook := test.NewGlobal()
bh.Consume(&e)
entries := hook.AllEntries()
if len(entries) < 1 {
t.Fatal("missing log entries")
}
if entries[0].Message != "invalid DNS type: 'foobar'" {
t.Fatal("wrong log entry for invalid DNS type")
}
}
|