Back to all posts

A Model File Is Executable Configuration: The GGUF Chat Template Attack

2026-11-29
luodaInt
A Model File Is Executable Configuration: The GGUF Chat Template Attack

A Model File Is Executable Configuration: The GGUF Chat Template Attack

Most teams treat a downloaded model as data. It arrives as a file of weights, it loads into a runtime, and the trust question feels like a licensing question. A scan of Hugging Face published on 27 November shows why that framing fails: a GGUF file carries a chat template that the runtime executes on every prompt, and five repositories poisoned that template, two of them also hiding a Windows trojan in bytes appended after the last tensor.

The numbers are small. The mechanism is not, and it generalises well past GGUF.

The template is a program

Every GGUF file can store a Jinja2 program under the metadata key tokenizer.chat_template. Before each reply, the runtime passes the conversation through that program to build the exact text the model reads. llama.cpp renders it in a restricted engine with no Python behind it, so classic template injection payloads that reach the operating system do not apply there. The point is narrower and worse: ordinary template logic can add text to the conversation that never appears in the chat window.

This is not a GGUF-only property. Safetensors and PyTorch models carry the same kind of template in tokenizer_config.json or chat_template.jinja. The template half of the attack travels with the format nobody thinks of as executable.

Two ways in

The first is template poisoning, seen in the timdedid models. When the user's latest message contains the phrase What is, the template inserts a [SYSTEM DIRECTIVE] telling an agent with a powershell tool to download value.ps1 from a WordPress site into C:\Windows\Temp\payload.ps1 and run it in a hidden window. The README advises against replacing the embedded template, which keeps the trigger in place. The second-stage script was not recovered, and public reputation services had no record of the URL, so what it would have installed is unknown.

The second is bytes after the last tensor, and it is specific to GGUF. The header and tensor table define where model data ends: the start of the data section plus the end of the farthest tensor. Everything past that point is never read, so it does not change how the model loads and no runtime warns about it. Safetensors does not allow this, because its loader refuses a file whose declared tensors do not fill it exactly.

Mir2k used both halves together. One model carried a plain Windows executable followed by about half a megabyte of filler, so a scanner that reads only the last few kilobytes sees nothing. The other carried the same program compressed with raw Deflate, which removes every recognisable file signature while PowerShell can still decompress it in memory.

Plant first, arm later

The repository history is the part worth copying into a threat model. On 22 November the Mir2k account created 14 repositories, 12 of them empty placeholders named after expected releases such as Qwen4-27B, DeepSeek-V4 and gemma-4-27b. The first model went up that day with the executable already appended and its original, clean template. A second model followed on 24 November with the same executable in compressed form, again with a clean template. For those days the payload sat in the files with nothing to start it.

On the morning of 26 November both templates were replaced within about an hour. Nine hours later both were replaced again, this time searching the whole user profile rather than a fixed folder. Downloads made before 26 November therefore contain the payload but not the trigger.

The armed template adds a French system message instructing the model to begin every reply with one PowerShell line, copied character by character, then answer normally. That line finds the largest .gguf file in the user profile, reads the last bytes of it and writes them to %TEMP%\d.exe. One model reads 11,474,944 bytes from 12,000,000 bytes before the end of the file; the other reads the last 4,200,000 bytes and decompresses them first.

Both unpack to the same 11.47 MB Windows x64 program. Most of its size is a copy of Tor 0.4.9.16. It checks for VirtualBox, VMware and Sandboxie drivers and for usernames such as sandbox and test, and exits if it finds them. On a real machine it installs a scheduled task that runs every 10 minutes with highest privileges, keeps two watchdog processes alive, and marks its process as critical so ending it crashes Windows. It reads saved passwords from Chrome, Edge and Brave through DPAPI (leaving Chrome's newer App-Bound entries undecrypted), targets the Phantom and MetaMask wallet extensions, and supports keylogging, clipboard reading and screenshots. When the machine is idle it mines Monero over Tor. Its folders are named ollie under AppData\Roaming, close enough to Ollama to look familiar. Its UAC bypass does not work: both registry paths were compiled without their backslashes, so Windows never finds them.

What the scan found

The scan covered all 3,273,389 public model repositories on Hugging Face on 26 November, including 226,310 GGUF repositories holding 2.56 PB of weights, and parsed 572,184 chat templates statically without executing any of them. After manual review removed security test repositories and published research samples, five remained: two from Mir2k with 1,488 and 458 downloads, and three from timdedid with 271, 136 and 75. That totals 2,428 downloads as of 27 November, up from 1,502 on 26 November.

Two caveats on those figures. They come from one research organisation's own scan, so treat them as single-source until someone reproduces the count. And Hugging Face counts downloads, not people, so the number of affected users is lower or equal. All five repository pages still answered on the Hugging Face API when I checked on 29 November; that means they are reachable, not that they are safe.

The writeup also reproduces three further hiding places with synthetic test files, none used by the live campaigns: a raised general.alignment that creates a multi-megabyte gap loaders skip without checking, a file that is both GGUF and ZIP so it also imports as Python through zipimport, and Q8_0 blocks whose scale is set to positive or negative zero so 32 bytes of payload produce zero weights. Signature scanning cannot keep up with that. Structural checks can.

What a team actually does

A model pull is a dependency install with no review step. The artifact contains executable configuration, so it belongs in the same intake gate as a package or a container image.

  1. Scan before the first load. gguf-radar (MIT licence) parses the template into a syntax tree without running it and flags file reads and writes, decompression, process starts, download commands and conditional hidden directives. It also computes where model data must end and reports bytes after that point, non-zero padding and non-zero zero-scale blocks. Its rebuild writes a canonical file with 32-byte alignment and leaves the dequantised weights identical.
  2. Pin a reviewed template. llama.cpp accepts --chat-template-file, and an Ollama Modelfile accepts TEMPLATE. That takes the embedded template out of the loop.
  3. Treat model-proposed commands as untrusted. The strongest form of this attack needs someone to paste a line into PowerShell. In an agent with shell access, the timdedid variant needs no paste at all, which is why the tool list matters more than the model name.
  4. Do not rely on antivirus or a tail read. The Deflate variant defeats both, and the padding and zero-scale variants defeat signature checks entirely.
  5. Record what you pulled. For each model in production, keep the repository, revision and file hash, so the day a repo turns malicious you know which machines loaded which version.

The fix that would end the trailing-bytes method belongs to the hubs and runtimes: reject GGUF files with bytes after the last tensor or non-zero padding, and extend template scanning to PowerShell loaders and hidden directives. Until that exists, the gate is yours. A chat template is code you did not write, shipped inside a file you thought was weights.

If you are putting local models into agents that hold credentials or run tools, get in touch and we will review the intake path with you.

Sources