Skip to content

Commit

Permalink
Merge pull request #2 from moldabekov/dev
Browse files Browse the repository at this point in the history
JSON support
  • Loading branch information
moldabekov authored Jan 18, 2018
2 parents f51d7bc + c451fc1 commit 2ce2608
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 57 deletions.
61 changes: 37 additions & 24 deletions filescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/moldabekov/virusgotal/vt"
"github.com/fatih/color"
"encoding/json"
)

func sha256sum(filename string) string {
Expand All @@ -24,25 +25,34 @@ func sha256sum(filename string) string {
}

func printFileResult(result *govt.FileReport) {
color.Set(color.FgHiYellow)
fmt.Printf("%s file scan results:\n", *filename)
if !*waitFile {
fmt.Printf("sha256 hashsum: %s\n", result.Sha256)
fmt.Printf("VirusTotal link: %s\n\n", result.Permalink)
}
color.Set(color.FgHiCyan)
fmt.Printf("Detection ratio: %v/%v\n\n", result.Positives, result.Total)
for i := range result.Scans {
if result.Scans[i].Detected {
color.Set(color.FgHiRed, color.Bold)
fmt.Printf("AV: %s\nResult: %s\n\n", i, result.Scans[i].Result)
} else {
color.Set(color.FgHiGreen, color.Bold)
fmt.Printf("AV: %s\nDetected: %t\n\n", i, result.Scans[i].Detected)
if (!*jsonFile) && (!*jsonHash) {
color.Set(color.FgHiYellow)
if len(*filename) > 0 {
fmt.Printf("%s file scan results:\n", *filename)
}
if !*waitFile {
fmt.Printf("sha256 hashsum: %s\n", result.Sha256)
fmt.Printf("VirusTotal link: %s\n\n", result.Permalink)
}
color.Set(color.FgHiCyan)
fmt.Printf("Detection ratio: %v/%v\n\n", result.Positives, result.Total)
for i := range result.Scans {
if result.Scans[i].Detected {
color.Set(color.FgHiRed, color.Bold)
fmt.Printf("AV: %s\nResult: %s\n\n", i, result.Scans[i].Result)
} else {
color.Set(color.FgHiGreen, color.Bold)
fmt.Printf("AV: %s\nDetected: %t\n\n", i, result.Scans[i].Detected)
}
}
color.Unset()
os.Exit(0)
} else {
j, err := json.MarshalIndent(result, "", " ")
check(err)
os.Stdout.Write(j)
os.Exit(0)
}
color.Unset()
os.Exit(0)
}

func scanFile(filename string) {
Expand All @@ -59,7 +69,6 @@ func scanFile(filename string) {
// If file was previously scanned print results
switch r.Status.ResponseCode {
case 1: // Results exist
fmt.Printf("%d", r.Status.ResponseCode)
printFileResult(r)
case -2: // Scan in progress
color.Set(color.FgHiRed)
Expand All @@ -73,18 +82,22 @@ func scanFile(filename string) {
report, err := vt.ScanFile(filename)
check(err)
color.Set(color.FgHiGreen, color.Bold)
fmt.Printf("Your file was submitted and scan was queued. Here are details:\n\n")
color.Set(color.Reset, color.FgHiCyan)
fmt.Printf("sha256 hash: %s\n", report.Sha256)
fmt.Printf("VirusTotal link: %s\n\n", report.Permalink)
color.Unset()
if !*jsonFile && !*jsonHash {
fmt.Printf("Your file was submitted and scan was queued. Here are details:\n\n")
color.Set(color.Reset, color.FgHiCyan)
fmt.Printf("sha256 hash: %s\n", report.Sha256)
fmt.Printf("VirusTotal link: %s\n\n", report.Permalink)
color.Unset()
}
if *waitFile { // Wait for results if user wishes
for m := 0; m <= 600; m += 30 { // m == minutes
loader(fmt.Sprintf("waiting for results for %d seconds", m))
r, err := vt.GetFileReport(sha256sum(filename))
check(err)
if r.Status.ResponseCode == 1 {
fmt.Printf("scan took ~ %d seconds\n", m)
if !*jsonFile && !*jsonHash {
fmt.Printf("scan took ~ %d seconds\n", m)
}
printFileResult(r)
}
}
Expand Down
22 changes: 13 additions & 9 deletions hashsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ func searchHash(hash string) {
fmt.Printf("Given hash isn't recognized by VirusTotal\n")
color.Unset()
os.Exit(1)
} else {
if r.Positives > 0 {
color.Set(color.FgHiRed)
fmt.Printf("\nGiven hash [%d/%d] is KNOWN by VirusTotal and has positive results\n", r.Positives, r.Total)
color.Unset()
} else {
color.Set(color.FgHiGreen)
fmt.Printf("\nGiven hash is KNOWN by VirusTotal and has no positive results\n", r.Positives, r.Total)
color.Unset()
}
if r.Positives > 0 {
color.Set(color.FgHiRed)
if !*jsonHash {
fmt.Printf("\nGiven hash is KNOWN by VirusTotal and has positive results [%d/%d]\n", r.Positives, r.Total)
}
printFileResult(r)
color.Unset()
} else {
color.Set(color.FgHiGreen)
fmt.Printf("\nGiven hash is KNOWN by VirusTotal and has no positive results\n", r.Positives, r.Total)
color.Unset()
}
if !*jsonHash {
fmt.Printf("Direct link: %s\n\n", r.Permalink)
}
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ var (
filename = filescan.Arg("FILE", "File to scan").Required().String()
forceFile = filescan.Flag("force", "rescan file").Bool()
waitFile = filescan.Flag("wait", "wait for results").Bool()
jsonFile = filescan.Flag("json","export results to JSON").Bool()

urlscan = app.Command("url", "URL scanning mode")
urlname = urlscan.Arg("URL", "URL to scan").Required().String()
forceUrl = urlscan.Flag("force", "rescan URL").Bool()
waitUrl = urlscan.Flag("wait", "wait for results").Bool()
jsonUrl = urlscan.Flag("json","export results to JSON").Bool()

hashscan = app.Command("hash", "Search files by hash")
hash = hashscan.Arg("HASH", "SHA1/SHA256/MD5 hash").Required().String()
jsonHash = hashscan.Flag("json","export results to JSON").Bool()
)

func main() {
Expand Down
59 changes: 35 additions & 24 deletions urlscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@ import (
"github.com/fatih/color"
"fmt"
"os"
"encoding/json"
)

func printUrlResult(result *govt.UrlReport) {
color.Set(color.FgHiYellow)
fmt.Printf("%s scan results:\n", *urlname)
if !*waitUrl {
fmt.Printf("VirusTotal link: %s\n\n", result.Permalink)
}
color.Set(color.FgHiCyan)
fmt.Printf("Detection ratio: %v/%v\n\n", result.Positives, result.Total)
for i := range result.Scans {
if result.Scans[i].Detected {
color.Set(color.FgHiRed, color.Bold)
fmt.Printf("AV: %s\nResult: %s\n\n", i, result.Scans[i].Result)
} else {
color.Set(color.FgHiGreen, color.Bold)
fmt.Printf("AV: %s\nDetected: %t\n\n", i, result.Scans[i].Detected)
if !*jsonUrl {
color.Set(color.FgHiYellow)
fmt.Printf("%s scan results:\n", *urlname)
if !*waitUrl {
fmt.Printf("VirusTotal link: %s\n\n", result.Permalink)
}
color.Set(color.FgHiCyan)
fmt.Printf("Detection ratio: %v/%v\n\n", result.Positives, result.Total)
for i := range result.Scans {
if result.Scans[i].Detected {
color.Set(color.FgHiRed, color.Bold)
fmt.Printf("AV: %s\nResult: %s\n\n", i, result.Scans[i].Result)
} else {
color.Set(color.FgHiGreen, color.Bold)
fmt.Printf("AV: %s\nDetected: %t\n\n", i, result.Scans[i].Detected)
}
}
color.Unset()
os.Exit(0)
} else {
j, err := json.MarshalIndent(result, "", " ")
check(err)
os.Stdout.Write(j)
os.Exit(0)
}
color.Unset()
os.Exit(0)
}

func scanUrl(urlname string) {
Expand All @@ -42,7 +50,6 @@ func scanUrl(urlname string) {
// If file was previously scanned print results
switch r.Status.ResponseCode {
case 1: // Results exist
fmt.Printf("%d", r.Status.ResponseCode)
printUrlResult(r)
case -2: // Scan in progress
color.Set(color.FgHiRed)
Expand All @@ -56,19 +63,23 @@ func scanUrl(urlname string) {
//if !*waitUrl {
report, err := vt.ScanUrl(urlname)
check(err)
color.Set(color.FgHiGreen, color.Bold)
fmt.Printf("Your URL was submitted and scan was queued. Here are details:\n\n")
color.Set(color.Reset, color.FgHiCyan)
fmt.Printf("Link: %s\n", report.Url)
fmt.Printf("VirusTotal link: %s\n\n", report.Permalink)
color.Unset()
if !*jsonUrl {
color.Set(color.FgHiGreen, color.Bold)
fmt.Printf("Your URL was submitted and scan was queued. Here are details:\n\n")
color.Set(color.Reset, color.FgHiCyan)
fmt.Printf("Link: %s\n", report.Url)
fmt.Printf("VirusTotal link: %s\n\n", report.Permalink)
color.Unset()
}
if *waitUrl { // Wait for results if user wishes
for m := 0; m <= 600; m += 30 {
loader(fmt.Sprintf("waiting for results for %d seconds", m))
r, err := vt.GetUrlReport(urlname)
check(err)
if r.Status.ResponseCode == 1 {
fmt.Printf("scan took ~ %d seconds\n", m)
if !*jsonUrl {
fmt.Printf("scan took ~ %d seconds\n", m)
}
printUrlResult(r)
}
}
Expand Down

0 comments on commit 2ce2608

Please sign in to comment.