6.2 Deserialization
Last updated
Last updated
Serialization is the process of converting an object into a stream of data, while deserialization is the reverse process of converting that stream back into an object. Serialization
Saves and transmits the state of an object
Can be used to store objects in files, databases, or memory
Can be used to exchange data between applications, over networks, and more
Can be used to identify changes in data over time
Deserialization
Reconstructs the original object from serialized data
Makes data easier to read and modify as a native structure in a programming language
Can be used to recreate objects after they have been serialized for transmission
Security considerations
Serialized objects can be vulnerable to security risks if not handled carefully, attackers can manipulate serialized objects to inject malicious code or objects into the application during deserialization
Insecure deserialization in Java occurs when an application deserializes untrusted data without proper validation, allowing an attacker to manipulate the serialized object and achieve remote code execution (RCE), privilege escalation, or other malicious activities.
Gadgets and Gadget Libraries
Gadget: A property or method inside an object that can be leveraged for exploitation when deserialized.
Gadget Library: Some Java libraries contain pre-existing gadgets that attackers can use to construct an exploit. Examples include:
Apache CommonsCollections (versions 1-6)
Spring Framework
JDK classes (e.g., java.rmi.server.UnicastRemoteObject
)
Jackson, Fastjson, and XStream
Even though these libraries are not inherently vulnerable, if an application deserializes untrusted input while these libraries are present in the classpath, an attacker can exploit them to create a gadget chain, leading to successful exploitation.
How Java Deserialization Works
Serialization in Java is the process of converting an object into a byte stream that can be stored or transmitted.
ObjectOutputStream.writeObject(obj)
: Serializes an object.
ObjectInputStream.readObject()
: Deserializes the byte stream into an object.
If the input passed to readObject()
is attacker-controlled, it can be exploited to execute arbitrary code.
Exploiting Java Insecure Deserialization
An attacker can exploit insecure deserialization by sending a crafted malicious serialized object to a vulnerable application. Common attack vectors include:
Cookies β Sending serialized payloads via HTTP cookies.
Web Requests β Injecting payloads through POST/GET parameters.
Files β Uploading serialized payloads as files.
Inter-Process Communication (IPC) β Sending malicious objects through network sockets, RMI, or JNDI.
Ysoserial Tool
This generates a payload that executes whoami
upon deserialization.
The payload is then sent to the vulnerable application, which executes the system command when deserialized.
If an application deserializes objects from an untrusted source, an attacker can craft a malicious object like:
When deserialized, this object spawns a calculator on Windows or executes a system command.
Avoid deserialization of untrusted data.
Use ObjectInputFilter
(Java 9+) to allowlist safe classes:
Use safer data formats like JSON or protobuf instead of Java serialization.
Keep dependencies updated to avoid known gadget chains.
Restrict available classes in the classpath to prevent gadget chains.
PHP serialization allows objects, arrays, and values to be converted into a storable string format using serialize()
. However, when unserialize()
is used on untrusted data, it can lead to arbitrary code execution, data manipulation, or unauthorized object injection.
Output:
This serialized string can be stored in a database, session, or sent over a network.
In PHP, special magic methods can be abused during deserialization:
__wakeup()
β Executes code when an object is unserialized.
__sleep()
β Executes code before serialization.
__destruct()
β Executes when an object is destroyed.
__toString()
β Can be used to trigger code execution via string conversion.
If a PHP application unserializes untrusted input, an attacker can inject a malicious object that triggers one of these methods.
A vulnerable PHP application:
An attacker can craft a malicious payload:
Example payload:
Sending this payload via ?data=O:9:"Malicious":0:{}
executes whoami
on the server when the object is destroyed.
Never use unserialize()
on untrusted input.
Use JSON instead of serialization.
Implement allowlisting to only accept expected classes:
Use Web Application Firewalls (WAFs) to detect and block serialized attack payloads.
is a popular tool for generating exploit payloads using known gadget libraries. Example command: