-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathplugin_system.py
More file actions
744 lines (616 loc) · 24.8 KB
/
plugin_system.py
File metadata and controls
744 lines (616 loc) · 24.8 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
"""
PySpector Plugin System
Secure, extensible plugin architecture for PySpector
"""
import ast
import importlib.util
import inspect
import json
import os
import re
import shutil
import sys
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Dict, List, Optional, Type
import hashlib
class PluginMetadata:
"""Metadata for a plugin"""
def __init__(
self,
name: str,
version: str,
author: str,
description: str,
requires: List[str] = None,
category: str = "general"
):
self.name = name
self.version = version
self.author = author
self.description = description
self.requires = requires or []
self.category = category
class PySpectorPlugin(ABC):
"""
Base class for all PySpector plugins.
Plugins must inherit from this class and implement required methods.
"""
@property
@abstractmethod
def metadata(self) -> PluginMetadata:
"""Return plugin metadata"""
pass
@abstractmethod
def initialize(self, config: Dict[str, Any]) -> bool:
"""
Initialize the plugin with configuration.
Args:
config: Plugin-specific configuration
Returns:
True if initialization succeeded, False otherwise
"""
pass
@abstractmethod
def process_findings(
self,
findings: List[Dict[str, Any]],
scan_path: Path,
**kwargs
) -> Dict[str, Any]:
"""
Process PySpector findings.
Args:
findings: List of vulnerability findings from PySpector
scan_path: Path that was scanned
**kwargs: Additional arguments passed from CLI
Returns:
Dictionary with plugin results:
{
'success': bool,
'message': str,
'data': Any,
'output_files': List[str] # Optional: paths to generated files
}
"""
pass
def cleanup(self) -> None:
"""
Cleanup resources before plugin unload.
Called automatically by the plugin manager.
"""
pass
def validate_config(self, config: Dict[str, Any]) -> tuple[bool, str]:
"""
Validate plugin configuration.
Args:
config: Configuration to validate
Returns:
Tuple of (is_valid, error_message)
"""
return True, ""
class PluginSecurity:
"""Security utilities for plugin system"""
DANGEROUS_MODULES = {
'os.system', 'subprocess.Popen', 'eval', 'exec',
'__import__', 'compile'
}
ALLOWED_IMPORTS = {
'json', 'pathlib', 'typing', 'dataclasses', 're',
'datetime', 'collections', 'itertools', 'functools'
}
@staticmethod
def calculate_checksum(file_path: Path) -> str:
"""Calculate SHA256 checksum of a plugin file"""
sha256 = hashlib.sha256()
with open(file_path, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b''):
sha256.update(chunk)
return sha256.hexdigest()
@staticmethod
def validate_plugin_code(plugin_path: Path) -> tuple[bool, str]:
"""
Basic static analysis of plugin code for security.
Returns:
Tuple of (is_safe, message)
"""
fatal_calls = {
"eval",
"exec",
"compile",
"__import__",
"vars",
"getattr",
"os.system",
"os.popen",
"subprocess.Popen",
"subprocess.run",
"subprocess.call",
"subprocess.check_call",
"subprocess.check_output",
}
warning_calls = {
"open",
"builtins.open",
}
try:
source = plugin_path.read_text()
tree = ast.parse(source, filename=str(plugin_path))
except Exception as exc:
return False, f"Error validating plugin: {exc}"
alias_map: Dict[str, str] = {}
detected_fatal: set[str] = set()
detected_warnings: set[str] = set()
def register_alias(alias: str, target: str) -> None:
alias_map[alias] = target
def resolve_name(node: ast.AST) -> Optional[str]:
if isinstance(node, ast.Name):
target = alias_map.get(node.id, node.id)
return target
if isinstance(node, ast.Attribute):
attrs: List[str] = []
current = node
while isinstance(current, ast.Attribute):
attrs.append(current.attr)
current = current.value
if isinstance(current, ast.Name):
base = alias_map.get(current.id, current.id)
attrs.append(base)
attrs.reverse()
return ".".join(attrs)
if isinstance(node, ast.Call):
inner = resolve_name(node.func)
if inner:
return inner
return None
class Analyzer(ast.NodeVisitor):
def visit_Import(self, node: ast.Import) -> None:
for alias in node.names:
register_alias(alias.asname or alias.name, alias.name)
self.generic_visit(node)
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
module = node.module or ""
for alias in node.names:
target = f"{module}.{alias.name}" if module else alias.name
register_alias(alias.asname or alias.name, target)
self.generic_visit(node)
def visit_Call(self, node: ast.Call) -> None:
name = resolve_name(node.func)
if name:
simplified = name.replace("builtins.", "")
# Handle alias that already resolved to dotted path
if simplified in fatal_calls:
detected_fatal.add(simplified)
elif simplified in warning_calls:
detected_warnings.add(simplified)
else:
# Check dotted paths by normalising alias root
parts = simplified.split(".")
if parts:
root = alias_map.get(parts[0], parts[0])
normalised = ".".join([root, *parts[1:]]) if len(parts) > 1 else root
normalised = normalised.replace("builtins.", "")
if normalised in fatal_calls:
detected_fatal.add(normalised)
elif normalised in warning_calls:
detected_warnings.add(normalised)
self.generic_visit(node)
Analyzer().visit(tree)
if detected_fatal:
ordered = ", ".join(sorted(detected_fatal))
return False, f"Plugin uses high-risk calls: {ordered}"
if detected_warnings:
ordered = ", ".join(sorted(detected_warnings))
return True, f"Plugin uses sensitive operations: {ordered}"
return True, ""
@staticmethod
def verify_checksum(plugin_path: Path, expected_checksum: str) -> bool:
"""Verify plugin file checksum"""
actual = PluginSecurity.calculate_checksum(plugin_path)
return actual == expected_checksum
def _resolve_project_root() -> Path:
"""
Resolve the root of the PySpector project.
Prefer an explicit override via PYSPECTOR_PLUGIN_ROOT, otherwise try to
locate the repository root by searching for common project markers. As a
final fallback use the current working directory.
"""
override = os.environ.get("PYSPECTOR_PLUGIN_ROOT")
if override:
return Path(override).expanduser().resolve()
markers = (
"pyproject.toml",
"setup.cfg",
".git",
"README.md",
"src",
)
def find_root(start: Path) -> Optional[Path]:
start = start.resolve()
for candidate in (start, *start.parents):
if any((candidate / marker).exists() for marker in markers):
if (candidate / "src" / "pyspector").exists() or (candidate / "plugins").exists():
return candidate
if any((candidate / marker).exists() for marker in ("pyproject.toml", "setup.cfg", ".git")):
return candidate
return None
module_path = Path(__file__).resolve().parent
for origin in (Path.cwd(), module_path):
root = find_root(origin)
if root:
return root
return Path.cwd().resolve()
def _ensure_directory(path: Path) -> Path:
"""Create the directory if it does not already exist and return it."""
path.mkdir(parents=True, exist_ok=True)
return path
def _resolve_plugin_directory() -> Path:
"""Return the managed plugin directory inside the project root."""
project_root = _ensure_directory(_resolve_project_root())
return _ensure_directory(project_root / "plugins")
def _resolve_registry_path() -> Path:
"""Return the path to the plugin registry file inside the plugin directory."""
plugin_dir = _ensure_directory(_resolve_plugin_directory())
return plugin_dir / "plugin_registry.json"
class PluginRegistry:
"""Registry for tracking installed and trusted plugins"""
def __init__(self, registry_path: Path):
self.registry_path = registry_path
self.plugins: Dict[str, Dict[str, Any]] = {}
self._load_registry()
def _load_registry(self):
"""Load plugin registry from disk"""
if self.registry_path.exists():
try:
with open(self.registry_path, 'r') as f:
self.plugins = json.load(f)
except (json.JSONDecodeError, IOError):
self.plugins = {}
def save_registry(self):
"""Save plugin registry to disk"""
self.registry_path.parent.mkdir(parents=True, exist_ok=True)
with open(self.registry_path, 'w') as f:
json.dump(self.plugins, f, indent=2)
def register_plugin(
self,
name: str,
path: str,
checksum: str,
metadata: PluginMetadata,
trusted: bool = False
):
"""Register a plugin in the registry"""
self.plugins[name] = {
'path': path,
'checksum': checksum,
'version': metadata.version,
'author': metadata.author,
'category': metadata.category,
'trusted': trusted,
'enabled': True
}
self.save_registry()
def is_trusted(self, name: str) -> bool:
"""Check if a plugin is trusted"""
return self.plugins.get(name, {}).get('trusted', False)
def get_plugin_info(self, name: str) -> Optional[Dict[str, Any]]:
"""Get plugin information from registry"""
return self.plugins.get(name)
def list_plugins(self) -> List[Dict[str, Any]]:
"""List all registered plugins"""
return [
{'name': name, **info}
for name, info in self.plugins.items()
]
class PluginManager:
"""Manages plugin loading, validation, and execution"""
def __init__(self, plugin_dir: Path, registry_path: Path):
self.plugin_dir = plugin_dir
self.plugin_dir.mkdir(parents=True, exist_ok=True)
self.registry = PluginRegistry(registry_path)
self.loaded_plugins: Dict[str, PySpectorPlugin] = {}
_PLUGIN_NAME_PATTERN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
def _normalize_plugin_name(self, plugin_name: str) -> Optional[str]:
"""Normalise user-supplied plugin identifiers to safe module names."""
candidate = plugin_name.strip().replace("-", "_")
if not candidate:
return None
if not self._PLUGIN_NAME_PATTERN.match(candidate):
return None
return candidate
def _plugin_file_path(self, plugin_name: str) -> Path:
"""Return the on-disk path for a plugin."""
return self.plugin_dir / f"{plugin_name}.py"
def _stage_plugin(self, source_path: Path, plugin_name: str, overwrite: bool) -> Optional[Path]:
"""
Copy a plugin source file into the managed plugins directory.
Returns the destination path on success.
"""
try:
resolved_source = source_path.resolve(strict=True)
except FileNotFoundError:
print(f"[!] Plugin source '{source_path}' not found")
return None
if not resolved_source.is_file():
print(f"[!] Plugin source '{resolved_source}' must be a Python file")
return None
if resolved_source.suffix.lower() != ".py":
print(f"[!] Plugin '{resolved_source.name}' must be a '.py' file")
return None
destination = self._plugin_file_path(plugin_name)
destination.parent.mkdir(parents=True, exist_ok=True)
destination_exists = destination.exists()
if destination_exists:
try:
if os.path.samefile(destination, resolved_source):
return destination
except (FileNotFoundError, OSError):
pass
if not overwrite:
print(f"[!] Plugin '{plugin_name}' already exists at {destination}")
return None
temp_destination = destination
if destination_exists:
temp_destination = destination.with_suffix(destination.suffix + ".tmp")
try:
if temp_destination.exists():
temp_destination.unlink()
except OSError:
pass
try:
shutil.copy2(resolved_source, temp_destination)
if temp_destination != destination:
temp_destination.replace(destination)
except Exception as exc:
print(f"[!] Failed to copy plugin to '{destination}': {exc}")
try:
if temp_destination != destination and temp_destination.exists():
temp_destination.unlink()
except OSError:
pass
return None
return destination
def install_plugin_file(self, plugin_name: str, source_path: Path, overwrite: bool = False) -> Optional[Path]:
"""
Install or update a plugin file in the managed directory without trusting it.
"""
normalised = self._normalize_plugin_name(plugin_name)
if not normalised:
print(f"[!] Invalid plugin name '{plugin_name}'. Use letters, numbers, and underscores.")
return None
return self._stage_plugin(source_path, normalised, overwrite)
def discover_plugins(self) -> List[Path]:
"""Discover all plugin files in the plugin directory"""
return list(self.plugin_dir.glob("*.py"))
def load_plugin(
self,
plugin_name: str,
require_trusted: bool = True,
force_load: bool = False
) -> Optional[PySpectorPlugin]:
"""
Load a plugin by name.
Args:
plugin_name: Name of the plugin to load
require_trusted: Only load trusted plugins
force_load: Force load even if security checks fail
Returns:
Loaded plugin instance or None
"""
normalised = self._normalize_plugin_name(plugin_name)
if not normalised:
print(f"[!] Invalid plugin name '{plugin_name}'. Plugins must use letters, numbers, or underscores.")
return None
# Check if already loaded
if normalised in self.loaded_plugins:
return self.loaded_plugins[normalised]
# Find plugin file
plugin_path = self._plugin_file_path(normalised)
if not plugin_path.exists():
print(f"[!] Plugin '{normalised}' not found at {plugin_path}")
return None
# Security checks
plugin_info = self.registry.get_plugin_info(normalised)
if require_trusted and not force_load:
if not plugin_info or not plugin_info.get('trusted'):
print(f"[!] Plugin '{normalised}' is not trusted.")
print(f"[*] Use 'pyspector plugin trust {normalised}' to trust it.")
# Perform security scan
is_safe, warning = PluginSecurity.validate_plugin_code(plugin_path)
if not is_safe:
print(f"[!] Security warning: {warning}")
return None
if warning:
print(f"[*] Security note: {warning}")
# Ask for confirmation
response = input(f"Do you want to trust and load '{normalised}'? [y/N]: ").strip().lower()
if response not in ['y', 'yes']:
return None
# Verify checksum if plugin is registered
if plugin_info:
checksum = PluginSecurity.calculate_checksum(plugin_path)
if checksum != plugin_info.get('checksum'):
print(f"[!] WARNING: Plugin '{normalised}' checksum mismatch!")
print("[!] File may have been modified.")
if not force_load:
response = input("Load anyway? [y/N]: ").strip().lower()
if response not in ['y', 'yes']:
return None
# Load the plugin module
try:
spec = importlib.util.spec_from_file_location(normalised, plugin_path)
if not spec or not spec.loader:
raise ImportError(f"Cannot load plugin spec for {normalised}")
module = importlib.util.module_from_spec(spec)
sys.modules[normalised] = module
spec.loader.exec_module(module)
# Find plugin class
plugin_class = self._find_plugin_class(module)
if not plugin_class:
print(f"[!] No valid plugin class found in '{normalised}'")
return None
# Instantiate plugin
plugin_instance = plugin_class()
# Register if not already registered
if not plugin_info:
checksum = PluginSecurity.calculate_checksum(plugin_path)
self.registry.register_plugin(
normalised,
str(plugin_path),
checksum,
plugin_instance.metadata,
trusted=force_load
)
self.loaded_plugins[normalised] = plugin_instance
print(f"[+] Loaded plugin: {plugin_instance.metadata.name} v{plugin_instance.metadata.version}")
return plugin_instance
except Exception as e:
print(f"[!] Error loading plugin '{normalised}': {e}")
import traceback
traceback.print_exc()
return None
def _find_plugin_class(self, module) -> Optional[Type[PySpectorPlugin]]:
"""Find the plugin class in a module"""
for name, obj in inspect.getmembers(module, inspect.isclass):
if (issubclass(obj, PySpectorPlugin) and
obj != PySpectorPlugin and
not inspect.isabstract(obj)):
return obj
return None
def execute_plugin(
self,
plugin: PySpectorPlugin,
findings: List[Dict[str, Any]],
scan_path: Path,
plugin_config: Dict[str, Any] = None,
**kwargs
) -> Dict[str, Any]:
"""
Execute a plugin.
Args:
plugin: Plugin instance to execute
findings: PySpector findings
scan_path: Path that was scanned
plugin_config: Plugin-specific configuration
**kwargs: Additional arguments
Returns:
Plugin execution results
"""
try:
# Initialize plugin
config = plugin_config or {}
# Validate config
is_valid, error_msg = plugin.validate_config(config)
if not is_valid:
return {
'success': False,
'message': f"Invalid plugin configuration: {error_msg}",
'data': None
}
if not plugin.initialize(config):
return {
'success': False,
'message': "Plugin initialization failed",
'data': None
}
print(f"[*] Executing plugin: {plugin.metadata.name}")
# Execute plugin
result = plugin.process_findings(findings, scan_path, **kwargs)
# Cleanup
plugin.cleanup()
return result
except Exception as e:
print(f"[!] Plugin execution error: {e}")
import traceback
traceback.print_exc()
return {
'success': False,
'message': f"Plugin execution failed: {e}",
'data': None
}
def unload_plugin(self, plugin_name: str):
"""Unload a plugin"""
normalised = self._normalize_plugin_name(plugin_name)
if not normalised or normalised not in self.loaded_plugins:
return
plugin = self.loaded_plugins[normalised]
plugin.cleanup()
del self.loaded_plugins[normalised]
if normalised in sys.modules:
del sys.modules[normalised]
def list_available_plugins(self) -> List[str]:
"""List all available plugins"""
return [p.stem for p in self.discover_plugins()]
def trust_plugin(self, plugin_name: str, source_path: Optional[Path] = None, overwrite: bool = False) -> bool:
"""Mark a plugin as trusted and ensure it resides in the managed directory."""
normalised = self._normalize_plugin_name(plugin_name)
if not normalised:
print(f"[!] Invalid plugin name '{plugin_name}'. Use letters, numbers, and underscores.")
return False
if source_path is not None:
plugin_path = self._stage_plugin(source_path, normalised, overwrite=overwrite)
if not plugin_path:
return False
else:
plugin_path = self._plugin_file_path(normalised)
if not plugin_path.exists():
print(f"[!] Plugin '{normalised}' not found at {plugin_path}")
return False
# Validate plugin code
is_safe, warning = PluginSecurity.validate_plugin_code(plugin_path)
if not is_safe:
print(f"[!] Security warning: {warning}")
response = input("Trust this plugin anyway? [y/N]: ").strip().lower()
if response not in ['y', 'yes']:
return False
elif warning:
print(f"[*] Security note: {warning}")
# Calculate checksum
checksum = PluginSecurity.calculate_checksum(plugin_path)
# Try to load to get metadata
plugin = self.load_plugin(normalised, require_trusted=False, force_load=True)
if not plugin:
return False
# Update registry
self.registry.register_plugin(
normalised,
str(plugin_path),
checksum,
plugin.metadata,
trusted=True
)
print(f"[+] Plugin '{normalised}' is now trusted")
return True
# Example plugin for reference
class ExamplePlugin(PySpectorPlugin):
"""Example plugin implementation"""
@property
def metadata(self) -> PluginMetadata:
return PluginMetadata(
name="example",
version="1.0.0",
author="PySpector Team",
description="Example plugin for demonstration",
category="example"
)
def initialize(self, config: Dict[str, Any]) -> bool:
self.config = config
return True
def process_findings(
self,
findings: List[Dict[str, Any]],
scan_path: Path,
**kwargs
) -> Dict[str, Any]:
return {
'success': True,
'message': f"Processed {len(findings)} findings",
'data': {'count': len(findings)}
}
def cleanup(self) -> None:
pass
def get_plugin_manager() -> PluginManager:
"""Get or create the global plugin manager instance"""
plugin_dir = _resolve_plugin_directory()
registry_path = _resolve_registry_path()
return PluginManager(plugin_dir, registry_path)