-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirusTotal.py
69 lines (63 loc) · 2.33 KB
/
VirusTotal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import subprocess
import platform
import requests
import sys
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# Get the username of the current user
username = os.getlogin()
# Directory to monitor for downloads
DOWNLOADS_DIR = os.path.join("C:", "Users", username, "Downloads")
# Function to download a file from a URL
def download_file(url, save_path):
with requests.get(url, stream=True) as response:
response.raise_for_status()
with open(save_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
# Function to scan a file using VirusTotal API
def scan_file(file_path, api_key):
print(f"Scanning file: {file_path}")
url = 'https://www.virustotal.com/vtapi/v2/file/scan'
params = {'apikey': api_key}
files = {'file': (file_path, open(file_path, 'rb'))}
response = requests.post(url, files=files, params=params)
if response.status_code == 200:
result = response.json()
if result['response_code'] == 1:
print("Scan completed.")
positives = result['positives']
if positives > 0:
print(f"Virus detected by {positives} out of {result['total']} scanners.")
# Optionally, you can remove the file here
# os.remove(file_path)
else:
print("No virus detected.")
else:
print("Scan failed:", result['verbose_msg'])
else:
print("Error occurred during scanning:", response.status_code)
# Event handler for file system events
class DownloadHandler(FileSystemEventHandler):
def on_created(self, event):
if event.is_directory:
return
elif event.src_path.endswith('.part'):
# Ignore incomplete downloads
return
else:
print(f"New file detected: {event.src_path}")
scan_file(event.src_path, sys.argv[1]) # Pass the API key as an argument
if __name__ == "__main__":
# Start monitoring the downloads directory
event_handler = DownloadHandler()
observer = Observer()
observer.schedule(event_handler, DOWNLOADS_DIR, recursive=False)
observer.start()
try:
while True:
pass
except KeyboardInterrupt:
observer.stop()
observer.join()