-
Notifications
You must be signed in to change notification settings - Fork 360
/
report-beacons.go
79 lines (60 loc) · 2.07 KB
/
report-beacons.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
package reporting
import (
"bytes"
"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"
"github.com/urfave/cli"
)
func printBeacons(db string, res *resources.Resources) error {
f, err := os.Create("beacons.html")
if err != nil {
return err
}
defer f.Close()
out, err := template.New("beacon.html").Parse(templates.BeaconsTempl)
if err != nil {
return err
}
data := getBeaconResultsView(res, 0)
if !(len(data) > 0) {
return cli.NewExitError("No results were found for "+db, -1)
}
w, err := getBeaconWriter(data)
if err != nil {
return err
}
return out.Execute(f, &templates.ReportingInfo{DB: db, Writer: template.HTML(w)})
}
func getBeaconWriter(beacons []beacon.AnalysisView) (string, error) {
tmpl := "<tr><td>{{printf \"%.3f\" .Score}}</td><td>{{.Src}}</td><td>{{.Dst}}</td><td>{{.Connections}}</td><td>{{printf \"%.3f\" .AvgBytes}}</td><td>"
tmpl += "{{.Ts.Range}}</td><td>{{.Ds.Range}}</td><td>{{.Ts.Mode}}</td><td>{{.Ds.Mode}}</td><td>{{.Ts.ModeCount}}</td><td>{{.Ds.ModeCount}}<td>"
tmpl += "{{printf \"%.3f\" .Ts.Skew}}</td><td>{{printf \"%.3f\" .Ds.Skew}}</td><td>{{.Ts.Dispersion}}</td><td>{{.Ds.Dispersion}}</td>"
tmpl += "</tr>\n"
out, err := template.New("beacon").Parse(tmpl)
if err != nil {
return "", err
}
w := new(bytes.Buffer)
for _, result := range beacons {
err = out.Execute(w, result)
if err != nil {
return "", err
}
}
return w.String(), nil
}
//getBeaconResultsView finds beacons greater than a given cutoffScore
//and links the data from the unique connections table back in to the results
func getBeaconResultsView(res *resources.Resources, cutoffScore float64) []beacon.AnalysisView {
ssn := res.DB.Session.Copy()
defer ssn.Close()
var beacons []beacon.AnalysisView
beaconQuery := bson.M{"score": bson.M{"$gt": cutoffScore}}
//TODO: Don't swallow this error
_ = ssn.DB(res.DB.GetSelectedDB()).C(res.Config.T.Beacon.BeaconTable).Find(beaconQuery).Sort("-score").All(&beacons)
return beacons
}