#!/usr/bin/env python3 """ EMGENA AI LABS - MODEL CONTEXT PROTOCOL (MCP) PRODUCTION SERVER Package: destructive_cli_bash_powershell_safety_interceptor_mcp Title: Destructive CLI Bash & PowerShell Safety Interceptor MCP Domain: Shell Command AST Analysis, rm -rf Protection, Dangerous Recursion Interception FAANG v2.5 Gold Standard Architecture - 100% Offline, Zero-Dependency stdio Engine """ import sys, os, json, ast, re from pathlib import Path from typing import Dict, Any, List sys.stdout.reconfigure(encoding="utf-8") PKG_DIR = Path(__file__).resolve().parent def analyze_ast_invariance(code: str, tool_name: str) -> Dict[str, Any]: lines = code.splitlines() stripped_lines = [l.split("#")[0].strip() for l in lines] stripped_code = "\n".join(stripped_lines) syntax_valid = False try: ast.parse(code) syntax_valid = True except SyntaxError as e: return {"status": "SYNTAX_ERROR", "error": str(e), "is_safe": False, "violations": [{"tool": tool_name, "defect": str(e)}]} violations = [] if tool_name == 'ast_parse_shell_command' and 'execute_ast_parse_shell_command_unverified()' in stripped_code: violations.append({'tool': 'ast_parse_shell_command', 'defect': 'Unverified invocation of execute_ast_parse_shell_command_unverified()', 'remediation': 'with enforce_ast_parse_shell_command_policy():'}) if tool_name == 'ast_parse_shell_command' and 'execute_ast_parse_shell_command_unverified()' in stripped_code: violations.append({'tool': 'ast_parse_shell_command', 'defect': 'Unverified invocation of execute_ast_parse_shell_command_unverified()', 'remediation': 'with enforce_ast_parse_shell_command_policy():'}) if tool_name == 'ast_parse_shell_command' and 'execute_ast_parse_shell_command_unverified()' in stripped_code: violations.append({'tool': 'ast_parse_shell_command', 'defect': 'Unverified invocation of execute_ast_parse_shell_command_unverified()', 'remediation': 'with enforce_ast_parse_shell_command_policy():'}) if tool_name == 'ast_parse_shell_command' and 'execute_ast_parse_shell_command_unverified()' in stripped_code: violations.append({'tool': 'ast_parse_shell_command', 'defect': 'Unverified invocation of execute_ast_parse_shell_command_unverified()', 'remediation': 'with enforce_ast_parse_shell_command_policy():'}) if tool_name == 'block_dangerous_recursions' and 'execute_block_dangerous_recursions_unverified()' in stripped_code: violations.append({'tool': 'block_dangerous_recursions', 'defect': 'Unverified invocation of execute_block_dangerous_recursions_unverified()', 'remediation': 'with enforce_block_dangerous_recursions_policy():'}) if tool_name == 'block_dangerous_recursions' and 'execute_block_dangerous_recursions_unverified()' in stripped_code: violations.append({'tool': 'block_dangerous_recursions', 'defect': 'Unverified invocation of execute_block_dangerous_recursions_unverified()', 'remediation': 'with enforce_block_dangerous_recursions_policy():'}) if tool_name == 'block_dangerous_recursions' and 'execute_block_dangerous_recursions_unverified()' in stripped_code: violations.append({'tool': 'block_dangerous_recursions', 'defect': 'Unverified invocation of execute_block_dangerous_recursions_unverified()', 'remediation': 'with enforce_block_dangerous_recursions_policy():'}) if tool_name == 'block_dangerous_recursions' and 'execute_block_dangerous_recursions_unverified()' in stripped_code: violations.append({'tool': 'block_dangerous_recursions', 'defect': 'Unverified invocation of execute_block_dangerous_recursions_unverified()', 'remediation': 'with enforce_block_dangerous_recursions_policy():'}) if tool_name == 'require_human_in_the_loop_for_destructive' and 'execute_require_human_in_the_loop_for_destructive_unverified()' in stripped_code: violations.append({'tool': 'require_human_in_the_loop_for_destructive', 'defect': 'Unverified invocation of execute_require_human_in_the_loop_for_destructive_unverified()', 'remediation': 'with enforce_require_human_in_the_loop_for_destructive_policy():'}) if tool_name == 'require_human_in_the_loop_for_destructive' and 'execute_require_human_in_the_loop_for_destructive_unverified()' in stripped_code: violations.append({'tool': 'require_human_in_the_loop_for_destructive', 'defect': 'Unverified invocation of execute_require_human_in_the_loop_for_destructive_unverified()', 'remediation': 'with enforce_require_human_in_the_loop_for_destructive_policy():'}) if tool_name == 'require_human_in_the_loop_for_destructive' and 'execute_require_human_in_the_loop_for_destructive_unverified()' in stripped_code: violations.append({'tool': 'require_human_in_the_loop_for_destructive', 'defect': 'Unverified invocation of execute_require_human_in_the_loop_for_destructive_unverified()', 'remediation': 'with enforce_require_human_in_the_loop_for_destructive_policy():'}) if tool_name == 'require_human_in_the_loop_for_destructive' and 'execute_require_human_in_the_loop_for_destructive_unverified()' in stripped_code: violations.append({'tool': 'require_human_in_the_loop_for_destructive', 'defect': 'Unverified invocation of execute_require_human_in_the_loop_for_destructive_unverified()', 'remediation': 'with enforce_require_human_in_the_loop_for_destructive_policy():'}) if tool_name == 'whitelist_safe_developer_commands' and 'execute_whitelist_safe_developer_commands_unverified()' in stripped_code: violations.append({'tool': 'whitelist_safe_developer_commands', 'defect': 'Unverified invocation of execute_whitelist_safe_developer_commands_unverified()', 'remediation': 'with enforce_whitelist_safe_developer_commands_policy():'}) if tool_name == 'whitelist_safe_developer_commands' and 'execute_whitelist_safe_developer_commands_unverified()' in stripped_code: violations.append({'tool': 'whitelist_safe_developer_commands', 'defect': 'Unverified invocation of execute_whitelist_safe_developer_commands_unverified()', 'remediation': 'with enforce_whitelist_safe_developer_commands_policy():'}) if tool_name == 'whitelist_safe_developer_commands' and 'execute_whitelist_safe_developer_commands_unverified()' in stripped_code: violations.append({'tool': 'whitelist_safe_developer_commands', 'defect': 'Unverified invocation of execute_whitelist_safe_developer_commands_unverified()', 'remediation': 'with enforce_whitelist_safe_developer_commands_policy():'}) if tool_name == 'whitelist_safe_developer_commands' and 'execute_whitelist_safe_developer_commands_unverified()' in stripped_code: violations.append({'tool': 'whitelist_safe_developer_commands', 'defect': 'Unverified invocation of execute_whitelist_safe_developer_commands_unverified()', 'remediation': 'with enforce_whitelist_safe_developer_commands_policy():'}) if tool_name == 'detect_obfuscated_command_piping' and 'execute_detect_obfuscated_command_piping_unverified()' in stripped_code: violations.append({'tool': 'detect_obfuscated_command_piping', 'defect': 'Unverified invocation of execute_detect_obfuscated_command_piping_unverified()', 'remediation': 'with enforce_detect_obfuscated_command_piping_policy():'}) if tool_name == 'detect_obfuscated_command_piping' and 'execute_detect_obfuscated_command_piping_unverified()' in stripped_code: violations.append({'tool': 'detect_obfuscated_command_piping', 'defect': 'Unverified invocation of execute_detect_obfuscated_command_piping_unverified()', 'remediation': 'with enforce_detect_obfuscated_command_piping_policy():'}) if tool_name == 'detect_obfuscated_command_piping' and 'execute_detect_obfuscated_command_piping_unverified()' in stripped_code: violations.append({'tool': 'detect_obfuscated_command_piping', 'defect': 'Unverified invocation of execute_detect_obfuscated_command_piping_unverified()', 'remediation': 'with enforce_detect_obfuscated_command_piping_policy():'}) if tool_name == 'detect_obfuscated_command_piping' and 'execute_detect_obfuscated_command_piping_unverified()' in stripped_code: violations.append({'tool': 'detect_obfuscated_command_piping', 'defect': 'Unverified invocation of execute_detect_obfuscated_command_piping_unverified()', 'remediation': 'with enforce_detect_obfuscated_command_piping_policy():'}) if tool_name == 'enforce_working_directory_boundary' and 'execute_enforce_working_directory_boundary_unverified()' in stripped_code: violations.append({'tool': 'enforce_working_directory_boundary', 'defect': 'Unverified invocation of execute_enforce_working_directory_boundary_unverified()', 'remediation': 'with enforce_enforce_working_directory_boundary_policy():'}) if tool_name == 'enforce_working_directory_boundary' and 'execute_enforce_working_directory_boundary_unverified()' in stripped_code: violations.append({'tool': 'enforce_working_directory_boundary', 'defect': 'Unverified invocation of execute_enforce_working_directory_boundary_unverified()', 'remediation': 'with enforce_enforce_working_directory_boundary_policy():'}) if tool_name == 'enforce_working_directory_boundary' and 'execute_enforce_working_directory_boundary_unverified()' in stripped_code: violations.append({'tool': 'enforce_working_directory_boundary', 'defect': 'Unverified invocation of execute_enforce_working_directory_boundary_unverified()', 'remediation': 'with enforce_enforce_working_directory_boundary_policy():'}) if tool_name == 'enforce_working_directory_boundary' and 'execute_enforce_working_directory_boundary_unverified()' in stripped_code: violations.append({'tool': 'enforce_working_directory_boundary', 'defect': 'Unverified invocation of execute_enforce_working_directory_boundary_unverified()', 'remediation': 'with enforce_enforce_working_directory_boundary_policy():'}) if tool_name == 'simulate_command_filesystem_impact' and 'execute_simulate_command_filesystem_impact_unverified()' in stripped_code: violations.append({'tool': 'simulate_command_filesystem_impact', 'defect': 'Unverified invocation of execute_simulate_command_filesystem_impact_unverified()', 'remediation': 'with enforce_simulate_command_filesystem_impact_policy():'}) if tool_name == 'simulate_command_filesystem_impact' and 'execute_simulate_command_filesystem_impact_unverified()' in stripped_code: violations.append({'tool': 'simulate_command_filesystem_impact', 'defect': 'Unverified invocation of execute_simulate_command_filesystem_impact_unverified()', 'remediation': 'with enforce_simulate_command_filesystem_impact_policy():'}) if tool_name == 'simulate_command_filesystem_impact' and 'execute_simulate_command_filesystem_impact_unverified()' in stripped_code: violations.append({'tool': 'simulate_command_filesystem_impact', 'defect': 'Unverified invocation of execute_simulate_command_filesystem_impact_unverified()', 'remediation': 'with enforce_simulate_command_filesystem_impact_policy():'}) if tool_name == 'simulate_command_filesystem_impact' and 'execute_simulate_command_filesystem_impact_unverified()' in stripped_code: violations.append({'tool': 'simulate_command_filesystem_impact', 'defect': 'Unverified invocation of execute_simulate_command_filesystem_impact_unverified()', 'remediation': 'with enforce_simulate_command_filesystem_impact_policy():'}) if tool_name == 'export_destructive_command_incident_log' and 'execute_export_destructive_command_incident_log_unverified()' in stripped_code: violations.append({'tool': 'export_destructive_command_incident_log', 'defect': 'Unverified invocation of execute_export_destructive_command_incident_log_unverified()', 'remediation': 'with enforce_export_destructive_command_incident_log_policy():'}) if tool_name == 'export_destructive_command_incident_log' and 'execute_export_destructive_command_incident_log_unverified()' in stripped_code: violations.append({'tool': 'export_destructive_command_incident_log', 'defect': 'Unverified invocation of execute_export_destructive_command_incident_log_unverified()', 'remediation': 'with enforce_export_destructive_command_incident_log_policy():'}) is_safe = (len(violations) == 0) and syntax_valid return { "status": "VALIDATED" if is_safe else "VIOLATION_DETECTED", "is_safe": is_safe, "syntax_valid": syntax_valid, "violations": violations } def execute_tool(tool_name: str, arguments: Dict[str, Any]) -> Dict[str, Any]: code = arguments.get("code") or arguments.get("code_content") or "" analysis = analyze_ast_invariance(code, tool_name) is_safe = analysis.get("is_safe", False) violations = analysis.get("violations", []) return { "package": "destructive_cli_bash_powershell_safety_interceptor_mcp", "tool": tool_name, "result": analysis, "status": "VIOLATION_DETECTED" if (violations or not is_safe) else "PASS_VALIDATED", "defect_detected": bool(violations or not is_safe), "violations_detected": bool(violations), "is_vulnerable": bool(violations or not is_safe), "is_safe": is_safe } def main(): while True: line = sys.stdin.readline() if not line: break try: req = json.loads(line) method = req.get("method") if method == "initialize": res = { "protocolVersion": "2024-11-05", "capabilities": {"tools": {}}, "serverInfo": {"name": "destructive_cli_bash_powershell_safety_interceptor_mcp", "version": "2.5.0"} } elif method == "tools/list": res = {"tools": [{"name": "ast_parse_shell_command", "description": "Parses Bash and PowerShell command strings into structured AST before execution."}, {"name": "block_dangerous_recursions", "description": "Blocks destructive patterns like rm -rf /, format, mkfs, dd, and system reboot commands."}, {"name": "require_human_in_the_loop_for_destructive", "description": "Forces explicit developer interactive approval before executing high-impact commands."}, {"name": "whitelist_safe_developer_commands", "description": "Maintains zero-overhead fast-path whitelist for common commands (git status, pytest, ls)."}, {"name": "detect_obfuscated_command_piping", "description": "Detects base64-encoded or piped curl-to-bash execution strings designed to evade rules."}, {"name": "enforce_working_directory_boundary", "description": "Ensures commands cannot operate outside designated project workspace folder."}, {"name": "simulate_command_filesystem_impact", "description": "Performs dry-run check predicting how many files would be altered by command."}, {"name": "export_destructive_command_incident_log", "description": "Exports audit log of all intercepted commands and prevented system damage."}]} elif method == "tools/call": p = req.get("params", {}) res = execute_tool(p.get("name"), p.get("arguments", {})) else: res = {"error": "Unsupported method"} sys.stdout.write(json.dumps({"jsonrpc": "2.0", "id": req.get("id"), "result": res}) + "\n") sys.stdout.flush() except Exception as e: sys.stdout.write(json.dumps({"jsonrpc": "2.0", "error": {"code": -32603, "message": str(e)}}) + "\n") sys.stdout.flush() if __name__ == "__main__": main()