-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
686 lines (582 loc) · 17.2 KB
/
main.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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
package main
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/malice-plugins/pkgs/utils"
"github.com/minio/minio-go"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
contagioDumpURL = "https://www.dropbox.com/sh/i6ed6v32x0fp94z/AAAQvOsOvbWrOs8T3_ZTXqQya?dl=1"
theZooURL = "https://github.com/ytisf/theZoo/archive/master.zip"
malwareSamplesURL = "https://github.com/fabrimagic72/malware-samples/archive/master.zip"
)
var (
// Version stores the plugin's version
Version string
// BuildTime stores the plugin's build time
BuildTime string
// local storage
outputDir string
// cloud storage
storageURL string
storageTLS bool
storageZone string
storageBucket string
storageID string
storageKey string
mc *minio.Client
)
// Copy copies a file from src to dst
func Copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
// FlattenDir copies and flattens a whole directory recursively
func FlattenDir(src string, dst string) error {
var err error
var fds []os.FileInfo
if fds, err = ioutil.ReadDir(src); err != nil {
return errors.Wrapf(err, "failed to read dir %s", src)
}
for _, fd := range fds {
srcfp := path.Join(src, fd.Name())
dstfp := path.Join(dst, fd.Name())
if fd.IsDir() {
if err = FlattenDir(srcfp, dst); err != nil {
log.Error(err)
}
} else {
// err := os.Rename(srcfp, dstfp)
_, err := Copy(srcfp, dstfp)
log.Debugf("wrote file to output folder: %s", dstfp)
if err != nil {
return errors.Wrap(err, "failed to copy file")
}
}
}
return nil
}
// PutDir puts all files into cloud storage
func PutDir(ctx context.Context, srcDir string) error {
var count uint64
var totalSize int64
err := filepath.Walk(srcDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrapf(err, "failed to get info on file: %s", path)
}
if !info.IsDir() {
n, err := mc.FPutObjectWithContext(ctx, storageBucket, filepath.Base(path), path, minio.PutObjectOptions{})
if err != nil {
return errors.Wrapf(err, "failed to write %s to cloud storage", filepath.Base(path))
}
count++
totalSize = totalSize + n
log.WithField("bytes", n).Debugf("wrote %s to cloud storage", filepath.Base(path))
}
return nil
})
if err != nil {
return errors.Wrapf(err, "failed to walk directory: %s", srcDir)
}
log.WithFields(log.Fields{
"count": count,
"total_size": humanize.Bytes(uint64(totalSize)),
}).Info("malware successfully sent to cloud storage")
return nil
}
func downloadFromURL(url string, tmpfile *os.File) error {
// Download file
log.WithField("url", url).Info("downloading file")
response, err := http.Get(url)
if err != nil {
return errors.Wrapf(err, "downloading %s failed", url)
}
defer response.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(response.Body)
if _, err := tmpfile.Write(buf.Bytes()); err != nil {
log.Fatal(err)
}
if err != nil {
return errors.Wrapf(err, "writing %s failed", tmpfile.Name())
}
return nil
}
func getPassword(fn string) string {
b := strings.TrimSuffix(fn, path.Ext(fn))
d, _ := base64.StdEncoding.DecodeString("aW5mZWN0ZWQ2NjY=")
return string(d) + b[len(b)-1:]
}
func findAllZips(dir string) ([]string, error) {
var zips []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dir, err)
return errors.Wrapf(err, "failure accessing a path %q", dir)
}
log.Debugf("visited file: %q", path)
if !info.IsDir() && (filepath.Ext(path) == ".zip" || filepath.Ext(path) == ".7z") {
zips = append(zips, path)
}
return nil
})
if err != nil {
return []string{}, errors.Wrapf(err, "failed to walk dir %s", dir)
}
return zips, nil
}
func unzip(ctx context.Context, path, password, outputFolder string) (string, error) {
var c *exec.Cmd
var args []string
if len(password) > 0 {
args = []string{"x", path, fmt.Sprintf("-p%s", password), fmt.Sprintf("-o%s", outputFolder), "-y"}
} else {
args = []string{"x", path, fmt.Sprintf("-o%s", outputFolder), "-y"}
}
if ctx != nil {
c = exec.CommandContext(ctx, "7z", args...)
} else {
c = exec.Command("7z", args...)
}
log.Debug("running command: ", c.Args)
output, err := c.Output()
if err != nil {
return string(output), errors.Wrap(err, "7zip extraction failed")
}
// check for exec context timeout
if ctx != nil {
if ctx.Err() == context.DeadlineExceeded {
return "", errors.Wrap(ctx.Err(), "command 7z x timed out")
}
}
return string(output), nil
}
func downloadAndUnzip(ctx context.Context, url, password string) error {
tmpfile, err := ioutil.TempFile("", "getmauled")
if err != nil {
return errors.Wrap(err, "failed to create tmp file")
}
defer os.Remove(tmpfile.Name()) // clean up
err = downloadFromURL(url, tmpfile)
if err != nil {
return errors.Wrapf(err, "downloading %s failed", url)
}
if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close tmp file")
}
tmpDir, err := ioutil.TempDir("", "getmauled")
if err != nil {
return errors.Wrap(err, "failed to create tmp directory")
}
defer os.RemoveAll(tmpDir)
out, err := unzip(ctx, tmpfile.Name(), password, tmpDir)
if err != nil {
return errors.Wrapf(err, "unzipping %s failed", url)
}
log.Debug(out)
if mc != nil {
return PutDir(ctx, tmpDir)
}
return FlattenDir(tmpDir, outputDir)
}
func exists(file string) bool {
for _, p := range strings.Split(os.Getenv("PATH"), ":") {
_, err := os.Stat(filepath.Join(p, file))
if err == nil {
return true
}
}
return false
}
// SetUpDestination configures the malware destination
func SetUpDestination() error {
var err error
if len(storageURL) > 0 {
mc, err = minio.New(storageURL, storageID, storageKey, storageTLS)
if err != nil {
return errors.Wrap(err, "unable to create s3/minio client")
}
err = mc.MakeBucket(storageBucket, storageZone)
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, err := mc.BucketExists(storageBucket)
if err == nil && exists {
log.Debugf("bucket %s already exists", storageBucket)
} else {
return errors.Wrapf(err, "unable to create bucket: %s", storageBucket)
}
} else {
log.Infof("successfully created bucket %s", storageBucket)
}
} else if len(outputDir) > 0 {
if _, err = os.Stat(outputDir); os.IsNotExist(err) {
return errors.Wrapf(err, "directory %s doesn't exist", outputDir)
}
} else {
outputDir, err = os.Getwd()
if err != nil {
return errors.Wrap(err, "unable to get current working directory")
}
}
return nil
}
func main() {
cli.AppHelpTemplate = utils.AppHelpTemplate
app := cli.NewApp()
app.Name = "get-mauled"
app.Author = "blacktop"
app.Email = "https://github.com/blacktop"
app.Version = Version + ", BuildTime: " + BuildTime
app.Compiled, _ = time.Parse("20060102", BuildTime)
app.Usage = "Malice DEMO Malware Downloader"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose, V",
Usage: "verbose output",
},
cli.BoolFlag{
Name: "proxy, x",
Usage: "proxy settings for Malice webhook endpoint",
EnvVar: "MALICE_PROXY",
},
cli.IntFlag{
Name: "timeout",
Value: 60 * 5,
Usage: "malice plugin timeout (in seconds)",
EnvVar: "MALICE_TIMEOUT",
},
cli.StringFlag{
Name: "output, o",
Usage: "set output directory",
EnvVar: "MALICE_OUTPUT_DIRECTORY",
Destination: &outputDir,
},
cli.StringFlag{
Name: "store-url",
Usage: "s3 or minio file server url",
EnvVar: "MALICE_STORAGE_URL",
Destination: &storageURL,
},
cli.BoolFlag{
Name: "store-tls",
Usage: "enable secure (HTTPS) access",
EnvVar: "MALICE_STORAGE_TLS",
Destination: &storageTLS,
},
cli.StringFlag{
Name: "store-zone",
Value: "us-east-1",
Usage: "s3 or minio availbility zone location",
EnvVar: "MALICE_STORAGE_ZONE",
Destination: &storageZone,
},
cli.StringFlag{
Name: "store-bucket",
Value: "malice",
Usage: "name of the minio or s3 bucket",
EnvVar: "MALICE_STORAGE_BUCKET",
Destination: &storageBucket,
},
cli.StringFlag{
Name: "store-id",
Usage: "user ID that uniquely identifies your account",
EnvVar: "MALICE_STORAGE_ID",
Destination: &storageID,
},
cli.StringFlag{
Name: "store-key",
Usage: "secret key is the password to your account",
EnvVar: "MALICE_STORAGE_KEY",
Destination: &storageKey,
},
}
app.Commands = []cli.Command{
{
Name: "all",
Aliases: []string{"a"},
Usage: "Gotta' Catch Em' All",
Action: func(c *cli.Context) error {
// var err error
// if c.GlobalBool("verbose") {
// log.SetLevel(log.DebugLevel)
// }
// if !exists("7z") {
// return fmt.Errorf("you need to install 7zip to use get-mauled")
// }
// ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.GlobalInt("timeout"))*time.Second)
// defer cancel()
// err = SetUpDestination()
// if err != nil {
// return errors.Wrap(err, "failed to setup malware destination")
// }
log.Error("this command hasn't been implimented yet")
return nil
},
},
{
Name: "the-zoo",
Aliases: []string{"z"},
Usage: "Download and Unzip The Zoo Malware",
Action: func(c *cli.Context) error {
var err error
if c.GlobalBool("verbose") {
log.SetLevel(log.DebugLevel)
}
if !exists("7z") {
return fmt.Errorf("you need to install 7zip to use get-mauled")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.GlobalInt("timeout"))*time.Second)
defer cancel()
err = SetUpDestination()
if err != nil {
return errors.Wrap(err, "failed to setup malware destination")
}
tmpfile, err := ioutil.TempFile("", "thezoo")
if err != nil {
return errors.Wrap(err, "failed to create tmp file")
}
err = downloadFromURL(theZooURL, tmpfile)
if err != nil {
return errors.Wrapf(err, "downloading %s failed", theZooURL)
}
if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close tmp file")
}
zipDir, err := ioutil.TempDir("", "thezoo_zip")
if err != nil {
return errors.Wrap(err, "failed to create tmp directory")
}
defer os.RemoveAll(zipDir)
out, err := unzip(ctx, tmpfile.Name(), "", zipDir)
if err != nil {
return errors.Wrapf(err, "unzipping %s failed", tmpfile.Name())
}
log.Debug(out)
os.Remove(tmpfile.Name())
log.Debugf("looking for zips in %s", zipDir)
zipFiles, err := findAllZips(filepath.Join(zipDir, "theZoo-master/malwares/Binaries"))
if err != nil {
return errors.Wrapf(err, "failed to find all zips in directory: %s", zipDir)
}
tmpDir, err := ioutil.TempDir("", "getmauled")
if err != nil {
return errors.Wrap(err, "failed to create tmp directory")
}
defer os.RemoveAll(tmpDir)
for _, zipFile := range zipFiles {
log.WithField("zip", zipFile).Debug("found zip")
out, _ := unzip(ctx, zipFile, "infected", tmpDir)
if err != nil {
return errors.Wrapf(err, "unzipping %s failed", zipFile)
}
log.Debug(out)
}
if mc != nil {
return PutDir(ctx, tmpDir)
}
return FlattenDir(tmpDir, outputDir)
},
},
{
Name: "contagio",
Aliases: []string{"c"},
Usage: "Download and Unzip contagiodump Malware",
Action: func(c *cli.Context) error {
var err error
if c.GlobalBool("verbose") {
log.SetLevel(log.DebugLevel)
}
if !exists("7z") {
return fmt.Errorf("you need to install 7zip to use get-mauled")
}
// increase timeout because it's downloading ~3GBs
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.GlobalInt("timeout"))*30*time.Second)
defer cancel()
err = SetUpDestination()
if err != nil {
return errors.Wrap(err, "failed to setup malware destination")
}
tmpfile, err := ioutil.TempFile("", "contagio")
if err != nil {
return errors.Wrap(err, "failed to create tmp file")
}
err = downloadFromURL(contagioDumpURL, tmpfile)
if err != nil {
return errors.Wrapf(err, "downloading %s failed", contagioDumpURL)
}
if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close tmp file")
}
zipDir, err := ioutil.TempDir("", "contagio_zip")
if err != nil {
return errors.Wrap(err, "failed to create tmp directory")
}
defer os.RemoveAll(zipDir)
out, err := unzip(ctx, tmpfile.Name(), "", zipDir)
if err != nil {
return errors.Wrapf(err, "unzipping %s failed", tmpfile.Name())
}
log.Debug(out)
os.Remove(tmpfile.Name())
log.Debugf("looking for zips in %s", zipDir)
zipFiles, err := findAllZips(zipDir)
if err != nil {
return errors.Wrapf(err, "failed to find all zips in directory: %s", zipDir)
}
tmpDir, err := ioutil.TempDir("", "getmauled")
if err != nil {
return errors.Wrap(err, "failed to create tmp directory")
}
defer os.RemoveAll(tmpDir)
for _, zipFile := range zipFiles {
log.WithField("zip", zipFile).Debug("found zip")
out, _ := unzip(ctx, zipFile, getPassword(zipFile), tmpDir)
if err != nil {
return errors.Wrapf(err, "unzipping %s failed", zipFile)
}
log.Debug(out)
}
if mc != nil {
return PutDir(ctx, tmpDir)
}
return FlattenDir(tmpDir, outputDir)
},
},
{
Name: "malware-samples",
Aliases: []string{"m"},
Usage: "Download and Unzip Malware Samples",
Action: func(c *cli.Context) error {
var err error
if c.GlobalBool("verbose") {
log.SetLevel(log.DebugLevel)
}
if !exists("7z") {
return fmt.Errorf("you need to install 7zip to use get-mauled")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.GlobalInt("timeout"))*time.Second)
defer cancel()
err = SetUpDestination()
if err != nil {
return errors.Wrap(err, "failed to setup malware destination")
}
tmpfile, err := ioutil.TempFile("", "malware_samples")
if err != nil {
return errors.Wrap(err, "failed to create tmp file")
}
err = downloadFromURL(malwareSamplesURL, tmpfile)
if err != nil {
return errors.Wrapf(err, "downloading %s failed", malwareSamplesURL)
}
if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close tmp file")
}
zipDir, err := ioutil.TempDir("", "malware_samples_zip")
if err != nil {
return errors.Wrap(err, "failed to create tmp directory")
}
defer os.RemoveAll(zipDir)
out, err := unzip(ctx, tmpfile.Name(), "", zipDir)
if err != nil {
return errors.Wrapf(err, "unzipping %s failed", tmpfile.Name())
}
log.Debug(out)
os.Remove(tmpfile.Name())
log.Debugf("looking for zips in %s", zipDir)
zipFiles, err := findAllZips(zipDir)
if err != nil {
return errors.Wrapf(err, "failed to find all zips in directory: %s", zipDir)
}
tmpDir, err := ioutil.TempDir("", "getmauled")
if err != nil {
return errors.Wrap(err, "failed to create tmp directory")
}
defer os.RemoveAll(tmpDir)
for _, zipFile := range zipFiles {
log.WithField("zip", zipFile).Debug("found zip")
out, _ := unzip(ctx, zipFile, "infected", tmpDir)
if err != nil {
return errors.Wrapf(err, "unzipping %s failed", zipFile)
}
log.Debug(out)
}
if mc != nil {
return PutDir(ctx, tmpDir)
}
return FlattenDir(tmpDir, outputDir)
},
},
{
Name: "download",
Aliases: []string{"d"},
Usage: "Download and Unzip Malware From URL",
ArgsUsage: "url-to-download",
Flags: []cli.Flag{
cli.StringFlag{
Name: "password, p",
Usage: "password of malware zip",
EnvVar: "MALICE_ZIP_PASSWORD",
},
},
Action: func(c *cli.Context) error {
var err error
if c.GlobalBool("verbose") {
log.SetLevel(log.DebugLevel)
}
if !exists("7z") {
return fmt.Errorf("you need to install 7zip to use get-mauled")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.GlobalInt("timeout"))*time.Second)
defer cancel()
err = SetUpDestination()
if err != nil {
return errors.Wrap(err, "failed to setup malware destination")
}
if c.Args().Present() {
err = downloadAndUnzip(ctx, c.Args().First(), c.String("password"))
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(fmt.Errorf("please supply a URL to download and unzip"))
}
return nil
},
},
}
err := app.Run(os.Args)
utils.Assert(err)
}