-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVirtualPyDetector.py
More file actions
417 lines (353 loc) · 15.1 KB
/
Copy pathVirtualPyDetector.py
File metadata and controls
417 lines (353 loc) · 15.1 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
410
411
412
413
414
415
416
417
import os
import re
import sys
import time
import ctypes
import platform
import subprocess
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
from functools import partial
from typing import List, Set, Dict
import psutil
__VERSION__ = "0.0.6"
class VPDError(Exception):
"""Base class for exceptions in VirtualPyDetector."""
def __init__(self, message):
super().__init__(message)
class Detector:
"""
Comprehensive detection system for virtual environments, sandboxes, and debuggers.
Combines multiple detection techniques across different platforms with multiprocessing.
"""
class VMChecks:
"""Virtual machine detection methods using hardware and system artifacts."""
@staticmethod
def check_vm_hardware() -> bool:
"""Detect VM through system hardware information."""
system = platform.system()
if system == "Windows":
try:
output = subprocess.check_output(
["wmic", "computersystem", "get", "model"],
encoding="utf-8",
timeout=3
)
vm_indicators = ("Virtual", "VMware", "VirtualBox", "Hyper-V", "QEMU")
return any(indicator in output for indicator in vm_indicators)
except subprocess.CalledProcessError as e: raise VPDError(f"Error checking VM hardware: {e}")
except subprocess.TimeoutExpired: return False
elif system == "Darwin": # macOS
try:
output = subprocess.check_output(
["sysctl", "hw.model"],
encoding="utf-8",
timeout=3
)
return any(vm in output for vm in ("VMware", "VirtualBox"))
except subprocess.CalledProcessError as e: raise VPDError(f"Error checking VM hardware: {e}")
except subprocess.TimeoutExpired: return False
elif system == "Linux":
try:
output = subprocess.check_output(
["systemd-detect-virt"],
encoding="utf-8",
timeout=3
)
return output.strip() != "none"
except subprocess.CalledProcessError as e: raise VPDError(f"Error checking VM hardware: {e}")
except subprocess.TimeoutExpired: return False
return False
@staticmethod
def check_mac_address() -> bool:
"""Check for virtualization-related MAC address prefixes."""
try:
command = "getmac" if platform.system() == "Windows" else "ifconfig"
output = subprocess.check_output(
[command],
encoding="utf-8",
timeout=3
)
mac_pattern = r"(00:05:69|00:0C:29|00:50:56|00:1C:14|00:03:FF|00:05:00)"
return re.search(mac_pattern, output) is not None
except subprocess.CalledProcessError as e: raise VPDError(f"Error checking MAC address: {e}")
except subprocess.TimeoutExpired: return False
@staticmethod
def check_vm_artifacts() -> bool:
"""Check for existence of known virtualization software artifacts."""
if platform.system() == "Windows":
vm_paths = [
"C:\\Program Files\\VMware\\VMware Tools",
"C:\\Program Files\\Oracle\\VirtualBox Guest Additions"
]
return Detector.HelperFunctions.check_paths_exist(vm_paths)
elif platform.system() == "Darwin":
vm_paths = [
"/Applications/VMware Tools",
"/Applications/VirtualBox.app"
]
return Detector.HelperFunctions.check_paths_exist(vm_paths)
return False
@staticmethod
def check_virtualbox_drivers() -> bool:
"""Detect VirtualBox drivers on Windows systems."""
if platform.system() != "Windows": return False # not windows to be return false
drivers = [
"VBoxGuest.sys",
"VBoxMouse.sys",
"VBoxSF.sys",
"VBoxVideo.sys",
"VBoxNetLwf.sys",
"VBoxNetAdp.sys",
"VBoxDrv.sys",
"VBoxUSBMon.sys",
"VBoxUSB.sys",
"VBoxWddm.sys",
"VBoxMRXNP.sys",
"VBoxCdd.sys",
"VBoxEhciR0.sys"
]
driver_paths = [f"C:\\Windows\\System32\\drivers\\{driver}" for driver in drivers]
return Detector.HelperFunctions.check_paths_exist(driver_paths)
@staticmethod
def check_cpu_features() -> bool:
"""Detect CPU features indicating virtualization environment."""
if platform.system() == "Linux":
try:
with open("/proc/cpuinfo", "r") as cpuinfo:
return any("hypervisor" in line for line in cpuinfo)
except FileNotFoundError: return False
elif platform.system() == "Darwin":
try:
output = subprocess.check_output(
["sysctl", "machdep.cpu.features"],
encoding="utf-8",
timeout=3
)
return "VMM" in output # Virtual Machine Monitor flag
except subprocess.CalledProcessError as e: raise VPDError(f"Error checking CPU features: {e}")
except subprocess.TimeoutExpired: return False
return False
class DebuggerChecks:
"""Debugger and sandbox detection methods."""
@staticmethod
def check_hypervisor() -> bool:
"""Detect hypervisor presence using platform-specific APIs."""
if platform.system() == "Windows":
try: return bool(ctypes.windll.kernel32.IsHypervisorPresent())
except (AttributeError, OSError): return False
elif platform.system() == "Darwin":
try:
output = subprocess.check_output(
["sysctl", "kern.hv_support"],
encoding="utf-8",
timeout=3
)
return "1" in output
except subprocess.CalledProcessError as e: raise VPDError(f"Error checking hypervisor: {e}")
except subprocess.TimeoutExpired: return False
return False
@staticmethod
def check_sandbox_files() -> bool:
"""Check for files/directories indicative of sandbox environments."""
sandbox_paths = [
"/Applications/WindowsSandbox.app", # Hypothetical macOS path
"C:\\Program Files\\WindowsApps\\Microsoft.WindowsSandbox_"
]
return Detector.HelperFunctions.check_paths_exist(sandbox_paths)
@staticmethod
def detect_debugger() -> bool:
"""Detect debugger presence through platform-specific methods."""
if platform.system() == "Windows":
try:
return bool(ctypes.windll.kernel32.IsDebuggerPresent())
except (AttributeError, OSError):
return False
elif platform.system() in {"Darwin", "Linux"}:
try:
parent_process = psutil.Process(os.getppid()).name().lower()
return parent_process in {"lldb", "gdb"}
except (psutil.NoSuchProcess, psutil.AccessDenied):
return False
return False
@staticmethod
def anti_timing_check(threshold: float = 0.5) -> bool:
"""
Detect timing anomalies suggestive of virtualization/debugging.
Args:
threshold: Maximum expected execution time for empty loop (seconds)
"""
start_time = time.perf_counter()
for _ in range(1_000_000):
pass # Intentional no-op for timing measurement
elapsed = time.perf_counter() - start_time
return elapsed > threshold
class ProcessChecks:
"""Detection of suspicious processes associated with analysis environments."""
@staticmethod
def detect_suspicious_processes() -> bool:
"""Threaded detection of known sandbox/VM-related processes."""
suspicious_processes: Set[str] = {
"vmtoolsd", "vboxservice", "wireshark",
"fiddler", "sandboxie", "processhacker"
}
def process_check(proc: psutil.Process) -> bool:
try: return proc.info["name"].lower() in suspicious_processes
except (psutil.NoSuchProcess, psutil.AccessDenied): return False
with ThreadPoolExecutor() as executor:
return any(f.result() for f in as_completed([executor.submit(process_check, p) for p in psutil.process_iter(["name"])]))
class HelperFunctions:
"""Enhanced utility methods with error handling."""
@staticmethod
def check_paths_exist(paths: List[str]) -> bool:
"""Safely check multiple paths with error handling."""
for path in paths:
try:
if os.path.exists(path):
return True
except PermissionError:
continue
except OSError:
continue
return False
@property
def venv_active(self) -> bool:
"""
Optimized multiprocess check with batch processing and improved cancellation.
"""
check_groups = [
# Group related checks to minimize process creation
[
self.VMChecks.check_vm_hardware,
self.VMChecks.check_mac_address,
self.VMChecks.check_vm_artifacts,
],
[
self.VMChecks.check_cpu_features,
self.DebuggerChecks.check_hypervisor,
self.DebuggerChecks.check_sandbox_files,
],
[
self.DebuggerChecks.detect_debugger,
partial(self.DebuggerChecks.anti_timing_check),
self.ProcessChecks.detect_suspicious_processes,
]
]
try:
with ProcessPoolExecutor(max_workers=3) as executor:
futures = []
for group in check_groups:
futures.append(executor.submit(self._run_check_group, group))
for future in as_completed(futures):
if future.result():
# Cancel remaining checks
for f in futures: f.cancel()
return True
return False
except Exception as e:
raise VPDError(f"Multiprocess check failed: {e}")
def _run_check_group(self, checks: List[callable]) -> bool:
"""Run a group of checks in the current process."""
return any(check() for check in checks)
@property
def is_virtualized(self) -> bool:
"""Check if the environment is virtualized."""
return any((
self.VMChecks.check_vm_hardware(),
self.VMChecks.check_mac_address(),
self.VMChecks.check_vm_artifacts(),
self.VMChecks.check_cpu_features(),
))
@property
def is_debugged(self) -> bool:
"""Check if a debugger is attached."""
return any((
self.DebuggerChecks.detect_debugger(),
self.DebuggerChecks.anti_timing_check(),
))
@property
def is_sandboxed(self) -> bool:
"""Check if in a sandbox environment."""
return any((
self.DebuggerChecks.check_sandbox_files(),
self.ProcessChecks.detect_suspicious_processes(),
))
@property
def is_analyzed(self) -> bool:
"""
Check if the environment is under analysis.
Returns:
bool: True if an analysis environment is detected, False otherwise.
"""
return any((
self.is_virtualized,
self.is_debugged,
self.is_sandboxed,
))
@property
def is_safe(self) -> bool:
"""
Check if the environment is safe.
Returns:
bool: True if no analysis environment is detected, False otherwise.
"""
return not self.is_analyzed
@property
def is_unsafe(self) -> bool:
"""
Check if the environment is unsafe.
Returns:
bool: True if an analysis environment is detected, False otherwise.
"""
return self.is_analyzed
@property
def is_virtual(self) -> bool:
"""
Check if the environment is virtual.
Returns:
bool: True if a virtual environment is detected, False otherwise.
"""
return self.is_virtualized
@property
def is_debug(self) -> bool:
"""
Check if a debugger is attached to the process.
Returns:
bool: True if a debugger is detected, False otherwise.
"""
return self.is_debugged
@property
def is_sandbox(self) -> bool:
"""
Check if the environment is a sandbox.
Returns:
bool: True if a sandbox environment is detected, False otherwise.
"""
return self.is_sandboxed
@property
def is_analysis(self) -> bool:
"""
Check if the environment is under analysis.
Returns:
bool: True if an analysis environment is detected, False otherwise.
"""
return self.is_analyzed
@property
def get_all_checks(self) -> Dict[str, bool]:
"""Return comprehensive check results."""
return {
"is_virtualized": self.is_virtualized,
"is_debugged": self.is_debugged,
"is_sandboxed": self.is_sandboxed,
"venv_active": self.venv_active,
"detailed": {
"vm_hardware": self.VMChecks.check_vm_hardware(),
"vm_mac": self.VMChecks.check_mac_address(),
"vm_artifacts": self.VMChecks.check_vm_artifacts(),
"cpu_features": self.VMChecks.check_cpu_features(),
"hypervisor": self.DebuggerChecks.check_hypervisor(),
"sandbox_files": self.DebuggerChecks.check_sandbox_files(),
"debugger_present": self.DebuggerChecks.detect_debugger(),
"suspicious_processes": self.ProcessChecks.detect_suspicious_processes(),
"timing_anomaly": self.DebuggerChecks.anti_timing_check(),
}
}