-
Notifications
You must be signed in to change notification settings - Fork 360
/
report-strobes.go
80 lines (68 loc) · 1.93 KB
/
report-strobes.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
package reporting
import (
"bytes"
"fmt"
"html/template"
"os"
"github.com/activecm/rita/pkg/beacon"
"github.com/activecm/rita/reporting/templates"
"github.com/activecm/rita/resources"
"github.com/globalsign/mgo/bson"
)
func printStrobes(db string, res *resources.Resources) error {
f, err := os.Create("strobes.html")
if err != nil {
return err
}
defer f.Close()
out, err := template.New("strobes.html").Parse(templates.StrobesTempl)
if err != nil {
return err
}
data := getStrobeResultsView(res, "conn_count", 1000)
w, err := getStrobesWriter(data)
if err != nil {
return err
}
return out.Execute(f, &templates.ReportingInfo{DB: db, Writer: template.HTML(w)})
}
func getStrobesWriter(strobes []beacon.StrobeAnalysisView) (string, error) {
tmpl := "<tr><td>{{.Src}}</td><td>{{.Dst}}</td><td>{{.ConnectionCount}}</td></tr>\n"
out, err := template.New("Strobes").Parse(tmpl)
if err != nil {
return "", err
}
w := new(bytes.Buffer)
for _, strobe := range strobes {
err := out.Execute(w, strobe)
if err != nil {
return "", err
}
}
return w.String(), nil
}
//getStrobeResultsView ...
func getStrobeResultsView(res *resources.Resources, sort string, limit int) []beacon.StrobeAnalysisView {
ssn := res.DB.Session.Copy()
defer ssn.Close()
var strobes []beacon.StrobeAnalysisView
strobeQuery := []bson.M{
bson.M{"$match": bson.M{"strobe": true}},
bson.M{"$unwind": "$dat"},
bson.M{"$project": bson.M{"src": 1, "dst": 1, "conns": "$dat.count"}},
bson.M{"$group": bson.M{
"_id": "$_id",
"src": bson.M{"$first": "$src"},
"dst": bson.M{"$first": "$dst"},
"conn_count": bson.M{"$sum": "$conns"},
}},
bson.M{"$sort": bson.M{sort: -1}},
bson.M{"$limit": limit},
}
err := ssn.DB(res.DB.GetSelectedDB()).C(res.Config.T.Structure.UniqueConnTable).Pipe(strobeQuery).AllowDiskUse().All(&strobes)
if err != nil {
//TODO: properly log this error
fmt.Println(err)
}
return strobes
}