Deserialization of Untrusted Data

Draft Base
Structure: Simple
Description

The product deserializes untrusted data without sufficiently ensuring that the resulting data will be valid.

The product deserializes untrusted data without sufficiently ensuring that the resulting data will be valid.
Common Consequences 3
Scope: Integrity

Impact: Modify Application DataUnexpected State

Attackers can modify unexpected objects or data that was assumed to be safe from modification. Deserialized data or code could be modified without using the provided accessor functions, or unexpected functions could be invoked.

Scope: Availability

Impact: DoS: Resource Consumption (CPU)

If a function is making an assumption on when to terminate, based on a sentry in a string, it could easily never terminate.

Scope: Other

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. Making an assumption that the code in the deserialized object is valid is dangerous and can enable exploitation. One example is attackers using gadget chains to perform unauthorized actions, such as generating a shell.

Detection Methods 1
Automated Static AnalysisHigh
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Potential Mitigations 7
Phase: Architecture and DesignImplementation
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.
Phase: Implementation
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.
Phase: Implementation
Explicitly define a final object() to prevent deserialization.
Phase: Architecture and DesignImplementation
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.
Phase: Implementation
Avoid having unnecessary types or gadgets (a sequence of instances and method invocations that can self-execute during the deserialization process, often found in libraries) 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. Add only acceptable classes to an allowlist. Note: new gadgets are constantly being discovered, so this alone is not a sufficient mitigation.
Phase: Architecture and DesignImplementation
Employ cryptography of the data or code for protection. However, it's important to note that it would still be client-side security. This is risky because if the client is compromised then the security implemented on the client (the cryptography) can be bypassed.
Phase: Operation

Strategy: Firewall

Use an application firewall that can detect attacks against this weakness. It can be beneficial in cases in which the code cannot be fixed (because it is controlled by a third party), as an emergency prevention measure while more comprehensive software assurance measures are applied, or to provide defense in depth [REF-1481].

Effectiveness: Moderate

Demonstrative Examples 2
This code snippet deserializes an object from a file and uses it as a UI button:

Code Example:

Bad
Java
java
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.
To mitigate this, explicitly define final readObject() to prevent deserialization. An example of this is:

Code Example:

Good
Java
java
In Python, the Pickle library handles the serialization and deserialization processes. In this example derived from [REF-467], the code receives and parses data, and afterwards tries to authenticate a user based on validating a token.

Code Example:

Bad
Python
python
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.
Observed Examples 13
CVE-2024-37052insecure deserialization in platform for managing AI/ML applications and models allows code execution via a crafted pickled object in a model file
CVE-2024-37288deserialization of untrusted YAML data in dashboard for data query and visualization of Elasticsearch data
CVE-2024-9314PHP object injection in WordPress plugin for AI-based SEO
CVE-2019-12799chain: bypass of untrusted deserialization issue (Deserialization of Untrusted Data) by using an assumed-trusted class (Permissive List of Allowed Inputs)
CVE-2015-8103Deserialization issue in commonly-used Java library allows remote execution.
CVE-2015-4852Deserialization issue in commonly-used Java library allows remote execution.
CVE-2013-1465Use of PHP unserialize function on untrusted input allows attacker to modify application configuration.
CVE-2012-3527Use of PHP unserialize function on untrusted input in content management system might allow code execution.
CVE-2012-0911Use of PHP unserialize function on untrusted input in content management system allows code execution using a crafted cookie value.
CVE-2012-0911Content management system written in PHP allows unserialize of arbitrary objects, possibly allowing code execution.
CVE-2011-2520Python script allows local users to execute code via pickled data.
CVE-2012-4406Unsafe deserialization using pickle in a Python script.
CVE-2003-0791Web browser allows execution of native methods via a crafted string to a JavaScript function that deserializes the string.
References 10
The CLASP Application Security Process
Secure Software, Inc.
2005
ID: REF-18
Exploiting Deserialization Vulnerabilities in Java
Matthias Kaiser
28-10-2015
ID: REF-461
PHP unserialization vulnerabilities: What are we missing?
Sam Thomas
27-08-2015
ID: REF-462
Marshalling Pickles: How deserializing objects can ruin your day
Gabriel Lawrence and Chris Frohoff
28-01-2015
ID: REF-463
Unserializing user-supplied data, a bad idea
Heine Deelstra
25-08-2010
ID: REF-464
Black Hat EU 2010 - Attacking Java Serialized Communication
Manish S. Saindane
26-04-2010
ID: REF-465
Why Python Pickle is Insecure
Nadia Alramli
09-09-2009
ID: REF-466
Exploiting misuse of Python's "pickle"
Nelson Elhage
20-03-2011
ID: REF-467
Deserialize My Shorts: Or How I Learned to Start Worrying and Hate Java Object Deserialization
Chris Frohoff
21-03-2016
ID: REF-468
D3FEND: Application Layer Firewall
D3FEND
ID: REF-1481
Likelihood of Exploit

Medium

Applicable Platforms
Languages:
Java : UndeterminedRuby : UndeterminedPHP : UndeterminedPython : UndeterminedJavaScript : Undetermined
Technologies:
ICS/OT : Often
Modes of Introduction
Architecture and Design
Implementation
Related Attack Patterns
Alternate Terms

Marshaling, Unmarshaling

Marshaling and unmarshaling are effectively synonyms for serialization and deserialization, respectively.

Pickling, Unpickling

In Python, the "pickle" functionality is used to perform serialization and deserialization.

PHP Object Injection

Some PHP application researchers use this term when attacking unsafe use of the unserialize() function; but it is also used for Improperly Controlled Modification of Dynamically-Determined Object Attributes.
Taxonomy Mapping
  • CLASP
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • Software Fault Patterns
Notes
MaintenanceThe relationships between Deserialization of Untrusted Data and Improperly Controlled Modification of Dynamically-Determined Object Attributes need further exploration. Improperly Controlled Modification of Dynamically-Determined Object Attributes is more narrowly scoped to object modification, and is not necessarily used for deserialization.