The application deserializes untrusted data without sufficiently verifying that the resulting data will be valid.
It is often convenient to serialize objects for communication or to save them for later use. However, deserialized data or code can often be modified without using the provided accessor functions if it does not use cryptography to protect itself. Furthermore, any cryptography would still be client-side security -- which is a dangerous security assumption.
Data that is untrusted can not be trusted to be well-formed.
When developers place no restrictions on "gadget chains," or series of instances and method invocations that can self-execute during the deserialization process (i.e., before the object is returned to the caller), it is sometimes possible for attackers to leverage them to perform unauthorized actions, like generating a shell.
Marshaling and unmarshaling are effectively synonyms for serialization
and deserialization, respectively.
In Python, the "pickle" functionality is used to perform serialization
Time of Introduction
Architecture and Design
Technical Impact: Varies by context
The consequences can vary widely, because it depends on which objects
or methods are being deserialized, and how they are used.
Technical Impact: Modify application
data; Unexpected state
Attackers can modify unexpected objects or data that was assumed to be
safe from modification.
Technical Impact: DoS: resource consumption
If a function is making an assumption on when to terminate, based on a
sentry in a string, it could easily never terminate.
Technical Impact: Other
Code could potentially make the assumption that information in the
deserialized object is valid. Functions that make this dangerous
assumption could be exploited.
Likelihood of Exploit
This code snippet deserializes an object from a file and uses it as
a UI button:
File file = new File("object.obj");
ObjectInputStream in = new ObjectInputStream(new
This code does not attempt to verify the source or contents of the
file before deserializing it. An attacker may be able to replace the
intended file with a file that contains arbitrary malicious code which
will be executed when the button is pressed.
In Python, the Pickle library handles the serialization and deserialization processes. In this example derived from [R.502.7], the code receives and parses data, and afterwards tries to authenticate a user based on validating a token.
def dataReceived(self, data):
# Code that would be here would parse the incoming
# After receiving headers, call confirmAuth() to
if not check_hmac(token['signature'], token['data'],
self.secure_data = token['data']
Unfortunately, the code does not verify that the incoming data is
legitimate. An attacker can construct a illegitimate, serialized object
"AuthToken" that instantiates one of Python's subprocesses to execute
arbitrary commands. For instance,the attacker could construct a pickle
that leverages Python's subprocess module, which spawns new processes
and includes a number of arguments for various uses. Since Pickle allows
objects to define the process for how they should be unpickled, the
attacker can direct the unpickle process to call Popen in the subprocess
module and execute /bin/sh.
Web browser allows execution of native methods via
Phases: Architecture and Design; Implementation
If available, use the signing/sealing features of the programming
language to assure that deserialized data has not been tainted. For
example, a hash-based message authentication code (HMAC) could be used
to ensure that data has not been modified.
When deserializing data, populate a new object rather than just
deserializing. The result is that the data flows through safe input
validation and that the functions are safe.
Explicitly define final readObject() to prevent deserialization. An
example of this is:
private final void readObject(ObjectInputStream in) throws
throw new java.io.IOException("Cannot be deserialized"); }
Phases: Architecture and Design; Implementation
Make fields transient to protect them from deserialization.
An attempt to serialize and then deserialize a class containing
transient fields will result in NULLs where the transient data should
be. This is an excellent way to prevent time, environment-based, or
sensitive variables from being carried over and used improperly.
Avoid having unnecessary types or gadgets available that can be
leveraged for malicious ends. This limits the potential for unintended
or unauthorized types and gadgets to be leveraged by the attacker.
Whitelist acceptable classes. Note: new gadgets are constantly being
discovered, so this alone is not a sufficient mitigation.
Serialization and deserialization refer to the process of taking
program-internal object-related data, packaging it in a way that allows the
data to be externally stored or transferred ("serialization"), then
extracting the serialized data to reconstruct the original object