Insecure Deserialization in .NET
You'll learn how .NET deserializers can be exploited to achieve remote code execution through carefully crafted malicious objects.
The Core Problem
In .NET applications, serialization converts objects into byte streams for storage or transmission, while deserialization reconstructs them. Several .NET deserializers are inherently dangerous because they automatically invoke methods during object reconstruction—attackers exploit this by crafting malicious serialized data containing "gadget chains" that execute arbitrary code when deserialized.
The Dangerous Deserializers
BinaryFormatter is the most notorious offender. It's a legacy serializer that preserves complete type information and can instantiate any .NET type during deserialization. When it processes untrusted data, attackers can embed chains of existing .NET classes (gadgets) that ultimately trigger code execution.
DataContractSerializer and similar XML-based deserializers can also be exploited when configured to accept arbitrary types rather than a safe allowlist. They can instantiate dangerous types that execute code in their constructors or property setters.
How Gadget Chains Work
Think of gadgets like dominoes: individually harmless classes that, when chained together through deserialization, trigger a cascade ending in code execution. Attackers leverage legitimate .NET framework classes—finding combinations where one class's deserialization triggers another's method, eventually reaching dangerous operations like file writes or process execution.
The ysoserial.net tool contains pre-built gadget chains for .NET applications, making exploitation straightforward once an insecure deserialization point is identified.
Real Attack Vector
An attacker sends a crafted base64-encoded payload to an endpoint deserializing with BinaryFormatter. The payload contains a gadget chain that, when reconstructed, executes system commands—all without any obvious vulnerability in the application's own code.
Key Takeaway: Never use BinaryFormatter on untrusted data—it's inherently unsafe. Use secure alternatives like JSON serializers, and when using DataContractSerializer, strictly limit deserializable types to a safe allowlist. The vulnerability exists in the deserializer's design, not your code alone.