This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.go
408 lines (352 loc) · 9.77 KB
/
process.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package cortexbot
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"sort"
"strconv"
"strings"
valid "github.com/asaskevich/govalidator"
"github.com/go-telegram-bot-api/telegram-bot-api"
cortex "github.com/ilyaglow/go-cortex/v3"
)
type updateMeta struct {
from *tgbotapi.User
chatID int64
msgID int
data string
typ string
}
func newUpdateMeta(update *tgbotapi.Update) (*updateMeta, error) {
if update.CallbackQuery != nil {
return &updateMeta{
from: update.CallbackQuery.From,
chatID: update.CallbackQuery.Message.Chat.ID,
msgID: update.CallbackQuery.Message.MessageID,
data: update.CallbackQuery.Data,
typ: "callback",
}, nil
}
if update.Message != nil {
return &updateMeta{
from: update.Message.From,
chatID: update.Message.Chat.ID,
msgID: update.Message.MessageID,
data: update.Message.Text,
typ: "message",
}, nil
}
return nil, fmt.Errorf("unknown update type: %v", update)
}
// sendReport sends a report depends on success
func (c *Cortexbot) sendReport(r *cortex.Report, callback *tgbotapi.CallbackQuery) error {
if r.Status == "Failure" {
msg := tgbotapi.NewMessage(callback.Message.Chat.ID, fmt.Sprintf("Analyzer %s failed on %s: %s", r.AnalyzerName, r.Data, r.ReportBody.ErrorMessage))
msg.ReplyToMessageID = callback.Message.MessageID
msg.DisableWebPagePreview = true
_, err := c.Bot.Send(msg)
return err
}
// Send JSON file with full report and taxonomies
tr, _ := json.MarshalIndent(r, "", " ")
fb := tgbotapi.FileBytes{
Name: fmt.Sprintf("%s-%s.log", r.AnalyzerName, r.ID),
Bytes: tr,
}
attachment := tgbotapi.NewDocumentUpload(callback.Message.Chat.ID, fb)
attachment.ReplyToMessageID = callback.Message.ReplyToMessage.MessageID
attachment.Caption = buildTaxonomies(r.Taxonomies())
_, err := c.Bot.Send(attachment)
return err
}
func (c *Cortexbot) processUpdate(update *tgbotapi.Update) error {
meta, err := newUpdateMeta(update)
if err != nil {
return err
}
log.Printf(
"%s: [%s:%d] %s",
meta.typ,
meta.from.UserName,
meta.from.ID,
meta.data,
)
msg := tgbotapi.NewMessage(meta.chatID, "")
msg.ReplyToMessageID = meta.msgID
isCmd := update.Message != nil && update.Message.IsCommand()
var cmd string
if isCmd {
cmd = update.Message.Command()
}
if cmd == "login" && c.noAdmin() {
if update.Message.CommandArguments() != c.Password {
msg.Text = "Wrong password"
_, err = c.Bot.Send(msg)
return err
}
u := &User{
ID: meta.from.ID,
Admin: 1,
About: meta.from.String(),
}
err = c.addUser(u)
if err != nil {
msg.Text = fmt.Sprintf("Can't register: %s", err.Error())
_, err = c.Bot.Send(msg)
return err
}
log.Printf("Registered new user %d", meta.from.ID)
msg.Text = "Success"
_, err = c.Bot.Send(msg)
return err
}
// user initiates interaction.
if cmd == "start" {
if c.noAdmin() {
msg.Text = "No admin assigned. Try /login password"
_, err = c.Bot.Send(msg)
return err
}
if c.CheckAuth(meta.from) {
msg.Text = "Already logged in, you can send indicators"
_, err := c.Bot.Send(msg)
return err
}
msg.Text = "Forward the next message to the bot admin"
_, err := c.Bot.Send(msg)
if err != nil {
return err
}
msg = tgbotapi.NewMessage(meta.chatID, "")
msg.Text = fmt.Sprintf("/approve %d %s", meta.from.ID, meta.from.String())
_, err = c.Bot.Send(msg)
return err
}
if c.CheckAdmin(meta.from) && cmd == "approve" {
parts := strings.SplitN(update.Message.Text, " ", 3)
if len(parts) < 3 {
msg.Text = fmt.Sprintf("Not enough parameters to approve: %s", update.Message.Text)
_, err = c.Bot.Send(msg)
return err
}
id, err := strconv.ParseInt(parts[1], 10, 32)
u := &User{
ID: int(id),
Admin: 0,
About: parts[2],
}
err = c.addUser(u)
if err != nil {
msg.Text = fmt.Sprintf("can't create user: %s", err.Error())
_, err = c.Bot.Send(msg)
if err != nil {
return err
}
}
msg.Text = "Successfully approved"
_, err = c.Bot.Send(msg)
return err
}
// auth guard
if !c.CheckAuth(meta.from) {
msg.Text = "Non-authorized action, type /start to login"
_, err := c.Bot.Send(msg)
return err
}
switch meta.typ {
case "callback":
if err := c.processCallback(update.CallbackQuery); err != nil {
return err
}
cbcfg := tgbotapi.NewCallback(update.CallbackQuery.ID, "")
_, err := c.Bot.AnswerCallbackQuery(cbcfg)
return err
case "message":
return c.processMessage(update.Message)
}
return nil
}
// processCallback analyzes observables with a selected set of analyzers
func (c *Cortexbot) processCallback(callback *tgbotapi.CallbackQuery) error {
var j cortex.Observable
var err error
if callback.Message.ReplyToMessage.Document != nil {
link, err := c.Bot.GetFileDirectURL(callback.Message.ReplyToMessage.Document.FileID)
if err != nil {
log.Println("Can't get direct link to file")
return err
}
j, err = newFileArtifactFromURL(link, callback.Message.ReplyToMessage.Document.FileName, c.TLP, c.PAP, c.Bot.Client)
if err != nil {
log.Println(err.Error())
return err
}
} else {
j = newArtifact(callback.Message.ReplyToMessage.Text, c.TLP, c.PAP)
}
switch callback.Data {
case "all":
mul := c.Cortex.Analyzers.NewMultiRun(context.Background(), c.Timeout)
mul.OnReport = func(r *cortex.Report) {
err = c.sendReport(r, callback)
log.Println(err)
}
mul.OnError = func(e error, o cortex.Observable, a *cortex.Analyzer) {
log.Println(fmt.Sprintf("Cortex analyzer %s failed on data %s with an error: %s", a.Name, o.Description(), e.Error()))
}
err = mul.Do(j)
if err != nil {
return err
}
case "close":
kb := showButton()
edit := tgbotapi.NewEditMessageReplyMarkup(callback.Message.Chat.ID, callback.Message.MessageID, *kb)
if _, err := c.Bot.Send(edit); err != nil {
return err
}
case "show":
var kind string
if callback.Message.ReplyToMessage.Document != nil {
kind = "file"
} else {
kind = dataType(callback.Message.ReplyToMessage.Text)
}
kb, err := c.analyzersButtons(kind)
if err != nil {
return err
}
edit := tgbotapi.NewEditMessageReplyMarkup(callback.Message.Chat.ID, callback.Message.MessageID, *kb)
if _, err := c.Bot.Send(edit); err != nil {
return err
}
default:
r, err := c.Cortex.Analyzers.Run(context.Background(), callback.Data, j, c.Timeout)
if err != nil {
msg := tgbotapi.NewMessage(callback.Message.Chat.ID, fmt.Sprintf("%s failed: %s", callback.Data, err.Error()))
msg.ReplyToMessageID = callback.Message.MessageID
msg.DisableWebPagePreview = true
if _, err := c.Bot.Send(msg); err != nil {
return err
}
} else {
err = c.sendReport(r, callback)
if err != nil {
return err
}
}
}
return nil
}
// analyzersButtons returns a markup of analyzers as buttons
func (c *Cortexbot) analyzersButtons(datatype string) (*tgbotapi.InlineKeyboardMarkup, error) {
analyzers, _, err := c.Cortex.Analyzers.ListByType(context.Background(), datatype)
if err != nil {
return nil, err
}
var names []string
for i := range analyzers {
names = append(names, analyzers[i].Name)
}
sort.Strings(names)
var rows [][]tgbotapi.InlineKeyboardButton
rows = append(rows, []tgbotapi.InlineKeyboardButton{
tgbotapi.NewInlineKeyboardButtonData("All", "all"),
})
for _, n := range names {
var buttons []tgbotapi.InlineKeyboardButton
b := tgbotapi.NewInlineKeyboardButtonData(n, n)
buttons = append(buttons, b)
rows = append(rows, buttons)
}
rows = append(rows, []tgbotapi.InlineKeyboardButton{
tgbotapi.NewInlineKeyboardButtonData("Close", "close"),
})
markup := tgbotapi.NewInlineKeyboardMarkup(rows...)
return &markup, nil
}
// showButton returns only one button, used on close callback
func showButton() *tgbotapi.InlineKeyboardMarkup {
var rows [][]tgbotapi.InlineKeyboardButton
rows = append(rows, []tgbotapi.InlineKeyboardButton{
tgbotapi.NewInlineKeyboardButtonData("Show", "show"),
})
markup := tgbotapi.NewInlineKeyboardMarkup(rows...)
return &markup
}
// processMessage asks Cortex about data submitted by a user
func (c *Cortexbot) processMessage(input *tgbotapi.Message) error {
msg := tgbotapi.NewMessage(input.Chat.ID, "Select analyzer to run. Choose <All> to run all of them.")
msg.ReplyToMessageID = input.MessageID
var dt string
if input.Document != nil {
dt = "file"
} else {
dt = dataType(input.Text)
}
bmarkup, err := c.analyzersButtons(dt)
if err != nil {
return err
}
msg.ReplyMarkup = bmarkup
_, err = c.Bot.Send(msg)
if err != nil {
return err
}
return nil
}
// buildTaxonomies joins taxonomies in one formatted string
// Every taxonomy is separated with two spaces from each other
func buildTaxonomies(txs []cortex.Taxonomy) string {
var stats []string
for _, t := range txs {
stats = append(stats, fmt.Sprintf("%s:%s = %v", t.Namespace, t.Predicate, t.Value))
}
return strings.Join(stats, "\n")
}
// newArtifact makes an Artifact depends on its type
func newArtifact(s string, tlp cortex.TLP, pap cortex.PAP) cortex.Observable {
return &cortex.Task{
Data: s,
DataType: dataType(s),
TLP: &tlp,
PAP: &pap,
}
}
func dataType(d string) (t string) {
if valid.IsIP(d) {
t = "ip"
} else if IsDNSName(d) {
t = "domain"
} else if IsHash(d) {
t = "hash"
} else if valid.IsEmail(d) {
t = "mail"
} else if valid.IsURL(d) {
t = "url"
} else {
t = "other"
}
return t
}
// newFileArtifactFromURL makes a FileArtifact from URL
func newFileArtifactFromURL(link, fname string, tlp cortex.TLP, pap cortex.PAP, client *http.Client) (cortex.Observable, error) {
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return &cortex.FileTask{
FileTaskMeta: cortex.FileTaskMeta{
DataType: "file",
TLP: &tlp,
PAP: &pap,
},
FileName: fname,
Reader: resp.Body,
}, nil
}