-
Notifications
You must be signed in to change notification settings - Fork 20
/
mlget.go
417 lines (358 loc) · 13.3 KB
/
mlget.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
package main
import (
"crypto"
"fmt"
"io"
"log"
"os"
"path"
"sync"
"time"
flag "github.com/spf13/pflag"
"golang.org/x/exp/slices"
)
var apiFlag string
var helpFlag bool
var checkConfFlag bool
var AddConfigEntryFlag bool
var doNotExtractFlag bool
var inputFileFlag string
var outputFileFlag bool
var uploadToMWDBAndDeleteFlag bool
var downloadOnlyFlag bool
var uploadToMWDBFlag bool
var readFromFileAndUpdateWithNotFoundHashesFlag string
var tagsFlag []string
var commentsFlag []string
var versionFlag bool
var noValidationFlag bool
var precheckdir bool
var uploadToAssemblyLineFlag bool
var uploadToAssemblyLineAndDeleteFlag bool
var forceResubmission bool
var version string = "3.4.1"
func usage() {
fmt.Println("mlget - A command line tool to download malware from a variety of sources")
fmt.Println("")
fmt.Printf("Usage: %s [OPTIONS] hash_arguments...\n", os.Args[0])
flag.PrintDefaults()
fmt.Println("")
fmt.Println("Example Usage: mlget <sha256> <md5> <sha1> <sha256>")
fmt.Println("Example Usage: mlget --from mb <sha256>")
fmt.Println("Example Usage: mlget --tag tag_one --tag tag_two --uploaddelete <sha256> <sha1> <md5>")
}
func init() {
flag.StringVar(&apiFlag, "from", "", "The service to download the malware from.\n Must be one of:\n - al (AssemblyLine)\n - cs (Cape Sandbox)\n - fs (FileScanIo)\n - ha (Hybird Anlysis)\n - iq (Inquest Labs)\n - js (Joe Sandbox)\n - mp (Malpedia)\n - ms (Malshare)\n - mb (Malware Bazaar)\n - mw (Malware Database)\n - os (Objective-See)\n - ps (PolySwarm)\n - tr (Triage)\n - um (UnpacMe)\n - us (URLScanIO)\n -ve (VExchange)\n - vt (VirusTotal)\n - vx (VxShare)\nIf omitted, all services will be tried.")
flag.StringVar(&inputFileFlag, "read", "", "Read in a file of hashes (one per line)")
flag.BoolVar(&outputFileFlag, "output", false, "Write to a file the hashes not found (requires using --read)")
flag.BoolVar(&helpFlag, "help", false, "Print the help message")
flag.BoolVar(&checkConfFlag, "config", false, "Parse and print the config file")
flag.BoolVar(&AddConfigEntryFlag, "addtoconfig", false, "Add entry to the config file")
flag.BoolVar(&doNotExtractFlag, "noextraction", false, "Do not extract malware from archive file.\nCurrently this only effects MalwareBazaar and HybridAnalysis")
flag.BoolVar(&uploadToMWDBFlag, "uploadmwdb", false, "Upload downloaded files to the Upload MWDB instances specified in the mlget.yml file.")
flag.BoolVar(&uploadToAssemblyLineFlag, "uploadal", false, "Upload downloaded files to the Upload AssemblyLine instances specified in the mlget.yml file.")
flag.StringVar(&readFromFileAndUpdateWithNotFoundHashesFlag, "readupdate", "", "Read hashes from file to download. Replace entries in the file with just the hashes that were not found (for next time).")
flag.BoolVar(&uploadToMWDBAndDeleteFlag, "uploaddeletemwdb", false, "Upload downloaded files to the Upload MWDB instance specified in the mlget.yml file.\nDelete the files after successful upload")
flag.BoolVar(&uploadToAssemblyLineAndDeleteFlag, "uploaddeleteal", false, "Upload downloaded files to the Upload AssemblyLine instances specified in the mlget.yml file.\nDelete the files after successful upload")
flag.BoolVar(&forceResubmission, "f", false, "Force resubmission to AssemblyLine when the files already exists on the AssemblyLine instance.")
flag.StringSliceVar(&tagsFlag, "tag", []string{}, "Tag the sample when uploading to your own instance of MWDB.")
flag.StringSliceVar(&commentsFlag, "comment", []string{}, "Add comment to the sample when uploading to your own instance of MWDB.")
flag.BoolVar(&downloadOnlyFlag, "downloadonly", false, "Download from any source, including your personal instance of MWDB.\nWhen this flag is set; it will NOT update any output file with the hashes not found.\nAnd it will not upload to any of the UploadMWDB.")
flag.BoolVar(&versionFlag, "version", false, "Print the version number")
flag.BoolVar(&noValidationFlag, "novalidation", false, "Turn off post download hash check verification")
flag.BoolVar(&precheckdir, "precheckdir", false, "Search current dir for files matching the hashes provided, if found don't redownload")
}
func main() {
doNotValidatehash := []MalwareRepoType{ObjectiveSee}
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
return
}
configFileName := path.Join(homeDir, ".mlget.yml")
cfg, err := LoadConfig(configFileName)
if err != nil {
fmt.Println(err)
return
}
flag.Parse()
if versionFlag {
fmt.Printf("Mlget version: %s\n", version)
return
}
if helpFlag {
usage()
return
}
if AddConfigEntryFlag {
AddToConfig(configFileName)
return
}
if checkConfFlag {
fmt.Printf("%+v", cfg)
return
}
args := flag.Args()
/*
if webserver {
runWebServer(ip, port)
} else {*/
downloadMalwareFromCLI(args, cfg, doNotValidatehash, precheckdir)
// }
}
func parseArgHashes(hashes []string, tags []string, comments []string) Hashes {
parsedHashes := Hashes{}
fmt.Printf("Hashes Passed Via the Command Line:\n")
for _, h := range hashes {
ht, err := hashType(h)
if err != nil {
fmt.Printf("\n Skipping %s because it's %s\n", h, err)
continue
}
fmt.Printf(" - %s\n", h) // token in unicode-char
hash := Hash{Hash: h, HashType: ht, Local: false}
if len(tags) > 0 {
hash.Tags = tags
}
if len(comments) > 0 {
hash.Comments = comments
}
parsedHashes, _ = addHash(parsedHashes, hash)
}
fmt.Println("")
return parsedHashes
}
func downloadMalwareFromCLI(args []string, cfg []RepositoryConfigEntry, doNotValidatehash []MalwareRepoType, precheckdir bool) {
if apiFlag != "" {
flaggedRepo := getMalwareRepoByFlagName(apiFlag)
if flaggedRepo == NotSupported {
fmt.Printf("Invalid or unsupported malware repo type: %s\nCheck the help for the values to pass to the --from parameter\n", apiFlag)
return
}
}
if apiFlag != "" && downloadOnlyFlag {
fmt.Printf(("Can't use both the --from flag and the --downloadonly flag together"))
return
}
hashes := parseArgHashes(args, tagsFlag, commentsFlag)
var err error
if inputFileFlag != "" {
hshs, err := parseFileForHashEntries(inputFileFlag)
if err != nil {
fmt.Printf("Error reading from %s\n", inputFileFlag)
fmt.Println(err)
} else {
for idx, hsh := range hshs {
if len(hshs) > 100 {
fmt.Printf("Adding Hash %d of %d\n", idx, len(hshs))
}
hashes, _ = addHash(hashes, hsh)
}
}
}
if readFromFileAndUpdateWithNotFoundHashesFlag != "" {
hshs, err := parseFileForHashEntries(readFromFileAndUpdateWithNotFoundHashesFlag)
if err != nil {
fmt.Printf("Error reading from %s\n", readFromFileAndUpdateWithNotFoundHashesFlag)
fmt.Println(err)
} else {
for idx, hsh := range hshs {
if len(hshs) > 100 {
fmt.Printf("Adding Hash %d of %d\n", idx, len(hshs))
}
hashes, _ = addHash(hashes, hsh)
}
}
}
var notFoundHashes Hashes
if len(hashes.Hashes) == 0 {
fmt.Println("No hashes found; displaying Help")
usage()
return
}
var osq ObjectiveSeeQuery
osConfigs := getConfigsByType(ObjectiveSee, cfg)
// Can have multiple Objective-See configs but only the first one to load will be used
for _, osc := range osConfigs {
osq, err = loadObjectiveSeeJson(osc.Host)
if err != nil {
fmt.Println("Unable to load Objective-See json data. Skipping...")
continue
}
fmt.Println("")
break
}
if doNotExtractFlag {
doNotValidatehash = append(doNotValidatehash, VxShare, MalwareBazaar, HybridAnalysis, FileScanIo)
}
if precheckdir {
files, err := os.ReadDir(".")
if err != nil {
fmt.Println(err)
}
filesHashing := 0
ch := make(chan Hash, len(files)*3)
var wg sync.WaitGroup
go func() {
wg.Wait()
close(ch)
}()
for _, file := range files {
if file.IsDir() {
continue
}
filesHashing++
wg.Add(1)
name := file.Name()
go func() {
defer wg.Done()
hashFileAndCheck(name, ch)
}()
}
fmt.Println("\nFiles Matching Hashes Found Locally:")
for i := range ch {
if hashes.hashExists(i.Hash) {
fmt.Printf(" - Found %s in current folder\n", i.Hash)
fmt.Printf(" File Name: %s\n", i.LocalFile)
hashes.updateLocalFile(i.Hash, i.LocalFile)
h, _ := hashes.getByHash(i.Hash)
if (uploadToMWDBFlag || uploadToMWDBAndDeleteFlag) && !downloadOnlyFlag {
err := UploadSampleToMWDBs(cfg, h.LocalFile, h, uploadToMWDBAndDeleteFlag)
if err != nil {
fmt.Printf(" ! %s\n", err)
}
}
if (uploadToAssemblyLineFlag || uploadToAssemblyLineAndDeleteFlag) && !downloadOnlyFlag {
err := UploadSampleToAssemblyLine(cfg, h.LocalFile, h, uploadToAssemblyLineAndDeleteFlag, forceResubmission)
if err != nil {
fmt.Printf(" ! %s\n", err)
}
}
}
}
}
for idx, h := range hashes.Hashes {
if h.Local {
continue
}
fmt.Printf("\nLook up %s (%s) - (%d of %d)\n", h.Hash, h.HashType, idx+1, len(hashes.Hashes))
if apiFlag != "" {
flaggedRepo := getMalwareRepoByFlagName(apiFlag)
fmt.Printf("Looking on %s\n", getMalwareRepoByFlagName(apiFlag))
found, filename, checkedRepo := flaggedRepo.QueryAndDownload(cfg, h, doNotExtractFlag, osq)
if !found {
fmt.Println(" [!] Not Found")
notFoundHashes, _ = addHash(notFoundHashes, h)
} else if found {
if !noValidationFlag {
if slices.Contains(doNotValidatehash, 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)
}
} else {
valid, calculatedHash := h.ValidateFile(filename)
if !valid {
fmt.Printf(" [!] Downloaded file hash %s\n does not match searched for hash %s\n", calculatedHash, h.Hash)
deleteInvalidFile(filename)
notFoundHashes, _ = addHash(notFoundHashes, h)
continue
} else {
fmt.Printf(" [+] Downloaded file %s validated as the requested hash\n", h.Hash)
}
}
}
if (uploadToMWDBFlag || uploadToMWDBAndDeleteFlag) && !downloadOnlyFlag {
err := UploadSampleToMWDBs(cfg, filename, h, uploadToMWDBAndDeleteFlag)
if err != nil {
fmt.Printf(" ! %s", err)
}
}
if (uploadToAssemblyLineFlag || uploadToAssemblyLineAndDeleteFlag) && !downloadOnlyFlag {
err := UploadSampleToAssemblyLine(cfg, filename, h, uploadToAssemblyLineAndDeleteFlag, forceResubmission)
if err != nil {
fmt.Printf(" ! %s", err)
}
}
}
} else {
fmt.Println("Querying all services")
found, filename, _ := queryAndDownloadAll(cfg, h, doNotExtractFlag, !downloadOnlyFlag, osq, noValidationFlag, doNotValidatehash)
if found {
if (uploadToMWDBFlag || uploadToMWDBAndDeleteFlag) && !downloadOnlyFlag {
err := UploadSampleToMWDBs(cfg, filename, h, uploadToMWDBAndDeleteFlag)
if err != nil {
fmt.Printf(" ! %s", err)
}
}
if (uploadToAssemblyLineFlag || uploadToAssemblyLineAndDeleteFlag) && !downloadOnlyFlag {
err := UploadSampleToAssemblyLine(cfg, filename, h, uploadToAssemblyLineAndDeleteFlag, forceResubmission)
if err != nil {
fmt.Printf(" ! %s", err)
}
}
continue
}
notFoundHashes, _ = addHash(notFoundHashes, h)
}
}
if len(notFoundHashes.Hashes) > 0 {
fmt.Printf("\nHashes not found!\n")
for i, s := range notFoundHashes.Hashes {
fmt.Printf(" %d: %s\n", i, s.Hash)
}
}
if !downloadOnlyFlag {
if readFromFileAndUpdateWithNotFoundHashesFlag != "" && !isValidUrl(readFromFileAndUpdateWithNotFoundHashesFlag) {
err := writeUnfoundHashesToFile(readFromFileAndUpdateWithNotFoundHashesFlag, notFoundHashes)
if err != nil {
fmt.Println("Error writing not found hashes to file")
fmt.Println(err)
}
fmt.Printf("\n\n%s refreshed to show only the hashes not found.\n", readFromFileAndUpdateWithNotFoundHashesFlag)
} else if outputFileFlag && len(notFoundHashes.Hashes) > 0 {
var filename string
if inputFileFlag != "" {
filename = time.Now().Format("2006-01-02__3_4_5__pm__") + inputFileFlag
} else {
filename = time.Now().Format("2006-01-02__3_4_5__pm") + "_not_found_hashes.txt"
}
err := writeUnfoundHashesToFile(filename, notFoundHashes)
if err != nil {
fmt.Println("Error writing unfound hashes to file")
fmt.Println(err)
}
fmt.Printf("\n\nUnfound hashes written to %s\n", filename)
} else if isValidUrl(readFromFileAndUpdateWithNotFoundHashesFlag) {
fmt.Println("File specified is a URL - can't update file. Please use --read and --output flags instead if you want to capture the hashes not found.")
}
}
}
func hashFileAndCheck(file string, c chan Hash) {
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
hasherMD5 := crypto.MD5.New()
if _, err := io.Copy(hasherMD5, f); err != nil {
log.Fatal(err)
}
sumMD5 := hasherMD5.Sum(nil)
c <- Hash{Hash: fmt.Sprintf("%x", sumMD5), HashType: md5, LocalFile: file}
f.Seek(0, 0)
hasherSHA1 := crypto.SHA1.New()
if _, err := io.Copy(hasherSHA1, f); err != nil {
log.Fatal(err)
}
sumSHA1 := hasherSHA1.Sum(nil)
c <- Hash{Hash: fmt.Sprintf("%x", sumSHA1), HashType: sha1, LocalFile: file}
f.Seek(0, 0)
hasherSHA256 := crypto.SHA256.New()
if _, err := io.Copy(hasherSHA256, f); err != nil {
log.Fatal(err)
}
sumSHA256 := hasherSHA256.Sum(nil)
c <- Hash{Hash: fmt.Sprintf("%x", sumSHA256), HashType: sha256, LocalFile: file}
}