The virustotal package provides access to the VirusTotal API v3, allowing you to scan files and URLs for malware, get domain and IP intelligence, and retrieve comprehensive threat analysis reports.
From CRAN:
Or the development version from GitHub, with pak:
Or set the VIRUSTOTAL_API_KEY environment variable (for
example in ~/.Renviron) and skip set_key()
entirely. The historical VirustotalToken variable is still
honored.
Scan a file for malware:
Get file analysis report:
## VirusTotal API Response
## ======================
##
## Type: File Report
## ID: 99017f6eebbac24f351415dd410d522d
## Resource Type: file
##
## Detection Summary:
## Malicious: 61
## Suspicious: 0
## Undetected: 4
## Harmless: 0
The raw response stays a list underneath:
scan_results <- report$data$attributes$last_analysis_results
detections <- sum(sapply(scan_results, function(x) x$category == "malicious"))Request file rescan:
Scan a URL:
Get URL analysis report:
The package paces itself to the public API’s allowance (4 requests
per minute) and retries transient failures (HTTP 429 and 503), honoring
the server’s Retry-After header. Four options control
this:
API failures are typed conditions, so specific failures can be handled without string-matching messages:
tryCatch(
file_report("0000000000000000000000000000000000000000"),
virustotal_rate_limit_error = function(e) {
message("Out of quota; retry after ", e$retry_after, "s")
},
virustotal_auth_error = function(e) message("Check the API key"),
virustotal_error = function(e) message("VT error: ", conditionMessage(e))
)The classes are virustotal_auth_error,
virustotal_rate_limit_error (with a
retry_after field),
virustotal_validation_error (bad input, thrown before any
network traffic), and their parent virustotal_error.