Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Web attacks

Lab · Hard · 3 hoursFree account

Insecure Deserialization

Find, exploit and fix PHP and Python deserialization sinks — plus how to flag a Java one without a working exploit.

  • deserialization
  • gadget-chains
  • rce
  • php
  • python
On this page 12 sections
  1. ·What Deserialization Attacks Exploit
  2. ·Prerequisites
  3. ·Build the target
  4. 1Recognise Serialized Data in Transit
  5. 2Confirm the Endpoint Deserialises Your Input
  6. Locked: Find the Gadget by Reading the Source
  7. Locked: Exploit the PHP Gadget to Run a Command
  8. Locked: Exploit the Python pickle Sink
  9. Locked: Flag the Java Sink Without Exploiting It
  10. Locked: Close Every Sink You Found
  11. Locked: Check Your Work
  12. Locked: Clean Up and Next Lab

A deserialization sink trusts the shape of an attacker's bytes to rebuild an object, and in PHP, Python and Java that trust can run arbitrary code. The lab reads a payload's format signature, proves the sink executes, exploits PHP and Python gadgets, and then closes every sink with allow-lists and safe formats.

Lab Objectives

  • Recognise serialized payloads from PHP, Python and Java by their format signatures
  • Confirm whether an endpoint actually deserialises attacker-controlled input
  • Exploit a PHP magic-method gadget to run a command through unserialize()
  • Exploit a Python __reduce__ gadget to run a command through pickle.loads()
  • Explain why a black-box Java signature match gets reported, not exploited, and what a tester does next
  • Apply allow-lists, safe formats and logging that close every sink you found

What Deserialization Attacks Exploit

Serialization turns an in-memory object into bytes so an app can store it in a cookie, a cache or a database, then rebuild the object later. PHP's unserialize(), Python's pickle.loads() and Java's ObjectInputStream.readObject() all do the same dangerous thing: they let the byte stream itself say which class to build and what its properties are. If that byte stream comes from the user, the user chooses the class.

Most of the time this is harmless — a UserPrefs object with a name and a theme property. It stops being harmless the moment the application already has a class whose constructor, destructor or a "magic method" (__wakeup, __destruct, __reduce__, readObject) does something dangerous with its own properties — write a file, run a command, open a connection. You do not need to find a bug in the deserializer. You need to find one class, already in the application, that does something dangerous with data you control. That object is called a gadget, and chaining several of them together is a gadget chain.

Prerequisites

  • White-Box Web Exploitation: Auth Bypass to RCE — reading application source to find a dangerous sink. This lab reuses that skill to find the PHP gadget in Step 3.
  • Broken Authentication & Sessions — covers how session and preference cookies are built. A serialized object most often arrives through exactly this kind of cookie.
  • API & Web Service Attacks — the Python and Java sinks in this lab are backend services you reach only over their API; that lab covers probing a service without source access.
  • Comfort with curl, Python 3, and reading a short PHP or Python class definition.

Build the target

KaliRange hosts no targets, no virtual machines and no lab files. You run this lab on your own machine, against three small services that you build. The address 10.0.4.60 below is the example address of this lab. Replace it with the address of your own target.

Your lab must have these three services, each with one of the sinks the steps use:

  • PHP, at http://10.0.4.60/account/preferences. It reads a prefs cookie and passes it straight to unserialize(), with no allowed_classes restriction. Put a file app/Logger.php in its source tree, next to the main app, with a class Logger that has a public cmd property and a __destruct method that runs $this->cmd with system(). The app must write anything it runs to a file it serves back over HTTP, so a command's result is visible (for example, serve a static/ folder and let a logged command redirect its output there).
  • Python, at http://10.0.4.60:5000/cart. It reads a state cookie, base64-decodes it, and passes the bytes straight to pickle.loads(). It must serve its own output folder over HTTP the same way, so a command's result is visible.
  • Java, at http://10.0.4.60:8080/session. It reads an X-Session header and passes the decoded bytes to ObjectInputStream.readObject(). You need no gadget chain here: the step only confirms the signature of the bytes, so any endpoint that deserializes the header is enough.

Put a read-only copy of the PHP source at ~/range/webapp-php/ on your own attack machine, so Step 3 can grep it. Give yourself no source for the Python or Java services, to keep Steps 5 and 6 black-box, as a real engagement often is.

Build these three services yourself: no public training application ships this exact mixed set of sinks. Keep each one small and isolated in its own container or virtual machine, with no route to a production network. Take a snapshot of each virtual machine (or note the container state) before Step 1.

Authorized lab only. Run every command in this lab only against a target that you own or have written permission to test, here 10.0.4.60 as you set it up. Never call unserialize(), pickle.loads() or an ObjectInputStream on data you did not just capture from your own target, and never run a payload against a system you do not own or do not have written authorisation to test.

Step 1: Recognise Serialized Data in Transit

1

Spot a serialized payload before you touch it

Each language's serializer has a signature you can read without any tool. Learn the three shapes and you can flag a sink from a single captured request.

# PHP — the `prefs` cookie on the account page
prefs=O%3A9%3A%22UserPrefs%22%3A2%3A%7Bs%3A4%3A%22name%22%3Bs%3A5%3A%22guest%22%3Bs%3A7%3A%22isAdmin%22%3Bb%3A0%3B%7D
# URL-decoded: O:9:"UserPrefs":2:{s:4:"name";s:5:"guest";s:7:"isAdmin";b:0;}
# "O:<len>:"<Class>":<numprops>:{...}" is the object marker; "a:" is an array.

# Python — the `state` cookie on the cart service
state=gAV9lC4=
# Base64-decoded, the first byte is 0x80 (the PROTO opcode), followed by the
# protocol number. Any cookie that decodes to a leading 0x80 byte is a pickle.

# Java — the X-Session header on the internal API
X-Session: rO0ABXNy...
# Base64-decoded this starts 0xAC 0xED 0x00 0x05: Java's STREAM_MAGIC and
# STREAM_VERSION. "rO0AB" is the signature to grep for in captured traffic.

Example output:

PHP:    object marker "O:" -> UserPrefs, 2 properties
Python: PROTO opcode 0x80  -> pickle, protocol 5
Java:   STREAM_MAGIC AC ED -> java.io serialized stream

Why it matters. None of this required sending a single payload. Reading the raw bytes of a cookie or header tells you that a sink exists and which language it uses. You learn this before you send any request against it. A defender reviewing the same traffic should flag the same three signatures.

Step 2: Confirm the Endpoint Deserialises Your Input

2

Build an oracle before you build a gadget

A value that merely looks serialized might be read as an opaque string and never passed to unserialize(). Corrupt it and compare the response to prove the server actually parses it. This true/false test is an oracle: a check that tells you about the server's internal behaviour without showing you the internal state directly.

# Baseline: the prefs cookie exactly as captured
curl -s -b 'prefs=O%3A9%3A%22UserPrefs%22%3A2%3A%7Bs%3A4%3A%22name%22%3Bs%3A5%3A%22guest%22%3Bs%3A7%3A%22isAdmin%22%3Bb%3A0%3B%7D' \
  http://10.0.4.60/account/preferences

# Corrupted: change the declared string length of the "name" value ("guest")
# from 5 to 9, so the parser reads past the closing quote looking for 4 extra
# bytes that do not exist
curl -s -b 'prefs=O%3A9%3A%22UserPrefs%22%3A2%3A%7Bs%3A4%3A%22name%22%3Bs%3A9%3A%22guest%22%3Bs%3A7%3A%22isAdmin%22%3Bb%3A0%3B%7D' \
  http://10.0.4.60/account/preferences

Example output:

Preferences saved for guest.
Unserialize failed: invalid serialized data

Why it matters. The second request changed nothing but one length digit, yet the server's response changed from success to a parse error. That difference is only possible if the server is really calling unserialize() on your cookie. If both requests had returned the same page, the cookie would be read as plain text somewhere else, and you would stop here — there is no deserialization sink to exploit.

Sign in to continue

7 more sections are for signed-in members. It is free: sign in with Google.

Related