-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathantivirus_manager.py
More file actions
409 lines (336 loc) · 14.9 KB
/
Copy pathantivirus_manager.py
File metadata and controls
409 lines (336 loc) · 14.9 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import shutil
import subprocess
import os
import time
from datetime import datetime
from PySide6.QtCore import QThread, Signal
# IMPORTANTE: Importamos el módulo de traducción
import locales
# --- HILO DE INSTALACIÓN ---
class InstallWorker(QThread):
log_signal = Signal(str)
finished_signal = Signal(bool)
def run(self):
# USO DE LOCALES
self.log_signal.emit(locales.get_text("log_pkg_detect"))
distro = self.get_distro_type()
cmd = []
if distro == "arch":
cmd = ["pkexec", "pacman", "-S", "clamav", "--noconfirm"]
elif distro == "debian":
cmd = ["pkexec", "apt", "install", "clamav", "clamav-daemon", "-y"]
elif distro == "redhat":
cmd = ["pkexec", "dnf", "install", "clamav", "clamav-update", "-y"]
elif distro == "suse":
cmd = ["pkexec", "zypper", "install", "-n", "clamav"]
else:
# USO DE LOCALES
self.log_signal.emit(locales.get_text("log_distro_error"))
self.finished_signal.emit(False)
return
# USO DE LOCALES
self.log_signal.emit(locales.get_text("log_executing").format(' '.join(cmd)))
self.log_signal.emit(locales.get_text("log_pass_prompt"))
try:
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
for line in process.stdout:
self.log_signal.emit(line.strip())
process.wait()
if process.returncode == 0:
self.log_signal.emit(locales.get_text("log_install_ok"))
self.finished_signal.emit(True)
else:
self.log_signal.emit(locales.get_text("log_process_error").format(process.returncode))
self.finished_signal.emit(False)
except Exception as e:
self.log_signal.emit(locales.get_text("log_critical_error").format(e))
self.finished_signal.emit(False)
def get_distro_type(self):
try:
if os.path.exists("/etc/os-release"):
with open("/etc/os-release", "r") as f:
content = f.read().lower()
if "arch" in content or "manjaro" in content: return "arch"
if "debian" in content or "ubuntu" in content or "mint" in content: return "debian"
if "fedora" in content or "rhel" in content: return "redhat"
if "suse" in content: return "suse"
except: pass
return "unknown"
# --- HILO DE ESCANEO ---
class ScanWorker(QThread):
log_signal = Signal(str)
# CAMBIO: Ahora devuelve (Exito, Lista de rutas infectadas)
finished_signal = Signal(bool, list)
def __init__(self, target_path):
super().__init__()
self.target_path = target_path
self.process = None
self.killed_by_user = False
def run(self):
self.log_signal.emit(locales.get_text("log_scan_start").format(self.target_path))
# CAMBIO: Quitamos --move. Solo detectamos.
cmd = ["clamscan", "-r", self.target_path]
infected_files = [] # Lista para guardar las rutas
try:
self.process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
)
for line in self.process.stdout:
line = line.strip()
self.log_signal.emit(line)
# PARSEO INTELIGENTE:
# Salida típica: /home/user/virus.txt: Eicar-Test-Signature FOUND
if " FOUND" in line:
# Sepamos por ": " y cogemos la primera parte (la ruta)
parts = line.split(": ")
if len(parts) > 0:
file_path = parts[0]
infected_files.append(file_path)
self.process.wait()
if self.killed_by_user:
self.log_signal.emit(locales.get_text("av_scan_stopped"))
self.finished_signal.emit(False, [])
else:
# 0 = Limpio, 1 = Virus encontrado
success = self.process.returncode in [0, 1]
self.finished_signal.emit(success, infected_files)
except Exception as e:
if not self.killed_by_user:
self.log_signal.emit(locales.get_text("log_critical_error").format(e))
self.finished_signal.emit(False, [])
def stop(self):
if self.process and self.process.poll() is None:
self.killed_by_user = True
self.process.terminate()
# --- HILO DE ACTUALIZACIÓN ---
class UpdateWorker(QThread):
log_signal = Signal(str)
finished_signal = Signal(bool)
def run(self):
# USO DE LOCALES
self.log_signal.emit(locales.get_text("log_update_start"))
try:
process = subprocess.Popen(
["pkexec", "freshclam"],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
)
for line in process.stdout:
self.log_signal.emit(line.strip())
process.wait()
self.finished_signal.emit(process.returncode == 0)
except Exception as e:
self.log_signal.emit(locales.get_text("log_generic_error").format(e))
self.finished_signal.emit(False)
class AntivirusManager:
def __init__(self):
# Definimos la ruta de cuarentena en el home del usuario
self.quarantine_dir = os.path.expanduser("~/.sentinelx/quarantine")
if not os.path.exists(self.quarantine_dir):
os.makedirs(self.quarantine_dir)
def is_installed(self):
return shutil.which("clamscan") is not None
def get_db_version(self):
if not self.is_installed(): return "N/A"
try:
res = subprocess.run(["clamscan", "--version"], capture_output=True, text=True)
return res.stdout.split("/")[0]
except:
return "Unknown"
def is_daemon_active(self):
"""Comprueba si clamav-daemon o su socket están corriendo"""
try:
# Verificamos el servicio principal
res_svc = subprocess.run(
["systemctl", "is-active", "--quiet", "clamav-daemon"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
# Verificamos también el socket (importante en Arch/Manjaro)
res_sock = subprocess.run(
["systemctl", "is-active", "--quiet", "clamav-daemon.socket"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
# Consideramos activo si cualquiera de los dos está vivo
return (res_svc.returncode == 0) or (res_sock.returncode == 0)
except:
return False
def set_daemon_state(self, enable: bool):
"""Activa/Desactiva Servicio Y Socket simultáneamente"""
try:
# Lista de unidades a controlar
units = ["clamav-daemon.service", "clamav-daemon.socket"]
if enable:
# 1. Desbloquear
cmd_unmask = ["pkexec", "systemctl", "unmask"] + units
subprocess.run(cmd_unmask, stderr=subprocess.DEVNULL)
# 2. Habilitar y arrancar AMBOS
cmd_enable = ["pkexec", "systemctl", "enable", "--now"] + units
subprocess.run(cmd_enable, check=True)
# 3. Esperar un segundo para que systemd respire (evita falsos negativos)
import time
time.sleep(1)
else:
# Deshabilitar y parar AMBOS
cmd_disable = ["pkexec", "systemctl", "disable", "--now"] + units
subprocess.run(cmd_disable, check=True)
return True
except subprocess.CalledProcessError as e:
print(f"Error cambiando estado daemon: {e}")
return False
def get_clamd_conf_path(self):
"""Detecta la ubicación del archivo de configuración de ClamAV"""
# Posibles rutas estándar
paths = [
"/etc/clamav/clamd.conf", # Debian/Ubuntu
"/etc/clamd.d/scan.conf", # Fedora/CentOS
"/etc/clamd.conf" # Arch/Manjaro
]
for p in paths:
if os.path.exists(p):
return p
return None
def is_on_access_active(self):
"""Verifica si el servicio clamonacc está corriendo"""
try:
# El servicio suele llamarse 'clamav-clamonacc' o simplemente 'clamonacc'
res = subprocess.run(
["systemctl", "is-active", "--quiet", "clamav-clamonacc"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
if res.returncode == 0: return True
# Intento alternativo (algunas distros lo llaman diferente)
res = subprocess.run(
["systemctl", "is-active", "--quiet", "clamonacc"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
return res.returncode == 0
except:
return False
def configure_on_access(self, enable: bool, watch_path="/home"):
conf_path = self.get_clamd_conf_path()
if not conf_path:
print("Error: No se encontró clamd.conf")
return False
helper = "/usr/local/bin/sentinelx-helper"
try:
if enable:
# 1. Configurar archivo (Esto llamará al helper v7 corregido)
subprocess.run(["pkexec", helper, "enable-on-access", conf_path, watch_path], check=True)
# 2. Reiniciar el demonio principal
subprocess.run(["pkexec", "systemctl", "restart", "clamav-daemon"], check=True)
# 3. ESPERA ACTIVA (Wait loop)
print("Esperando a que ClamAV Daemon esté listo...")
import time
max_retries = 30
daemon_ready = False
for _ in range(max_retries):
res = subprocess.run(["systemctl", "is-active", "clamav-daemon"], stdout=subprocess.DEVNULL)
# Chequeamos socket en rutas comunes de Linux
socket_exists = os.path.exists("/run/clamav/clamd.ctl") or \
os.path.exists("/var/run/clamav/clamd.ctl") or \
os.path.exists("/run/clamd.scan/clamd.sock") # Ruta Fedora
if res.returncode == 0 and socket_exists:
daemon_ready = True
break
time.sleep(1)
if not daemon_ready:
print("Error: Tiempo de espera agotado. ClamAV Daemon no arrancó a tiempo.")
return False
# 4. Ahora sí, arrancamos el vigilante
subprocess.run(["pkexec", "systemctl", "enable", "--now", "clamav-clamonacc"], check=True)
else:
subprocess.run(["pkexec", "systemctl", "disable", "--now", "clamav-clamonacc"], check=True)
subprocess.run(["pkexec", helper, "disable-on-access", conf_path], check=True)
subprocess.run(["pkexec", "systemctl", "restart", "clamav-daemon"], check=True)
return True
except Exception as e:
print(f"Error configurando On-Access: {e}")
return False
def move_to_quarantine(self, file_path):
"""Mueve un archivo infectado a la carpeta segura y le quita permisos"""
if not os.path.exists(file_path):
return False
try:
# Nombre único: fecha_nombreoriginal
timestamp = int(time.time())
filename = os.path.basename(file_path)
new_name = f"{timestamp}_{filename}"
dest_path = os.path.join(self.quarantine_dir, new_name)
# 1. Mover
# Usamos shutil.move que funciona entre discos
shutil.move(file_path, dest_path)
# 2. Neutralizar (Quitar permisos de ejecución y escritura)
# 0o400 = Solo lectura para el dueño (r--------)
os.chmod(dest_path, 0o400)
# 3. Guardar metadatos (Opcional, para saber de dónde vino)
# Creamos un archivo .meta con la ruta original
with open(dest_path + ".meta", "w") as f:
f.write(file_path)
return True
except Exception as e:
print(f"Error cuarentena: {e}")
return False
def get_quarantine_files(self):
"""Devuelve lista de diccionarios con info de archivos en cuarentena"""
files = []
if not os.path.exists(self.quarantine_dir):
return files
for name in os.listdir(self.quarantine_dir):
if name.endswith(".meta"): continue # Ignorar metadatos
full_path = os.path.join(self.quarantine_dir, name)
# Intentar leer origen
original_path = "Desconocido"
meta_path = full_path + ".meta"
if os.path.exists(meta_path):
with open(meta_path, "r") as f:
original_path = f.read().strip()
# Parsear fecha del nombre (timestamp_nombre)
try:
ts = int(name.split("_")[0])
date_str = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M')
except:
date_str = "Unknown"
files.append({
"id": name, # El nombre real en disco
"original_path": original_path,
"date": date_str
})
return files
def restore_file(self, file_id):
"""Devuelve el archivo a su lugar original"""
quarantine_path = os.path.join(self.quarantine_dir, file_id)
meta_path = quarantine_path + ".meta"
if not os.path.exists(quarantine_path) or not os.path.exists(meta_path):
return False
try:
with open(meta_path, "r") as f:
original_dest = f.read().strip()
# Verificar si el directorio original aun existe
dest_dir = os.path.dirname(original_dest)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# Mover de vuelta
shutil.move(quarantine_path, original_dest)
# Restaurar permisos normales (Lectura/Escritura usuario)
os.chmod(original_dest, 0o644)
# Borrar meta
os.remove(meta_path)
return True
except Exception as e:
print(f"Error restaurando: {e}")
return False
def delete_quarantine_file(self, file_id):
"""Borra definitivamente el archivo y su meta"""
path = os.path.join(self.quarantine_dir, file_id)
meta = path + ".meta"
try:
if os.path.exists(path): os.remove(path)
if os.path.exists(meta): os.remove(meta)
return True
except:
return False