Skip to content

Commit 1c68f3f

Browse files
Add check before scan feature to CLI
Related to VirusTotal#76 Adds a feature to check if a file is already scanned before initiating a new scan using the `--check-before-scan` flag with the `vt scan file` command. - Implements a new flag `--check-before-scan` in `cmd/scan.go` to enable the pre-check feature. - Modifies the `fileScanner` struct to include a `checkBeforeScan` boolean field. - Adds logic in the `fileScanner.Do` method to check if the file is already scanned by calculating the file's hash and querying it before proceeding with a new scan. - Updates the `README.md` to document the usage of the new `--check-before-scan` flag. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/VirusTotal/vt-cli/issues/76?shareId=c6d9605a-3f1b-4676-b6d0-c0ce931a9fbb).
1 parent 2068b95 commit 1c68f3f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,13 @@ $ vt url http://www.virustotal.com --include=_id,_type,**.result
375375
```
376376

377377
The `--exclude` option works similarly to `--include` but instead of including the matching fields in the output, it includes everything except the matching fields. You can use this option when you want to keep most of the fields, but leave out a few of them that are not interesting. If you use `--include` and `--exclude` simultaneously `--include` enters in action first, including only the fields that match the `--include` patterns, while `--exclude` comes in after that, removing any remaining field that matches the `--exclude` patterns.
378+
379+
### Scan a file and check if it's already scanned
380+
381+
To scan a file and check if it's already scanned before initiating a new scan, you can use the `--check-before-scan` flag with the `vt scan file` command. This feature allows you to save time and resources by avoiding unnecessary scans of files that have already been analyzed by VirusTotal.
382+
383+
```sh
384+
$ vt scan file <yourfile> --check-before-scan
385+
```
386+
387+
If the file has already been scanned, the CLI will output the existing analysis result without scanning the file again. If the file has not been scanned before, it will be uploaded for analysis, and the CLI will return the analysis ID as usual.

cmd/scan.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type fileScanner struct {
7878
showInVT bool
7979
waitForCompletion bool
8080
password string
81+
checkBeforeScan bool
8182
}
8283

8384
func (s *fileScanner) Do(path interface{}, ds *utils.DoerState) string {
@@ -101,6 +102,25 @@ func (s *fileScanner) Do(path interface{}, ds *utils.DoerState) string {
101102
}
102103
defer f.Close()
103104

105+
if s.checkBeforeScan {
106+
fileInfo, err := f.Stat()
107+
if err != nil {
108+
return err.Error()
109+
}
110+
fileSize := fileInfo.Size()
111+
hash, err := utils.GetFileHash(f, fileSize)
112+
if err != nil {
113+
return err.Error()
114+
}
115+
analysis, err := s.cli.GetObject(vt.URL("files/%s", hash))
116+
if err == nil {
117+
// If file is found, print the existing analysis result and return.
118+
s.printer.PrintObject(analysis)
119+
return ""
120+
}
121+
// If file is not found, proceed with the scan.
122+
}
123+
104124
var analysis *vt.Object
105125
if s.password != "" {
106126
analysis, err = s.scanner.ScanFileWithParameters(
@@ -181,6 +201,7 @@ func NewScanFileCmd() *cobra.Command {
181201
showInVT: viper.GetBool("open"),
182202
waitForCompletion: viper.GetBool("wait"),
183203
password: viper.GetString("password"),
204+
checkBeforeScan: viper.GetBool("check-before-scan"),
184205
printer: p,
185206
cli: client}
186207
c.DoWithStringsFromReader(s, argReader)
@@ -193,6 +214,7 @@ func NewScanFileCmd() *cobra.Command {
193214
addPasswordFlag(cmd.Flags())
194215
addWaitForCompletionFlag(cmd.Flags())
195216
addIncludeExcludeFlags(cmd.Flags())
217+
cmd.Flags().Bool("check-before-scan", false, "Check if the file is already scanned before scanning")
196218
cmd.MarkZshCompPositionalArgumentFile(1)
197219

198220
return cmd

0 commit comments

Comments
 (0)