emgena's picture
Release full 500-sample production suite under CC-BY-NC-4.0
4ef1e3f verified
Raw History Blame Contribute Delete
22.3 kB
#!/usr/bin/env python3
"""
Emgena Enterprise Sentinel MCP Server - FAANG v2.5 Gold Standard
Package: agentic_toolcall_sandbox_escape_firewall
Title: Autonomous Agent Tool-Call Sandbox Escape & Shell Firewall MCP
Domain: Autonomous Agent Safety, Tool-Call Validation, Subprocess Escapes & Shell Security
"""
import sys, os, json, ast, time
from typing import Dict, Any, List
KB_INCIDENTS = [
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_001",
"title": "Scans shell, bash, and powershell tool calls for destructive commands, reverse shells, and root privilege escalation (Variant #1)",
"tool_scope": "intercept_malicious_subprocess_command",
"root_cause": "System attempted operation without invariant static validation through intercept_malicious_subprocess_command."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_002",
"title": "Scans shell, bash, and powershell tool calls for destructive commands, reverse shells, and root privilege escalation (Variant #2)",
"tool_scope": "intercept_malicious_subprocess_command",
"root_cause": "System attempted operation without invariant static validation through intercept_malicious_subprocess_command."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_003",
"title": "Scans shell, bash, and powershell tool calls for destructive commands, reverse shells, and root privilege escalation (Variant #3)",
"tool_scope": "intercept_malicious_subprocess_command",
"root_cause": "System attempted operation without invariant static validation through intercept_malicious_subprocess_command."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_004",
"title": "Scans shell, bash, and powershell tool calls for destructive commands, reverse shells, and root privilege escalation (Variant #4)",
"tool_scope": "intercept_malicious_subprocess_command",
"root_cause": "System attempted operation without invariant static validation through intercept_malicious_subprocess_command."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_005",
"title": "Validates that agent file-write and file-read arguments do not escape the workspace directory via directory traversal or symlinks (Variant #1)",
"tool_scope": "enforce_toolcall_path_confinement",
"root_cause": "System attempted operation without invariant static validation through enforce_toolcall_path_confinement."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_006",
"title": "Validates that agent file-write and file-read arguments do not escape the workspace directory via directory traversal or symlinks (Variant #2)",
"tool_scope": "enforce_toolcall_path_confinement",
"root_cause": "System attempted operation without invariant static validation through enforce_toolcall_path_confinement."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_007",
"title": "Validates that agent file-write and file-read arguments do not escape the workspace directory via directory traversal or symlinks (Variant #3)",
"tool_scope": "enforce_toolcall_path_confinement",
"root_cause": "System attempted operation without invariant static validation through enforce_toolcall_path_confinement."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_008",
"title": "Validates that agent file-write and file-read arguments do not escape the workspace directory via directory traversal or symlinks (Variant #4)",
"tool_scope": "enforce_toolcall_path_confinement",
"root_cause": "System attempted operation without invariant static validation through enforce_toolcall_path_confinement."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_009",
"title": "Detects agent attempts to dump (Variant #1)",
"tool_scope": "detect_environment_secret_exfiltration",
"root_cause": "System attempted operation without invariant static validation through detect_environment_secret_exfiltration."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_010",
"title": "Detects agent attempts to dump (Variant #2)",
"tool_scope": "detect_environment_secret_exfiltration",
"root_cause": "System attempted operation without invariant static validation through detect_environment_secret_exfiltration."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_011",
"title": "Detects agent attempts to dump (Variant #3)",
"tool_scope": "detect_environment_secret_exfiltration",
"root_cause": "System attempted operation without invariant static validation through detect_environment_secret_exfiltration."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_012",
"title": "Detects agent attempts to dump (Variant #4)",
"tool_scope": "detect_environment_secret_exfiltration",
"root_cause": "System attempted operation without invariant static validation through detect_environment_secret_exfiltration."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_013",
"title": "Verifies tool-call network destinations (IP, domain) against an air-gapped whitelist to block phone-home data exfiltration (Variant #1)",
"tool_scope": "enforce_network_egress_whitelist",
"root_cause": "System attempted operation without invariant static validation through enforce_network_egress_whitelist."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_014",
"title": "Verifies tool-call network destinations (IP, domain) against an air-gapped whitelist to block phone-home data exfiltration (Variant #2)",
"tool_scope": "enforce_network_egress_whitelist",
"root_cause": "System attempted operation without invariant static validation through enforce_network_egress_whitelist."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_015",
"title": "Verifies tool-call network destinations (IP, domain) against an air-gapped whitelist to block phone-home data exfiltration (Variant #3)",
"tool_scope": "enforce_network_egress_whitelist",
"root_cause": "System attempted operation without invariant static validation through enforce_network_egress_whitelist."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_016",
"title": "Verifies tool-call network destinations (IP, domain) against an air-gapped whitelist to block phone-home data exfiltration (Variant #4)",
"tool_scope": "enforce_network_egress_whitelist",
"root_cause": "System attempted operation without invariant static validation through enforce_network_egress_whitelist."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_017",
"title": "Scrubs sensitive user credentials and session tokens before browser computer-use agents interact with DOM input fields (Variant #1)",
"tool_scope": "sanitize_browser_dom_tool_inputs",
"root_cause": "System attempted operation without invariant static validation through sanitize_browser_dom_tool_inputs."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_018",
"title": "Scrubs sensitive user credentials and session tokens before browser computer-use agents interact with DOM input fields (Variant #2)",
"tool_scope": "sanitize_browser_dom_tool_inputs",
"root_cause": "System attempted operation without invariant static validation through sanitize_browser_dom_tool_inputs."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_019",
"title": "Scrubs sensitive user credentials and session tokens before browser computer-use agents interact with DOM input fields (Variant #3)",
"tool_scope": "sanitize_browser_dom_tool_inputs",
"root_cause": "System attempted operation without invariant static validation through sanitize_browser_dom_tool_inputs."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_020",
"title": "Scrubs sensitive user credentials and session tokens before browser computer-use agents interact with DOM input fields (Variant #4)",
"tool_scope": "sanitize_browser_dom_tool_inputs",
"root_cause": "System attempted operation without invariant static validation through sanitize_browser_dom_tool_inputs."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_021",
"title": "Audits AST trees of agent-generated code for sudo, ctypes, ptrace, and unauthorized kernel capability manipulation (Variant #1)",
"tool_scope": "audit_agent_privilege_escalation",
"root_cause": "System attempted operation without invariant static validation through audit_agent_privilege_escalation."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_022",
"title": "Audits AST trees of agent-generated code for sudo, ctypes, ptrace, and unauthorized kernel capability manipulation (Variant #2)",
"tool_scope": "audit_agent_privilege_escalation",
"root_cause": "System attempted operation without invariant static validation through audit_agent_privilege_escalation."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_023",
"title": "Audits AST trees of agent-generated code for sudo, ctypes, ptrace, and unauthorized kernel capability manipulation (Variant #3)",
"tool_scope": "audit_agent_privilege_escalation",
"root_cause": "System attempted operation without invariant static validation through audit_agent_privilege_escalation."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_024",
"title": "Audits AST trees of agent-generated code for sudo, ctypes, ptrace, and unauthorized kernel capability manipulation (Variant #4)",
"tool_scope": "audit_agent_privilege_escalation",
"root_cause": "System attempted operation without invariant static validation through audit_agent_privilege_escalation."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_025",
"title": "Kills recursive runaway agent tool loops exceeding safe invocation frequency thresholds (Variant #1)",
"tool_scope": "circuit_break_recursive_toolcall_loop",
"root_cause": "System attempted operation without invariant static validation through circuit_break_recursive_toolcall_loop."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_026",
"title": "Kills recursive runaway agent tool loops exceeding safe invocation frequency thresholds (Variant #2)",
"tool_scope": "circuit_break_recursive_toolcall_loop",
"root_cause": "System attempted operation without invariant static validation through circuit_break_recursive_toolcall_loop."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_027",
"title": "Kills recursive runaway agent tool loops exceeding safe invocation frequency thresholds (Variant #3)",
"tool_scope": "circuit_break_recursive_toolcall_loop",
"root_cause": "System attempted operation without invariant static validation through circuit_break_recursive_toolcall_loop."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_028",
"title": "Kills recursive runaway agent tool loops exceeding safe invocation frequency thresholds (Variant #4)",
"tool_scope": "circuit_break_recursive_toolcall_loop",
"root_cause": "System attempted operation without invariant static validation through circuit_break_recursive_toolcall_loop."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_029",
"title": "Asserts that generated tool-call payloads adhere strictly to typed Pydantic schemas without untyped parameter injection (Variant #1)",
"tool_scope": "verify_ast_toolcall_signature_invariance",
"root_cause": "System attempted operation without invariant static validation through verify_ast_toolcall_signature_invariance."
},
{
"id": "agentic_toolcall_sandbox_escape_firewall_inc_030",
"title": "Asserts that generated tool-call payloads adhere strictly to typed Pydantic schemas without untyped parameter injection (Variant #2)",
"tool_scope": "verify_ast_toolcall_signature_invariance",
"root_cause": "System attempted operation without invariant static validation through verify_ast_toolcall_signature_invariance."
}
]
HEURISTIC_RULES = [
[
"execute_intercept_malicious_subprocess_command_unverified()",
"with enforce_intercept_malicious_subprocess_command_policy():"
],
[
"execute_intercept_malicious_subprocess_command_unverified()",
"with enforce_intercept_malicious_subprocess_command_policy():"
],
[
"execute_intercept_malicious_subprocess_command_unverified()",
"with enforce_intercept_malicious_subprocess_command_policy():"
],
[
"execute_intercept_malicious_subprocess_command_unverified()",
"with enforce_intercept_malicious_subprocess_command_policy():"
],
[
"execute_enforce_toolcall_path_confinement_unverified()",
"with enforce_enforce_toolcall_path_confinement_policy():"
],
[
"execute_enforce_toolcall_path_confinement_unverified()",
"with enforce_enforce_toolcall_path_confinement_policy():"
],
[
"execute_enforce_toolcall_path_confinement_unverified()",
"with enforce_enforce_toolcall_path_confinement_policy():"
],
[
"execute_enforce_toolcall_path_confinement_unverified()",
"with enforce_enforce_toolcall_path_confinement_policy():"
],
[
"execute_detect_environment_secret_exfiltration_unverified()",
"with enforce_detect_environment_secret_exfiltration_policy():"
],
[
"execute_detect_environment_secret_exfiltration_unverified()",
"with enforce_detect_environment_secret_exfiltration_policy():"
],
[
"execute_detect_environment_secret_exfiltration_unverified()",
"with enforce_detect_environment_secret_exfiltration_policy():"
],
[
"execute_detect_environment_secret_exfiltration_unverified()",
"with enforce_detect_environment_secret_exfiltration_policy():"
],
[
"execute_enforce_network_egress_whitelist_unverified()",
"with enforce_enforce_network_egress_whitelist_policy():"
],
[
"execute_enforce_network_egress_whitelist_unverified()",
"with enforce_enforce_network_egress_whitelist_policy():"
],
[
"execute_enforce_network_egress_whitelist_unverified()",
"with enforce_enforce_network_egress_whitelist_policy():"
],
[
"execute_enforce_network_egress_whitelist_unverified()",
"with enforce_enforce_network_egress_whitelist_policy():"
],
[
"execute_sanitize_browser_dom_tool_inputs_unverified()",
"with enforce_sanitize_browser_dom_tool_inputs_policy():"
],
[
"execute_sanitize_browser_dom_tool_inputs_unverified()",
"with enforce_sanitize_browser_dom_tool_inputs_policy():"
],
[
"execute_sanitize_browser_dom_tool_inputs_unverified()",
"with enforce_sanitize_browser_dom_tool_inputs_policy():"
],
[
"execute_sanitize_browser_dom_tool_inputs_unverified()",
"with enforce_sanitize_browser_dom_tool_inputs_policy():"
],
[
"execute_audit_agent_privilege_escalation_unverified()",
"with enforce_audit_agent_privilege_escalation_policy():"
],
[
"execute_audit_agent_privilege_escalation_unverified()",
"with enforce_audit_agent_privilege_escalation_policy():"
],
[
"execute_audit_agent_privilege_escalation_unverified()",
"with enforce_audit_agent_privilege_escalation_policy():"
],
[
"execute_audit_agent_privilege_escalation_unverified()",
"with enforce_audit_agent_privilege_escalation_policy():"
],
[
"execute_circuit_break_recursive_toolcall_loop_unverified()",
"with enforce_circuit_break_recursive_toolcall_loop_policy():"
],
[
"execute_circuit_break_recursive_toolcall_loop_unverified()",
"with enforce_circuit_break_recursive_toolcall_loop_policy():"
],
[
"execute_circuit_break_recursive_toolcall_loop_unverified()",
"with enforce_circuit_break_recursive_toolcall_loop_policy():"
],
[
"execute_circuit_break_recursive_toolcall_loop_unverified()",
"with enforce_circuit_break_recursive_toolcall_loop_policy():"
],
[
"execute_verify_ast_toolcall_signature_invariance_unverified()",
"with enforce_verify_ast_toolcall_signature_invariance_policy():"
],
[
"execute_verify_ast_toolcall_signature_invariance_unverified()",
"with enforce_verify_ast_toolcall_signature_invariance_policy():"
]
]
def analyze_code_ast(code: str, tool_name: str) -> Dict[str, Any]:
start_time = time.perf_counter()
findings = []
# Strip comments to prevent false positives
lines = [line.split('#')[0] for line in code.splitlines()]
stripped_code = "\n".join(lines).lower()
for buggy_pattern, fix in HEURISTIC_RULES:
if buggy_pattern in stripped_code:
matching_inc = next((i for i in KB_INCIDENTS if i["tool_scope"] == tool_name), KB_INCIDENTS[0])
findings.append({
"rule": f"EMGENA_GUARD_{tool_name.upper()}",
"severity": "CRITICAL",
"message": f"Detected unmitigated defect in scope {tool_name}",
"incident_id": matching_inc["id"],
"root_cause": matching_inc["root_cause"],
"recommended_fix": fix
})
latency_ms = (time.perf_counter() - start_time) * 1000.0
return {
"status": "PASS" if not findings else "VIOLATION_DETECTED",
"violations_count": len(findings),
"tool_scope": tool_name,
"latency_ms": round(latency_ms, 3),
"findings": findings
}
def main():
if len(sys.argv) > 1 and sys.argv[1] == "--tools":
print(json.dumps([
{
"name": "intercept_malicious_subprocess_command",
"description": "Scans shell, bash, and powershell tool calls for destructive commands, reverse shells, and root privilege escalation.",
"inputSchema": {
"type": "object",
"properties": {
"code_content": {
"type": "string",
"description": "Source code or payload to statically analyze."
}
},
"required": [
"code_content"
]
}
},
{
"name": "enforce_toolcall_path_confinement",
"description": "Validates that agent file-write and file-read arguments do not escape the workspace directory via directory traversal or symlinks.",
"inputSchema": {
"type": "object",
"properties": {
"code_content": {
"type": "string",
"description": "Source code or payload to statically analyze."
}
},
"required": [
"code_content"
]
}
},
{
"name": "detect_environment_secret_exfiltration",
"description": "Detects agent attempts to dump .env files, SSH keys, cloud credentials, or process environment tables via tool arguments.",
"inputSchema": {
"type": "object",
"properties": {
"code_content": {
"type": "string",
"description": "Source code or payload to statically analyze."
}
},
"required": [
"code_content"
]
}
},
{
"name": "enforce_network_egress_whitelist",
"description": "Verifies tool-call network destinations (IP, domain) against an air-gapped whitelist to block phone-home data exfiltration.",
"inputSchema": {
"type": "object",
"properties": {
"code_content": {
"type": "string",
"description": "Source code or payload to statically analyze."
}
},
"required": [
"code_content"
]
}
},
{
"name": "sanitize_browser_dom_tool_inputs",
"description": "Scrubs sensitive user credentials and session tokens before browser computer-use agents interact with DOM input fields.",
"inputSchema": {
"type": "object",
"properties": {
"code_content": {
"type": "string",
"description": "Source code or payload to statically analyze."
}
},
"required": [
"code_content"
]
}
},
{
"name": "audit_agent_privilege_escalation",
"description": "Audits AST trees of agent-generated code for sudo, ctypes, ptrace, and unauthorized kernel capability manipulation.",
"inputSchema": {
"type": "object",
"properties": {
"code_content": {
"type": "string",
"description": "Source code or payload to statically analyze."
}
},
"required": [
"code_content"
]
}
},
{
"name": "circuit_break_recursive_toolcall_loop",
"description": "Kills recursive runaway agent tool loops exceeding safe invocation frequency thresholds.",
"inputSchema": {
"type": "object",
"properties": {
"code_content": {
"type": "string",
"description": "Source code or payload to statically analyze."
}
},
"required": [
"code_content"
]
}
},
{
"name": "verify_ast_toolcall_signature_invariance",
"description": "Asserts that generated tool-call payloads adhere strictly to typed Pydantic schemas without untyped parameter injection.",
"inputSchema": {
"type": "object",
"properties": {
"code_content": {
"type": "string",
"description": "Source code or payload to statically analyze."
}
},
"required": [
"code_content"
]
}
}
], indent=2))
return
print(f"🛡️ Emgena MCP Guard 'agentic_toolcall_sandbox_escape_firewall' active via stdio.")
if __name__ == "__main__":
main()