-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathziphandler.py
More file actions
48 lines (43 loc) · 1.78 KB
/
Copy pathziphandler.py
File metadata and controls
48 lines (43 loc) · 1.78 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
import zipfile
import io
import logging
CHUNK_SIZE = 64 * 1024
def zip_scan_handler(data: bytes) -> str:
"""
Scannt ein (verschachteltes) ZIP-Archiv komplett im Speicher.
- Erkennt ZIPs anhand des PKZIP-Magic-Headers (b'PK\\x03\\x04').
- Entpackt rekursiv und prüft jeden Eintrag chunkweise.
- Gibt 'FOUND <Sig>' zurück, wenn irgendwo eine Signatur matched,
sonst 'OK'.
"""
# 1) Erkennen: Magic-Bytes prüfen
if not data.startswith(b'PK\x03\x04'):
return "OK"
def scan_stream(stream: io.BytesIO) -> str:
try:
with zipfile.ZipFile(stream) as zf:
for info in zf.infolist():
# Eintrag öffnen
with zf.open(info) as entry:
# Wenn selbst ZIP → rekursiv prüfen
if info.filename.lower().endswith('.zip'):
nested = io.BytesIO(entry.read())
result = scan_stream(nested)
if result.startswith("FOUND"):
return result
else:
continue
# sonst chunkweise prüfen
while True:
chunk = entry.read(CHUNK_SIZE)
if not chunk:
break
# Beispiel-Signaturtest: 'eicar'
if b"eicar" in chunk.lower():
return "FOUND Dummy.Test.Virus"
return "OK"
except zipfile.BadZipFile:
logging.error("Bad zip file in handler")
return "OK"
# Starte Scan mit dem kompletten Daten-Stream
return scan_stream(io.BytesIO(data))