-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
api_indicators.go
330 lines (279 loc) · 9.63 KB
/
api_indicators.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// PhishDetect
// Copyright (c) 2018-2021 Claudio Guarnieri.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/botherder/go-savetime/hashes"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"gopkg.in/go-playground/validator.v9"
)
const (
IndicatorTypeEmail = "email"
IndicatorTypeDomain = "domain"
IndicatorsGroupEmails = "emails"
IndicatorsGroupDomains = "domains"
IndicatorsLimitAll = 0
IndicatorsLimit3Months = 1
IndicatorsLimit6Months = 2
IndicatorsLimit24Hours = 3
IndicatorsStatusPending = "pending"
IndicatorsStatusEnabled = "enabled"
IndicatorsStatusDisabled = "disabled"
)
// RequestIndicatorsAdd contains the fields submitted by an administrator or
// submitter when requesting to add new indicators to the database.
type RequestIndicatorsAdd struct {
Type string `json:"type"`
Indicators []string `json:"indicators"`
Tags []string `json:"tags"`
Enabled bool `json:"enabled"`
}
func cleanIndicator(indicator string) string {
indicator = strings.TrimSpace(indicator)
indicator = strings.ToLower(indicator)
indicator = strings.Replace(indicator, "[@]", "@", -1)
indicator = strings.Replace(indicator, "[.]", ".", -1)
indicator = strings.Replace(indicator, "\\@", "@", -1)
indicator = strings.Replace(indicator, "\\.", ".", -1)
if !strings.Contains(indicator, "@") && strings.HasPrefix(indicator, "www.") {
indicator = indicator[4:]
}
return indicator
}
func detectIndicatorType(indicator string) (string, error) {
validate := validator.New()
if validate.Var(indicator, "email") == nil {
return IndicatorTypeEmail, nil
}
if validate.Var(indicator, "fqdn") == nil {
return IndicatorTypeDomain, nil
}
return "", errors.New("Invalid indicator type")
}
func prepareIndicators(iocs []Indicator) map[string][]string {
// We loop through the list of indicators and generate the response.
emails := []string{}
domains := []string{}
for _, ioc := range iocs {
switch ioc.Type {
case IndicatorTypeEmail:
emails = append(emails, ioc.Hashed)
case IndicatorTypeDomain:
domains = append(domains, ioc.Hashed)
}
}
// We assemble the response.
indicators := map[string][]string{
IndicatorsGroupEmails: emails,
IndicatorsGroupDomains: domains,
}
return indicators
}
func apiIndicatorsFetch(w http.ResponseWriter, r *http.Request) {
// We get the indicators from the DB.
iocs, err := db.GetIndicators(IndicatorsLimit3Months, IndicatorsStatusEnabled)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
}
indicators := prepareIndicators(iocs)
responseWithJSON(w, indicators)
}
func apiIndicatorsFetchRecent(w http.ResponseWriter, r *http.Request) {
iocs, err := db.GetIndicators(IndicatorsLimit24Hours, IndicatorsStatusEnabled)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
}
indicators := prepareIndicators(iocs)
responseWithJSON(w, indicators)
}
func apiIndicatorsFetchAll(w http.ResponseWriter, r *http.Request) {
iocs, err := db.GetIndicators(IndicatorsLimitAll, IndicatorsStatusEnabled)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
}
indicators := prepareIndicators(iocs)
responseWithJSON(w, indicators)
}
func apiIndicatorsFetchPending(w http.ResponseWriter, r *http.Request) {
iocs, err := db.GetIndicators(IndicatorsLimitAll, IndicatorsStatusPending)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
}
responseWithJSON(w, iocs)
}
func apiIndicatorsFetchDisabled(w http.ResponseWriter, r *http.Request) {
iocs, err := db.GetIndicators(IndicatorsLimitAll, IndicatorsStatusDisabled)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
}
responseWithJSON(w, iocs)
}
func apiIndicatorsAdd(w http.ResponseWriter, r *http.Request) {
// We decode the request to an RequestIndicatorsAdd.
decoder := json.NewDecoder(r.Body)
var req RequestIndicatorsAdd
err := decoder.Decode(&req)
if err != nil {
errorWithJSON(w, ErrorMsgParseRequestFailed, http.StatusBadRequest, err)
return
}
// This should not typically raise an error because we should have already
// gone through the authMiddleware, but we need to do this anyway in
// order to store a reference of the owner of the indicator in the DB.
user, err := db.GetUserByKey(getAPIKeyFromRequest(r))
if err != nil {
errorWithJSON(w, ErrorMsgNotAuthorized, http.StatusUnauthorized, nil)
return
}
// If the user provided a type, we first check if it is valid,
// and if so we just use that.
// NOTE: This should be typically specified only when the submitter is
// trying to add hashed indicators for which the type can't be
// automatically determined.
if req.Type != "" {
log.Debug().Str("type", req.Type).Msg("Received request to add indicators")
// First we lower-case and trim the specified type.
req.Type = strings.TrimSpace(strings.ToLower(req.Type))
// Then we check if it's a valid "domain" or "email".
// TODO: Eventually we might want to add some flexibility or ability
// to configure supported indicators types.
if req.Type != IndicatorTypeDomain && req.Type != IndicatorTypeEmail {
errorWithJSON(w, ErrorMsgInvalidIndicatorsType, http.StatusBadRequest, nil)
return
}
}
// We loop through the submitted indicators and try to add them to the DB.
addedCounter := 0
for _, indicator := range req.Indicators {
ioc := Indicator{
Tags: req.Tags,
Datetime: time.Now().UTC(),
Owner: user.Name,
}
// Check if we received an already hashed IOC.
if hashes.ValidateSHA256(indicator) {
// If the submitted sent an hashed indicator but did not
// specify the type, we have to skip it because we can't
// automatically determine one.
if req.Type == "" {
log.Debug().Str("indicator", indicator).Msg("Indicator is hashed, but no type specified. Skip.")
continue
}
// Use the indicator as hashed value.
ioc.Hashed = indicator
// The original value shall be blank.
ioc.Original = ""
// Use the type specified by the submitter.
ioc.Type = req.Type
} else {
// Otherwise, we first clean the indicator...
ioc.Original = cleanIndicator(indicator)
// ... then we hash the original indicator.
ioc.Hashed, _ = hashes.StringSHA256(indicator)
// If the user did not specify a type, we try to automatically
// determine it.
if req.Type == "" {
ioc.Type, err = detectIndicatorType(indicator)
// If we can't, we skip this indicator as it might be of an
// unsupported format.
if err != nil {
log.Debug().Str("indicator", indicator).Msg("Failed to detect type for indicator")
continue
}
}
}
// By default, we add indicators as enabled.
ioc.Status = IndicatorsStatusEnabled
if !req.Enabled {
// If the submitter specifies enabled=False,
// then we add the indicators as "pending".
// NOTE: We don't add indicators directly "disabled", as that does
// not make much sense. Anything new should be either approved
// or pending approval.
ioc.Status = IndicatorsStatusPending
}
// Finally add the indicator to the database.
err = db.AddIndicator(ioc)
if err != nil {
log.Error().Err(err).Msg("Failed to add indicator to database")
continue
}
// If the addition was successful, we increase the counter.
addedCounter++
}
response := map[string]interface{}{
"msg": fmt.Sprintf("Added %d indicators", addedCounter),
"counter": addedCounter,
}
responseWithJSON(w, response)
}
func apiIndicatorsChangeStatus(w http.ResponseWriter, r *http.Request, newStatus string) {
decoder := json.NewDecoder(r.Body)
var indicators []string
err := decoder.Decode(&indicators)
if err != nil {
errorWithJSON(w, ErrorMsgParseRequestFailed, http.StatusBadRequest, err)
return
}
toggledCounter := 0
for _, indicator := range indicators {
if !hashes.ValidateSHA256(indicator) {
continue
}
ioc, err := db.GetIndicatorByHash(indicator)
if err != nil {
continue
}
ioc.Status = newStatus
err = db.UpdateIndicator(ioc)
if err != nil {
log.Error().Err(err).Msg("Failed to update indicator")
continue
}
toggledCounter++
}
response := map[string]interface{}{
"msg": fmt.Sprintf("Changed status to %s for %d indicators", newStatus, toggledCounter),
"counter": toggledCounter,
}
responseWithJSON(w, response)
}
func apiIndicatorsEnable(w http.ResponseWriter, r *http.Request) {
apiIndicatorsChangeStatus(w, r, IndicatorsStatusEnabled)
}
func apiIndicatorsDisable(w http.ResponseWriter, r *http.Request) {
apiIndicatorsChangeStatus(w, r, IndicatorsStatusDisabled)
}
func apiIndicatorsDetails(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ioc, err := db.GetIndicatorByHash(vars["ioc"])
if err != nil {
errorWithJSON(w, "Failed to fetch indicator from database", http.StatusInternalServerError, err)
return
}
responseWithJSON(w, ioc)
}