GGUF Radar 不运行模型查出投毒模板,重建后权重误差为0
GGUF Radar 能在不运行 LLM 模型文件的情况下,发现其中被投毒的聊天模板和隐藏载荷,并在模型权重不变的前提下重建一个干净的 GGUF 文件。
它可读取本地 .gguf 文件、Hugging Face 仓库和直链,以及 Safetensors 和 PyTorch 模型的聊天模板文件。检查 Hugging Face 模型时使用 HTTP Range 请求,只下载每个文件的头部和尾部,不下载权重。这一点很关键:恶意模板可能在模型加载或对话时触发代码执行,静态扫描能在使用前拦截。
它针对的攻击,以及背后对 327 万个 Hugging Face 仓库的扫描,见博文《Hugging Face 上带隐藏恶意软件的 GGUF LLM 模型已影响 2400+ 受害者》。
它检查什么
| 位置 | GGUF Radar 检查什么 |
|---|---|
聊天模板(tokenizer.chat_template、tokenizer_config.json、chat_template.json、chat_template.jinja) | 解析为 Jinja2 语法树,绝不渲染。标记文件读写、解压缩、启动进程、下载命令、给 agent(智能体)的隐藏指令、条件触发、隐藏新增代码的指令,以及已知的 Python 模板注入 gadget(可被滥用的代码片段)。 |
| 最后一个 tensor 之后的字节 | 根据 header 和 tensor 表计算文件应有的结束位置。之后的任何内容都会报告,并给出大小、熵和文件签名(PE、ZIP、原始 Deflate)。 |
| tensor 之间的对齐填充和空隙 | 报告本应为零的空间中的非零字节,同时不拒绝合法的大 general.alignment 值。 |
| 零 scale 量化块(Q8_0、Q4_K) | 报告 scale 为零但数据字节非零的块,以及负零(0x8000)scale。需要完整文件,因此只在本地扫描中运行。 |
| 元数据 | 重复键和格式错误的结构。 |
Safetensors 和 PyTorch 模型只覆盖聊天模板。追加字节在这里不是问题,因为 safetensors 加载器会拒绝 tensor 未精确填满文件的文件。
sanitize 命令会以规范形式重建 GGUF 文件。它写入 32 字节对齐并清零填充,丢弃最后一个 tensor 之后的所有内容,清除零 scale 块的数据字节,把负零 scale 设为 +0.0,并可替换或剥离聊天模板。去量化后的权重保持完全一致。
安装
GGUF Radar 需要 Python 3.10 或更高版本。运行时仅依赖 numpy 和 jinja2。
pip install git+https://github.com/Root0ne/gguf-radar
从克隆仓库安装:
git clone https://github.com/Root0ne/gguf-radar.git
cd gguf-radar
pip install .
使用
扫描本地文件或文件夹:
gguf-radar scan ./model.gguf
gguf-radar scan ./models/
扫描 Hugging Face 仓库或单个文件。接受仓库 URL、文件 URL 和 owner/repo 标识:
gguf-radar scan Qwen/Qwen2.5-0.5B-Instruct-GGUF
gguf-radar scan https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/blob/main/qwen2.5-0.5b-instruct-q4_k_m.gguf
扫描目标列表,每行一个,允许 # 注释(见 examples/targets.txt),并写出 JSON 报告:
gguf-radar scan --list targets.txt --workers 6 --json report.json
公开模型不需要 token。对于受限或私有仓库,或更大的批量任务,可通过 --token 或 HF_TOKEN 环境变量传入 Hugging Face token。如果受限仓库的模板无法读取,会报告为 ERROR,绝不会当成 CLEAN。
重建干净文件:
gguf-radar sanitize suspicious.gguf -o clean.gguf
gguf-radar sanitize suspicious.gguf -o clean.gguf --replace-template verified_template.jinja
gguf-radar sanitize suspicious.gguf -o clean.gguf --strip-template
运行 gguf-radar scan --help 和 gguf-radar sanitize --help 查看所有选项。
示例
一个合成测试文件,带有自解压模板,并在最后一个 tensor 后追加了压缩载荷:
$ gguf-radar scan demo-model.gguf
[MALICIOUS] demo-model.gguf
[MALICIOUS] demo-model.gguf
Info: GGUF v3, arch=llama, size=5.79 KB, alignment=32B
Overlay: +4,093 bytes past expected EOF (entropy=7.96)
Chat template findings (risk=100/100, sha256=51be7d2fddd59b27...):
- [CRITICAL] self_extracting_file_io: Self-extracting binary file-I/O or Deflate decompression API in template: 'IO.File]::OpenRead'
- [CRITICAL] self_extracting_file_io: Self-extracting binary file-I/O or Deflate decompression API in template: 'IO.Compression.DeflateStream'
- [CRITICAL] self_extracting_file_io: Self-extracting binary file-I/O or Deflate decompression API in template: 'WriteAllBytes'
- [CRITICAL] powershell_dropper_execution: Hidden PowerShell execution or EOF offset seek detected in template: '.Length-4096'
- [CRITICAL] powershell_dropper_execution: Hidden PowerShell execution or EOF offset seek detected in template: 'Start-Process'
- [HIGH] shell_command_directive: Shell execution command embedded in template: 'powershell'
Summary (unauthenticated (public API)): 1 target(s) | MALICIOUS=1 SUSPICIOUS=0 CLEAN=0 ERROR=0
$ gguf-radar sanitize demo-model.gguf -o demo-model.clean.gguf --strip-template
Sanitized GGUF written to: demo-model.clean.gguf
EOF overlay bytes removed: 4,093 B
Chat template action: stripped
Zero weight distortion: exact (max |dW| = 0.0)
$ gguf-radar scan demo-model.clean.gguf
[CLEAN] demo-model.clean.gguf
判定和退出码
| 判定 | 退出码 | 含义 |
|---|---|---|
| CLEAN | 0 | 扫描目标中没有任何发现。 |
| SUSPICIOUS | 2 | 有异常,需要人工查看:高或中危模板发现、最后一个 tensor 之后的小型低熵尾部、负零 scale、重复键或其他结构异常。 |
| MALICIOUS | 2 | 有严重模板发现、带文件签名或高熵的追加字节、非零填充,或零 scale 块中有数据。 |
| ERROR | 1 | 目标无法读取:路径错误、格式不支持、HTTP 或认证错误。 |
扫描多个目标时,只要有任意 SUSPICIOUS 或 MALICIOUS 结果,退出码就是 2;否则只要有任意 ERROR,退出码就是 1。
在 CI 中使用
退出码让在模型被使用前拦截它变得容易。GitHub Actions 步骤:
- name: Scan models with GGUF Radar
run: |
pip install git+https://github.com/Root0ne/[email protected]
gguf-radar scan ./models/ --json gguf-radar-report.json
Python API
from gguf_radar import sanitize_gguf_file
from gguf_radar.hf_scanner import scan_local_gguf
result = scan_local_gguf("model.gguf")
print(result.verdict) # CLEAN、SUSPICIOUS、MALICIOUS 或 ERROR
for finding in result.template_findings:
print(finding.severity, finding.category, finding.description)
if result.verdict != "CLEAN":
report = sanitize_gguf_file("model.gguf", "model.clean.gguf", strip_template=True)
print(report.eof_overlay_bytes_removed)
YARA 规则
rules/gguf_supply_chain_threats.yar 包含博文中描述的样本和模板规则:被恢复的 Windows 植入程序、从 GGUF 文件末尾解包数据的模板、劫持 agent 的模板,以及 Python 模板注入 gadget。
限制
GGUF Radar 是静态扫描器。它不运行模型或模板,因此除了语法树显示的内容外,无法看到模板在运行时实际做什么。模板检查基于模式:刻意规避的模板可能通过,无害模板也可能误报。把 SUSPICIOUS 当作需要查看的理由,而不是证据。
零 scale 块检查需要完整文件,仅在本地扫描运行。对于非常小的权重块,零 scale 也可能是正常舍入结果,而 GGUF Radar 目前会把此类块中的任何数据报告为 MALICIOUS。仅依赖此检查得出的结论值得进一步查看。无论哪种情况,sanitize 都会清除这些字节,且不会改变权重。
贡献与安全
欢迎通过 issue 报告 bug、误报和漏报。提交 pull request 前请先看 CONTRIBUTING.md。GGUF Radar 自身的漏洞请按 SECURITY.md 所述私下报告。
变更记录在 CHANGELOG.md。如果在研究中使用 GGUF Radar,引用信息见 CITATION.cff。
许可证
源代码以 MIT License 发布,见 LICENSE。Logo、文档和 YARA 规则以 CC BY 4.0 发布,见 LICENSE-ASSETS。两者都只要求保留对 @Root0ne 的署名。