Can plain data run code on your server? Insecure deserialization

Insecure deserialization happens when your app rebuilds objects from outside data in a language's native format. Per OWASP, avoiding native formats greatly reduces the risk, and JSON is a safe format to use instead.

Published 2 min read#App security#Web development

A file that holds nothing but data: a shopping cart, user settings, a session. Your server reads it, and runs code you never wrote. That is insecure deserialization, and by the end of this article you will know where to look for it in your code and what to put in its place.

What is deserialization?

Deserialization is when your app takes stored or sent data and rebuilds an object from it in memory. The reverse, serialization, turns an object into a format that can be stored or sent.

That is how the OWASP Deserialization Cheat Sheet defines them. Think of a flat-pack furniture box that comes with assembly instructions: you follow them to the letter. If a stranger wrote the instructions, the stranger decides what gets built in your room.

Why does it become a vulnerability?

Deserialization becomes a vulnerability when the data comes from the user. The cheat sheet's rule is that attackers should be prevented from steering the type that will be instantiated, and that avoiding native formats greatly reduces the risk.

The mistake is human and common: we want to keep the cart in a cookie, so we serialize the whole object because it is easier, and decode it again on the next request. The user can edit the cookie. And the cheat sheet warns that it is inevitable that more “gadget” classes will be discovered that can be abused.

The cheat sheet says it plainly about Python: unpickling can execute arbitrary code, so never unpickle untrusted data.

What is the risk in each language?

The risk is the same in every language: a function that builds objects from the language's native format, run on outside data.

Language

The function to watch

What the cheat sheet says

Python

pickle

Never unpickle untrusted data

PHP

unserialize()

Use json_decode() and json_encode() instead

Java

ObjectInputStream

An allowlist of classes in resolveClass() may be useful, but filtering does not make untrusted data safe

.NET

BinaryFormatter

Microsoft said it is dangerous and cannot be secured, so do not use it

How do you close it in your code today?

You close it by moving away from the language's native format, sending and receiving data as JSON, and building the object yourself from the fields you expect.

  1. Search your code for unserialize(, pickle.loads, ObjectInputStream and BinaryFormatter. At the end of this step you have a list.

  2. For each line on the list, check where the data comes from: a cookie, a form field, a header, an uploaded file, or a message from a queue.

  3. Where it comes from outside, switch it to JSON, and copy only the known fields into the object, as you do against mass assignment.

  4. If you cannot switch now, add an allowlist of classes, and put the switch on your next task list, because, per the cheat sheet, filtering does not make arbitrary untrusted deserialization safe.

// before: the cookie decides which objects get built
$cart = unserialize($_COOKIE['cart']);

// after: plain arrays only, then check each field
$cart = json_decode($_COOKIE['cart'], true);

You will find the same idea in XXE: the parser does extra work because the data asked it to.

Where do you start now?

Data that comes from the user is read as data, and it does not choose what gets built on your server. Open your project now, search for the first function in the table, and check where its data comes from.

If you want someone to look for this kind of flaw across your whole platform with you, there is Muhassan. What was the first function you found in your code?

FAQ

If the data is encrypted, is there still a risk?

The cheat sheet's answer is a signature: you can sign the data as part of serialization, and not deserialize any message without an authenticated signature. Moving away from the native format is still better.

Does JSON protect me from everything?

No. Per the cheat sheet, switching to a pure data format like JSON lessens the chance that custom deserialization logic is put to malicious use, and with JSON.Net, TypeNameHandling must only be set to None. You still have to check every field before you use it, like any user input.

How do I know if my code has this problem?

Search your code for your language's native decoding functions, and check whether any of them reads from a cookie, a form, an uploaded file or a header.

Share

More articles