🔒 OpenClaw安全加固与权限控制:别让你的Agent裸奔

凌晨1点39分,我正要睡觉,突然发现某个人没有权限的API Key正在调用我的生产环境Skills。那一刻,我终于悟了:"安全不是功能,是底线。"

别让你的Agent像裸奔一样暴露在网络中——这篇指南会帮你把OpenClaw打造成"军事级堡垒"。🏰

⚠️ 常见安全威胁

威胁类型 风险等级 说明 应对措施
API Key泄露 严重 Key被commit到GitHub等公开场所 自动扫描、轮换、加密存储
Skills命令注入 高危 用户通过输入注入恶意命令 输入校验、沙箱隔离
权限越界 高危 低权限用户访问高权限功能 RBAC、最小权限原则
数据泄露 中危 Skills处理的数据被意外暴露 加密、审计日志
拒绝服务 中危 恶意请求导致资源耗尽 限流、配额管理

🔐 API Key管理

1. 创建与管理API Key

# 创建带权限范围的API Key openclaw apikey create \ --name="production-api" \ --scope="skills:read,skills:execute" \ --rate-limit=1000/hour \ --namespace=production # 输出: # API Key: sk-prod-xxxxxxxxxxxxxxxxxxxx # 此密钥只显示一次,请妥善保管! # 创建有有效期的一次性Key(适合CI/CD) openclaw apikey create \ --name="ci-cd-deploy" \ --scope="skills:deploy" \ --expires-in=30d \ --max-uses=100 # 列出所有Key openclaw apikey list # 撤销一个Key openclaw apikey revoke sk-prod-leaked-key-xxxxx

2. Key扫描与轮换

# 扫描Git仓库中泄露的API Key openclaw security scan-keys \ --repo=./my-project \ --pattern="sk-*" \ --report=json # 输出示例: # Found 3 potential leaks: # - ./config/.env.local (line 15): OPENCLAW_API_KEY=xxx... # - ./README.md (line 48): example key (safe) # - ./test/fixtures/auth.json (line 3): test key (safe) # ⚠️ 发现1个真实泄露!正在通知... # 自动轮换泄露的Key openclaw apikey rotate sk-prod-leaked-key-xxxxx \ --notify=true \ --grace-period=24h

👥 RBAC权限控制

1. 定义角色

# 创建系统角色 openclaw security role create admin \ --display-name="管理员" \ --description="拥有所有权限" openclaw security role create developer \ --display-name="开发者" \ --description="可以开发和测试Skills,但不能部署到生产" openclaw security role create viewer \ --display-name="只读用户" \ --description="只能查看日志和指标" # 为角色分配权限 openclaw security role grant admin \ --permissions="skills:*","namespaces:*","config:*","apikey:*" openclaw security role grant developer \ --permissions="skills:create","skills:update","skills:test","skills:read" openclaw security role grant viewer \ --permissions="skills:read","metrics:read","logs:read"

2. 用户管理

# 创建用户 openclaw security user create user-a \ --display-name="张三" \ --email="zhangsan@company.com" \ --role=developer # 批量创建用户(从CSV) openclaw security user import ./users.csv \ --role=viewer \ --send-invite=true # 查看用户权限 openclaw security user permissions user-a # 临时提升权限(比如给某个人紧急修复的权限) openclaw security user elevate user-a --role=admin --duration=4h

🛡️ Skills安全审计

关键提示:每个从ClawHub安装的第三方Skills都可能包含恶意代码。在安装前务必进行安全审计!

1. 使用Skills安全扫描器

# 安装Skills安全扫描器 openclaw skills install openclaw-skill-security-scanner # 扫描单个Skills openclaw skills security-scan my-skill # 扫描所有已安装的Skills openclaw skills security-scan --all --severity=high # 扫描结果示例: # Skill: my-skill # Security Score: B # Scan Results: # ⚠️ Medium - 使用了eval()函数 (index.js:42) # ⚠️ Low - 存在敏感变量名 "password" (config.json:10) # ✅ 通过 - 无外部命令调用 # ✅ 通过 - secrets隔离检查 # 禁止安装安全评分为D或F的Skills openclaw config set security.skillsBlocklist.minScore "C"

2. Skills沙箱隔离

# 启用Skills沙箱 openclaw config set skills.sandbox.enabled true openclaw config set skills.sandbox.type "container" openclaw config set skills.sandbox.timeout 30000 openclaw config set skills.sandbox.network "restricted" # 配置白名单路径(Skills可访问的目录) openclaw config set skills.sandbox.allowedPaths '["/tmp/skills-data","/var/lib/openclaw/skills"]' # 配置禁止的操作 openclaw config set skills.sandbox.blockedOperations '["exec","spawn","child_process"]' # 查看沙箱隔离状态 openclaw skills sandbox status my-skill

🌐 网络与传输安全

1. TLS配置

# 配置HTTPS openclaw config set gateway.tls.enabled true openclaw config set gateway.tls.cert "/etc/ssl/certs/miaoquai.crt" openclaw config set gateway.tls.key "/etc/ssl/private/miaoquai.key" # 强制HTTPS重定向 openclaw config set gateway.tls.forceHttps true # 配置HSTS openclaw config set gateway.tls.hsts.maxAge 31536000 openclaw config set gateway.tls.hsts.includeSubdomains true

2. IP白名单

# 限制访问来源 openclaw config set security.ipWhitelist.enabled true openclaw config set security.ipWhitelist.ranges '["10.0.0.0/8","172.16.0.0/12","192.168.0.0/16"]' # 允许VPN/公司出口IP openclaw config set security.ipWhitelist.extraIps '["8.8.8.8","1.1.1.1"]'

📝 审计日志

# 启用审计日志 openclaw config set audit.enabled true openclaw config set audit.events '["api_call","skill_install","config_change","user_login","apikey_create"]' # 配置审计日志存储 openclaw config set audit.storage "elasticsearch" openclaw config set audit.retention "180d" # 查询审计日志 openclaw audit query \ --event-type="api_call" \ --duration=24h \ --format=table # 导出审计日志(合规审计用) openclaw audit export \ --start=2026-01-01 \ --end=2026-05-27 \ --format=csv > audit-export-q1-2026.csv

🚨 告警与响应

# 配置安全告警 openclaw config set alerts.security.enabled true openclaw config set alerts.security.channels '["webhook://https://hooks.company.com/alerts","email://security@company.com"]' # 配置告警规则 cat > security-alerts.yaml << 'EOF' alerts: - name: "suspicious_login" condition: "event_count(login_failed) > 5 IN 5m" severity: "critical" action: "notify_security_team" - name: "apikey_leak" condition: "event_match(scan_findings.severity == 'critical')" severity: "critical" action: "auto_revoke_key" - name: "high_error_rate" condition: "metric(error_rate) > 10%" severity: "warning" action: "notify_admin" EOF # 应用告警规则 openclaw alerts apply security-alerts.yaml

✅ 安全加固检查清单

🎯 安全检查自动化脚本:
#!/bin/bash # security-audit.sh - 快速安全检测脚本 echo "🔍 OpenClaw Security Audit" echo "=========================" echo "1. Checking API Key expiry..." openclaw apikey list --expiring-in=7d echo "2. Scanning installed Skills..." openclaw skills security-scan --all --severity=medium echo "3. Checking RBAC configuration..." openclaw security users list --with-roles echo "4. Checking TLS..." openclaw config get gateway.tls.enabled echo "5. Checking audit logs..." openclaw audit query --event-type="config_change" --duration=7d | head -20 echo "✅ Audit Complete!"

📚 相关资源

「凌晨3点20分,安全审计跑完了。0个漏洞,0个告警。我关掉终端,终于可以安心睡觉了——至少今晚,没有人能用我的Agent去搞诈骗了。」——妙趣AI