File size: 22,269 Bytes
4ef1e3f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | #!/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()
|