-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclamscanner.py
215 lines (181 loc) · 7.49 KB
/
clamscanner.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
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
#!/usr/bin/env python3
import json
import queue
import sys
import time
import argparse
import threading
from multiprocessing import cpu_count
from pathlib import Path
from subprocess import Popen, PIPE, TimeoutExpired
from threading import Thread, Lock, Event
from typing import List, Tuple, Iterator, Set, Optional, NamedTuple, TextIO
SCAN_COMMAND = Tuple[str, Path]
class AlreadyScannedCache(NamedTuple):
cache: Set[Path]
lock: Lock
def generate_scan_commands(path: Path, already_scanned: Optional[AlreadyScannedCache] = None) -> Iterator[SCAN_COMMAND]:
try:
resolved = path.resolve(strict=True)
if already_scanned is not None:
with already_scanned.lock:
if resolved in already_scanned.cache:
print(f"{path}: SKIP")
return
already_scanned.cache.add(resolved)
except (FileNotFoundError, RuntimeError):
return
if path.is_file():
yield ["FILE_SCAN", path]
return
yield ["DIR_OPEN", path]
try:
for child in sorted(path.iterdir(), key=lambda x: 0 if x.is_dir() else 1):
for sub in generate_scan_commands(child, already_scanned):
yield sub
except (PermissionError, OSError):
pass
yield ["DIR_DONE", path]
def scan(path: Path, log_file: Optional[TextIO], file_cache: Optional[Path] = None) -> None:
skip_files: Set[Path] = set()
if file_cache is not None and file_cache.exists():
with file_cache.open('r') as f:
skip_files = set(filter(
lambda x: x.is_file(),
map(lambda x: Path(x), json.load(f))
))
print(f'Loaded {len(skip_files)} entries from cache')
already_scanned_cache = AlreadyScannedCache(cache=skip_files, lock=Lock())
commands: queue.Queue[Tuple[int, SCAN_COMMAND]] = queue.Queue(maxsize=cpu_count() * 20)
commands_unfinished = Event()
commands_unfinished.set()
log_lines_to_write: queue.Queue[Tuple[TextIO, str]] = queue.Queue(maxsize=cpu_count() * 100)
counter = {
'scanned-files': 0,
'infected-files': 0,
'time-start': time.time()
}
def thread_commands_generator():
for i, cmd in enumerate(generate_scan_commands(path, already_scanned_cache)):
while True:
if not commands_unfinished.is_set():
return
try:
commands.put((i, cmd), timeout=5)
break
except queue.Full:
pass
print("\n[SCANNER] Command generation finished")
commands_unfinished.clear()
def thread_write_log():
last_line_length = 0
while True:
if not commands_unfinished.is_set() and log_lines_to_write.empty():
alive_threads = sum(map(lambda _: 1, filter(lambda x: x.is_alive(), threading.enumerate())))
if alive_threads <= 2:
break
print(f'Waiting for log to finish, threads alive: {alive_threads}')
try:
console, line = log_lines_to_write.get(timeout=1)
is_infected = not line.endswith(': OK')
spaces = " " * (last_line_length - len(line))
print(line + spaces, end='\n' if is_infected else '\r', file=console, flush=True)
if log_file is not None:
log_file.write(line + '\n')
last_line_length = len(line)
except queue.Empty:
pass
def thread_scanning():
while True:
if not commands_unfinished.is_set():
print("\n[SCANNER] Thread finished")
break
try:
command_id, (command, target) = commands.get(timeout=1)
try:
if command == "FILE_SCAN":
p = Popen(["clamdscan", "--fdpass", "--no-summary", target.absolute()],
stdin=PIPE,
stderr=PIPE,
stdout=PIPE,
text=True
)
time_start = time.time()
while True:
try:
stdout, stderr = p.communicate(input=None, timeout=30)
break
except TimeoutExpired:
print(f"\n[SCANNER] "
f"{target} taking longer than it should ({int(time.time() - time_start)}s)")
stdout, stderr = stdout.strip(), stderr.strip()
if stdout:
counter['scanned-files'] += 1
if not stdout.endswith(': OK'):
counter['infected-files'] += 1
log_lines_to_write.put((sys.stdout, stdout))
if stderr:
log_lines_to_write.put((sys.stderr, stderr))
finally:
pass
except queue.Empty:
print("\n[SCANNER] ... nothing to do")
pass
threads: List[Thread] = []
for f in (thread_commands_generator, thread_write_log):
t = Thread(target=f)
t.setDaemon(True)
t.start()
threads.append(t)
print(f"\n[SCANNER] Starting {cpu_count()} scanning threads")
for _ in range(cpu_count()):
t = Thread(target=thread_scanning)
threads.append(t)
t.start()
try:
for t in threads:
t.join()
print()
print('=========================================')
print(f'Scan competed in {int(time.time() - counter["time-start"])} s')
print(f'Scanned files: {counter["scanned-files"]}')
print(f'Infected files: {counter["infected-files"]}')
except KeyboardInterrupt:
print("^C received, ending")
if file_cache is not None:
print(f'Saving cache to {file_cache}')
with already_scanned_cache.lock:
with file_cache.open('w') as f:
json.dump(
list(
map(lambda x: str(x),
filter(
lambda x: x.is_file(),
already_scanned_cache.cache
)
)
),
f
)
print('Cache saved')
commands_unfinished.clear()
print('Waiting for other thread to terminate')
for t in threads:
t.join()
print('Finishing')
def main() -> None:
parser = argparse.ArgumentParser(description='Recursive and fast scan')
parser.add_argument('path', metavar='path', type=str,
help='a path to scan')
parser.add_argument('--log', dest='log', type=str, help='log file to write information to', default=None)
parser.add_argument('--cache', dest='cache', type=str, help='where to store scanned cache info', default=None)
args = parser.parse_args()
log_file: Optional[TextIO] = None
if args.log is not None:
log_file = open(args.log, 'a')
scan(Path(args.path), log_file, Path(args.cache) if args.cache is not None else None)
if log_file is not None:
log_file.flush()
log_file.close()
if __name__ == '__main__':
main()