-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
case.go
196 lines (163 loc) · 5.77 KB
/
case.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
192
193
194
195
196
package thehive
import (
"bytes"
"encoding/json"
"fmt"
"github.com/levigross/grequests"
)
// Stores a hive case
type HiveCase struct {
Title string `json:"title"`
Description string `json:"description"`
Tlp int `json:"tlp"`
Severity int `json:"severity"`
Tags []string `json:"tags"`
Tasks []CaseTask `json:"tasks"`
Flag bool `json:"flag"`
Date int64 `json:"date,omitempty"`
Status string `json:"status,omitempty"`
Id string `json:"id,omitempty"`
Owner string `json:"owner,omitempty"`
CustomFields map[string]interface{} `json:"customFields"`
Raw []byte `json:"-"`
}
// Stores multiple hive cases from searches
type HiveCaseMulti struct {
Raw []byte
Detail []HiveCase
}
// Stores the response of a case from thehive
type HiveCaseResp struct {
Title string `json:"title"`
Description string `json:"description"`
Tlp int `json:"tlp"`
Severity int `json:"severity"`
Date int64 `json:"date,omitempty"`
Tags []string `json:"tags"`
Tasks []CaseTask `json:"tasks"`
Flag bool `json:"flag"`
Owner string `json:"owner"`
Status string `json:"status"`
CreatedAt int64 `json:"createdAt"`
CustomFields map[string]interface{} `json:"customFields"`
Id string `json:"id"`
Summary string `json:"summary"`
ResolutionStatus string `json:"resolutionStatus"`
ImpactStatus string `json:"impactStatus"`
Raw []byte `json:"-"`
}
// Stores a response if there are multiple cases
type HiveCaseRespMulti struct {
Raw []byte
Detail []HiveCaseResp
}
// Defines creation of a case
// Takes two parameters:
// 1. title string
// 2. description string
// 3. tlp int
// 4. severity int
// 5. tasks []CaseTask
// 6. tags []string
// 7. flag bool
// Returns HiveCase struct and response error
func (hive *Hivedata) CreateCase(title string, description string, tlp int, severity int, tasks []CaseTask, tags []string, flag bool) (*HiveCase, error) {
var curcase HiveCase
var url string
if title == "" {
fmt.Println("Missing title in API call. Set title.")
title = "Missing title in API call"
}
if description == "" {
fmt.Println("Description not set.")
description = ""
}
// Creates case struct for json usage
curcase = HiveCase{
Title: title,
Description: description,
Tlp: tlp,
Severity: severity,
Tags: tags,
Tasks: tasks,
Flag: flag,
}
// Encodes struct as json
jsondata, err := json.Marshal(curcase)
if err != nil {
return nil, err
}
// FIX - might point to same memory, so make a duplicate without editing
hive.Ro.RequestBody = bytes.NewReader(jsondata)
url = fmt.Sprintf("%s%s", hive.Url, "/api/case")
ret, err := grequests.Post(url, &hive.Ro)
parsedRet := new(HiveCase)
_ = json.Unmarshal(ret.Bytes(), parsedRet)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}
// Defines creation of a case task within a case
// Takes two parameters:
// 1. title string
// 2. description string
// 3. tlp int
// 4. severity int
// 5. tasks []CaseTask
// 6. tags []string
// 7. flag bool
// Returns HiveCase struct and response error
func (hive *Hivedata) GetCase(case_id string) (*HiveCaseResp, error) {
var url, urlpath string
urlpath = fmt.Sprintf("/api/case/%s", case_id)
url = fmt.Sprintf("%s%s", hive.Url, urlpath)
ret, err := grequests.Get(url, &hive.Ro)
parsedRet := new(HiveCaseResp)
_ = json.Unmarshal(ret.Bytes(), parsedRet)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}
// Finds all cases based on search parameter
// Takes one argument
// 1. search []byte defined as a marshalled json string
// FIX - Missing sort and range, can use struct
func (hive *Hivedata) FindCases(search []byte) (*HiveCaseRespMulti, error) {
var url = fmt.Sprintf("%s%s", hive.Url, "/api/case/_search?sort=%2Btlp&range=all")
hive.Ro.JSON = search
ret, err := grequests.Post(url, &hive.Ro)
// Not yet fixed
parsedRet := new(HiveCaseRespMulti)
_ = json.Unmarshal(ret.Bytes(), &parsedRet.Detail)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}
// Defines case task log creation
// Takes three parameters:
// 1. caseId string
// 2. name string
// 3. data string
// Returns CaseTaskLogresponse struct and response error
// FIX - only supports string currently
func (hive *Hivedata) AddCustomFieldData(caseId string, name string, data string) (*HiveCase, error) {
jsonQuery := fmt.Sprintf(`{"customFields.%s": {"string": "%s"}}`, name, data)
jsondata := []byte(jsonQuery)
hive.Ro.RequestBody = bytes.NewReader(jsondata)
url := fmt.Sprintf("%s/api/case/%s", hive.Url, caseId)
//resp, err := grequests.Post(url, &hive.Ro)
resp, err := grequests.Patch(url, &hive.Ro)
parsedRet := new(HiveCase)
_ = json.Unmarshal(resp.Bytes(), parsedRet)
parsedRet.Raw = resp.Bytes()
return parsedRet, err
}
func (hive *Hivedata) PatchCaseFieldInt(alertId string, field string, value int64) (*HiveCase, error) {
url := fmt.Sprintf("%s/api/case/%s", hive.Url, alertId)
data := fmt.Sprintf(`{"%s": %d}`, field, value)
fmt.Println(data)
jsondata := []byte(data)
hive.Ro.RequestBody = bytes.NewReader(jsondata)
ret, err := grequests.Patch(url, &hive.Ro)
parsedRet := new(HiveCase)
_ = json.Unmarshal(ret.Bytes(), parsedRet)
parsedRet.Raw = ret.Bytes()
return parsedRet, err
}