Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /autonomous /exploit_validator_example.py
| #!/usr/bin/env python3 | |
| """ | |
| Exploit Validator Usage Examples | |
| Beispiele für die Nutzung des Exploit Validators mit Sandboxed Execution. | |
| ⚠️ LEGAL WARNING: Only scan systems you own or have EXPLICIT WRITTEN | |
| permission to test. Unauthorized scanning is ILLEGAL. | |
| """ | |
| import asyncio | |
| import logging | |
| # Import Exploit Validator | |
| from autonomous.exploit_validator import ( | |
| ExploitType, | |
| ExploitValidator, | |
| ExploitValidatorPool, | |
| SandboxConfig, | |
| ScopeConfig, | |
| ValidationLevel, | |
| ) | |
| from autonomous.sandbox_executor import ResourceLimits, SandboxType | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| async def example_basic_validation(): | |
| """ | |
| Beispiel 1: Basis-Validierung eines Exploits | |
| """ | |
| print("=" * 60) | |
| print("Beispiel 1: Basis Exploit Validierung") | |
| print("=" * 60) | |
| # Initialisiere Validator mit SAFE Level | |
| validator = ExploitValidator( | |
| validation_level=ValidationLevel.SAFE, scope_config=ScopeConfig(allowed_hosts=["scanme.nmap.org", "example.com"]) | |
| ) | |
| # Exploit Code (nur Proof-of-Concept, kein Schaden) | |
| exploit_code = """ | |
| import requests | |
| import time | |
| # Zeitbasierte SQLi Detection (SAFE) | |
| url = "http://scanme.nmap.org" | |
| payload = "1' AND SLEEP(5)--" | |
| start = time.time() | |
| response = requests.get(url, timeout=30) | |
| elapsed = time.time() - start | |
| print(f"Response time: {elapsed:.2f}s") | |
| print(f"Status code: {response.status_code}") | |
| if elapsed > 4: | |
| print("Potential time-based SQL injection detected") | |
| else: | |
| print("No timing anomaly detected") | |
| """ | |
| # Führe Validierung durch | |
| result = await validator.validate( | |
| exploit_code=exploit_code, | |
| target="scanme.nmap.org", | |
| exploit_type=ExploitType.WEB_SQLI, | |
| cve_id="CVE-2023-TEST", | |
| level=ValidationLevel.SAFE, | |
| ) | |
| print("\nErgebnis:") | |
| print(f" Success: {result.success}") | |
| print(f" Status: {result.status.name}") | |
| print(f" Execution Time: {result.execution_time:.2f}s") | |
| print(f" Output: {result.output[:200]}...") | |
| if result.error: | |
| print(f" Error: {result.error}") | |
| return result | |
| async def example_cve_validation(): | |
| """ | |
| Beispiel 2: CVE-basierte Validierung | |
| """ | |
| print("\n" + "=" * 60) | |
| print("Beispiel 2: CVE-basierte Validierung") | |
| print("=" * 60) | |
| validator = ExploitValidator( | |
| validation_level=ValidationLevel.SAFE, scope_config=ScopeConfig(allowed_hosts=["example.com"]) | |
| ) | |
| # Füge CVE zur Datenbank hinzu | |
| validator.add_to_cve_db( | |
| cve_id="CVE-2021-44228", | |
| description="Apache Log4j2 JNDI Injection", | |
| cvss_score=10.0, | |
| severity="critical", | |
| affected_versions=["2.0-beta9", "2.14.1"], | |
| exploit_available=True, | |
| validation_level=ValidationLevel.SAFE, | |
| ) | |
| # Prüfe CVE-Anwendbarkeit | |
| is_applicable, reason = await validator.check_cve_applicable( | |
| cve_id="CVE-2021-44228", target="example.com", target_version="2.14.0" | |
| ) | |
| print(f"CVE Anwendbarkeit: {is_applicable}") | |
| print(f"Grund: {reason}") | |
| # Validiere CVE | |
| result = await validator.validate_cve(cve_id="CVE-2021-44228", target="http://example.com", level=ValidationLevel.SAFE) | |
| print("\nErgebnis:") | |
| print(f" Success: {result.success}") | |
| print(f" Status: {result.status.name}") | |
| return result | |
| async def example_sandboxed_execution(): | |
| """ | |
| Beispiel 3: Sandboxed Execution mit Resource Limits | |
| """ | |
| print("\n" + "=" * 60) | |
| print("Beispiel 3: Sandboxed Execution") | |
| print("=" * 60) | |
| # Konfiguriere Sandbox mit Resource Limits | |
| sandbox_config = SandboxConfig( | |
| sandbox_type=SandboxType.DOCKER, | |
| resource_limits=ResourceLimits( | |
| cpu_cores=1.0, | |
| memory_mb=512, | |
| network_enabled=False, | |
| read_only_root=True, | |
| timeout_seconds=300, # Isolated network | |
| ), | |
| image="zenai/exploit-sandbox:latest", | |
| ) | |
| validator = ExploitValidator( | |
| validation_level=ValidationLevel.SAFE, sandbox_config=sandbox_config, enable_playwright=True, enable_video=False | |
| ) | |
| # Exploit Code | |
| exploit_code = """ | |
| import requests | |
| import json | |
| # Teste auf XSS Reflektion | |
| url = "http://scanme.nmap.org" | |
| payload = "<script>alert(1)</script>" | |
| response = requests.get(url, params={"test": payload}, timeout=10) | |
| result = { | |
| "status_code": response.status_code, | |
| "payload_reflected": payload in response.text, | |
| "response_length": len(response.text) | |
| } | |
| print(json.dumps(result, indent=2)) | |
| """ | |
| result = await validator.validate( | |
| exploit_code=exploit_code, target="scanme.nmap.org", exploit_type=ExploitType.WEB_XSS, level=ValidationLevel.SAFE | |
| ) | |
| print("Sandbox Ergebnis:") | |
| print(f" Success: {result.success}") | |
| print(f" Container ID: {result.validator_id}") | |
| print(f" Output: {result.output[:300]}...") | |
| return result | |
| async def example_evidence_collection(): | |
| """ | |
| Beispiel 4: Evidence Collection mit Screenshot | |
| """ | |
| print("\n" + "=" * 60) | |
| print("Beispiel 4: Evidence Collection") | |
| print("=" * 60) | |
| validator = ExploitValidator( | |
| validation_level=ValidationLevel.SAFE, | |
| enable_playwright=True, | |
| output_dir="/tmp/exploit_evidence", # nosec B108 | |
| ) | |
| # Exploit durchführen | |
| exploit_code = """ | |
| import requests | |
| url = "http://scanme.nmap.org" | |
| response = requests.get(url, timeout=10) | |
| print(f"Status: {response.status_code}") | |
| print(f"Server: {response.headers.get('Server', 'unknown')}") | |
| """ | |
| result = await validator.validate( | |
| exploit_code=exploit_code, | |
| target="scanme.nmap.org", | |
| exploit_type=ExploitType.WEB_XSS, | |
| cve_id="CVE-2024-TEST", | |
| level=ValidationLevel.SAFE, | |
| ) | |
| if result.success and result.exploit_evidence: | |
| # Speichere Evidence | |
| evidence_path = await validator.collect_evidence(result=result, case_id="PT-2024-001") | |
| print(f"Evidence gespeichert unter: {evidence_path}") | |
| print("\nEvidence Details:") | |
| print(f" CVE: {result.exploit_evidence.cve_id}") | |
| print(f" Target: {result.exploit_evidence.target}") | |
| print(f" Success: {result.exploit_evidence.success}") | |
| print(f" Screenshot: {'Ja' if result.exploit_evidence.screenshot else 'Nein'}") | |
| print(f" Risk Level: {result.exploit_evidence.risk_level}") | |
| print(f" Chain of Custody: {len(result.exploit_evidence.chain_of_custody)} Einträge") | |
| return result | |
| async def example_rate_limiting(): | |
| """ | |
| Beispiel 5: Rate Limiting Demonstration | |
| """ | |
| print("\n" + "=" * 60) | |
| print("Beispiel 5: Rate Limiting (max 3/hour)") | |
| print("=" * 60) | |
| validator = ExploitValidator( | |
| validation_level=ValidationLevel.SAFE, scope_config=ScopeConfig(allowed_hosts=["scanme.nmap.org"]) | |
| ) | |
| exploit_code = """ | |
| import requests | |
| response = requests.get("http://scanme.nmap.org", timeout=10) | |
| print(f"Status: {response.status_code}") | |
| """ | |
| # Führe mehrere Validierungen durch | |
| for i in range(5): | |
| print(f"\nValidierung {i + 1}:") | |
| result = await validator.validate( | |
| exploit_code=exploit_code, target="scanme.nmap.org", exploit_type=ExploitType.WEB_XSS, level=ValidationLevel.SAFE | |
| ) | |
| print(f" Status: {result.status.name}") | |
| if result.status.name == "BLOCKED": | |
| print(f" Grund: {result.error}") | |
| print(" -> Rate Limit erreicht!") | |
| break | |
| return result | |
| async def example_concurrent_validation(): | |
| """ | |
| Beispiel 6: Parallele Validierung mit Pool | |
| """ | |
| print("\n" + "=" * 60) | |
| print("Beispiel 6: Parallele Validierung") | |
| print("=" * 60) | |
| # Erstelle Validator Pool | |
| pool = ExploitValidatorPool( | |
| pool_size=3, | |
| validation_level=ValidationLevel.SAFE, | |
| scope_config=ScopeConfig(allowed_hosts=["scanme.nmap.org", "example.com", "test.com"]), | |
| ) | |
| await pool.initialize() | |
| # Mehrere Exploits gleichzeitig | |
| exploits = [ | |
| { | |
| "target": "scanme.nmap.org", | |
| "type": ExploitType.WEB_SQLI, | |
| "code": "import requests; r = requests.get('http://scanme.nmap.org'); print(r.status_code)", | |
| }, | |
| { | |
| "target": "example.com", | |
| "type": ExploitType.WEB_XSS, | |
| "code": "import requests; r = requests.get('http://example.com'); print(r.status_code)", | |
| }, | |
| { | |
| "target": "test.com", | |
| "type": ExploitType.WEB_LFI, | |
| "code": "import requests; r = requests.get('http://test.com'); print(r.status_code)", | |
| }, | |
| ] | |
| # Führe parallele Validierungen durch | |
| tasks = [] | |
| for exploit in exploits: | |
| task = pool.submit(exploit_code=exploit["code"], target=exploit["target"], exploit_type=exploit["type"]) | |
| tasks.append(task) | |
| results = await asyncio.gather(*tasks, return_exceptions=True) | |
| print("\nErgebnisse:") | |
| for i, result in enumerate(results): | |
| if isinstance(result, Exception): | |
| print(f" Exploit {i + 1}: Fehler - {result}") | |
| else: | |
| print(f" Exploit {i + 1}: {result.status.name}") | |
| print("\nPool Statistiken:") | |
| print(f" Total: {pool.get_stats()['total']}") | |
| return results | |
| async def example_security_controls(): | |
| """ | |
| Beispiel 7: Sicherheitskontrollen Demonstration | |
| """ | |
| print("\n" + "=" * 60) | |
| print("Beispiel 7: Sicherheitskontrollen") | |
| print("=" * 60) | |
| # Scope mit IP Blocklist | |
| scope = ScopeConfig( | |
| allowed_hosts=["example.com"], | |
| blocked_cidrs=["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"], | |
| blocked_hosts=["localhost", "127.0.0.1", "169.254.169.254"], # AWS Metadata | |
| ) | |
| validator = ExploitValidator(validation_level=ValidationLevel.SAFE, scope_config=scope) | |
| # Teste verschiedene Targets | |
| test_targets = [ | |
| ("example.com", True), | |
| ("10.0.0.1", False), # Private IP | |
| ("127.0.0.1", False), # Loopback | |
| ("169.254.169.254", False), # AWS Metadata | |
| ] | |
| exploit_code = "print('Test')" | |
| for target, should_pass in test_targets: | |
| result = await validator.validate(exploit_code=exploit_code, target=target, exploit_type=ExploitType.WEB_XSS) | |
| status = "✓ ERLAUBT" if result.status.name != "BLOCKED" else "✗ BLOCKED" | |
| expected = "✓" if should_pass else "✗" | |
| print(f" {target}: {status} (erwartet: {expected})") | |
| if result.error: | |
| print(f" -> {result.error}") | |
| # Zeige Audit Log | |
| print("\nAudit Log Einträge:") | |
| for entry in validator.get_audit_log()[:5]: | |
| print(f" [{entry['timestamp']}] {entry['action']}") | |
| async def example_react_agent_integration(): | |
| """ | |
| Beispiel 8: Integration mit ReAct Agent | |
| """ | |
| print("\n" + "=" * 60) | |
| print("Beispiel 8: ReAct Agent Integration") | |
| print("=" * 60) | |
| from autonomous.agent_loop import AgentLoop | |
| # Erstelle Agent Loop | |
| AgentLoop(goal="Validiere CVE-2024-TEST auf example.com", target="example.com") | |
| # Initialisiere Validator | |
| validator = ExploitValidator( | |
| validation_level=ValidationLevel.SAFE, scope_config=ScopeConfig(allowed_hosts=["example.com"]) | |
| ) | |
| # Simuliere ReAct Loop | |
| print("\nReAct Loop Simulation:") | |
| print(" 1. Agent plant CVE-Validierung") | |
| print(" 2. Führt Validierung durch...") | |
| # CVE Validierung | |
| result = await validator.validate_cve(cve_id="CVE-2024-TEST", target="http://example.com", level=ValidationLevel.SAFE) | |
| print(f" 3. Ergebnis: {result.status.name}") | |
| if result.success: | |
| print(" 4. Exploit erfolgreich - sammle Evidence...") | |
| # Sammle Evidence | |
| evidence_path = await validator.collect_evidence(result) | |
| print(f" 5. Evidence gespeichert: {evidence_path}") | |
| print(" 6. Füge Finding zum Report hinzu") | |
| # Erstelle Finding | |
| finding = { | |
| "cve_id": result.cve_id, | |
| "target": result.target, | |
| "severity": result.validation_level.name, | |
| "evidence_path": evidence_path, | |
| "validator_id": result.validator_id, | |
| } | |
| print(f"\n Finding: {finding}") | |
| else: | |
| print(f" 4. Validierung fehlgeschlagen: {result.error}") | |
| return result | |
| async def main(): | |
| """ | |
| Hauptfunktion - führt alle Beispiele aus | |
| """ | |
| print("\n" + "=" * 60) | |
| print("Exploit Validator - Usage Examples") | |
| print("=" * 60) | |
| print() | |
| try: | |
| # Führe Beispiele aus | |
| await example_basic_validation() | |
| await example_cve_validation() | |
| # await example_sandboxed_execution() # Benötigt Docker | |
| # await example_evidence_collection() # Benötigt Playwright | |
| await example_rate_limiting() | |
| # await example_concurrent_validation() # Benötigt Docker | |
| await example_security_controls() | |
| await example_react_agent_integration() | |
| print("\n" + "=" * 60) | |
| print("Alle Beispiele abgeschlossen!") | |
| print("=" * 60) | |
| except Exception as e: | |
| print(f"\nFehler: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| if __name__ == "__main__": | |
| # ⚠️ WICHTIG: Nur auf autorisierten Systemen ausführen! | |
| print("⚠️ LEGAL WARNING: Dieses Beispiel ist nur für autorisierte Tests!") | |
| print() | |
| asyncio.run(main()) | |
Xet Storage Details
- Size:
- 13.3 kB
- Xet hash:
- 31d5609200a8a3e7ae338d3986b193950b9397f0a070e891d6eeefe64d209c49
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.