Skip to content

Commit

Permalink
Add --check-before-scan flag to scan command
Browse files Browse the repository at this point in the history
Related to VirusTotal#76

Adds a new `--check-before-scan` flag to the `vt scan file` command to enable checking if a file is already known to VirusTotal before uploading it for scanning. This feature aims to save resources by avoiding unnecessary scans of files that have already been analyzed.

- Introduces a new boolean flag `checkBeforeScan` in the `fileScanner` struct to store the state of the `--check-before-scan` flag.
- Modifies the `NewScanFileCmd` function to register the `--check-before-scan` flag and update the command's help and example texts to reflect the new functionality.
- Updates the `scanFileCmdHelp` and `scanFileCmdExample` variables with information about the new flag.
- Documentation in `README.md` and `doc/vt_scan_file.md` is updated to include the new `--check-before-scan` flag and its intended use.


---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/VirusTotal/vt-cli/issues/76?shareId=5e1b3985-b434-466e-8b93-41c586cbcbc6).
  • Loading branch information
PeterDaveHello committed May 22, 2024
1 parent 2068b95 commit 8ef047a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 59 deletions.
61 changes: 7 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ Restart the shell.
$ vt search "positives:5+ type:pdf" -i sha256,last_analysis_stats.malicious,tags --format json
```

* Scan a file and check if it is already known by VirusTotal before uploading it for scanning:

```sh
$ vt scan file <yourfile> --check-before-scan
```

## Getting only what you want

When you ask for information about a file, URL, domain, IP address or any other object in VirusTotal, you get a lot of data (by default in YAML format) that is usually more than what you need. You can narrow down the information shown by the vt-cli tool by using the `--include` and `--exclude` command-line options (`-i` and `-x` in short form).
Expand Down Expand Up @@ -321,57 +327,4 @@ $ vt url http://www.virustotal.com --include=last_http_response_headers.*
```

```sh
$ vt url http://www.virustotal.com --include=last_analysis_results.**
- last_analysis_results:
ADMINUSLabs:
category: "harmless"
engine_name: "ADMINUSLabs"
result: "clean"
AegisLab WebGuard:
category: "harmless"
engine_name: "AegisLab WebGuard"
result: "clean"
AlienVault:
category: "harmless"
engine_name: "AlienVault"
result: "clean"
```

```sh
$ vt url http://www.virustotal.com --include=last_analysis_results.*.result
- last_analysis_results:
ADMINUSLabs:
result: "clean"
AegisLab WebGuard:
result: "clean"
AlienVault:
result: "clean"
```

```sh
$ vt url http://www.virustotal.com --include=**.result
- last_analysis_results:
ADMINUSLabs:
result: "clean"
AegisLab WebGuard:
result: "clean"
AlienVault:
result: "clean"
```

Also notice that `_id` and `_type` are also field names and therefore you can use them in your filters:

```sh
$ vt url http://www.virustotal.com --include=_id,_type,**.result
- _id: "1db0ad7dbcec0676710ea0eaacd35d5e471d3e11944d53bcbd31f0cbd11bce31"
_type: "file"
last_analysis_results:
ADMINUSLabs:
result: "clean"
AegisLab WebGuard:
result: "clean"
AlienVault:
result: "clean"
```

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.
$ vt url http://www.virustotal.com --include=
9 changes: 8 additions & 1 deletion cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type fileScanner struct {
showInVT bool
waitForCompletion bool
password string
checkBeforeScan bool
}

func (s *fileScanner) Do(path interface{}, ds *utils.DoerState) string {
Expand Down Expand Up @@ -142,7 +143,9 @@ analysis is completed.
If the command receives a single hypen (-) the file paths are read from the standard
input, one per line.
The command can also receive a directory to scan all files contained on it.`
The command can also receive a directory to scan all files contained on it.
The --check-before-scan flag allows checking if the file is already known by VirusTotal before uploading it for scanning.`

var scanFileCmdExample = ` vt scan file foo.exe
vt scan file foo.exe bar.exe
Expand Down Expand Up @@ -181,6 +184,7 @@ func NewScanFileCmd() *cobra.Command {
showInVT: viper.GetBool("open"),
waitForCompletion: viper.GetBool("wait"),
password: viper.GetString("password"),
checkBeforeScan: viper.GetBool("check-before-scan"),
printer: p,
cli: client}
c.DoWithStringsFromReader(s, argReader)
Expand All @@ -194,6 +198,9 @@ func NewScanFileCmd() *cobra.Command {
addWaitForCompletionFlag(cmd.Flags())
addIncludeExcludeFlags(cmd.Flags())
cmd.MarkZshCompPositionalArgumentFile(1)
cmd.Flags().BoolP(
"check-before-scan", "c", false,
"Check if the file is already known by VirusTotal before uploading it for scanning.")

return cmd
}
Expand Down
10 changes: 6 additions & 4 deletions doc/vt_scan_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ input, one per line.

The command can also receive a directory to scan all files contained on it.

The `--check-before-scan` flag allows checking if the file is already known by VirusTotal before uploading it for scanning.

```
vt scan file [[dir] | [file]...] [flags]
```
Expand All @@ -32,9 +34,10 @@ vt scan file [[dir] | [file]...] [flags]
### Options

```
-h, --help help for file
-o, --open Return an URL to see the analysis report at the VirusTotal web GUI
-t, --threads int number of threads working in parallel (default 5)
-c, --check-before-scan Check if the file is already known by VirusTotal before uploading it for scanning.
-h, --help help for file
-o, --open Return an URL to see the analysis report at the VirusTotal web GUI
-t, --threads int number of threads working in parallel (default 5)
```

### Options inherited from parent commands
Expand All @@ -48,4 +51,3 @@ vt scan file [[dir] | [file]...] [flags]
### SEE ALSO

* [vt scan](vt_scan.md) - Scan files or URLs

0 comments on commit 8ef047a

Please sign in to comment.