import subprocess import shutil from loguru import logger class ToolService: """ Service for executing real security tools (nmap, sqlmap) in the background. """ def __init__(self): self.nmap_path = shutil.which("nmap") self.sqlmap_path = shutil.which("sqlmap") async def run_nmap(self, target: str) -> str: if not self.nmap_path: logger.warning("Nmap not found in PATH. Returning simulated scan.") return f"Nmap simulation for {target}:\n- Port 80/tcp OPEN (HTTP)\n- Port 443/tcp OPEN (HTTPS)\n- Service: Nginx 1.18.0" try: # Simple port scan for demonstration result = subprocess.run( [self.nmap_path, "-F", target], capture_output=True, text=True, timeout=30 ) return result.stdout except Exception as e: logger.error(f"Nmap error: {e}") return f"Error running nmap: {e}" async def run_sqlmap(self, target: str) -> str: if not self.sqlmap_path: logger.warning("Sqlmap not found in PATH. Returning simulated scan.") return f"Sqlmap simulation for {target}:\n- Testing GET parameter 'id'\n- Target is NOT vulnerable to SQL injection (Level 1)." try: # Basic vulnerability check result = subprocess.run( [self.sqlmap_path, "-u", target, "--batch", "--wizard"], capture_output=True, text=True, timeout=60 ) return result.stdout except Exception as e: logger.error(f"Sqlmap error: {e}") return f"Error running sqlmap: {e}" tool_service = ToolService()