-
Notifications
You must be signed in to change notification settings - Fork 363
/
analyzer.go
191 lines (153 loc) · 4.74 KB
/
analyzer.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
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
package useragent
import (
"strconv"
"sync"
"github.com/activecm/rita/config"
"github.com/activecm/rita/database"
"github.com/globalsign/mgo/bson"
)
type (
//analyzer : structure for exploded dns analysis
analyzer struct {
chunk int //current chunk (0 if not on rolling analysis)
chunkStr string //current chunk (0 if not on rolling analysis)
db *database.DB // provides access to MongoDB
conf *config.Config // contains details needed to access MongoDB
analyzedCallback func(update) // called on each analyzed result
closedCallback func() // called when .close() is called and no more calls to analyzedCallback will be made
analysisChannel chan *Input // holds unanalyzed data
analysisWg sync.WaitGroup // wait for analysis to finish
}
)
//newAnalyzer creates a new collector for parsing hostnames
func newAnalyzer(chunk int, db *database.DB, conf *config.Config, analyzedCallback func(update), closedCallback func()) *analyzer {
return &analyzer{
chunk: chunk,
chunkStr: strconv.Itoa(chunk),
db: db,
conf: conf,
analyzedCallback: analyzedCallback,
closedCallback: closedCallback,
analysisChannel: make(chan *Input),
}
}
//collect sends a group of domains to be analyzed
func (a *analyzer) collect(data *Input) {
a.analysisChannel <- data
}
//close waits for the collector to finish
func (a *analyzer) close() {
close(a.analysisChannel)
a.analysisWg.Wait()
a.closedCallback()
}
//start kicks off a new analysis thread
func (a *analyzer) start() {
a.analysisWg.Add(1)
go func() {
ssn := a.db.Session.Copy()
defer ssn.Close()
for data := range a.analysisChannel {
// set up writer output
var output update
if len(data.OrigIps) > 10 {
data.OrigIps = data.OrigIps[:10]
}
if len(data.Requests) > 10 {
data.Requests = data.Requests[:10]
}
// create query
query := bson.M{
"$push": bson.M{
"dat": bson.M{
"seen": data.Seen,
"orig_ips": data.OrigIps,
"hosts": data.Requests,
"cid": a.chunk,
},
},
"$set": bson.M{"cid": a.chunk},
"$setOnInsert": bson.M{"ja3": data.JA3},
}
output.query = query
output.collection = a.conf.T.UserAgent.UserAgentTable
// create selector for output
output.selector = bson.M{"user_agent": data.name}
// set to writer channel
a.analyzedCallback(output)
// this is for flagging rarely used j3 and useragent hosts
if len(data.OrigIps) < 5 {
maxLeft := 5 - len(data.OrigIps)
query := []bson.M{
bson.M{"$match": bson.M{"user_agent": data.name}},
bson.M{"$project": bson.M{"ips": "$dat.orig_ips", "user_agent": 1}},
bson.M{"$unwind": "$ips"},
bson.M{"$unwind": "$ips"}, // not an error, needs to be done twice
bson.M{"$group": bson.M{
"_id": "$user_agent",
"ips": bson.M{"$addToSet": "$ips"},
}},
bson.M{"$project": bson.M{
"count": bson.M{"$size": bson.M{"$ifNull": []interface{}{"$ips", []interface{}{}}}},
"ips": "$ips",
}},
bson.M{"$match": bson.M{"count": bson.M{"$lte": maxLeft}}},
}
var resList struct {
ID string `bson:"_id"`
IPS []string `bson:"ips"`
Count int `bson:"count"`
}
_ = ssn.DB(a.db.GetSelectedDB()).C(a.conf.T.UserAgent.UserAgentTable).Pipe(query).AllowDiskUse().One(&resList)
for _, entry := range resList.IPS {
newRecordFlag := false
type hostRes struct {
CID int `bson:"cid"`
}
var res2 []hostRes
_ = ssn.DB(a.db.GetSelectedDB()).C(a.conf.T.Structure.HostTable).Find(bson.M{"ip": entry, "dat.rsig": data.name}).All(&res2)
if !(len(res2) > 0) {
newRecordFlag = true
// fmt.Println("host no results", res2, data.Host)
} else {
if res2[0].CID != a.chunk {
// fmt.Println("host existing", a.chunk, res2, data.Host)
newRecordFlag = true
}
}
output := hostQuery(a.chunk, data.name, entry, newRecordFlag)
output.collection = a.conf.T.Structure.HostTable
// set to writer channel
a.analyzedCallback(output)
}
}
}
a.analysisWg.Done()
}()
}
//hostQuery ...
func hostQuery(chunk int, useragentStr string, ip string, newFlag bool) update {
var output update
// create query
query := bson.M{}
if newFlag {
query["$push"] = bson.M{
"dat": bson.M{
"rsig": useragentStr,
"rsigc": 1,
"cid": chunk,
}}
// create selector for output ,
output.query = query
output.selector = bson.M{"ip": ip}
} else {
query["$set"] = bson.M{
"dat.$.rsigc": 1,
"dat.$.chunk": chunk,
}
// create selector for output
output.query = query
output.selector = bson.M{"ip": ip, "dat.cid": chunk}
}
return output
}