# PHP Insecure Deserialization (unserialize)

> Detects PHP unserialize() called on user-controlled input, enabling object injection and remote code execution.

- Skill: `zakirkun/php-insecure-deserialization-unserialize` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add zakirkun/php-insecure-deserialization-unserialize`
- Raw SKILL.md: https://api.skillmd.com/api/skills/zakirkun/php-insecure-deserialization-unserialize/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: zakirkun (https://skillmd.com/u/zakirkun)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/zakirkun/php-insecure-deserialization-unserialize

---


# PHP Insecure Deserialization

## Overview
PHP's `unserialize()` function reconstructs PHP objects from a string representation. When called with user-controlled input, attackers can craft malicious serialized strings that:
- Invoke `__wakeup()` and `__destruct()` magic methods
- Chain gadgets from existing classes to achieve RCE
- Read/write arbitrary files

This is the basis of PHP Object Injection attacks.

## Remediation
- Never call `unserialize()` on user input
- Use `json_decode()` for data exchange
- If deserialization is required, validate with `allowed_classes` option

**Safe:**
```php
$data = json_decode($_POST['data'], true); // Safe alternative
// OR if PHP object needed, restrict classes:
$obj = unserialize($data, ['allowed_classes' => ['SafeClass']]);
```

