-
Notifications
You must be signed in to change notification settings - Fork 359
/
analyze.go
173 lines (159 loc) · 4.7 KB
/
analyze.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
package commands
import (
"fmt"
"time"
"github.com/activecm/rita/analysis/beacon"
"github.com/activecm/rita/analysis/blacklist"
"github.com/activecm/rita/analysis/crossref"
"github.com/activecm/rita/analysis/dns"
"github.com/activecm/rita/analysis/structure"
"github.com/activecm/rita/analysis/useragent"
"github.com/activecm/rita/resources"
"github.com/activecm/rita/util"
"github.com/blang/semver"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func init() {
analyzeCommand := cli.Command{
Name: "analyze",
Usage: "Analyze imported databases",
UsageText: "rita analyze [command options] [database]\n\n" +
"If no database is specified, every database will be analyzed.",
Flags: []cli.Flag{
configFlag,
},
Action: func(c *cli.Context) error {
r := analyze(c.Args().Get(0), c.String("config"))
fmt.Printf(updateCheck(c.String("config")))
return r
},
}
bootstrapCommands(analyzeCommand)
}
func analyze(inDb string, configFile string) error {
res := resources.InitResources(configFile)
var toRunDirty []string
var toRun []string
// Check to see if we want to run a full database or just one off the command line
if inDb == "" {
res.Log.Info("Running analysis against all databases")
toRunDirty = append(toRun, res.MetaDB.GetAnalyzeReadyDatabases()...)
} else {
toRunDirty = append(toRun, inDb)
}
// Check for problems
for _, possDB := range toRunDirty {
info, err := res.MetaDB.GetDBMetaInfo(possDB)
if err != nil {
errStr := fmt.Sprintf("Error: %s not found.", possDB)
res.Log.Errorf(errStr)
fmt.Println(errStr)
continue
}
if !info.ImportFinished {
errStr := fmt.Sprintf("Error: %s hasn't finished being imported.", possDB)
res.Log.Errorf(errStr)
fmt.Println(errStr)
continue
}
if info.Analyzed {
errStr := fmt.Sprintf("Error: %s is already analyzed.", possDB)
res.Log.Errorf(errStr)
fmt.Println(errStr)
continue
}
semVer, err := semver.ParseTolerant(info.ImportVersion)
if err != nil {
errStr := fmt.Sprintf("Error: %s is labelled with an incorrect version tag", possDB)
res.Log.Errorf(errStr)
fmt.Println(errStr)
continue
}
if semVer.Major != res.Config.R.Version.Major {
errStr := fmt.Sprintf("Error: %s was parsed by an incompatible version of RITA", possDB)
res.Log.Errorf(errStr)
fmt.Println(errStr)
continue
}
toRun = append(toRun, possDB)
}
startAll := time.Now()
fmt.Println("[+] Analyzing:")
for _, db := range toRun {
fmt.Println("\t[-] " + db)
}
res.Log.WithFields(log.Fields{
"databases": toRun,
"start_time": startAll.Format(util.TimeFormat),
}).Info("Preparing to analyze ")
for _, td := range toRun {
startIndiv := time.Now()
res.Log.WithFields(log.Fields{
"database": td,
"start_time": startIndiv.Format(util.TimeFormat),
}).Info("Analyzing")
fmt.Println("[+] Analyzing " + td)
res.DB.SelectDB(td)
logAnalysisFunc("Unique Connections", td, res,
structure.BuildUniqueConnectionsCollection,
)
logAnalysisFunc("Unique Hosts", td, res,
func(innerRes *resources.Resources) {
structure.BuildHostsCollection(innerRes)
structure.BuildIPv4Collection(innerRes)
structure.BuildIPv6Collection(innerRes)
},
)
logAnalysisFunc("Unique Hostnames", td, res,
dns.BuildHostnamesCollection,
)
logAnalysisFunc("Exploded DNS", td, res,
dns.BuildExplodedDNSCollection,
)
logAnalysisFunc("User Agent", td, res,
useragent.BuildUserAgentCollection,
)
logAnalysisFunc("Blacklisted", td, res,
blacklist.BuildBlacklistedCollections,
)
logAnalysisFunc("Beaconing", td, res,
beacon.BuildBeaconCollection,
)
logAnalysisFunc("Cross Reference", td, res,
crossref.BuildXRefCollection,
)
res.MetaDB.MarkDBAnalyzed(td, true)
endIndiv := time.Now()
res.Log.WithFields(log.Fields{
"database": td,
"end_time": endIndiv.Format(util.TimeFormat),
"duration": endIndiv.Sub(startIndiv),
}).Info("Analysis complete")
}
endAll := time.Now()
res.Log.WithFields(log.Fields{
"end_time": endAll.Format(util.TimeFormat),
"duration": endAll.Sub(startAll),
}).Info("Analysis complete")
return nil
}
func logAnalysisFunc(analysisName string, databaseName string,
resources *resources.Resources, analysis func(*resources.Resources)) {
analysisName += " Analysis"
start := time.Now()
resources.Log.WithFields(log.Fields{
"analysis": analysisName,
"database": databaseName,
"start_time": start.Format(util.TimeFormat),
}).Infof("Running analysis")
fmt.Println("\t[-] Running " + analysisName)
analysis(resources)
end := time.Now()
resources.Log.WithFields(log.Fields{
"analysis": analysisName,
"database": databaseName,
"end_time": end.Format(util.TimeFormat),
"duration": end.Sub(start),
}).Infof("Analysis complete")
}