Buckets:
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import logging | |
| import logging.handlers | |
| import os | |
| import sys | |
| from pathlib import Path | |
| from typing import Any | |
| from dotenv import load_dotenv | |
| from decoyable.scanners import deps, sast, secrets | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # /g:/TECH/DECOYABLE/main.py | |
| # DECOYABLE - Cybersecurity scanning tool for dependencies and secrets. | |
| # Scans Python projects for security vulnerabilities including exposed secrets | |
| # and missing dependencies. | |
| # Package / app metadata | |
| APP_NAME = "decoyable" | |
| VERSION = "1.2.2" | |
| def setup_logging(level: str = "INFO", logfile: Path | None = None) -> None: | |
| """ | |
| Configure root logger. | |
| level: one of DEBUG, INFO, WARNING, ERROR, CRITICAL | |
| logfile: optional Path to write logs to (rotating file). | |
| """ | |
| numeric_level = getattr(logging, level.upper(), logging.INFO) | |
| logger = logging.getLogger() | |
| logger.setLevel(numeric_level) | |
| formatter = logging.Formatter( | |
| fmt="%(asctime)s %(levelname)s [%(name)s] %(message)s", | |
| datefmt="%Y-%m-%d %H:%M:%S", | |
| ) | |
| # Console handler | |
| ch = logging.StreamHandler(sys.stdout) | |
| ch.setLevel(numeric_level) | |
| ch.setFormatter(formatter) | |
| # Remove existing handlers to avoid duplicate logs when re-imported/reused | |
| if logger.handlers: | |
| logger.handlers = [] | |
| logger.addHandler(ch) | |
| # Optional rotating file handler | |
| if logfile: | |
| fh = logging.handlers.RotatingFileHandler( | |
| filename=str(logfile), | |
| maxBytes=5 * 1024 * 1024, | |
| backupCount=3, | |
| encoding="utf-8", | |
| ) | |
| fh.setLevel(numeric_level) | |
| fh.setFormatter(formatter) | |
| logger.addHandler(fh) | |
| def load_config(path: Path | None) -> dict[str, Any]: | |
| """ | |
| Load configuration from a file. | |
| Supports JSON by default. If PyYAML is installed and file has .yaml/.yml extension, YAML is supported. | |
| Returns an empty dict if no path provided. | |
| """ | |
| if not path: | |
| return {} | |
| if not path.exists(): | |
| raise FileNotFoundError(f"Config file not found: {path}") | |
| suffix = path.suffix.lower() | |
| if suffix in {".json"}: | |
| with path.open("r", encoding="utf-8") as fh: | |
| return json.load(fh) | |
| if suffix in {".yml", ".yaml"}: | |
| try: | |
| import yaml # type: ignore | |
| except Exception as exc: | |
| raise RuntimeError( | |
| "PyYAML is required to load YAML config files. Install with 'pip install pyyaml'" | |
| ) from exc | |
| with path.open("r", encoding="utf-8") as fh: | |
| return yaml.safe_load(fh) or {} | |
| # Fallback: try JSON parse | |
| with path.open("r", encoding="utf-8") as fh: | |
| return json.load(fh) | |
| def run_main_task(config: dict[str, Any], args: argparse.Namespace) -> int: | |
| """ | |
| Core application logic for DECOYABLE scanning. | |
| Returns an exit code (0 for success). | |
| """ | |
| log = logging.getLogger(__name__) | |
| log.debug("run_main_task start: args=%s config=%s", args, config) | |
| # Handle scanning commands | |
| if hasattr(args, "scan_type"): | |
| return run_scan(args) | |
| # Legacy greeting functionality (for backward compatibility) | |
| name = args.name or config.get("name") or "World" | |
| log.info("Hello, %s!", name) | |
| if args.decoy: | |
| decoy_path = Path(args.decoy) | |
| try: | |
| decoy_path.write_text(f"decoy for {name}\n", encoding="utf-8") | |
| log.info("Wrote decoy file: %s", decoy_path) | |
| except Exception as exc: | |
| log.exception("Failed to write decoy file: %s", exc) | |
| return 2 | |
| log.debug("run_main_task completed successfully") | |
| return 0 | |
| def run_scan(args: argparse.Namespace) -> int: | |
| """ | |
| Run security scans based on command line arguments. | |
| """ | |
| log = logging.getLogger(__name__) | |
| scan_type = getattr(args, "scan_type", "all") | |
| target_path = getattr(args, "path", ".") | |
| output_format = getattr(args, "format", "text") | |
| log.info(f"Starting {scan_type} scan on: {target_path}") | |
| # Collect all issues for JSON output | |
| all_issues = [] | |
| has_issues = False | |
| try: | |
| if scan_type in ("secrets", "all"): | |
| log.info("Scanning for exposed secrets...") | |
| findings = secrets.scan_paths([target_path]) | |
| if findings: | |
| has_issues = True | |
| log.warning(f"Found {len(findings)} potential secrets:") | |
| for finding in findings: | |
| issue = { | |
| "file": finding.filename, | |
| "line": finding.lineno, | |
| "type": "SECRET", | |
| "severity": "high", | |
| "title": f"Hardcoded {finding.secret_type}", | |
| "description": f"Found potential {finding.secret_type} secret", | |
| "value": finding.masked() | |
| } | |
| all_issues.append(issue) | |
| if output_format != "json": | |
| line_display = f"{finding.filename}:{finding.lineno} [{finding.secret_type}] {finding.masked()}" | |
| print(line_display) | |
| if output_format == "verbose": | |
| ctx_display = f" Context: {finding.context}" | |
| print(ctx_display) | |
| if scan_type == "secrets" and output_format != "json": | |
| return 1 # Exit with error if secrets found | |
| else: | |
| log.info("No secrets found.") | |
| if scan_type in ("deps", "all"): | |
| log.info("Scanning for dependency issues...") | |
| missing_imports, import_mapping = deps.missing_dependencies(target_path) | |
| if missing_imports: | |
| has_issues = True | |
| log.warning(f"Found {len(missing_imports)} missing dependencies:") | |
| for imp in sorted(missing_imports): | |
| providers = import_mapping.get(imp, []) | |
| issue = { | |
| "file": target_path, | |
| "line": 0, | |
| "type": "MISSING_DEPENDENCY", | |
| "severity": "medium", | |
| "title": f"Missing dependency: {imp}", | |
| "description": f"Import '{imp}' not found", | |
| "providers": providers if providers else [] | |
| } | |
| all_issues.append(issue) | |
| if output_format != "json": | |
| if providers: | |
| print(f"{imp} -> {', '.join(providers)}") | |
| else: | |
| print(f"{imp} -> (no known providers)") | |
| if scan_type == "deps" and output_format != "json": | |
| return 1 # Exit with error if missing deps | |
| else: | |
| log.info("All dependencies appear to be satisfied.") | |
| if scan_type in ("sast", "all"): | |
| log.info("Performing Static Application Security Testing (SAST)...") | |
| sast_results = sast.scan_sast(target_path) | |
| vulnerabilities = sast_results.get("vulnerabilities", []) | |
| summary = sast_results.get("summary", {}) | |
| if vulnerabilities: | |
| has_issues = True | |
| log.warning(f"Found {len(vulnerabilities)} potential security vulnerabilities:") | |
| severity_order = ["CRITICAL", "HIGH", "MEDIUM", "LOW", "INFO"] | |
| for vuln in sorted( | |
| vulnerabilities, | |
| key=lambda x: severity_order.index( | |
| x["severity"].value if hasattr(x["severity"], "value") else str(x["severity"]) | |
| ), | |
| ): | |
| severity = vuln["severity"].value if hasattr(vuln["severity"], "value") else vuln["severity"] | |
| vuln_type = ( | |
| vuln["vulnerability_type"].value | |
| if hasattr(vuln["vulnerability_type"], "value") | |
| else vuln["vulnerability_type"] | |
| ) | |
| # Add to issues list for JSON output | |
| issue = { | |
| "file": vuln['file_path'], | |
| "line": vuln['line_number'], | |
| "type": vuln_type, | |
| "severity": severity.lower(), | |
| "title": f"{vuln_type} vulnerability", | |
| "description": vuln['description'], | |
| "recommendation": vuln['recommendation'], | |
| "code_snippet": vuln['code_snippet'] | |
| } | |
| all_issues.append(issue) | |
| if output_format != "json": | |
| print(f"[{severity}] {vuln_type} - {vuln['file_path']}:{vuln['line_number']}") | |
| print(f" {vuln['description']}") | |
| if output_format == "verbose": | |
| print(f" Recommendation: {vuln['recommendation']}") | |
| print(" Code snippet:") | |
| for line in vuln["code_snippet"].split("\n"): | |
| print(f" {line}") | |
| print() | |
| if scan_type == "sast" and output_format != "json": | |
| return 1 # Exit with error if vulnerabilities found | |
| else: | |
| log.info("No security vulnerabilities found.") | |
| # Print summary | |
| if summary and output_format != "json": | |
| print(f"\nSummary: {summary['total_vulnerabilities']} vulnerabilities found") | |
| print(f"Files scanned: {summary['files_scanned']}") | |
| if summary["severity_breakdown"]: | |
| print("Severity breakdown:") | |
| for severity, count in summary["severity_breakdown"].items(): | |
| print(f" {severity}: {count}") | |
| # Output JSON format if requested | |
| if output_format == "json": | |
| import json | |
| output = { | |
| "scan_type": scan_type, | |
| "target_path": target_path, | |
| "issues": all_issues, | |
| "summary": { | |
| "total_issues": len(all_issues), | |
| "has_issues": has_issues | |
| } | |
| } | |
| print(json.dumps(output, indent=2)) | |
| return 1 if has_issues else 0 | |
| log.info("Scan completed successfully.") | |
| return 0 | |
| except Exception as exc: | |
| log.exception(f"Scan failed: {exc}") | |
| return 1 | |
| def run_ai_analyze(config: dict[str, Any], args: argparse.Namespace) -> int: | |
| """ | |
| Run AI-powered comprehensive security analysis (WOW MODE!). | |
| Returns an exit code (0 for success). | |
| """ | |
| log = logging.getLogger(__name__) | |
| print("\n" + "="*80) | |
| print("🚀 DECOYABLE AI-POWERED SECURITY ANALYSIS".center(80)) | |
| print("="*80 + "\n") | |
| target_path = getattr(args, "path", ".") | |
| deploy_defense = getattr(args, "deploy_defense", False) | |
| show_dashboard = getattr(args, "dashboard", False) | |
| try: | |
| import asyncio | |
| from decoyable.orchestrator import get_orchestrator | |
| # Get orchestrator instance | |
| orchestrator = get_orchestrator() | |
| # Run comprehensive analysis | |
| print("🔍 Initializing AI systems...") | |
| print(" ✓ Predictive Threat Intelligence") | |
| print(" ✓ Behavioral Anomaly Detection") | |
| print(" ✓ Adaptive Honeypot System") | |
| print(" ✓ Attack Pattern Learning\n") | |
| # Run analysis | |
| loop = asyncio.get_event_loop() | |
| report = loop.run_until_complete(orchestrator.analyze_codebase_comprehensive(target_path)) | |
| # Display results | |
| print("\n" + "="*80) | |
| print(f"📊 ANALYSIS RESULTS - Risk Level: {report['risk_emoji']} {report['overall_risk_level']}") | |
| print("="*80) | |
| print(f"\n📈 Overall Risk Score: {report['overall_risk_score']:.1f}") | |
| print(f"⏱️ Analysis Duration: {report['analysis_duration_seconds']:.2f}s") | |
| summary = report['summary'] | |
| print(f"\n🔍 Traditional Vulnerabilities Found: {summary['total_vulnerabilities']}") | |
| print(f" ⚠️ Critical: {summary['critical_vulnerabilities']}") | |
| print(f" 🔴 High: {summary['high_vulnerabilities']}") | |
| print(f"\n🤖 AI Threat Predictions: {summary['predicted_threats']}") | |
| print(f"⛓️ Exploit Chains Detected: {summary['exploit_chains_detected']}") | |
| # Show top threat predictions | |
| if report.get('threat_predictions'): | |
| print("\n" + "-"*80) | |
| print("🎯 TOP PREDICTED THREATS:") | |
| print("-"*80) | |
| for i, pred in enumerate(report['threat_predictions'][:5], 1): | |
| print(f"\n{i}. {pred['threat_type'].upper()}") | |
| print(f" Probability: {pred['probability']} | Confidence: {pred['confidence']}") | |
| print(f" Severity: {pred['severity']} | Risk Score: {pred['risk_score']:.1f}") | |
| print(f" Time to Exploitation: {pred['time_to_exploitation']}") | |
| print(f" Top Recommendations:") | |
| for rec in pred['recommendations']: | |
| print(f" • {rec}") | |
| # Show exploit chains | |
| if report.get('exploit_chains'): | |
| print("\n" + "-"*80) | |
| print("⛓️ CRITICAL EXPLOIT CHAINS:") | |
| print("-"*80) | |
| for chain in report['exploit_chains']: | |
| print(f"\n🔴 {chain['severity']}: {chain['impact']}") | |
| print(f" File: {chain['file_path']}") | |
| print(f" Chain: {' → '.join(chain['vulnerability_types'])}") | |
| print(f" Combined Risk Score: {chain['combined_risk_score']}") | |
| print(f" Exploitation Steps:") | |
| for step in chain['exploitation_steps']: | |
| print(f" {step}") | |
| # Show defense recommendations | |
| defense = report.get('defense_recommendations', {}) | |
| if defense: | |
| print("\n" + "="*80) | |
| print("🛡️ DEFENSE STRATEGY") | |
| print("="*80) | |
| if defense.get('immediate_actions'): | |
| print("\n🚨 IMMEDIATE ACTIONS:") | |
| for action in defense['immediate_actions']: | |
| print(f" • {action}") | |
| if defense.get('short_term_fixes'): | |
| print("\n⚡ SHORT-TERM FIXES:") | |
| for fix in defense['short_term_fixes'][:5]: | |
| print(f" • {fix}") | |
| if defense.get('honeypot_deployment'): | |
| honeypot = defense['honeypot_deployment'] | |
| if honeypot.get('recommended'): | |
| print(f"\n🍯 HONEYPOT DEPLOYMENT RECOMMENDED:") | |
| print(f" Type: {honeypot.get('type')}") | |
| print(f" Targets: {', '.join(honeypot.get('targets', [])[:3])}") | |
| # Show dashboard if requested | |
| if show_dashboard: | |
| print("\n" + "="*80) | |
| print("📊 LIVE SECURITY DASHBOARD") | |
| print("="*80) | |
| dashboard = loop.run_until_complete(orchestrator.get_security_dashboard_data()) | |
| print(f"\n{dashboard['overall_status']}") | |
| print(f"Defense Score: {dashboard['defense_score']:.1f}/100") | |
| metrics = dashboard['metrics'] | |
| print(f"\n📈 METRICS:") | |
| print(f" 🛡️ Attacks Blocked: {metrics['total_attacks_blocked']}") | |
| print(f" 🔮 Threats Predicted: {metrics['threats_predicted']}") | |
| print(f" ⏰ Attacker Time Wasted: {metrics['attacker_time_wasted_hours']:.1f} hours") | |
| print(f" 🍯 Active Honeypots: {metrics['active_honeypots']}") | |
| print(f" 👤 Unique Attackers Tracked: {metrics['unique_attackers_tracked']}") | |
| print(f" 🚨 Anomalies Detected: {metrics['behavioral_anomalies_detected']}") | |
| print(f" 💀 Potential Zero-Days: {metrics['potential_zero_days']}") | |
| print(f"\n🤖 AI SYSTEMS STATUS:") | |
| for system, status in dashboard['ai_systems_status'].items(): | |
| print(f" {status} {system.replace('_', ' ').title()}") | |
| # Deploy defense if requested | |
| if deploy_defense and report.get('exploit_chains'): | |
| print("\n" + "="*80) | |
| print("🚀 DEPLOYING ACTIVE DEFENSE MEASURES...") | |
| print("="*80) | |
| # Simulate deployment | |
| attack_data = { | |
| "ip_address": "192.168.1.100", | |
| "attack_type": "exploit_chain_attempt", | |
| "tools": ["automated_scanner"], | |
| "target": report['exploit_chains'][0]['file_path'], | |
| "duration": 300, | |
| } | |
| defense_result = loop.run_until_complete(orchestrator.deploy_active_defense(attack_data)) | |
| print(f"\n✅ {defense_result['status'].upper()}") | |
| print(f"\n👤 Attacker Profile:") | |
| profile = defense_result['attacker_profile'] | |
| print(f" Skill Level: {profile['skill_level']}") | |
| print(f" Sophistication: {profile['sophistication_score']:.1%}") | |
| honeypot_info = defense_result['honeypot_deployed'] | |
| print(f"\n🍯 Honeypot Deployed:") | |
| print(f" Type: {honeypot_info['type']}") | |
| print(f" Complexity: {honeypot_info['complexity']}") | |
| print(f" Fake Vulnerabilities: {honeypot_info['fake_vulnerabilities']}") | |
| print(f"\n🛡️ Countermeasures: {'ACTIVE' if defense_result['countermeasures_active'] else 'INACTIVE'}") | |
| print("\n" + "="*80) | |
| print("✨ ANALYSIS COMPLETE! Your security is now AI-enhanced! ✨".center(80)) | |
| print("="*80 + "\n") | |
| return 0 | |
| except Exception as exc: | |
| log.exception(f"AI analysis failed: {exc}") | |
| print(f"\n❌ Error: {exc}") | |
| return 1 | |
| def run_ai_status() -> int: | |
| """ | |
| Show AI provider status and available models. | |
| Returns an exit code (0 for success). | |
| """ | |
| print("\n" + "="*60) | |
| print("🤖 DECOYABLE AI STATUS CHECK".center(60)) | |
| print("="*60 + "\n") | |
| try: | |
| from decoyable.llm.model_router import get_router | |
| router = get_router() | |
| router.print_status() | |
| return 0 | |
| except Exception as exc: | |
| print(f"❌ Failed to check AI status: {exc}") | |
| print("\nℹ️ AI features are optional. DECOYABLE works without them.") | |
| return 1 | |
| def run_fix_command(config: dict[str, Any], args: argparse.Namespace) -> int: | |
| """ | |
| Apply automated fixes for security issues. | |
| Returns an exit code (0 for success). | |
| """ | |
| log = logging.getLogger(__name__) | |
| scan_results_path = getattr(args, "scan_results", None) | |
| auto_approve = getattr(args, "auto_approve", False) | |
| confirm = getattr(args, "confirm", False) | |
| if not scan_results_path: | |
| log.error("Scan results file is required (--scan-results)") | |
| return 1 | |
| if not scan_results_path.exists(): | |
| log.error("Scan results file not found: %s", scan_results_path) | |
| return 1 | |
| # Load scan results | |
| try: | |
| # Use utf-8-sig to handle BOM (Byte Order Mark) from Windows editors | |
| with scan_results_path.open("r", encoding="utf-8-sig") as f: | |
| scan_data = json.load(f) | |
| except Exception as exc: | |
| log.exception("Failed to load scan results: %s", exc) | |
| return 1 | |
| issues = scan_data.get("issues", []) | |
| if not issues: | |
| log.info("No issues found in scan results") | |
| return 0 | |
| log.info("Found %d issues to fix", len(issues)) | |
| # Group issues by file | |
| issues_by_file = {} | |
| for issue in issues: | |
| file_path = issue.get("file", "") | |
| if file_path not in issues_by_file: | |
| issues_by_file[file_path] = [] | |
| issues_by_file[file_path].append(issue) | |
| # Apply fixes | |
| fixed_count = 0 | |
| for file_path, file_issues in issues_by_file.items(): | |
| if not file_path: | |
| continue | |
| full_path = Path(file_path) | |
| if not full_path.exists(): | |
| log.warning("File not found: %s", file_path) | |
| continue | |
| log.info("Fixing %d issues in %s", len(file_issues), file_path) | |
| try: | |
| # Read file content | |
| with full_path.open("r", encoding="utf-8") as f: | |
| content = f.read() | |
| original_content = content | |
| lines = content.splitlines() | |
| # Apply fixes to this file | |
| for issue in file_issues: | |
| severity = issue.get("severity", "low") | |
| issue_type = issue.get("type", "unknown") | |
| title = issue.get("title", "") | |
| # Skip low severity issues unless auto-approve | |
| if severity == "low" and not auto_approve: | |
| continue | |
| # Apply specific fixes based on issue type and title | |
| if _apply_fix_to_issue(lines, issue): | |
| fixed_count += 1 | |
| log.info("Fixed: %s", title) | |
| # Write back if changed | |
| new_content = "\n".join(lines) | |
| if new_content != original_content: | |
| if confirm and not auto_approve: | |
| # In a real implementation, you'd prompt for confirmation | |
| # For now, we'll assume confirmation | |
| pass | |
| with full_path.open("w", encoding="utf-8") as f: | |
| f.write(new_content) | |
| log.info("Updated file: %s", file_path) | |
| except Exception as exc: | |
| log.exception("Failed to fix issues in %s: %s", file_path, exc) | |
| log.info("Fixed %d out of %d issues", fixed_count, len(issues)) | |
| return 0 if fixed_count > 0 else 1 | |
| def _apply_fix_to_issue(lines: list[str], issue: dict[str, Any]) -> bool: | |
| """Apply a fix for a specific issue. Returns True if fix was applied.""" | |
| import re | |
| title = issue.get("title", "").lower() | |
| issue_type = issue.get("type", "") | |
| line_num = issue.get("line", 0) - 1 # Convert to 0-based indexing | |
| if line_num >= len(lines): | |
| return False | |
| line = lines[line_num] | |
| indent = len(line) - len(line.lstrip()) | |
| indent_str = " " * indent | |
| # ===== SQL INJECTION AUTO-FIX ===== | |
| if "sql" in title.lower() and "injection" in title.lower(): | |
| # Pattern 1: SELECT with % formatting: query = "SELECT ... WHERE id = %s" % uid | |
| match = re.search(r'(\w+)\s*=\s*["\'](.*(SELECT|INSERT|UPDATE|DELETE).*\%s.*)["\'].*\%\s*(\w+)', line, re.IGNORECASE) | |
| if match: | |
| var_name = match.group(1) | |
| query_template = match.group(2) | |
| param_var = match.group(4) | |
| # Replace %s with ? | |
| safe_query = query_template.replace("%s", "?") | |
| # Generate fix | |
| lines[line_num] = f'{indent_str}{var_name} = "{safe_query}"' | |
| lines.insert(line_num + 1, f'{indent_str}{var_name}_params = ({param_var},)') | |
| return True | |
| # Pattern 2: Multiple parameters with tuple formatting | |
| match = re.search(r'(\w+)\s*=\s*["\'](.*(SELECT|INSERT|UPDATE|DELETE).*)["\'].*\%\s*\(([^)]+)\)', line, re.IGNORECASE) | |
| if match: | |
| var_name = match.group(1) | |
| query_template = match.group(2) | |
| params_str = match.group(4) | |
| # Count %s occurrences | |
| param_count = query_template.count('%s') | |
| safe_query = query_template.replace("%s", "?") | |
| lines[line_num] = f'{indent_str}{var_name} = "{safe_query}"' | |
| lines.insert(line_num + 1, f'{indent_str}{var_name}_params = ({params_str})') | |
| return True | |
| # Pattern 3: String concatenation with + | |
| match = re.search(r'(\w+)\s*=\s*["\'](.*(SELECT|INSERT|UPDATE|DELETE).*)["\'].*\+\s*(\w+)', line, re.IGNORECASE) | |
| if match: | |
| var_name = match.group(1) | |
| query_template = match.group(2) | |
| param_var = match.group(4) | |
| # Add ? placeholder | |
| safe_query = query_template + " ?" | |
| lines[line_num] = f'{indent_str}{var_name} = "{safe_query}"' | |
| lines.insert(line_num + 1, f'{indent_str}{var_name}_params = ({param_var},)') | |
| return True | |
| # ===== COMMAND INJECTION AUTO-FIX ===== | |
| if "command" in title.lower() and "injection" in title.lower(): | |
| # Pattern 1: os.system with string concatenation | |
| match = re.search(r'os\.system\s*\(\s*["\']([^"\']+)["\'].*\+\s*(\w+)\s*\)', line) | |
| if match: | |
| command = match.group(1).strip() | |
| var_name = match.group(2) | |
| # Split command into parts | |
| cmd_parts = command.split() | |
| if cmd_parts: | |
| # Transform to subprocess.run with list | |
| args_list = ", ".join([f"'{part}'" for part in cmd_parts] + [var_name]) | |
| lines[line_num] = f'{indent_str}subprocess.run([{args_list}], check=True)' | |
| # Add import if not present | |
| if not any('import subprocess' in l for l in lines[:line_num]): | |
| # Find import block | |
| import_idx = 0 | |
| for i, l in enumerate(lines): | |
| if l.strip().startswith('import ') or l.strip().startswith('from '): | |
| import_idx = i + 1 | |
| lines.insert(import_idx, 'import subprocess') | |
| return True | |
| # Pattern 2: os.system with f-string or format | |
| match = re.search(r'os\.system\s*\(\s*["\']([^"\']+)["\']', line) | |
| if match and ('+' in line or '{' in line): | |
| command = match.group(1).strip() | |
| cmd_parts = command.split() | |
| if cmd_parts: | |
| # Extract variable from concatenation | |
| var_match = re.search(r'\+\s*(\w+)', line) | |
| if var_match: | |
| var_name = var_match.group(1) | |
| args_list = ", ".join([f"'{part}'" for part in cmd_parts] + [var_name]) | |
| lines[line_num] = f'{indent_str}subprocess.run([{args_list}], check=True)' | |
| # Add import if needed | |
| if not any('import subprocess' in l for l in lines[:line_num]): | |
| import_idx = 0 | |
| for i, l in enumerate(lines): | |
| if l.strip().startswith('import ') or l.strip().startswith('from '): | |
| import_idx = i + 1 | |
| lines.insert(import_idx, 'import subprocess') | |
| return True | |
| # ===== HARDCODED SECRETS FIX ===== | |
| if "hardcoded" in title and "secret" in title: | |
| # Look for patterns like SECRET_KEY = "value" or API_KEY = 'value' | |
| pattern = r'(\w+)\s*=\s*["\']([^"\']+)["\']' | |
| match = re.search(pattern, line) | |
| if match: | |
| var_name = match.group(1) | |
| # Replace with environment variable | |
| lines[line_num] = f'{indent_str}{var_name} = os.getenv("{var_name}", "")' | |
| return True | |
| # ===== WEAK CRYPTO FIX ===== | |
| if "md5" in title.lower() or "weak crypto" in title.lower(): | |
| if "md5" in line.lower(): | |
| lines[line_num] = line.replace("md5", "sha256").replace("MD5", "SHA256") | |
| return True | |
| # ===== INSECURE RANDOM FIX ===== | |
| if "insecure random" in title.lower() or "weak random" in title.lower(): | |
| if "random." in line: | |
| lines[line_num] = line.replace("random.random()", "secrets.token_hex(16)") | |
| lines[line_num] = lines[line_num].replace("random.randint", "secrets.randbelow") | |
| lines[line_num] = lines[line_num].replace("random.choice", "secrets.choice") | |
| # Add import if needed | |
| if not any('import secrets' in l for l in lines[:line_num]): | |
| import_idx = 0 | |
| for i, l in enumerate(lines): | |
| if l.strip().startswith('import ') or l.strip().startswith('from '): | |
| import_idx = i + 1 | |
| lines.insert(import_idx, 'import secrets') | |
| return True | |
| return False | |
| def build_arg_parser() -> argparse.ArgumentParser: | |
| p = argparse.ArgumentParser(prog=APP_NAME, description="DECOYABLE CLI") | |
| p.add_argument("--version", action="version", version=f"%(prog)s {VERSION}") | |
| p.add_argument( | |
| "-v", | |
| "--verbose", | |
| action="count", | |
| default=0, | |
| help="Increase verbosity (repeatable)", | |
| ) | |
| p.add_argument("--logfile", type=Path, help="Optional path to a rotating log file") | |
| p.add_argument("--config", type=Path, help="Path to JSON/YAML configuration file") | |
| sub = p.add_subparsers(dest="command", required=False) | |
| # default/run command | |
| run = sub.add_parser("run", help="Run the main task") | |
| run.add_argument("--name", "-n", help="Name to greet") | |
| run.add_argument("--decoy", "-d", help="Path to write a decoy file (optional)") | |
| # scan command | |
| scan = sub.add_parser("scan", help="Scan for security vulnerabilities") | |
| scan.add_argument( | |
| "scan_type", | |
| choices=["secrets", "deps", "sast", "all"], | |
| help="Type of scan to perform", | |
| ) | |
| scan.add_argument("path", nargs="?", default=".", help="Path to scan (default: current directory)") | |
| scan.add_argument("--format", choices=["text", "verbose", "json"], default="text", help="Output format") | |
| # fix command | |
| fix = sub.add_parser("fix", help="Apply automated fixes for security issues") | |
| fix.add_argument("--scan-results", type=Path, help="Path to JSON file with scan results") | |
| fix.add_argument("--auto-approve", action="store_true", help="Apply fixes without confirmation") | |
| fix.add_argument("--confirm", action="store_true", help="Confirm before applying fixes") | |
| # test command (lightweight) | |
| tst = sub.add_parser("test", help="Run self-test checks") | |
| tst.add_argument("--fast", action="store_true", help="Run a fast subset of tests") | |
| # ai-analyze command (NEW!) | |
| ai_analyze = sub.add_parser("ai-analyze", help="🚀 AI-powered comprehensive security analysis (WOW MODE!)") | |
| ai_analyze.add_argument("path", type=str, help="Path to analyze") | |
| ai_analyze.add_argument("--deploy-defense", action="store_true", help="Deploy active defense measures") | |
| ai_analyze.add_argument("--dashboard", action="store_true", help="Show live security dashboard") | |
| # ai-status command (NEW in v1.2.0!) | |
| ai_status = sub.add_parser("ai-status", help="🤖 Show AI provider status and configuration") | |
| return p | |
| def main(argv: list[str] | None = None) -> int: | |
| """ | |
| Application entry point. Returns an exit code. | |
| """ | |
| argv = argv if argv is not None else sys.argv[1:] | |
| parser = build_arg_parser() | |
| args = parser.parse_args(argv) | |
| # Logging level: default INFO, -v increases verbosity | |
| level = "WARNING" | |
| if args.verbose >= 2: | |
| level = "DEBUG" | |
| elif args.verbose == 1: | |
| level = "INFO" | |
| setup_logging(level=level, logfile=args.logfile if "logfile" in args else None) | |
| log = logging.getLogger(__name__) | |
| log.debug("Starting %s version %s", APP_NAME, VERSION) | |
| # Load config if provided | |
| try: | |
| config = load_config(args.config) if getattr(args, "config", None) else {} | |
| except Exception as exc: | |
| log.exception("Failed to load configuration: %s", exc) | |
| return 3 | |
| # Dispatch commands | |
| cmd = getattr(args, "command", None) or "run" | |
| try: | |
| if cmd in ("run", "scan"): | |
| return run_main_task(config, args) | |
| elif cmd == "fix": | |
| return run_fix_command(config, args) | |
| elif cmd == "ai-analyze": | |
| return run_ai_analyze(config, args) | |
| elif cmd == "ai-status": | |
| return run_ai_status() | |
| elif cmd == "test": | |
| log.info("Running self-tests (fast=%s)", getattr(args, "fast", False)) | |
| # Simple internal checks | |
| if getattr(args, "fast", False): | |
| log.info("Fast tests passed") | |
| return 0 | |
| log.info("Full tests passed") | |
| return 0 | |
| else: | |
| log.error("Unknown command: %s", cmd) | |
| return 4 | |
| except KeyboardInterrupt: | |
| log.warning("Interrupted by user") | |
| return 130 | |
| except Exception: | |
| log.exception("Unhandled exception") | |
| return 1 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |
Xet Storage Details
- Size:
- 33.2 kB
- Xet hash:
- 7fb84e30080bb4c21be8871720fbd9775fa3e6fc2025352cac5de1482828a7ec
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.