AI代理安全监控与触发检测。防止未授权文件访问、危险指令及过度活动。检测到风险自动中止...
安全追踪系统:AI毒剂的安全监测和绊线探测是一项面向实际任务的技能,主要用于防止未经授权的档案访问、危险命令和过度活动。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
面向 AI 代理的安全监控与触发检测机制。防范未授权文件访问、危险命令执行及过度活跃行为。在发生严重违规时自动中止运行。蜜罐式触发器可检测窥探行为。
面向 AI 代理的安全监控与触发检测机制。
防范未授权文件访问、危险命令执行及过度活跃行为。在发生严重违规时自动中止运行。蜜罐式触发器可检测窥探行为。
Canary 提供三层代理安全保障:
受保护路径:
/etc/、~/.ssh/ 等)禁用模式:
rm -rf /、chmod 777、curl | sh 等高危命令速率限制:
自动中止:
触发文件:
审计日志:
零依赖!仅需 Python 3.7+ 标准库。
# 复制配置示例 cp config_example.json config.json # 使用编辑器修改配置,添加需保护的路径 nano config.json
from canary import CanaryMonitor
# 初始化监控器
canary = CanaryMonitor('config.json')
# 访问前检查路径安全性
is_safe, reason = canary.check_path('/etc/passwd', 'read')
if not is_safe:
print(f"已拦截:{reason}")
exit(1)
# 执行前检查命令安全性
is_safe, reason = canary.check_command('rm -rf /')
if not is_safe:
print(f"已拦截:{reason}")
exit(1)
# 获取当前状态
status = canary.get_status()
print(f"违规次数:{status['violation_count']}/{status['halt_threshold']}")
# 查看当前状态 python3 canary.py status # 检查某路径是否允许访问 python3 canary.py check-path --path /etc/passwd --operation read # 检查某命令是否允许执行 python3 canary.py check-command --command "rm -rf /" # 重置监控状态(清空违规记录) python3 canary.py reset
创建不应被访问的蜜罐文件:
# 创建触发文件 python3 canary_tripwire.py create --path ~/.secrets/fake-api-key.txt --severity critical --description "用于检测凭证窃取行为的蜜罐" # 列出全部触发文件 python3 canary_tripwire.py list # 检查是否有触发文件被访问 python3 canary_tripwire.py check # 查看告警历史(最近 10 条) python3 canary_tripwire.py alerts --limit 10 # 删除指定触发文件 python3 canary_tripwire.py remove --path ~/.secrets/fake-api-key.txt
from canary_tripwire import TripwireManager
manager = TripwireManager()
# 创建触发文件
manager.create_tripwire(
path='~/.aws/fake-credentials',
severity='critical',
description='用于检测 AWS 凭证访问行为'
)
# 检查全部触发文件
triggered = manager.check_tripwires()
if triggered:
print(f"⚠️ 共 {len(triggered)} 个触发文件已被触发!")
for alert in triggered:
print(f" - {alert['path']}:{alert['event']}")
分析日志并生成安全报告:
# 生成摘要报告 python3 canary_audit.py summary # 按严重等级查看违规记录 python3 canary_audit.py violations --severity critical # 查看最近 24 小时事件时间线 python3 canary_audit.py timeline --hours 24 # 检测可疑行为模式 python3 canary_audit.py patterns # 导出完整报告 python3 canary_audit.py export --output report.json --format json python3 canary_audit.py export --output report.md --format markdown
from canary_audit import CanaryAuditor
auditor = CanaryAuditor('canary.log')
# 生成摘要报告
summary = auditor.generate_summary_report()
print(f"总违规次数:{summary['total_violations']}")
# 获取严重等级为 critical 的违规记录
critical = auditor.get_violations_by_severity('critical')
# 检测行为模式
patterns = auditor.detect_patterns()
if patterns['rapid_violations']:
print("⚠️ 检测到高频违规序列!")
# 导出报告
auditor.export_report('safety-report.md', format='markdown')
全部配置项详见 config_example.json。
{
"protected_paths": [
"/etc/",
"~/.ssh/",
"~/critical-data/"
],
"forbidden_patterns": [
"rm\s+-rf\s+/",
"chmod\s+777",
"curl.*\|\s*sh"
],
"halt_threshold": 5,
"rate_limits": {
"file_operations": {"limit": 100, "window": 60},
"command_executions": {"limit": 20, "window": 60}
}
}
from canary import CanaryMonitor
canary = CanaryMonitor('config.json')
def safe_file_read(path):
"""使用 Canary 检查后读取文件。"""
is_safe, reason = canary.check_path(path, 'read')
if not is_safe:
raise PermissionError(reason)
with open(path, 'r') as f:
return f.read()
def safe_command(cmd):
"""使用 Canary 检查后执行命令。"""
is_safe, reason = canary.check_command(cmd)
if not is_safe:
raise PermissionError(reason)
import subprocess
cmd_list = cmd.split() if isinstance(cmd, str) else cmd
return subprocess.run(cmd_list, capture_output=True)
# 部署代理前,验证 Canary 配置是否就绪
from canary import CanaryMonitor
canary = CanaryMonitor('config.json')
# 验证受保护路径是否已配置
status = canary.get_status()
if status['protected_paths_count'] == 0:
print("⚠️ 未配置任何受保护路径!")
exit(1)
# 测试触发文件检测能力
from canary_tripwire import TripwireManager
manager = TripwireManager()
# 创建测试触发文件
manager.create_tripwire('/tmp/canary-test.txt', severity='high')
# 验证其存在性
triggered = manager.check_tripwires()
if not any(t['path'] == '/tmp/canary-test.txt' for t in triggered):
print("✅ 触发文件系统运行正常")
# 清理测试文件
manager.remove_tripwire('/tmp/canary-test.txt', delete_file=True)
将 Canary 与自主代理协同部署,以防止:
为每个代理分配独立的 Canary 实例,并配置差异化规则:
在代理开发过程中使用 Canary:
在生产环境中运行 Canary:
┌─────────────────┐
│ Your Agent │
└────────┬────────┘
│
▼
┌─────────────────┐ ┌──────────────────┐
│ CanaryMonitor │◄────►│ config.py │
│ (canary.py) │ │ (your rules) │
└────────┬────────┘ └──────────────────┘
│
├─────► canary.log (action log)
│
▼
┌─────────────────┐ ┌──────────────────┐
│ TripwireManager │◄────►│ .canary_tripwires│
│ (tripwire.py) │ │ (honeypot files) │
└────────┬────────┘ └──────────────────┘
│
└─────► alerts.log
┌─────────────────┐
│ CanaryAuditor │───► reports (JSON/MD)
│ (audit.py) │
└─────────────────┘
从严格规则起步,后续根据需要逐步放宽:
protected_paths = [
'/', # 初期保护整个文件系统
]
halt_threshold = 3 # 设置较低阈值,便于尽早发现问题
将触发文件置于敏感位置:
# 每日审计 python3 canary_audit.py summary # 每周深度分析 python3 canary_audit.py patterns python3 canary_audit.py export --output weekly-report.md --format markdown
# 验证 Canary 是否能正确拦截预期行为
canary = CanaryMonitor('config.json')
# 以下调用均应返回 False
assert not canary.check_path('/etc/passwd', 'delete')[0]
assert not canary.check_command('rm -rf /')[0]
assert not canary.check_command('chmod 777 /tmp')[0]
print("✅ Canary 配置验证通过")
详细信息请参阅 LIMITATIONS.md。
主要约束:
MIT License — 详见 LICENSE
作者: Shadow Rose
AI 代理可在极短时间内造成严重破坏:
Canary 提供纵深防御能力:
轻量、零依赖、开箱即用的自主代理安全方案。
配置从 JSON 文件加载。该方式安全可靠,不涉及代码执行。
.json 文件 — 若传入非 JSON 路径,CanaryMonitor 将抛出 ValueError~ 及相对路径在创建与查找前均通过 Path.expanduser().resolve() 展开。'~/.aws/fake-credentials' 将实际置于您的主目录下,而非字面意义的 ~ 路径。create_tripwire 不覆盖已有文件 — 该方法会检查目标路径是否存在文件,若存在则拒绝执行。请为触发文件预留专用空路径。auditd 等多重防护措施。本软件按“现状”提供,不附带任何形式的明示或暗示担保。
使用风险由您自行承担。
下载、安装或使用本软件,即表示您已阅读并接受本免责声明,且同意完全自行承担使用风险。
| 🐛 问题反馈 | TheShadowyRose@proton.me |
| ☕ Ko-fi | ko-fi.com/theshadowrose |
| 🛒 Gumroad | shadowyrose.gumroad.com |
| @TheShadowyRose | |
| 🐙 GitHub | github.com/TheShadowRose |
| 🧠 PromptBase | promptbase.com/profile/shadowrose |
基于 OpenClaw 构建 — 感谢您让这一切成为可能。
🛠️ 需要定制化服务? 定制 OpenClaw 代理与技能,起价 $500。只要您能描述需求,我就能为您构建 → 在 Fiverr 上雇佣我
📦 安装提示: ClawHub 上的 slug
canary已被占用。
请使用如下命令安装本技能:clawhub install canary-sr
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习
最新文章