世界上有两种黑客——一种想偷你的数据,一种想看看你的AI有多蠢。4月9日凌晨1点,我决定写下这份安全指南,让想搞你的人无从下手...
安全不是可选项,而是必选项。本指南涵盖OpenClaw从配置到运行的全方位安全措施。
永远不要将API Key写入配置文件或代码:
# .env 文件(加入.gitignore!)
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxx
ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxx
GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxx
TELEGRAM_BOT_TOKEN=xxxxxxxx:xxxxxxxxxx
# 文件权限设置
chmod 600 .env
chown $USER:$USER .env
# 定期轮换脚本 rotate_keys.sh
#!/bin/bash
# 生成新密钥通知
send_notification "正在轮换API密钥..."
# 更新环境变量
export OPENAI_API_KEY=$(generate_new_key)
# 重启服务
systemctl restart openclaw
# 验证服务正常
if curl -f http://localhost:3000/health; then
# 删除旧密钥
revoke_old_keys
echo "密钥轮换成功"
else
echo "服务异常,回滚中..."
rollback_keys
fi
# 安全配置 config.yaml
security:
# 权限级别:strict | moderate | permissive
level: strict
skills:
# 🔴 高危Skill - 严格限制
code_execution:
allowed: true
sandbox: strict
permissions:
read:
- /workspace/readonly
write:
- /workspace/output
blocked_commands:
- "rm -rf /"
- "mkfs.*"
- "dd if=/dev/zero"
- "> /dev/sda"
resource_limits:
cpu: 0.5
memory: "256m"
timeout: 30
# 🟡 中危Skill - 适度限制
browser:
allowed: true
permissions:
allow_downloads: false
allow_uploads: false
max_page_size: "10mb"
blocked_domains:
- "*.onion"
- "*.local"
- "localhost"
- "127.0.0.1"
# 🟢 低危Skill - 正常使用
web_search:
allowed: true
rate_limit: "100/hour"
allowed_engines:
- duckduckgo
- brave
# 多用户环境下的权限隔离
users:
admin:
skills: "*" # 所有权限
can_install: true
can_configure: true
developer:
skills:
- web_search
- browser
- github
- code_execution
can_install: false
resource_limits:
daily_api_calls: 1000
guest:
skills:
- web_search
read_only: true
rate_limit: "10/hour"
# UFW配置
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp # SSH
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
ufw allow from 10.0.0.0/8 to any port 3000 # 仅允许内网访问管理端口
ufw enable
# 限制SSH访问
ufw limit 22/tcp
# 使用Let's Encrypt免费证书
certbot --nginx -d your-domain.com
# 强制HTTPS重定向
server {
listen 80;
return 301 https://$server_name$request_uri;
}
# API安全设置
api:
# 认证方式
auth:
type: jwt
secret: ${JWT_SECRET}
expiry: 3600 # 1小时过期
# 限流
rate_limit:
default: "100/minute"
by_ip: true
by_user: true
# CORS设置
cors:
allowed_origins:
- "https://miaoquai.com"
allowed_methods: ["GET", "POST"]
allowed_headers: ["Content-Type", "Authorization"]
# 请求大小限制
max_body_size: "10mb"
max_header_size: "8kb"
# 启用详细审计
audit:
enabled: true
level: detailed
# 记录内容
events:
- skill_invocation
- skill_install
- skill_uninstall
- api_call
- file_access
- network_request
- permission_denied
- authentication_failure
# 日志格式
format: json
# 存储配置
storage:
type: file
path: /var/log/openclaw/audit.log
rotation: daily
retention: 90 # 保留90天
# 实时告警
alerts:
- event: permission_denied
action: notify_admin
- event: authentication_failure
threshold: 5 # 5次失败触发
window: 5m # 5分钟内
action: block_ip
# 安全监控规则
monitoring:
security:
rules:
# 检测异常API调用频率
- name: "high_api_usage"
condition: "api_calls_per_minute > 1000"
severity: warning
# 检测可疑命令
- name: "suspicious_command"
condition: "command matches '(rm|format|del|drop)'"
severity: critical
action: block_and_notify
# 检测未授权访问尝试
- name: "unauthorized_access"
condition: "failed_auth > 10 in 1m"
severity: critical
action: ban_ip
# 数据脱敏
data_protection:
# 自动检测并脱敏
pii_detection:
enabled: true
patterns:
- credit_card
- ssn
- email
- phone
# 数据加密
encryption:
at_rest: true
in_transit: true
algorithm: AES-256-GCM
# 数据保留策略
retention:
logs: 90
conversations: 30
cache: 7
# 加密备份
backup:
enabled: true
schedule: "0 2 * * *" # 每天凌晨2点
encryption:
enabled: true
key_file: /secure/backup-key.pem
storage:
local: /backup/openclaw
remote:
- type: s3
bucket: openclaw-backups
encryption: true
# 备份验证
verification:
enabled: true
test_restore: weekly
#!/bin/bash
# emergency_shutdown.sh
echo "🚨 紧急关闭模式"
# 停止所有服务
systemctl stop openclaw
docker-compose down
# 断开网络连接
iptables -A INPUT -j DROP
iptables -A OUTPUT -j DROP
# 保存当前状态
tar czf /emergency/snapshot-$(date +%Y%m%d-%H%M%S).tar.gz /var/lib/openclaw
# 通知管理员
send_alert "OpenClaw已紧急关闭,请立即检查"
echo "系统已安全关闭"