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.)
Deserialization of Untrusted Data
This vulnerability occurs when an application accepts and processes serialized data from an untrusted source without proper validation, allowing an attacker to manipulate the data to execute…
What is CWE-502?
Real-world CVEs caused by CWE-502
-
insecure deserialization in platform for managing AI/ML applications and models allows code execution via a crafted pickled object in a model file
-
deserialization of untrusted YAML data in dashboard for data query and visualization of Elasticsearch data
-
PHP object injection in WordPress plugin for AI-based SEO
-
chain: bypass of untrusted deserialization issue (CWE-502) by using an assumed-trusted class (CWE-183)
-
Deserialization issue in commonly-used Java library allows remote execution.
-
Deserialization issue in commonly-used Java library allows remote execution.
-
Use of PHP unserialize function on untrusted input allows attacker to modify application configuration.
-
Use of PHP unserialize function on untrusted input in content management system might allow code execution.
Parcours de l'attaquant étape par étape
- 1
This code snippet deserializes an object from a file and uses it as a UI button:
- 2
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.
- 3
To mitigate this, explicitly define final readObject() to prevent deserialization. An example of this is:
- 4
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.
- 5
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.
Vulnerable Java
This code snippet deserializes an object from a file and uses it as a UI button:
try {
File file = new File("object.obj");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
javax.swing.JButton button = (javax.swing.JButton) in.readObject();
in.close();
} Secure Java
To mitigate this, explicitly define final readObject() to prevent deserialization. An example of this is:
private final void readObject(ObjectInputStream in) throws java.io.IOException {
throw new java.io.IOException("Cannot be deserialized"); } How to prevent CWE-502
- 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.
- 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.
- Implementation Explicitly define a final object() to prevent deserialization.
- 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.
- 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.
- Architecture and Design / Implementation 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.
- Operation 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].
How to detect CWE-502
Plexicus détecte automatiquement CWE-502 et ouvre une PR de correction en moins de 60 secondes.
Codex Remedium analyse chaque commit, identifie cette faiblesse précise et livre une pull request prête à être relue avec le correctif. Pas de tickets. Pas de transferts.
Frequently asked questions
Qu'est-ce que CWE-502 ?
This vulnerability occurs when an application accepts and processes serialized data from an untrusted source without proper validation, allowing an attacker to manipulate the data to execute malicious code or cause unexpected behavior.
Quelle est la gravité de CWE-502 ?
MITRE évalue la probabilité d'exploitation comme Moyenne — l'exploitation est réaliste mais nécessite généralement des conditions spécifiques.
Quels langages ou plateformes sont affectés par CWE-502 ?
MITRE lists the following affected platforms: Java, Ruby, PHP, Python, JavaScript, ICS/OT.
Comment puis-je prévenir CWE-502 ?
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.
Comment Plexicus détecte et corrige CWE-502 ?
Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-502 à chaque commit. Lorsqu'une correspondance est trouvée, notre agent Codex Remedium ouvre une PR de correction avec le code corrigé, les tests et un résumé d'une ligne pour le relecteur.
Où puis-je en savoir plus sur CWE-502 ?
MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/502.html. Vous pouvez également consulter la documentation OWASP et NIST pour des conseils adjacents.
Weaknesses related to CWE-502
Improper Control of Dynamically-Managed Code Resources
This vulnerability occurs when an application fails to properly secure access to code resources that can be created or altered at runtime,…
Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')
Prototype pollution occurs when an application takes user-supplied input and uses it to improperly modify the properties of a JavaScript…
Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')
This vulnerability occurs when an application uses unvalidated external input, like a URL parameter or form field, to dynamically decide…
Improper Control of Dynamically-Identified Variables
This vulnerability occurs when an application fails to properly secure access to variables whose names are determined at runtime, allowing…
Improperly Controlled Modification of Dynamically-Determined Object Attributes
This vulnerability occurs when an application accepts user input that specifies which object attributes or fields to create or update, but…
Improper Control of Generation of Code ('Code Injection')
This vulnerability occurs when an application builds executable code using unvalidated external input, such as user data. Because the…
Further reading
- MITRE — CWE-502 officiel https://cwe.mitre.org/data/definitions/502.html
- The CLASP Application Security Process https://cwe.mitre.org/documents/sources/TheCLASPApplicationSecurityProcess.pdf
- Exploiting Deserialization Vulnerabilities in Java https://www.slideshare.net/codewhitesec/exploiting-deserialization-vulnerabilities-in-java-54707478
- PHP unserialization vulnerabilities: What are we missing? https://www.slideshare.net/_s_n_t/php-unserialization-vulnerabilities-what-are-we-missing
- Marshalling Pickles: How deserializing objects can ruin your day https://www.slideshare.net/frohoff1/appseccali-2015-marshalling-pickles
- Unserializing user-supplied data, a bad idea https://drupalsun.com/heine/2010/08/25/unserializing-user-supplied-data-bad-idea
- Black Hat EU 2010 - Attacking Java Serialized Communication https://www.slideshare.net/msaindane/black-hat-eu-2010-attacking-java-serialized-communication
Arrêtez de payer par développeur.
Commencez à fermer la boucle.
Plexicus est l'ASPM natif IA qui scanne, filtre, corrige, penteste et explique — de façon autonome. Développeurs illimités, dépôts illimités, actions IA à usage équitable. Vrai niveau gratuit, €269/mo annuel quand vous êtes prêt.