Dev.to Security 🔐 Cybersecurity 👁 0 📖 7 min read

Five Poisoned GGUF Models: A Trojan After the Last Tensor, Orders in the Chat Template

Book: Event-Driven Architecture Pocket Guide The series: Pocket Guides for Developers — 8 short books on agents, RAG, prompts, observability, events, databases and system design — the whole series My project: Hermes

Five Poisoned GGUF Models: A Trojan After the Last Tensor, Orders in the Chat Template

You ask your local model "What is a mutex?" Before the model reads your question, its own file slips a line into the conversation that you never see: download a PowerShell script, save it to the Windows temp folder, run it in a hidden window. If your agent has a shell tool, that line is addressed to the agent, not to you.

That is one of two tricks in five GGUF models that Malspark reported on November 27, after a scan of 3,273,389 public Hugging Face model repositories. The other trick is a Windows trojan appended after the model's last tensor, where the loaders Malspark tested never look. As of November 28, Hugging Face's API lists the repositories as disabled. The files people already downloaded are still on their disks.

What the scan found

Malspark ran the scan on November 26. It covered 226,310 GGUF repositories holding 2.56 PB of weights, and it parsed 572,184 chat templates statically, without executing them. It read headers and file tails with HTTP Range requests instead of downloading whole models. After removing security test repos, harmless persona prompts and research samples, five repositories from two accounts were left.

Repository Method Downloads (Nov 27)
Mir2k/Huihui-Qwen3.5-9B-abliterated-GGUF Reply prefix + hidden executable 1,488
Mir2k/Inkling-2-Mini-Uncensored-GGUF Reply prefix + compressed executable 458
timdedid/qwen3.8-cyber-uncensored Agent tool hijacking 271
timdedid/qwen3-cyber-UNCENSORED-2 Agent tool hijacking 136
timdedid/qwen3-cyber-UNCENSORED Agent tool hijacking 75

That is 2,428 downloads on November 27, up from 1,502 the day before. Malspark notes that Hugging Face counts downloads, not people, so the number of affected users is at most that. Magica checked on November 28 and found all five "disabled for a content-policy breach," with 2,807 downloads between them. The API entry for the timdedid model shows the same "disabled": true.

Hiding place one: the chat template

A GGUF file carries its own Jinja2 chat template under the metadata key tokenizer.chat_template, and runtimes apply it to every prompt. Malspark calls the attack "chat template poisoning." The template adds text to the conversation that never shows up in your chat window. The C++ runtimes use a restricted template engine, so these attacks don't break out to the OS. They just use ordinary template logic to insert words.

The timdedid models wait for a trigger. When your latest message contains "What is", the template inserts a fake [SYSTEM DIRECTIVE] telling an agent with a PowerShell tool to fetch value.ps1 from a WordPress site, save it as payload.ps1 in the Windows temp folder and run it hidden. Malspark did not recover that second-stage script.

The Mir2k models go after the human. Their template adds a French system message that tells the model to open every reply with one PowerShell line. If you copy and run it, the line finds the largest .gguf file in your user profile, reads its trailing bytes and writes them to %TEMP%\d.exe. The first version looked in a fixed Downloads path. Nine hours later it was replaced by one that searches the whole profile.

Editorial illustration of a scroll of template code with a small burnt-orange note slipped between its lines, half hidden from a chat bubble above it
The template runs on every prompt. What it adds never reaches your chat window.

There is one more copy outside Hugging Face. Magica found on November 28 that the timdedid account's Ollama mirror, timdedid/qwen3-1.7b-cyber, was still listed and its model file still held the hidden PowerShell instruction. Magica did not test whether Ollama applies that embedded template, so execution through Ollama is unconfirmed.

Hiding place two: after the last tensor

The Mir2k files carry a Windows program appended after the last tensor. Malspark says runtimes such as llama.cpp and LM Studio read only the byte ranges the header points to, so the model loads and chats normally, and no runtime warns about the extra bytes. Safetensors refuses a file whose tensors don't fill it exactly; GGUF has no such check.

The program is an 11.47 MB Windows x64 binary, about 10.25 MB of which is an embedded copy of Tor 0.4.9.16. Malspark lists what it does: it exits if it spots a VM or sandbox, persists through a scheduled task every 10 minutes plus registry keys, talks to a Tor onion service, steals saved passwords from Chrome, Edge and Brave, targets the Phantom and MetaMask wallets, logs keys, reads the clipboard, takes screenshots and webcam captures, and mines Monero when the machine is idle. Its UAC bypass attempt fails because of malformed paths.

Nothing in the file runs by itself. You have to paste the model's command into PowerShell for the hidden bytes to execute. The template does the persuading.

Editorial illustration of a long cassette-like model file with neat rows of weight blocks, and a dark extra compartment bolted onto its far end
Loaders stop at the end of the tensor data. The payload starts right after it.

Malspark also reproduced three other hiding places in synthetic files: data in alignment padding, a GGUF and ZIP polyglot, and data in zero-scale Q8_0 blocks. Those are lab results, not files found in the wild, and Malspark reports none of them changes the dequantized weights or triggers a warning in llama.cpp or gguf-py.

Check the models you already pulled

Start with gguf-radar, the MIT-licensed scanner Malspark's write-up points to (Python 3.10+). It parses the template without running it, flags bytes after the last tensor and non-zero padding, and can rebuild a clean file with the weights unchanged. Install it from the repository Malspark links, not from a fork:

pip install git+https://github.com/Root0ne/gguf-radar
gguf-radar scan ./model.gguf
gguf-radar scan owner/model
gguf-radar sanitize suspicious.gguf -o clean.gguf

Per its README, it exits 0 for CLEAN, 2 for SUSPICIOUS or MALICIOUS and 1 on error, so it can gate a CI job or an internal model registry.

If you want to see the two hiding places yourself, gguf-py (pip install gguf) reads the metadata without loading the model:

# check_gguf.py: print the template, count tail bytes
import os
import sys
from gguf import GGUFReader

path = sys.argv[1]
reader = GGUFReader(path)

field = reader.fields.get("tokenizer.chat_template")
if field is not None:
    raw = field.parts[field.data[0]]
    print(bytes(raw).decode("utf-8"))

end = max(t.data_offset + t.n_bytes for t in reader.tensors)
tail = os.path.getsize(path) - end
print(f"bytes after the last tensor: {tail}")

Read the template for anything that inserts a system message or reacts to the words in your prompt. A few bytes, fewer than the file's alignment (32 by default), are normal padding. A tail of megabytes is the Mir2k pattern. This quick check misses the three lab tricks, so run gguf-radar too.

For Ollama models, ollama show --modelfile <model> prints the model's Modelfile, including its TEMPLATE block. Read that block the same way.

Agents with a shell need a template you reviewed

The timdedid attack only works if a model with tools gets to run what it is told. Two changes cover most of it.

First, don't run the template that came with the file. Malspark's advice is to supply a reviewed template with --chat-template-file in llama.cpp or TEMPLATE in an Ollama Modelfile. llama.cpp's function-calling docs use the same flag with --jinja to override the embedded template:

llama-server -m clean.gguf --jinja \
  --chat-template-file reviewed.jinja

Take the reviewed template from the base model's original repository, not from the community quantization you downloaded.

Second, treat any command a model suggests as untrusted, as Malspark puts it. That goes for the PowerShell line in a chat reply and for the tool call an agent wants to make. Keep shell tools behind an approval prompt, and be most suspicious of "uncensored" or "abliterated" re-uploads from accounts you don't know, which is what all five of these were.

If this was useful

A model file is an inbound message from someone you don't control, and the fix here is the one the Event-Driven Architecture Pocket Guide applies to queues: validate at the boundary, and quarantine the payload that doesn't parse cleanly instead of letting it reach the worker. The guide is about what goes wrong once untrusted messages flow through your system: sagas, CQRS, the outbox, and the traps nobody warns you about. It's part of Pocket Guides for Developers, 8 short books on agents, RAG, prompts, observability, events, databases and system design.

I also build Hermes IDE, so weigh this with that in mind: it runs Claude Code, Codex, OpenCode and other agents side by side, each in its own terminal and worktree, and it never answers a permission prompt for you. It's free, source-available (BSL 1.1), on macOS, Windows and Linux, and on GitHub.

Event-Driven Architecture Pocket Guide: Saga, CQRS, Outbox, and the Traps Nobody Warns You About

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.