#!/usr/bin/env python3 """ EMGENA AI LABS - MODEL CONTEXT PROTOCOL (MCP) PRODUCTION SERVER Package: agent_docker_daemon_socket_mount_escape_shield Title: Agent Docker Daemon Socket Mount & Escape Shield MCP Domain: Container Security, Docker in Docker, Socket Hijacking, Privilege Escalation & Sandbox Escape 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 = [] if tool_name == 'intercept_docker_sock_mount_request' and 'execute_intercept_docker_sock_mount_request_unverified()' in stripped_code: violations.append({'tool': 'intercept_docker_sock_mount_request', 'defect': 'Unverified invocation of execute_intercept_docker_sock_mount_request_unverified()', 'remediation': 'with enforce_intercept_docker_sock_mount_request_policy():'}) if tool_name == 'intercept_docker_sock_mount_request' and 'execute_intercept_docker_sock_mount_request_unverified()' in stripped_code: violations.append({'tool': 'intercept_docker_sock_mount_request', 'defect': 'Unverified invocation of execute_intercept_docker_sock_mount_request_unverified()', 'remediation': 'with enforce_intercept_docker_sock_mount_request_policy():'}) if tool_name == 'intercept_docker_sock_mount_request' and 'execute_intercept_docker_sock_mount_request_unverified()' in stripped_code: violations.append({'tool': 'intercept_docker_sock_mount_request', 'defect': 'Unverified invocation of execute_intercept_docker_sock_mount_request_unverified()', 'remediation': 'with enforce_intercept_docker_sock_mount_request_policy():'}) if tool_name == 'intercept_docker_sock_mount_request' and 'execute_intercept_docker_sock_mount_request_unverified()' in stripped_code: violations.append({'tool': 'intercept_docker_sock_mount_request', 'defect': 'Unverified invocation of execute_intercept_docker_sock_mount_request_unverified()', 'remediation': 'with enforce_intercept_docker_sock_mount_request_policy():'}) if tool_name == 'detect_privileged_container_flag' and 'execute_detect_privileged_container_flag_unverified()' in stripped_code: violations.append({'tool': 'detect_privileged_container_flag', 'defect': 'Unverified invocation of execute_detect_privileged_container_flag_unverified()', 'remediation': 'with enforce_detect_privileged_container_flag_policy():'}) if tool_name == 'detect_privileged_container_flag' and 'execute_detect_privileged_container_flag_unverified()' in stripped_code: violations.append({'tool': 'detect_privileged_container_flag', 'defect': 'Unverified invocation of execute_detect_privileged_container_flag_unverified()', 'remediation': 'with enforce_detect_privileged_container_flag_policy():'}) if tool_name == 'detect_privileged_container_flag' and 'execute_detect_privileged_container_flag_unverified()' in stripped_code: violations.append({'tool': 'detect_privileged_container_flag', 'defect': 'Unverified invocation of execute_detect_privileged_container_flag_unverified()', 'remediation': 'with enforce_detect_privileged_container_flag_policy():'}) if tool_name == 'detect_privileged_container_flag' and 'execute_detect_privileged_container_flag_unverified()' in stripped_code: violations.append({'tool': 'detect_privileged_container_flag', 'defect': 'Unverified invocation of execute_detect_privileged_container_flag_unverified()', 'remediation': 'with enforce_detect_privileged_container_flag_policy():'}) if tool_name == 'enforce_docker_readonly_rootfs' and 'execute_enforce_docker_readonly_rootfs_unverified()' in stripped_code: violations.append({'tool': 'enforce_docker_readonly_rootfs', 'defect': 'Unverified invocation of execute_enforce_docker_readonly_rootfs_unverified()', 'remediation': 'with enforce_enforce_docker_readonly_rootfs_policy():'}) if tool_name == 'enforce_docker_readonly_rootfs' and 'execute_enforce_docker_readonly_rootfs_unverified()' in stripped_code: violations.append({'tool': 'enforce_docker_readonly_rootfs', 'defect': 'Unverified invocation of execute_enforce_docker_readonly_rootfs_unverified()', 'remediation': 'with enforce_enforce_docker_readonly_rootfs_policy():'}) if tool_name == 'enforce_docker_readonly_rootfs' and 'execute_enforce_docker_readonly_rootfs_unverified()' in stripped_code: violations.append({'tool': 'enforce_docker_readonly_rootfs', 'defect': 'Unverified invocation of execute_enforce_docker_readonly_rootfs_unverified()', 'remediation': 'with enforce_enforce_docker_readonly_rootfs_policy():'}) if tool_name == 'enforce_docker_readonly_rootfs' and 'execute_enforce_docker_readonly_rootfs_unverified()' in stripped_code: violations.append({'tool': 'enforce_docker_readonly_rootfs', 'defect': 'Unverified invocation of execute_enforce_docker_readonly_rootfs_unverified()', 'remediation': 'with enforce_enforce_docker_readonly_rootfs_policy():'}) if tool_name == 'audit_container_cap_add_sys_admin' and 'execute_audit_container_cap_add_sys_admin_unverified()' in stripped_code: violations.append({'tool': 'audit_container_cap_add_sys_admin', 'defect': 'Unverified invocation of execute_audit_container_cap_add_sys_admin_unverified()', 'remediation': 'with enforce_audit_container_cap_add_sys_admin_policy():'}) if tool_name == 'audit_container_cap_add_sys_admin' and 'execute_audit_container_cap_add_sys_admin_unverified()' in stripped_code: violations.append({'tool': 'audit_container_cap_add_sys_admin', 'defect': 'Unverified invocation of execute_audit_container_cap_add_sys_admin_unverified()', 'remediation': 'with enforce_audit_container_cap_add_sys_admin_policy():'}) if tool_name == 'audit_container_cap_add_sys_admin' and 'execute_audit_container_cap_add_sys_admin_unverified()' in stripped_code: violations.append({'tool': 'audit_container_cap_add_sys_admin', 'defect': 'Unverified invocation of execute_audit_container_cap_add_sys_admin_unverified()', 'remediation': 'with enforce_audit_container_cap_add_sys_admin_policy():'}) if tool_name == 'audit_container_cap_add_sys_admin' and 'execute_audit_container_cap_add_sys_admin_unverified()' in stripped_code: violations.append({'tool': 'audit_container_cap_add_sys_admin', 'defect': 'Unverified invocation of execute_audit_container_cap_add_sys_admin_unverified()', 'remediation': 'with enforce_audit_container_cap_add_sys_admin_policy():'}) if tool_name == 'quarantine_host_pid_network_namespace' and 'execute_quarantine_host_pid_network_namespace_unverified()' in stripped_code: violations.append({'tool': 'quarantine_host_pid_network_namespace', 'defect': 'Unverified invocation of execute_quarantine_host_pid_network_namespace_unverified()', 'remediation': 'with enforce_quarantine_host_pid_network_namespace_policy():'}) if tool_name == 'quarantine_host_pid_network_namespace' and 'execute_quarantine_host_pid_network_namespace_unverified()' in stripped_code: violations.append({'tool': 'quarantine_host_pid_network_namespace', 'defect': 'Unverified invocation of execute_quarantine_host_pid_network_namespace_unverified()', 'remediation': 'with enforce_quarantine_host_pid_network_namespace_policy():'}) if tool_name == 'quarantine_host_pid_network_namespace' and 'execute_quarantine_host_pid_network_namespace_unverified()' in stripped_code: violations.append({'tool': 'quarantine_host_pid_network_namespace', 'defect': 'Unverified invocation of execute_quarantine_host_pid_network_namespace_unverified()', 'remediation': 'with enforce_quarantine_host_pid_network_namespace_policy():'}) if tool_name == 'quarantine_host_pid_network_namespace' and 'execute_quarantine_host_pid_network_namespace_unverified()' in stripped_code: violations.append({'tool': 'quarantine_host_pid_network_namespace', 'defect': 'Unverified invocation of execute_quarantine_host_pid_network_namespace_unverified()', 'remediation': 'with enforce_quarantine_host_pid_network_namespace_policy():'}) if tool_name == 'fuzz_dockerfile_ast_for_root_user' and 'execute_fuzz_dockerfile_ast_for_root_user_unverified()' in stripped_code: violations.append({'tool': 'fuzz_dockerfile_ast_for_root_user', 'defect': 'Unverified invocation of execute_fuzz_dockerfile_ast_for_root_user_unverified()', 'remediation': 'with enforce_fuzz_dockerfile_ast_for_root_user_policy():'}) if tool_name == 'fuzz_dockerfile_ast_for_root_user' and 'execute_fuzz_dockerfile_ast_for_root_user_unverified()' in stripped_code: violations.append({'tool': 'fuzz_dockerfile_ast_for_root_user', 'defect': 'Unverified invocation of execute_fuzz_dockerfile_ast_for_root_user_unverified()', 'remediation': 'with enforce_fuzz_dockerfile_ast_for_root_user_policy():'}) if tool_name == 'fuzz_dockerfile_ast_for_root_user' and 'execute_fuzz_dockerfile_ast_for_root_user_unverified()' in stripped_code: violations.append({'tool': 'fuzz_dockerfile_ast_for_root_user', 'defect': 'Unverified invocation of execute_fuzz_dockerfile_ast_for_root_user_unverified()', 'remediation': 'with enforce_fuzz_dockerfile_ast_for_root_user_policy():'}) if tool_name == 'fuzz_dockerfile_ast_for_root_user' and 'execute_fuzz_dockerfile_ast_for_root_user_unverified()' in stripped_code: violations.append({'tool': 'fuzz_dockerfile_ast_for_root_user', 'defect': 'Unverified invocation of execute_fuzz_dockerfile_ast_for_root_user_unverified()', 'remediation': 'with enforce_fuzz_dockerfile_ast_for_root_user_policy():'}) if tool_name == 'sanitize_docker_build_secret_mounts' and 'execute_sanitize_docker_build_secret_mounts_unverified()' in stripped_code: violations.append({'tool': 'sanitize_docker_build_secret_mounts', 'defect': 'Unverified invocation of execute_sanitize_docker_build_secret_mounts_unverified()', 'remediation': 'with enforce_sanitize_docker_build_secret_mounts_policy():'}) if tool_name == 'sanitize_docker_build_secret_mounts' and 'execute_sanitize_docker_build_secret_mounts_unverified()' in stripped_code: violations.append({'tool': 'sanitize_docker_build_secret_mounts', 'defect': 'Unverified invocation of execute_sanitize_docker_build_secret_mounts_unverified()', 'remediation': 'with enforce_sanitize_docker_build_secret_mounts_policy():'}) if tool_name == 'sanitize_docker_build_secret_mounts' and 'execute_sanitize_docker_build_secret_mounts_unverified()' in stripped_code: violations.append({'tool': 'sanitize_docker_build_secret_mounts', 'defect': 'Unverified invocation of execute_sanitize_docker_build_secret_mounts_unverified()', 'remediation': 'with enforce_sanitize_docker_build_secret_mounts_policy():'}) if tool_name == 'sanitize_docker_build_secret_mounts' and 'execute_sanitize_docker_build_secret_mounts_unverified()' in stripped_code: violations.append({'tool': 'sanitize_docker_build_secret_mounts', 'defect': 'Unverified invocation of execute_sanitize_docker_build_secret_mounts_unverified()', 'remediation': 'with enforce_sanitize_docker_build_secret_mounts_policy():'}) if tool_name == 'circuit_break_container_escape_syscall' and 'execute_circuit_break_container_escape_syscall_unverified()' in stripped_code: violations.append({'tool': 'circuit_break_container_escape_syscall', 'defect': 'Unverified invocation of execute_circuit_break_container_escape_syscall_unverified()', 'remediation': 'with enforce_circuit_break_container_escape_syscall_policy():'}) if tool_name == 'circuit_break_container_escape_syscall' and 'execute_circuit_break_container_escape_syscall_unverified()' in stripped_code: violations.append({'tool': 'circuit_break_container_escape_syscall', 'defect': 'Unverified invocation of execute_circuit_break_container_escape_syscall_unverified()', 'remediation': 'with enforce_circuit_break_container_escape_syscall_policy():'}) is_safe = (len(violations) == 0) and syntax_valid return { "status": "VALIDATED" if is_safe else "POLICY_VIOLATIONS_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", "") analysis = analyze_ast_invariance(code, tool_name) return { "package": "agent_docker_daemon_socket_mount_escape_shield", "tool": tool_name, "result": analysis } def main(): while True: line = sys.stdin.readline() if not line: break try: req = json.loads(line) method = req.get("method") if method == "tools/list": res = {"tools": [{"name": "intercept_docker_sock_mount_request", "description": "Blocks agent container launch commands mounting /var/run/docker.sock into containers."}, {"name": "detect_privileged_container_flag", "description": "Rejects docker run --privileged flags that expose host devices to container escape."}, {"name": "enforce_docker_readonly_rootfs", "description": "Mandates --read-only root filesystem on agent execution sandbox containers."}, {"name": "audit_container_cap_add_sys_admin", "description": "Flags capability grants CAP_SYS_ADMIN, CAP_SYS_PTRACE, and CAP_NET_ADMIN."}, {"name": "quarantine_host_pid_network_namespace", "description": "Prevents use of --pid=host or --net=host to isolate agent container namespace."}, {"name": "fuzz_dockerfile_ast_for_root_user", "description": "Verifies Dockerfiles declare a dedicated non-root USER before entrypoint."}, {"name": "sanitize_docker_build_secret_mounts", "description": "Ensures docker build uses BuildKit --secret instead of ARG for credentials."}, {"name": "circuit_break_container_escape_syscall", "description": "Kills container via seccomp filter if unauthorized kernel syscalls are invoked."}]} 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()