Skip to content

Commit

Permalink
feat(scan): Return report URL instead of analysis ID (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmacias95 committed Oct 13, 2022
1 parent 331af40 commit 6b82e3a
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"github.com/VirusTotal/vt-cli/utils"
vt "github.com/VirusTotal/vt-go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

type fileScanner struct {
scanner *vt.FileScanner
scanner *vt.FileScanner
showInVT bool
}

func (s *fileScanner) Do(path interface{}, ds *utils.DoerState) string {
Expand Down Expand Up @@ -53,6 +55,13 @@ func (s *fileScanner) Do(path interface{}, ds *utils.DoerState) string {
return fmt.Sprintf("%s", err)
}

if s.showInVT {
// Return the analysis URL in VT so users can visit it
return fmt.Sprintf(
"%s https://www.virustotal.com/gui/file-analysis/%s",
path.(string), analysis.ID())
}

return fmt.Sprintf("%s %s", path.(string), analysis.ID())
}

Expand Down Expand Up @@ -96,27 +105,37 @@ func NewScanFileCmd() *cobra.Command {
if err != nil {
return err
}
s := &fileScanner{scanner: client.NewFileScanner()}
s := &fileScanner{
scanner: client.NewFileScanner(),
showInVT: viper.GetBool("open")}
c.DoWithStringsFromReader(s, argReader)
return nil
},
}

addThreadsFlag(cmd.Flags())
addOpenInVTFlag(cmd.Flags())
cmd.MarkZshCompPositionalArgumentFile(1)

return cmd
}

type urlScanner struct {
scanner *vt.URLScanner
scanner *vt.URLScanner
showInVT bool
}

func (s *urlScanner) Do(url interface{}, ds *utils.DoerState) string {
analysis, err := s.scanner.Scan(url.(string))
if err != nil {
return fmt.Sprintf("%s", err)
}

if s.showInVT {
return fmt.Sprintf(
"%s https://www.virustotal.com/gui/url-analysis/%s", url, analysis.ID())
}

return fmt.Sprintf("%s %s", url, analysis.ID())
}

Expand Down Expand Up @@ -155,13 +174,16 @@ func NewScanURLCmd() *cobra.Command {
if err != nil {
return err
}
s := &urlScanner{scanner: client.NewURLScanner()}
s := &urlScanner{
scanner: client.NewURLScanner(),
showInVT: viper.GetBool("open")}
c.DoWithStringsFromReader(s, argReader)
return nil
},
}

addThreadsFlag(cmd.Flags())
addOpenInVTFlag(cmd.Flags())

return cmd
}
Expand All @@ -184,3 +206,9 @@ func NewScanCmd() *cobra.Command {

return cmd
}

func addOpenInVTFlag(flags *pflag.FlagSet) {
flags.BoolP(
"open", "o", false,
"Return an URL to see the analysis report at the VirusTotal web GUI")
}

0 comments on commit 6b82e3a

Please sign in to comment.