Skip to content
RCE Write-up Advanced

Hunting Vulnerabilities in Keras Model Deserialization

Keras model files contain a config.json that names Python modules and classes to import and instantiate during loading. In versions before 3.9, the deserializer imported any module named in that file, so a crafted model could run arbitrary Python code when a victim loaded it.

blog.huntr.com

Published on blog.huntr.com

The ability to save (serialize) and load (deserialize) trained models is fundamental to machine learning frameworks. Training a neural network can take hours, days or even weeks on expensive hardware, so developers need to save their work and share with others.

Faisal Khan, on blog.huntr.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The deserializer passed a module name taken from the model file straight to importlib.import_module without restricting which modules were allowed.

Impact

A crafted model could cause arbitrary code execution on the machine that loads it; the post's own downloader example is limited because the Lambda layer always passes the input as the first argument, but it still shows arbitrary Python calls remain possible.

What the attacker needed

Victim loads an attacker-supplied model file; Keras version before 3.9 for the import flaw

The fix

Keras was fixed by restricting imports to an allowlist of Keras packages, enabling safe mode by default and adding basic type checks.

How it was found

  1. Create and save a normal Keras model to get a valid .keras archive.
  2. Open the archive and read config.json to keep the layer structure.
  3. Replace a layer's config with one that names an allowed Keras function and its arguments.
  4. Write the modified config.json back into a new .keras archive.
  5. Load the crafted model with safe_mode enabled.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

A Keras model file is really a zip of small text files plus the learned numbers. One text file lists which Python classes to build, and older Keras trusted that list completely. So opening a model someone sent you could run their code on your machine, like opening a document that secretly installs a program.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.