-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathartifact.go
162 lines (133 loc) · 4.18 KB
/
artifact.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
package thehive
import (
"bytes"
"encoding/json"
"fmt"
"github.com/levigross/grequests"
)
// Stores a file attachment response
type FileAttachment struct {
Name string `json:"name"`
Hashes []string `json:"hashes"`
Size int `json:"size"`
ContentType string `json:"contentType"`
Id string `json:"id"`
}
// Stores an artifact
type Artifact struct {
DataType string `json:"dataType"`
Message string `json:"message"`
Tlp int `json:"tlp"`
Tags []string `json:"tags"`
Files []grequests.FileUpload `json:"-"`
Data string `json:"data,omitempty"`
Ioc bool `json:"ioc"`
}
//Raw []byte `json:"-"`
// Stores multiple artifacts from searches
type HiveArtifactMulti struct {
Raw []byte
Detail []Artifact
}
type HiveArtifactMultiResponse struct {
Raw []byte
Detail []ArtifactResponse
}
// Missing Reports
type ArtifactResponse struct {
DataType string `json:"dataType"`
CreatedBy string `json:"createdBy"`
Sighted bool `json:"sighted"`
Tlp int `json:"tlp"`
_Id string `json:"_id"`
Tags []string `json:"tags"`
Message string `json:"message"`
Data string `json:"data"`
Ioc bool `json:"ioc"`
Status string `json:"status"`
Attachment FileAttachment `json:"attachment"`
Id string `json:"id"`
Type string `json:"_type"`
Raw []byte `json:"-"`
}
func (hive *Hivedata) GetCaseArtifacts(caseId string) (*HiveArtifactMultiResponse, error) {
url := fmt.Sprintf("%s/api/case/artifact/_search?range=all", hive.Url)
rawJson := fmt.Sprintf(
`{"query": {
"_and": [{
"_parent": {
"_type": "case",
"_query": {
"_id": "AWHCBwmJGM-hjLdzubbW"
}
}
}, {
"status": "Ok"
}]
}}`,
)
jsondata := []byte(rawJson)
hive.Ro.RequestBody = bytes.NewReader(jsondata)
ret, err := grequests.Post(url, &hive.Ro)
//type HiveArtifactMulti struct {
parsedRet := new(HiveArtifactMultiResponse)
err = json.Unmarshal(ret.Bytes(), &parsedRet.Detail)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}
// FIX - Doesn't map back to a struct yet
func (hive *Hivedata) AnalyzeArtifact(cortexId string, artifactId string, analyzerId string) (*grequests.Response, error) {
rawJson := fmt.Sprintf(`{"cortexId":"%s","artifactId":"%s","analyzerId":"%s"}`, cortexId, artifactId, analyzerId)
jsondata := []byte(rawJson)
hive.Ro.RequestBody = bytes.NewReader(jsondata)
url := fmt.Sprintf("%s/api/connector/cortex/job", hive.Url)
ret, err := grequests.Post(url, &hive.Ro)
if err != nil {
return ret, err
}
return ret, nil
}
// Defines the creation of an artifact
// Takes two parameters:
// 1. caseId string
// 2. caseArtifact Artifact struct
// Returns ArtifactResponse struct and response error
func (hive *Hivedata) AddCaseArtifact(caseId string, caseArtifact Artifact) (*ArtifactResponse, error) {
var err error
var ret *grequests.Response
url := fmt.Sprintf("%s/api/case/%s/artifact", hive.Url, caseId)
jsondata, err := json.Marshal(caseArtifact)
if err != nil {
return new(ArtifactResponse), err
}
if caseArtifact.DataType == "file" {
fd, err := grequests.FileUploadFromDisk(caseArtifact.Data)
if err != nil {
return nil, err
}
caseArtifact.Data = ""
// Custom solution because files suck. Couldn't get it to work with hive.Ro for some reason
marshalData, err := json.Marshal(caseArtifact)
if err != nil {
return nil, err
}
data := map[string]string{"_json": fmt.Sprintf("%s", string(marshalData))}
fd[0].FieldName = "attachment"
requestOptions := &grequests.RequestOptions{
Files: fd,
Data: data,
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", hive.Apikey),
},
InsecureSkipVerify: !false,
}
ret, err = grequests.Post(url, requestOptions)
} else {
hive.Ro.RequestBody = bytes.NewReader(jsondata)
ret, err = grequests.Post(url, &hive.Ro)
}
parsedRet := new(ArtifactResponse)
_ = json.Unmarshal(ret.Bytes(), parsedRet)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}