-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add private scan support with code insight (#204)
Co-authored-by: Marta Gómez Macías <mgmacias@google.com>
- Loading branch information
1 parent
0fd07b8
commit 75f2e90
Showing
3 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
Tool for scanning files privately using VirusTotal API. | ||
Supports waiting for scan completion. | ||
""" | ||
|
||
import sys | ||
import asyncio | ||
import argparse | ||
from pathlib import Path | ||
import vt | ||
from rich.console import Console | ||
from rich.progress import Progress | ||
|
||
console = Console() | ||
|
||
async def scan_file_private( | ||
api_key: str, | ||
file_path: Path, | ||
wait: bool = False | ||
) -> None: | ||
""" | ||
Scan a file privately on VirusTotal. | ||
Args: | ||
api_key: VirusTotal API key | ||
file_path: Path to file to scan | ||
wait: Wait for scan completion | ||
""" | ||
async with vt.Client(api_key) as client: | ||
try: | ||
with Progress() as progress: | ||
task = progress.add_task( | ||
"Scanning file...", | ||
total=None if wait else 1 | ||
) | ||
|
||
analysis = await client.scan_file_private_async( | ||
str(file_path), | ||
wait_for_completion=wait | ||
) | ||
|
||
progress.update(task, advance=1) | ||
|
||
console.print("\n[green]Scan submitted successfully[/green]") | ||
console.print(f"Analysis ID: {analysis.id}") | ||
|
||
if wait: | ||
console.print(f"\nScan Status: {analysis.status}") | ||
if hasattr(analysis, 'stats'): | ||
console.print("Detection Stats:") | ||
for k, v in analysis.stats.items(): | ||
console.print(f" {k}: {v}") | ||
|
||
except vt.error.APIError as e: | ||
console.print(f"[red]API Error: {e}[/red]") | ||
except Exception as e: | ||
console.print(f"[red]Error: {e}[/red]") | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Scan file privately using VirusTotal API" | ||
) | ||
parser.add_argument("--apikey", help="VirusTotal API key") | ||
parser.add_argument("--file_path", help="Path to file to scan") | ||
parser.add_argument( | ||
"--wait", | ||
action="store_true", | ||
help="Wait for scan completion" | ||
) | ||
|
||
args = parser.parse_args() | ||
file_path = Path(args.file_path) | ||
|
||
if not file_path.exists(): | ||
console.print(f"[red]Error: File {file_path} not found[/red]") | ||
sys.exit(1) | ||
|
||
if not file_path.is_file(): | ||
console.print(f"[red]Error: {file_path} is not a file[/red]") | ||
sys.exit(1) | ||
|
||
asyncio.run(scan_file_private( | ||
args.apikey, | ||
file_path, | ||
args.wait | ||
)) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters