-
Notifications
You must be signed in to change notification settings - Fork 9
/
virustotal.go
513 lines (438 loc) · 12.7 KB
/
virustotal.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/levigross/grequests"
"github.com/malice-plugins/pkgs/database"
"github.com/malice-plugins/pkgs/database/elasticsearch"
"github.com/malice-plugins/pkgs/utils"
"github.com/mitchellh/mapstructure"
"github.com/parnurzeal/gorequest"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
const (
name = "virustotal"
category = "intel"
)
var (
// Version stores the plugin's version
Version string
// BuildTime stores the plugin's build time
BuildTime string
hash string
// es is the elasticsearch database object
es elasticsearch.Database
)
// VirusTotal is a type
type VirusTotal struct {
Data interface{} `json:"data" structs:"data"`
Now string `json:"now" structs:"now"`
}
type pluginResults struct {
ID string `json:"id" structs:"id,omitempty"`
VT VirusTotal `json:"virustotal" structs:"virustotal"`
}
// virustotal json object
type virustotal struct {
Results ResultsData `json:"virustotal"`
}
// ResultsData json object
type ResultsData struct {
Scans map[string]Scan `json:"scans"`
Permalink string `json:"permalink"`
Resource string `json:"resource"`
ResponseCode int `json:"response_code" mapstructure:"response_code"`
Total int `json:"total"`
Positives int `json:"positives"`
ScanID string `json:"scan_id" mapstructure:"scan_id"`
ScanDate string `json:"scan_date" mapstructure:"scan_date"`
FirstSeen string `json:"first_seen" mapstructure:"first_seen"`
VerboseMsg string `json:"verbose_msg" mapstructure:"verbose_msg"`
MD5 string `json:"md5"`
Sha1 string `json:"sha1"`
Sha256 string `json:"sha256"`
Ratio string `json:"ratio"`
}
// Scan is a VirusTotal AV scan JSON object
type Scan struct {
Detected bool `json:"detected"`
Version string `json:"version"`
Result string `json:"result"`
Update string `json:"update"`
}
// ScanResults json object
type ScanResults struct {
Permalink string `json:"permalink"`
Resource string `json:"resource"`
ResponseCode int `json:"response_code" mapstructure:"response_code"`
ScanID string `json:"scan_id" mapstructure:"scan_id"`
VerboseMsg string `json:"verbose_msg" mapstructure:"verbose_msg"`
MD5 string `json:"md5"`
Sha1 string `json:"sha1"`
Sha256 string `json:"sha256"`
}
type bitly struct {
StatusCode int `json:"status_code"`
StatusTxt string `json:"status_txt"`
Data bitlyData `json:"data"`
}
type bitlyData struct {
LongURL string `json:"long_url"`
URL string `json:"url"`
NewHash int `json:"new_hash"`
Hash string `json:"hash"`
GlobalHash string `json:"global_hash"`
}
var apikey string
func assert(err error) {
if err != nil {
log.WithFields(log.Fields{
"plugin": name,
"category": category,
"hash": hash,
}).Fatal(err)
}
}
// scanFile uploads file to virustotal
func scanFile(path string, apikey string) map[string]interface{} {
// fmt.Println("Uploading file to virustotal...")
fd, err := grequests.FileUploadFromDisk(path)
if err != nil {
log.Println("Unable to open file: ", err)
}
// This will upload the file as a multipart mime request
resp, err := grequests.Post("https://www.virustotal.com/vtapi/v2/file/scan",
&grequests.RequestOptions{
Files: fd,
Params: map[string]string{
"apikey": apikey,
// "notify_url": notify_url,
// "notify_changes_only": bool,
},
})
if err != nil {
log.Println("Unable to make request", resp.Error)
}
if resp.Ok != true {
log.Println("Request did not return OK")
}
fmt.Println(resp.String())
var scanResults ScanResults
resp.JSON(&scanResults)
// fmt.Printf("%#v", scanResults)
// TODO: wait for an hour!?!?!? or create a notify URL endpoint?!?!?!
ro := &grequests.RequestOptions{
Params: map[string]string{
"resource": scanResults.Sha256,
"scan_id": scanResults.ScanID,
"apikey": apikey,
"allinfo": "1",
},
}
resp, err = grequests.Get("https://www.virustotal.com/vtapi/v2/file/report", ro)
if err != nil {
log.Fatalln("Unable to make request: ", err)
}
if resp.Ok != true {
log.Println("Request did not return OK")
}
// fmt.Println(resp.String())
var results map[string]interface{}
resp.JSON(&results)
return results
}
// lookupHash retreieves the virustotal file report for the given hash
func lookupHash(hash string, apikey string) map[string]interface{} {
// NOTE: https://godoc.org/github.com/levigross/grequests
// fmt.Println("Getting virustotal report...")
ro := &grequests.RequestOptions{
Params: map[string]string{
"resource": hash,
"apikey": apikey,
"allinfo": "1",
},
}
resp, err := grequests.Get("https://www.virustotal.com/vtapi/v2/file/report", ro)
if err != nil {
log.Fatalln("Unable to make request: ", err)
}
if resp.StatusCode == 204 {
log.Fatalln("Used more than 4 queries per minute")
}
if resp.Ok != true {
log.Println("Request did not return OK")
}
var results map[string]interface{}
resp.JSON(&results)
// resp.JSON(&vtResult)
// var vtResult ResultsData
// err = mapstructure.Decode(results.Data, &vtResult)
// assert(err)
// vtJSON, err := json.Marshal(vtResult)
// assert(err)
// // write to stdout
// fmt.Println(string(vtJSON))
// results.Now = time.Now().Format(time.RFC3339Nano)
// t, _ := time.Parse("2006-01-02 15:04:05", vtResult.ScanDate)
// vtResult.ScanDate = t.Format("Mon 2006Jan02 15:04:05")
return results
}
func getRatio(positives int, total int) string {
ratio := 100.0 * float64(positives) / float64(total)
return fmt.Sprintf("%.f%%", ratio)
}
func shortenPermalink(longURL string) string {
// NOTE: http://dev.bitly.com/api.html
// https://github.com/bitly/go-simplejson
var btl bitly
ro := &grequests.RequestOptions{
Params: map[string]string{
"access_token": "23382325dd472aed14518ec5b8c8f4c2293e114a",
"longUrl": longURL,
},
}
resp, err := grequests.Get("https://api-ssl.bitly.com/v3/shorten", ro)
if err != nil {
log.Fatalln("Unable to make request: ", err)
}
fmt.Println(resp.String())
resp.JSON(&btl)
return btl.Data.URL
}
func generateMarkDownTable(virustotal map[string]interface{}) string {
var tplOut bytes.Buffer
var vt ResultsData
err := mapstructure.Decode(virustotal, &vt)
assert(err)
// calculate ratio
vt.Ratio = getRatio(vt.Positives, vt.Total)
t := template.Must(template.New("vt").Parse(tpl))
err = t.Execute(&tplOut, vt)
if err != nil {
log.Println("executing template:", err)
}
return tplOut.String()
}
func printStatus(resp gorequest.Response, body string, errs []error) {
fmt.Println(body)
}
func webService() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/scan", webAvScan).Methods("POST")
router.HandleFunc("/lookup/{hash}", webAvLookup)
log.WithFields(log.Fields{
"plugin": name,
"category": category,
}).Info("web service listening on port :3993")
log.Fatal(http.ListenAndServe(":3993", router))
}
func webAvScan(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(32 << 20)
file, header, err := r.FormFile("malware")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "Please supply a valid file to scan.")
log.WithFields(log.Fields{
"plugin": name,
"category": category,
}).Error(err)
}
defer file.Close()
log.WithFields(log.Fields{
"plugin": name,
"category": category,
}).Debug("Uploaded fileName: ", header.Filename)
tmpfile, err := ioutil.TempFile("/malware", "web_")
assert(err)
defer os.Remove(tmpfile.Name()) // clean up
data, err := ioutil.ReadAll(file)
assert(err)
if _, err = tmpfile.Write(data); err != nil {
assert(err)
}
if err = tmpfile.Close(); err != nil {
assert(err)
}
// Do AV scan
path := tmpfile.Name()
virustotal := scanFile(path, apikey)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(virustotal); err != nil {
assert(err)
}
}
func webAvLookup(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hash = vars["hash"]
hashType, _ := utils.GetHashType(hash)
if !strings.EqualFold(hashType, "md5") && !strings.EqualFold(hashType, "sha1") && !strings.EqualFold(hashType, "sha256") {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "please supply a proper MD5/SHA1/SHA256 hash to query")
return
}
// Do AV scan
virustotal := lookupHash(hash, apikey)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(virustotal); err != nil {
assert(err)
}
}
func main() {
cli.AppHelpTemplate = utils.AppHelpTemplate
app := cli.NewApp()
app.Name = "virustotal"
app.Author = "blacktop"
app.Email = "https://github.com/blacktop"
app.Version = Version + ", BuildTime: " + BuildTime
app.Compiled, _ = time.Parse("20060102", BuildTime)
app.Usage = "Malice VirusTotal Plugin"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose, V",
Usage: "verbose output",
},
cli.StringFlag{
Name: "api",
Value: "",
Usage: "VirusTotal API key",
EnvVar: "MALICE_VT_API",
Destination: &apikey,
},
}
app.Commands = []cli.Command{
{
Name: "scan",
Aliases: []string{"s"},
Usage: "Upload binary to VirusTotal for scanning",
ArgsUsage: "FILE to upload to VirusTotal",
Action: func(c *cli.Context) error {
// Check for valid apikey
if apikey == "" {
log.Fatal(fmt.Errorf("please supply a valid MALICE_VT_API key with the flag '--api'"))
}
if c.GlobalBool("verbose") {
log.SetLevel(log.DebugLevel)
}
if c.Args().Present() {
path := c.Args().First()
// Check that file exists
if _, err := os.Stat(path); os.IsNotExist(err) {
assert(err)
}
// upload file to virustotal.com
scanFile(path, apikey)
} else {
log.Fatal(fmt.Errorf("please supply a file to upload to virustotal.com"))
}
return nil
},
},
{
Name: "lookup",
Aliases: []string{"l"},
Usage: "Get file hash scan report",
ArgsUsage: "MD5/SHA1/SHA256 hash of file",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "post, p",
Usage: "POST results to Malice webhook",
EnvVar: "MALICE_ENDPOINT",
},
cli.BoolFlag{
Name: "proxy, x",
Usage: "proxy settings for Malice webhook endpoint",
EnvVar: "MALICE_PROXY",
},
cli.BoolFlag{
Name: "table, t",
Usage: "output as Markdown table",
},
cli.StringFlag{
Name: "elasticsearch",
Value: "",
Usage: "elasticsearch url for Malice to store results",
EnvVar: "MALICE_ELASTICSEARCH_URL",
Destination: &es.URL,
},
},
Action: func(c *cli.Context) error {
// Check for valid apikey
if apikey == "" {
log.Fatal(fmt.Errorf("please supply a valid MALICE_VT_API key with the flag '--api'"))
}
if c.GlobalBool("verbose") {
log.SetLevel(log.DebugLevel)
}
if c.Args().Present() {
hash := c.Args().First()
vtReport := lookupHash(hash, apikey)
vtReport["markdown"] = generateMarkDownTable(vtReport)
// upsert into Database
if len(c.String("elasticsearch")) > 0 {
err := es.Init()
if err != nil {
return errors.Wrap(err, "failed to initalize elasticsearch")
}
err = es.StorePluginResults(database.PluginResults{
ID: utils.Getopt("MALICE_SCANID", hash),
Name: name,
Category: category,
Data: vtReport,
})
if err != nil {
return errors.Wrapf(err, "failed to index malice/%s results", name)
}
}
if c.Bool("table") {
fmt.Println(vtReport["markdown"])
} else {
vtJSON, err := json.Marshal(vtReport)
assert(err)
if c.Bool("post") {
request := gorequest.New()
if c.Bool("proxy") {
request = gorequest.New().Proxy(os.Getenv("MALICE_PROXY"))
}
request.Post(os.Getenv("MALICE_ENDPOINT")).
Set("X-Malice-ID", utils.Getopt("MALICE_SCANID", hash)).
Send(string(vtJSON)).
End(printStatus)
return nil
}
// write to stdout
fmt.Println(string(vtJSON))
}
} else {
log.Fatal(fmt.Errorf("please supply a MD5/SHA1/SHA256 hash to query"))
}
return nil
},
},
{
Name: "web",
Usage: "Create a VirusTotal scan web service",
Action: func(c *cli.Context) error {
// Check for valid apikey
if apikey == "" {
log.Fatal(fmt.Errorf("please supply a valid MALICE_VT_API key with the flag '--api'"))
}
webService()
return nil
},
},
}
err := app.Run(os.Args)
assert(err)
}