-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconfig.go
641 lines (590 loc) · 16.8 KB
/
config.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package main
import (
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v2"
)
type MalwareRepoType int64
const (
NotSupported MalwareRepoType = iota //NotSupported must always be first, or other things won't work as expected
AssemblyLine
CapeSandbox
FileScanIo
HybridAnalysis
InQuest
JoeSandbox
Malpedia
Malshare
MalwareBazaar
MWDB
ObjectiveSee
Polyswarm
Triage
UnpacMe
URLScanIO
VirusExchange
VirusTotal
VxShare
UploadAssemblyLine
//UploadMWDB must always be last, or other things won't work as expected
UploadMWDB
)
// var MalwareRepoList = []MalwareRepoType{CapeSandbox, HybridAnalysis, InQuest, JoeSandbox, Malpedia, Malshare, MalwareBazaar, MWDB, ObjectiveSee, Polyswarm, Triage, UnpacMe, VirusTotal, UploadMWDB}
func getMalwareRepoList() []MalwareRepoType {
var malwareRepoList []MalwareRepoType
for repo := range [UploadMWDB + 1]int64{} {
if int64(repo) > int64(NotSupported) && int64(repo) <= int64(UploadMWDB) {
malwareRepoList = append(malwareRepoList, MalwareRepoType(repo))
}
}
return malwareRepoList
}
func (malrepo MalwareRepoType) QueryAndDownload(repos []RepositoryConfigEntry, hash Hash, doNotExtract bool, osq ObjectiveSeeQuery) (bool, string, MalwareRepoType) {
matchingConfigRepos := getConfigsByType(malrepo, repos)
if len(matchingConfigRepos) == 0 {
fmt.Printf(" [!] %s is not found in the yml config file\n", malrepo)
}
for _, mcr := range matchingConfigRepos {
found := false
filename := ""
checkedRepo := NotSupported
fmt.Printf(" [*] %s: %s\n", mcr.Type, mcr.Host)
switch malrepo {
case MalwareBazaar:
found, filename = malwareBazaar(mcr.Host, hash, doNotExtract, "infected")
checkedRepo = MalwareBazaar
case MWDB:
found, filename = mwdb(mcr.Host, mcr.Api, hash)
checkedRepo = MWDB
case Malshare:
found, filename = malshare(mcr.Host, mcr.Api, hash)
checkedRepo = Malshare
case Triage:
found, filename = triage(mcr.Host, mcr.Api, hash)
checkedRepo = Triage
case InQuest:
found, filename = inquestlabs(mcr.Host, mcr.Api, hash)
checkedRepo = InQuest
case HybridAnalysis:
found, filename = hybridAnlysis(mcr.Host, mcr.Api, hash, doNotExtract)
checkedRepo = HybridAnalysis
case Polyswarm:
found, filename = polyswarm(mcr.Host, mcr.Api, hash)
checkedRepo = Polyswarm
case VirusTotal:
found, filename = virustotal(mcr.Host, mcr.Api, hash)
checkedRepo = VirusTotal
case JoeSandbox:
found, filename = joesandbox(mcr.Host, mcr.Api, hash)
checkedRepo = JoeSandbox
case CapeSandbox:
found, filename = capesandbox(mcr.Host, mcr.Api, hash)
checkedRepo = CapeSandbox
case ObjectiveSee:
if len(osq.Malware) > 0 {
found, filename = objectivesee(osq, hash, doNotExtract)
checkedRepo = ObjectiveSee
}
case UnpacMe:
found, filename = unpacme(mcr.Host, mcr.Api, hash)
checkedRepo = UnpacMe
case Malpedia:
found, filename = malpedia(mcr.Host, mcr.Api, hash)
checkedRepo = Malpedia
case VxShare:
found, filename = vxshare(mcr.Host, mcr.Api, hash, doNotExtract, "infected")
checkedRepo = VxShare
case FileScanIo:
found, filename = filescanio(mcr.Host, mcr.Api, hash, doNotExtract, "infected")
checkedRepo = FileScanIo
case URLScanIO:
found, filename = urlscanio(mcr.Host, mcr.Api, hash)
checkedRepo = URLScanIO
case VirusExchange:
found, filename = virusexchange(mcr.Host, mcr.Api, hash)
checkedRepo = VirusExchange
case AssemblyLine:
found, filename = assemblyline(mcr.Host, mcr.User, mcr.Api, mcr.IgnoreTLSErrors, hash)
checkedRepo = AssemblyLine
case UploadAssemblyLine:
found, filename = assemblyline(mcr.Host, mcr.User, mcr.Api, mcr.IgnoreTLSErrors, hash)
checkedRepo = UploadAssemblyLine
case UploadMWDB:
found, filename = mwdb(mcr.Host, mcr.Api, hash)
checkedRepo = UploadMWDB
}
// So some repos we can't download from but we want to know that it exists at that service
// At the moment, this is just Any.Run but suspect more will be added as time goes on
if found {
return found, filename, checkedRepo
}
}
return false, "", NotSupported
}
func (malrepo MalwareRepoType) VerifyRepoParams(repo RepositoryConfigEntry) bool {
switch malrepo {
case NotSupported:
return false
case MalwareBazaar:
if repo.Host != "" {
return true
}
case ObjectiveSee:
if repo.Host != "" {
return true
}
case AssemblyLine:
if repo.Host != "" && repo.Api != "" && repo.User != "" {
return true
}
case UploadAssemblyLine:
if repo.Host != "" && repo.Api != "" && repo.User != "" {
return true
}
default:
if repo.Host != "" && repo.Api != "" {
return true
}
}
return false
}
func (malrepo MalwareRepoType) CreateEntry() (RepositoryConfigEntry, error) {
var host string
var api string
var user string
tls := false
var default_url string
switch malrepo {
case NotSupported:
return RepositoryConfigEntry{}, fmt.Errorf("malware repository rype, %s, is not supported", malrepo.String())
case MalwareBazaar:
default_url = "https://mb-api.abuse.ch/api/v1/"
case Malshare:
default_url = "https://malshare.com"
case MWDB:
default_url = "https://mwdb.cert.pl/api"
case CapeSandbox:
default_url = "https://www.capesandbox.com/apiv2"
case JoeSandbox:
default_url = "https://jbxcloud.joesecurity.org/api/v2"
case InQuest:
default_url = "https://labs.inquest.net/api"
case HybridAnalysis:
default_url = "https://www.hybrid-analysis.com/api/v2"
case Triage:
default_url = "https://api.tria.ge/v0"
case VirusTotal:
default_url = "https://www.virustotal.com/api/v3"
case Polyswarm:
default_url = "https://api.polyswarm.network/v2"
case ObjectiveSee:
default_url = "https://objective-see.com/malware.json"
case UnpacMe:
default_url = "https://api.unpac.me/api/v1"
case Malpedia:
default_url = "https://malpedia.caad.fkie.fraunhofer.de/api"
case VxShare:
default_url = "https://virusshare.com/apiv2"
case FileScanIo:
default_url = "https://www.filescan.io/api"
case URLScanIO:
default_url = "https://urlscan.io/downloads"
case VirusExchange:
default_url = "https://virus.exchange/api"
}
if default_url != "" {
fmt.Printf("Enter Host [ Press enter for default - %s ]:\n", default_url)
} else {
fmt.Printf("Enter Host:\n")
}
fmt.Print(">> ")
fmt.Scanln(&host)
if host == "" {
fmt.Println("Using the default url")
host = default_url
}
if malrepo == AssemblyLine || malrepo == UploadAssemblyLine {
fmt.Println("Enter User Name:")
fmt.Print(">> ")
fmt.Scanln(&user)
for {
fmt.Println("Disable TLS Verification (true|false):")
fmt.Print(">> ")
var tlss string
fmt.Scanln(&tlss)
boolvalue, err := strconv.ParseBool(tlss)
if err == nil {
tls = boolvalue
break
}
fmt.Println("Invalid option entered")
}
}
if malrepo != MalwareBazaar && malrepo != ObjectiveSee {
fmt.Println("Enter API Key:")
fmt.Print(">> ")
fmt.Scanln(&api)
}
return RepositoryConfigEntry{Host: host, User: user, Api: api, Type: malrepo.String(), IgnoreTLSErrors: tls}, nil
}
func (malrepo MalwareRepoType) String() string {
switch malrepo {
case JoeSandbox:
return "JoeSandbox"
case MWDB:
return "MWDB"
case HybridAnalysis:
return "HybridAnalysis"
case CapeSandbox:
return "CapeSandbox"
case InQuest:
return "InQuest"
case MalwareBazaar:
return "MalwareBazaar"
case Triage:
return "Triage"
case Malshare:
return "Malshare"
case VirusTotal:
return "VirusTotal"
case Polyswarm:
return "Polyswarm"
case ObjectiveSee:
return "ObjectiveSee"
case UnpacMe:
return "UnpacMe"
case Malpedia:
return "Malpedia"
case VxShare:
return "VxShare"
case FileScanIo:
return "FileScanIo"
case URLScanIO:
return "URLScanIO"
case AssemblyLine:
return "AssemblyLine"
case UploadAssemblyLine:
return "UploadAssemblyLine"
case UploadMWDB:
return "UploadMWDB"
case VirusExchange:
return "VirusExchange"
}
return "NotSupported"
}
func allowedMalwareRepoTypes() {
for _, mr := range getMalwareRepoList() {
fmt.Printf(" %s\n", mr.String())
}
}
func printAllowedMalwareRepoTypeOptions() {
fmt.Println("")
for _, mr := range getMalwareRepoList() {
fmt.Printf(" [%d] %s\n", mr, mr.String())
}
}
func queryAndDownloadAll(repos []RepositoryConfigEntry, hash Hash, doNotExtract bool, skipUpload bool, osq ObjectiveSeeQuery, doNotValidateHash bool, doNotValidateHashList []MalwareRepoType) (bool, string, MalwareRepoType) {
found := false
filename := ""
checkedRepo := NotSupported
sort.Slice(repos[:], func(i, j int) bool {
return repos[i].QueryOrder < repos[j].QueryOrder
})
// Hack for now
// Due to Multiple entries of the same type, for each type instance in the config it will
// try to download for type the number of entries for type in config squared
// This array is meant to ensure that for each type it will only try it once
var completedTypes []MalwareRepoType
for _, repo := range repos {
if (repo.Type == UploadMWDB.String() || repo.Type == UploadAssemblyLine.String()) && skipUpload {
continue
}
mr := getMalwareRepoByName(repo.Type)
if !contains(completedTypes, mr) {
found, filename, checkedRepo = mr.QueryAndDownload(repos, hash, doNotExtract, osq)
if found {
if !doNotValidateHash {
if slices.Contains(doNotValidateHashList, checkedRepo) {
if checkedRepo == ObjectiveSee {
fmt.Printf(" [!] Not able to validate hash for repo %s\n", checkedRepo.String())
} else {
fmt.Printf(" [!] Not able to validate hash for repo %s when noextraction flag is set to %t\n", checkedRepo.String(), doNotExtractFlag)
}
break
} else {
valid, calculatedHash := hash.ValidateFile(filename)
if !valid {
fmt.Printf(" [!] Downloaded file hash %s\n does not match searched for hash %s\nTrying another source.\n", calculatedHash, hash.Hash)
deleteInvalidFile(filename)
continue
} else {
fmt.Printf(" [+] Downloaded file %s validated as the requested hash\n", hash.Hash)
break
}
}
}
break
}
completedTypes = append(completedTypes, mr)
}
}
return found, filename, checkedRepo
}
func getMalwareRepoByFlagName(name string) MalwareRepoType {
switch strings.ToLower(name) {
case strings.ToLower("js"):
return JoeSandbox
case strings.ToLower("md"):
return MWDB
case strings.ToLower("ha"):
return HybridAnalysis
case strings.ToLower("cs"):
return CapeSandbox
case strings.ToLower("iq"):
return InQuest
case strings.ToLower("mb"):
return MalwareBazaar
case strings.ToLower("tr"):
return Triage
case strings.ToLower("ms"):
return Malshare
case strings.ToLower("vt"):
return VirusTotal
case strings.ToLower("ps"):
return Polyswarm
case strings.ToLower("os"):
return ObjectiveSee
case strings.ToLower("um"):
return UnpacMe
case strings.ToLower("mp"):
return Malpedia
case strings.ToLower("vx"):
return VxShare
case strings.ToLower("fs"):
return FileScanIo
case strings.ToLower("us"):
return URLScanIO
case strings.ToLower("al"):
return AssemblyLine
case strings.ToLower("ve"):
return VirusExchange
}
return NotSupported
}
func getMalwareRepoByName(name string) MalwareRepoType {
switch strings.ToLower(name) {
case strings.ToLower("JoeSandbox"):
return JoeSandbox
case strings.ToLower("MWDB"):
return MWDB
case strings.ToLower("HybridAnalysis"):
return HybridAnalysis
case strings.ToLower("CapeSandbox"):
return CapeSandbox
case strings.ToLower("InQuest"):
return InQuest
case strings.ToLower("MalwareBazaar"):
return MalwareBazaar
case strings.ToLower("Triage"):
return Triage
case strings.ToLower("Malshare"):
return Malshare
case strings.ToLower("VirusTotal"):
return VirusTotal
case strings.ToLower("Polyswarm"):
return Polyswarm
case strings.ToLower("ObjectiveSee"):
return ObjectiveSee
case strings.ToLower("UnpacMe"):
return UnpacMe
case strings.ToLower("Malpedia"):
return Malpedia
case strings.ToLower("VxShare"):
return VxShare
case strings.ToLower("FileScanIo"):
return FileScanIo
case strings.ToLower("URLScanIO"):
return URLScanIO
case strings.ToLower("AssemblyLine"):
return AssemblyLine
case strings.ToLower("UploadAssemblyLine"):
return UploadAssemblyLine
case strings.ToLower("UploadMWDB"):
return UploadMWDB
case strings.ToLower("VirusExchange"):
return VirusExchange
}
return NotSupported
}
func getConfigsByType(repoType MalwareRepoType, repos []RepositoryConfigEntry) []RepositoryConfigEntry {
var filteredRepos []RepositoryConfigEntry
for _, v := range repos {
if v.Type == repoType.String() {
filteredRepos = append(filteredRepos, v)
}
}
return filteredRepos
}
type RepositoryConfigEntry struct {
Type string `yaml:"type"`
Host string `yaml:"url"`
Api string `yaml:"api"`
QueryOrder int `yaml:"queryorder"`
Password string `yaml:"pwd"`
User string `yaml:"user"`
IgnoreTLSErrors bool `yaml:"ignoretlserrors"`
}
func LoadConfig(filename string) ([]RepositoryConfigEntry, error) {
cfg, err := parseFile(filename)
if os.IsNotExist(err) {
fmt.Printf("%s does not exists. Creating...\n", filename)
filename, err = initConfig(filename)
if err != nil {
log.Fatal(err)
return nil, err
}
cfg, err = parseFile(filename)
if err != nil {
log.Fatal(err)
return nil, err
}
} else if err != nil {
log.Fatal(err)
return nil, err
}
return verifyConfig(cfg)
}
func verifyConfig(repos map[string]RepositoryConfigEntry) ([]RepositoryConfigEntry, error) {
var verifiedConfigRepos []RepositoryConfigEntry
for k, v := range repos {
mr := getMalwareRepoByName(v.Type)
if mr == NotSupported {
fmt.Printf("%s is not a supported type. Skipping...\n\nSupported types include:\n", v.Type)
allowedMalwareRepoTypes()
fmt.Println("")
} else {
valid := mr.VerifyRepoParams(v)
if !valid {
fmt.Printf(" Skipping %s (Type: %s, URL: %s, API: %s) as it's missing a parameter.\n", k, v.Type, v.Host, v.Api)
} else {
verifiedConfigRepos = append(verifiedConfigRepos, v)
}
}
}
return verifiedConfigRepos, nil
}
func parseFile(path string) (map[string]RepositoryConfigEntry, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return nil, err
}
f, err := os.ReadFile(path)
if err != nil {
fmt.Printf("%v", err)
return nil, err
}
data := make(map[string]RepositoryConfigEntry)
err = yaml.Unmarshal(f, &data)
if err != nil {
fmt.Printf("%v", err)
return nil, err
}
return data, nil
}
func AddToConfig(filename string) (string, error) {
repoConfigEntries, err := parseFile(filename)
if err != nil {
fmt.Printf("Error parsing %s - %v", filename, err)
return "", err
}
data, err := createNewEntries(0)
if err != nil {
fmt.Printf("Error creating new config entries : %v", err)
return "", err
}
finalConfigEntryList := make(map[string]RepositoryConfigEntry)
entryNumber := 0
// Add items from reporConfigEntries (pre-existing items) to the final list
for _, v := range repoConfigEntries {
finalConfigEntryList["repository "+fmt.Sprint(entryNumber)] = v
entryNumber++
}
// Add items not found in repoConfigEnties (the pre-existing items)
for _, v1 := range data {
found := false
for _, v2 := range repoConfigEntries {
if v1.Type == v2.Type && v1.Host == v1.Api {
found = true
}
}
if !found {
finalConfigEntryList["repository "+fmt.Sprint(entryNumber)] = v1
entryNumber++
}
}
return writeConfigToFile(filename, finalConfigEntryList)
}
func initConfig(filename string) (string, error) {
data, err := createNewEntries(0)
if err != nil {
fmt.Printf("Error creating new Repository Config Entries: %v\n", err)
return "", err
}
return writeConfigToFile(filename, data)
}
func createNewEntries(entryNumber int) (map[string]RepositoryConfigEntry, error) {
data := make(map[string]RepositoryConfigEntry)
var option int64
for {
fmt.Printf("\nEnter the corresponding Repository Config Entry number you want to add to .mlget.yml.\n")
fmt.Printf("Enter 0 to exit.\n")
printAllowedMalwareRepoTypeOptions()
fmt.Print(">> ")
fmt.Scan(&option)
if option > int64(NotSupported) && option <= int64(UploadMWDB) {
entry, err := MalwareRepoType(option).CreateEntry()
if err != nil {
continue
}
data["repository "+fmt.Sprint(entryNumber)] = entry
entryNumber++
} else if option == 0 {
break
}
}
return data, nil
}
func writeConfigToFile(filename string, repoConfigEntries map[string]RepositoryConfigEntry) (string, error) {
_, err := os.Stat(filename)
if err == nil {
os.Remove(filename)
}
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
fmt.Printf("error creating file: %v\n", err)
return "", err
}
defer file.Close()
enc := yaml.NewEncoder(file)
err = enc.Encode(repoConfigEntries)
if err != nil {
fmt.Printf("error encoding: %v\n", err)
return "", err
} else {
fmt.Printf("Config written to %s\n\n", file.Name())
}
return file.Name(), nil
}
func contains(list []MalwareRepoType, x MalwareRepoType) bool {
for _, item := range list {
if item == x {
return true
}
}
return false
}