This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
orchestration.go
157 lines (132 loc) · 4.53 KB
/
orchestration.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
package main
import (
"fmt"
"log"
"strings"
"crypto/tls"
"github.com/parnurzeal/gorequest"
"gopkg.in/mgo.v2/bson"
)
// OrkaMon is a function for tracking which plays exist and then running them sequentually.
func OrkaMon() {
sessionClone := Sessions.Clone()
c := sessionClone.DB("test").C("playbook")
var plays []PlaybookEntry
_ = c.Find(nil).All(&plays)
if plays != nil {
for _, play := range plays {
go PlayRun(play)
}
}
}
// PlayRun is a function for running a playbook entry received from OrkaMon.
func PlayRun(play PlaybookEntry) {
var indicators []RawIndicators
sessionClone := Sessions.Clone()
c := sessionClone.DB("test").C("pre_processing")
log.Printf("Take indicators from %v, process, and send to %v\n", play.Source, play.Dest)
_ = c.Find(bson.M{"source": play.Source}).All(&indicators)
ID := play.ID
var indicators2 []RawIndicators
// Check and make sure the indicator has not already been processed through this play.
for _, entry := range indicators {
if contains(entry.OrkaTracker, ID) == false {
indicators2 = append(indicators2, entry)
}
}
h := sessionClone.DB("test").C("pre_processing")
// Mark all indicators as being processed by the play.
h.UpdateAll(bson.M{"source": play.Source}, bson.M{"$addToSet": bson.M{"orkatracker": play.ID}})
for _, operator := range play.Operators {
log.Println(operator)
}
if play.Dest != "" {
switch play.Dest {
case "crits":
go OrkaToCrits(indicators2)
case "postprocessing":
go SendToPost(indicators2)
}
}
}
// Function to see if a string slice contains a string.
func contains(array []string, test string) bool {
for _, value := range array {
if value == test {
return true
}
}
return false
}
// SendToPost is a gadget for the orchestration to be used for sending indicators to post-processing.
func SendToPost(indicators []RawIndicators) {
// Connect to databse and insert document.
sessionClone := Sessions.Clone()
c := sessionClone.DB("test").C("post_processing")
d := sessionClone.DB("test").C("pre_processing")
for _, processedindicator := range indicators {
log.Printf("Received request to move %v to post-processing.\n", processedindicator.Indicator)
err := c.Insert(processedindicator)
if err != nil {
FatalError(err)
}
count, err := c.Find(bson.M{"guid": processedindicator.Guid}).Count()
if err != nil {
FatalError(err)
}
// Check if indicator is present in post-processing.
if count > 0 {
_, err := d.RemoveAll(bson.M{"guid": processedindicator.Guid})
if err != nil {
FatalError(err)
}
log.Printf("%v successfully transferred from pre-processing to post-processing.\n", processedindicator.Indicator)
} else {
log.Printf("Indicator %v failed to be moved to post-processing.\n", processedindicator.Indicator)
}
}
}
// OrkaToCrits is a gadget for the orchestration to be used for sending indicators to CRITs.
func OrkaToCrits(indicators []RawIndicators) {
sessionClone := Sessions.Clone()
d := sessionClone.DB("test").C("pre_processing")
for _, processedindicator := range indicators {
// Switch to conform GOSINT indicator types to CRITs indicator types.
var indicator_type string
switch processedindicator.Ind_type {
case "domain":
indicator_type = "Domain"
case "url":
indicator_type = "URI"
case "ip":
indicator_type = "IPv4 Address"
case "sha256":
indicator_type = "SHA256"
case "md5":
indicator_type = "MD5"
}
joinedTags := strings.Join(processedindicator.Tags, ",")
if joinedTags == "" {
joinedTags = "gosint"
} else {
joinedTags = joinedTags + ", gosint"
}
// Build structure for indicator to be sent to CRITs.
jayson := fmt.Sprintf(`{"type":"%v", "indicator_confidence":"medium", "indicator_impact":"medium", "source":"%v", "value":"%v", "description":"%v", "reference":"gosint", "bucket_list":"%v"}`, indicator_type, Config.CRITsSource, processedindicator.Indicator, processedindicator.Context, joinedTags)
request := gorequest.New().TLSClientConfig(&tls.Config{InsecureSkipVerify: true})
jayson = strings.Replace(jayson, "\n", " ", -1)
jayson = strings.Replace(jayson, "\r", " ", -1)
// Make POST request to CRITs API to upload indicator.
_, _, err := request.Post(Config.CRITsServer + "/api/v1/indicators/?username=" + Config.CRITsUser + "&api_key=" + Config.CRITsKey).
Send(jayson).
End()
if err != nil {
log.Println(err)
}
_, err2 := d.RemoveAll(bson.M{"guid": processedindicator.Guid})
if err2 != nil {
FatalError(err2)
}
log.Printf("%v successfully transferred from pre-processing to CRITs.\n", processedindicator.Indicator)
}
}