Anthropic-Cybersecurity-Skills 向量与嵌入弱点评估:API 与命令参考实战指南
发布时间:2026/9/10 8:11:54来源:尧图网络
Anthropic-Cybersecurity-Skills 向量与嵌入弱点评估API 与命令参考实战指南【免费下载链接】Anthropic-Cybersecurity-Skills817 structured cybersecurity skills for AI agents · Mapped to 6 frameworks: MITRE ATTCK, NIST CSF 2.0, MITRE ATLAS, D3FEND, NIST AI RMF MITRE F3 (Fight Fraud) · agentskills.io standard · Works with Claude Code, GitHub Copilot, Codex CLI, Cursor, Gemini CLI 20 platforms · 29 security domains · Apache 2.0项目地址: https://gitcode.com/GitHub_Trending/an/Anthropic-Cybersecurity-Skills导读本指南围绕assessing-vector-and-embedding-weaknesses技能中的 API 与命令参考文档展开面向对 RAG检索增强生成流水线检索层进行授权安全评估的 AI 安全工程师。你将掌握 sentence-transformers、scikit-learn、Qdrant、Chroma、Pinecone 五大组件的核心调用方式理解嵌入反转embedding inversion、成员推断membership inference、跨租户泄露cross-tenant leakage与知识库投毒poisoning四类评估指标的含义与阈值判定并学会使用仓库自带的agent.py命令行评估器一键跑通全部检测。技能背景OWASP LLM08:2025 与 MITRE ATLAS AML.T0024该技能是 skills/assessing-vector-and-embedding-weaknesses/SKILL.md 所定义的授权安全评估方法的具体执行层。RAG 系统把文档转换为嵌入向量存入向量数据库Pinecone、Qdrant、Weaviate、Chroma、pgvector、FAISS检索时取最近邻向量来支撑 LLM 回答。OWASPLLM08:2025 Vector and Embedding Weaknesses正是针对这一层特有的安全风险嵌入反转嵌入不是单向的攻击者可训练反转模型或进行黑盒重构攻击从向量中恢复出原文的相当大一部分对应 MITRE ATLASAML.T0024.001 Invert ML Model成员推断探测某条记录是否存在于语料库中AML.T0024.000跨租户泄露当 namespace/collection 被共享或过滤隔离缺失时一个租户能检索到另一个租户的 chunk知识库投毒能写入语料库的攻击者插入精心构造的 chunk使其对预期查询拥有极高的余弦相似度从而主导检索并携带间接提示注入载荷检索操纵为大量无关查询定制的对抗文档检索劫持。父级技术是AML.T0024 — Exfiltration via ML Inference API攻击者利用合法的推理/查询访问来窃取数据。完整的框架映射见 skills/assessing-vector-and-embedding-weaknesses/references/standards.md其中将 LLM08 进一步关联到 NIST AI RMF 的 MEASURE-2.7AI 系统安全性与韧性被评估并记录以及 OWASP LLM02敏感信息泄露、LLM01提示注入。⚠️ 授权使用声明这些测试仅可作用于你自己拥有或获准评估的向量存储与嵌入模型。针对不受你控制的系统进行嵌入反转与跨租户探测可能暴露第三方数据未经授权一律禁止。完整约束见 SKILL.md 开头的 Authorized use only 声明。环境准备技能要求 Python 3.10以及目标嵌入端点和向量存储的读取投毒测试需要写权限——注意永远使用测试 collection而非生产语料库# 向量库客户端 嵌入 相似度工具 python -m pip install numpy scikit-learn sentence-transformers python -m pip install qdrant-client chromadb pinecone-client weaviate-client # 可选文本嵌入反转研究基线 python -m pip install vec2textAPI 参考详解以下内容继承自 references/api-reference.md并补充了阈值判定、参数取值等实战细节。sentence-transformers嵌入生成调用用途SentenceTransformer(all-MiniLM-L6-v2)加载嵌入模型384 维model.encode([texts])返回嵌入的 numpy 数组model.encode(text, normalize_embeddingsTrue)返回 L2 归一化向量用于余弦相似度默认模型all-MiniLM-L6-v2输出 384 维向量是 agent.py 中--model参数的默认值见main()中p.add_argument(--model, defaultall-MiniLM-L6-v2)。在反转/成员推断场景中建议对向量做 L2 归一化后再计算余弦相似度确保相似度分数稳定可比。scikit-learn相似度计算调用用途cosine_similarity(a, b)两两余弦相似度矩阵在反转测试中cosine_similarity(target_vec, cand_vecs)得到候选重构文本与目标向量之间的相似度列表用于衡量猜出的文本与真实 secret 的接近程度。Qdrant 客户端qdrant-client调用用途QdrantClient(urlhttp://localhost:6333)连接向量库client.get_collection(name)查看向量维度与距离度量client.count(name)语料规模client.search(collection_name, query_vector, limit, query_filter)带可选过滤的 k-NN 搜索client.upsert(name, points[PointStruct(id, vector, payload)])插入/更新数据点Filter(must[FieldCondition(key, matchMatchValue(value))])元数据过滤租户隔离get_collection的返回对象中info.config.params.vectors可直接读出向量 size 与 distance metriccosine/dot/L2这是 SKILL.md 工作流 Step 1盘点 RAG 流水线的关键信息源。Chromachromadb调用用途chromadb.Client()/PersistentClient(path)连接内存/持久化collection.query(query_embeddings[...], n_resultsk, where{...})带元数据过滤的 k-NNcollection.add(ids, embeddings, metadatas, documents)插入数据Chroma 的where{...}过滤语法对应 Qdrant 的Filter是测试元数据级租户隔离的等效接口。Pineconepinecone-client调用用途Pinecone(api_key...)连接index.query(vector..., top_kk, namespacetenant, filter{...})k-NNnamespace 即租户边界index.upsert(vectors[(id, vec, meta)], namespace...)插入Pinecone 中 namespace 本身就是租户隔离单元filter{...}是元数据过滤这对应 SKILL.md 中独立 namespace/collection 每租户一份的服务端隔离推荐做法。四类评估指标含义与判定阈值指标含义反转余弦Inversion cosine重构候选与目标向量之间的相似度越高 越可恢复成员增量Membership deltatop-1 分数语料内查询− top-1 分数对照查询大的正值 成员泄露投毒主导度Poison dominance无关查询在 top_k 中返回投毒 chunk 的比例跨租户计数Cross-tenant count租户查询返回的外部租户行数应为 0阈值判定来自 agent.py反转inversion cosine 0.85判定为 HIGH 风险 0.6为 moderate否则 low见cmd_inversion同时作为进程退出码1信号成员推断delta 0.2判定为 MEMBERSHIP LEAK见cmd_membership跨租户带过滤查询返回外部租户行 → CRITICAL仅无过滤查询泄露 → 说明隔离只存在于客户端见cmd_isolation的三级判定逻辑间接注入命中INJECTION_PATTERNS任一正则即标记。vec2text研究级反转基线调用用途vec2text.load_pretrained_corrector(gtr-base)为兼容的嵌入器加载反转纠正器vec2text.invert_embeddings(embeddings, corrector)从嵌入重建文本vec2text 是可选的锦上添花工具当需要把反转暴露从相似度度量升级为全文本恢复演示时用它针对兼容的嵌入模型展示整段原文重建从而在报告中量化最坏情况的信息泄露程度。端到端工作流含可运行代码Step 1盘点 RAG 流水线记录嵌入模型与维度、向量存储及其租户模型、分块策略、检索top_k与相似度度量、查询时应用的元数据过滤from qdrant_client import QdrantClient client QdrantClient(urlhttp://localhost:6333) info client.get_collection(docs) print(info.config.params.vectors) # size distance metric print(client.count(docs)) # corpus sizeStep 2嵌入反转暴露测试思路相似文本的嵌入彼此接近攻击者利用嵌入端点迭代重构出与目标向量匹配的文本用重构候选与目标的余弦相似度衡量恢复程度import numpy as np from sentence_transformers import SentenceTransformer from sklearn.metrics.pairwise import cosine_similarity model SentenceTransformer(all-MiniLM-L6-v2) secret Patient John Doe, MRN 553120, diagnosed with hypertension. target_vec model.encode([secret]) # 攻击者只有 target_vec 和嵌入端点对候选文本爬山搜索 candidates [ Patient name and medical record number with a diagnosis., John Doe medical record hypertension diagnosis, Patient John Doe MRN diagnosed hypertension, ] cand_vecs model.encode(candidates) sims cosine_similarity(target_vec, cand_vecs)[0] for c, s in sorted(zip(candidates, sims), keylambda x: -x[1]): print(f{s:.3f} {c}) # 高度相似的近乎逐字猜测 该模型反转风险真实存在Step 3成员推断用精确引用查询的 top-1 检索相似度判断某文档是否在语料中——语料内条目比语料外对照返回显著更高的最大相似度def membership_score(client, collection, embed, text): vec embed([text])[0].tolist() hits client.search(collection_namecollection, query_vectorvec, limit1) return hits[0].score if hits else 0.0 in_corpus membership_score(client, docs, model.encode, exact quote from a known chunk) control membership_score(client, docs, model.encode, An unrelated random sentence.) print(fin-corpus{in_corpus:.3f} control{control:.3f} delta{in_corpus-control:.3f}) # 大的正 delta 表示条目在语料中成员泄露Step 4多租户隔离测试确认租户 B 无法检索租户 A 的 chunk。核心要义分别用带过滤与不带过滤的查询各测一次——若仅客户端过滤生效而服务端无过滤查询能返回租户 A 数据说明隔离是客户端自欺式必须判定失败from qdrant_client.models import Filter, FieldCondition, MatchValue vec model.encode([confidential salary information])[0].tolist() hits client.search( collection_namedocs, query_vectorvec, limit10, query_filterFilter(must[FieldCondition(keytenant_id, matchMatchValue(valueB))]), ) leaked [h for h in hits if h.payload.get(tenant_id) ! B] print(CROSS-TENANT LEAK if leaked else isolation OK, -, len(leaked), foreign rows) # 关键测试去掉过滤重复一次确认是服务端而非客户端强制隔离 hits_nofilter client.search(collection_namedocs, query_vectorvec, limit10) print(server-side isolation FAILS if any(h.payload.get(tenant_id) ! B for h in hits_nofilter) else OK)Step 5知识库投毒仅限测试 collection插入一个良性投毒 chunk含标记性 canary 载荷而非真实注入测量它对无关查询的 top_k 主导度from qdrant_client.models import PointStruct poison POISON-CANARY. .join( [password reset billing refund account login support error help] * 8 ) client.upsert(docs_test, points[ PointStruct(id999999, vectormodel.encode([poison])[0].tolist(), payload{tenant_id: B, source: poison-test}) ]) queries [how do I get a refund, reset my password, what is the weather] for q in queries: hits client.search(docs_test, model.encode([q])[0].tolist(), limit5) dominated any(h.payload.get(source) poison-test for h in hits) print(f{POISON in top5 if dominated else clean}: {q})Step 6检索 chunk 中的间接提示注入检测在 chunk 文本拼入 prompt 之前扫描注入标记import re INJECTION_PATTERNS [ rignore (all|previous|the above) instructions, rsystem prompt, ryou are now, rdisregard, r/?(system|instructions), ] def chunk_is_injection(text): low text.lower() return [p for p in INJECTION_PATTERNS if re.search(p, low)] for hit in client.search(docs, model.encode([help])[0].tolist(), limit10): flags chunk_is_injection(hit.payload.get(text, )) if flags: print(INDIRECT INJECTION in chunk, hit.id, flags)注意agent.py中该模式的集合略有扩充额外包含disregard previous表明注入标记库是可演进的见 scripts/agent.py。仓库自带评估器agent.py 一行命令跑通四类检测scripts/agent.py 把上述四类检测封装为四个子命令全部通过argparse驱动并以退出码1 发现问题供自动化接入。其内部与参考文档一一对应inversion对应 Step 2内部调用sentence_transformerscosine_similarity输出相似度与 HIGH/moderate/low 判定membership对应 Step 3内部实现top1()辅助函数计算 in-corpus 与 control 的 top-1 分数差isolation对应 Step 4同时执行带过滤与不带过滤两种 Qdrant 搜索并输出三级判定injection对应 Step 6扫描检索 chunk 文本报告命中注入模式的 chunk id。典型用法见脚本 docstring 示例# 反转测量猜测文本与 secret 向量的余弦接近程度 python agent.py inversion --secret MRN 553120 hypertension \ --guess patient MRN hypertension diagnosis # 成员推断对比语料内引用与无关对照的 top-1 分数 python agent.py membership --url http://localhost:6333 --collection docs \ --quote exact chunk quote --control unrelated sentence # 隔离以租户 B 身份查询验证服务端过滤是否强制 python agent.py isolation --url http://localhost:6333 --collection docs \ --tenant-field tenant_id --tenant B --query salary information参数要点--model默认all-MiniLM-L6-v2--tenant-field默认tenant_idinjection子命令的--query默认help、--limit默认10。脚本在依赖缺失时如未安装 sentence-transformers 或 qdrant-client会输出[!] ...提示并退出任何异常都会被捕获并打印[!] {cmd} failed: {exc}便于排障。报告与修复建议评估完成后针对每类弱点落地修复继承自 SKILL.md Step 7 及 standards.md 的弱点-控制映射弱点类控制措施嵌入反转 / 成员推断认证 限流嵌入端点避免暴露原始相似度分数限制可查询嵌入的权限增加查询审计对应 standards.md 中 LLM02 敏感信息泄露的缓解跨租户泄露服务端强制租户过滤每租户独立 collection/namespace绝不依赖客户端提供的过滤条件知识库投毒对每个摄取 chunk 做来源标记provenance tagging与内容校验扫描注入限制单一来源在检索结果中的占比检索中的间接注入净化检索文本并应用输出护栏可结合仓库内defending-llms-with-guardrails技能验证标准Checklist完整评估应逐项确认取自 SKILL.md Validation CriteriaRAG 流水线已盘点嵌入模型、存储、租户模型、度量、top_k、过滤嵌入反转暴露已测量并评级语料内 vs 对照条目的成员推断 delta 已计算多租户隔离在带/不带客户端过滤两种情况下均已测试服务端强制得到确认投毒主导度仅在测试 collection 中测量检索 chunk 已扫描间接注入内容每类弱点均产出带修复建议的发现评估过程中未修改生产语料库。延伸阅读技能完整定义与工作流SKILL.md框架映射NIST AI RMF / ATLAS / OWASPreferences/standards.md可执行评估器源码scripts/agent.py【免费下载链接】Anthropic-Cybersecurity-Skills817 structured cybersecurity skills for AI agents · Mapped to 6 frameworks: MITRE ATTCK, NIST CSF 2.0, MITRE ATLAS, D3FEND, NIST AI RMF MITRE F3 (Fight Fraud) · agentskills.io standard · Works with Claude Code, GitHub Copilot, Codex CLI, Cursor, Gemini CLI 20 platforms · 29 security domains · Apache 2.0项目地址: https://gitcode.com/GitHub_Trending/an/Anthropic-Cybersecurity-Skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
网站建设高端定制企业官网