Insecure deserialization
Insecure deserialization makes the server build objects from attacker-supplied data, which lets a chain of unrelated library classes run code during parsing.
Material
JSON stores data. Other formats such as Java serialization, PHP serialize, or YAML tags store data plus instructions about how to rebuild objects. The second kind is what turns a parsing bug into remote code execution.
How it works
During deserialization the incoming bytes decide which class gets instantiated and what each property becomes. Constructors, setters, and magic methods such as __wakeup or __destruct then run as a side effect of parsing.
A chain of classes that call each other until something dangerous happens is called a gadget chain. Exploitation needs three pieces: a reachable deserialization point, a class with useful side effects, and a way to assemble them.
import base64
import pickle
class Exploit:
def __reduce__(self):
import os
return (os.system, ('id',))
payload = base64.b64encode(pickle.dumps(Exploit())).decode()
print(payload)POST /api/profile HTTP/1.1
Host: lab.example
Content-Type: application/x-java-serialized-object
Content-Length: 68
rO0ABXNyABdqYXZhLnV0aWwuUHJpb3JpdHlRdWV1ZQAAAAAAAA==Attack shapes
- Remote code execution through a gadget chain in Java, PHP, Ruby, or Python libraries.
- Arbitrary object injection that mutates application state, for example flipping a permission flag on a user object.
- Denial of service with payloads that expand into extremely deep or extremely wide object graphs.
- Data exfiltration through a gadget that writes a file or issues an outbound request.
- Format confusion that bypasses signing, where a tag-based or alternative serialization format is accepted instead of the signed one.
Preventing it
- Do not deserialize untrusted input at all. Prefer plain data formats such as JSON and map fields explicitly.
- Enforce an allowlist of permitted classes and types in the parser configuration.
- Verify a signature over the serialized bytes before parsing, and keep the signing key outside the application.
- Run parsing in a sandbox process without network access and with the lowest possible privileges.
- Keep libraries that expose gadget classes up to date so known chains stop working.
import yaml
# yaml.load(raw, Loader=yaml.UnsafeLoader) bisa membangun objek Python apa saja
data = yaml.safe_load(raw) # hanya tipe data biasa
print(data)Labs in this topic
Labs run on a domain separate from this site, so a payload cannot reach your account session.
Labs for this category land in Phase 2. The material is readable now.
Challenges in this category
Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.