-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLAMAV.py
110 lines (94 loc) · 3.77 KB
/
CLAMAV.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import subprocess
import platform
import requests
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 install ClamAV
def install_clamav():
# Check if ClamAV is already installed
try:
result = subprocess.run("clamscan --version", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
print("ClamAV is already installed.")
return
except FileNotFoundError:
pass # ClamAV not found, proceed with installation
# Determine the platform (e.g., Windows 64-bit)
system = platform.system()
machine = platform.machine()
if system != 'Windows':
print("Automatic installation is only supported on Windows.")
return
# Determine the download URL based on the platform
if machine.endswith('64'):
url = 'https://www.clamav.net/downloads/production/clamav-0.103.0-win-x64-portable.zip'
else:
url = 'https://www.clamav.net/downloads/production/clamav-0.103.0-win-x86-portable.zip'
# Path to save the downloaded ClamAV installer
installer_path = os.path.join(os.path.dirname(__file__), 'clamav_installer.zip')
# Download the ClamAV installer
print(f"Downloading ClamAV installer from {url}")
download_file(url, installer_path)
# Extract the installer
print("Extracting ClamAV installer...")
extract_cmd = f'powershell Expand-Archive -Path "{installer_path}" -DestinationPath "{os.path.dirname(__file__)}"'
subprocess.run(extract_cmd, shell=True)
# Find the installer executable
installer_executable = os.path.join(os.path.dirname(__file__), 'clamav_installer.exe')
# Execute the ClamAV installer
print("Installing ClamAV...")
result = subprocess.run(installer_executable, shell=True)
if result.returncode == 0:
print("ClamAV installation successful.")
else:
print("ClamAV installation failed.")
# Clean up - remove the downloaded installer
os.remove(installer_path)
# Function to scan a file using ClamAV
def scan_file(file_path):
print(f"Scanning file: {file_path}")
result = subprocess.run(f'clamscan --stdout --infected --remove "{file_path}"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
print("No virus found.")
elif result.returncode == 1:
print("Virus found. Removing file...")
os.remove(file_path)
else:
print("Error occurred during scanning:", result.stderr.decode('utf-8'))
# 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)
if __name__ == "__main__":
# Install ClamAV if not already installed
install_clamav()
# 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()