-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.go
130 lines (110 loc) · 2.72 KB
/
mongodb.go
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
package beacon
import (
"runtime"
"time"
"github.com/activecm/rita/pkg/uconn"
"github.com/activecm/rita/resources"
"github.com/activecm/rita/util"
"github.com/globalsign/mgo"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
type repo struct {
res *resources.Resources
min int64
max int64
}
//NewMongoRepository create new repository
func NewMongoRepository(res *resources.Resources) Repository {
min, max, _ := res.MetaDB.GetTSRange(res.DB.GetSelectedDB())
return &repo{
res: res,
min: min,
max: max,
}
}
func (r *repo) CreateIndexes() error {
session := r.res.DB.Session.Copy()
defer session.Close()
// set collection name
collectionName := r.res.Config.T.Beacon.BeaconTable
// check if collection already exists
names, _ := session.DB(r.res.DB.GetSelectedDB()).CollectionNames()
// if collection exists, we don't need to do anything else
for _, name := range names {
if name == collectionName {
return nil
}
}
// set desired indexes
indexes := []mgo.Index{
{Key: []string{"-score"}},
{Key: []string{"src", "dst"}, Unique: true},
{Key: []string{"$hashed:src"}},
{Key: []string{"$hashed:dst"}},
{Key: []string{"-connection_count"}},
}
// create collection
err := r.res.DB.CreateCollection(collectionName, indexes)
if err != nil {
return err
}
return nil
}
//Upsert loops through every new uconn ....
func (r *repo) Upsert(uconnMap map[string]*uconn.Pair) {
//Create the workers
writerWorker := newWriter(
r.res.Config.T.Beacon.BeaconTable,
r.res.DB,
r.res.Config,
r.res.Log,
)
analyzerWorker := newAnalyzer(
r.min,
r.max,
r.res.Config.S.Bro.CurrentChunk,
r.res.DB,
r.res.Config,
writerWorker.collect,
writerWorker.close,
)
sorterWorker := newSorter(
r.res.DB,
r.res.Config,
analyzerWorker.collect,
analyzerWorker.close,
)
dissectorWorker := newDissector(
int64(r.res.Config.S.Strobe.ConnectionLimit),
r.res.DB,
r.res.Config,
sorterWorker.collect,
sorterWorker.close,
)
//kick off the threaded goroutines
for i := 0; i < util.Max(1, runtime.NumCPU()/2); i++ {
dissectorWorker.start()
sorterWorker.start()
analyzerWorker.start()
writerWorker.start()
}
// progress bar for troubleshooting
p := mpb.New(mpb.WithWidth(20))
bar := p.AddBar(int64(len(uconnMap)),
mpb.PrependDecorators(
decor.Name("\t[-] Beacon Analysis:", decor.WC{W: 30, C: decor.DidentRight}),
decor.CountersNoUnit(" %d / %d ", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage()),
)
// loop over map entries
for _, entry := range uconnMap {
start := time.Now()
dissectorWorker.collect(entry)
bar.IncrBy(1, time.Since(start))
}
p.Wait()
// start the closing cascade (this will also close the other channels)
dissectorWorker.close()
}