← Lab /Security & privacy

PDO emulated prepares can be bypassed to achieve SQL injection via null-byte tricks

PDO's emulated prepared statements allow SQL injection when crafted inputs exploit parser misparsing.

Security & privacy Assetnote / Searchlight 21 Jul 2025 Attack Prototype evidence

Published as “Novel SQL Injection in PDO Prepared Statements”

Why it matters

Developers should avoid mixing manually constructed SQL fragments with PDO bindings and disable emulated prepares to prevent this class of injection.

Summary

The author shows that PHP PDO, when using emulated prepares, parses SQL with its own scanner that can be fooled by null bytes and comment delimiters. By injecting a null byte and a question mark into a backticked identifier, the scanner treats the question mark as a bound parameter, creating an extra placeholder that can be abused to inject arbitrary SQL. The technique works on MySQL and PostgreSQL in PHP 8.3 and earlier, and is mitigated by disabling emulation or using PHP 8.4's improved parser. The evidence consists of a single CTF challenge implementation and manual testing on local servers.

What they found 1

  1. PDO's parser treats a question mark after a null byte in a backticked identifier as a bound parameter.

    “Thus the PDO parser sees the first ? as a bound parameter.”

Caveats 6

  • If we disable query emulation, or we instead escape $_GET['name'] manually, the code is no longer exploitable.
  • PHP 8.4 is actually much more resilient against these sort of attacks than older PHP versions.
  • Only works when PDO::ATTR_EMULATE_PREPARES is enabled.
  • Demonstrated on MySQL and PostgreSQL with PHP 8.3 and earlier; PHP 8.4 mitigates the issue.
  • Requires ability to inject a null byte or special characters into identifier positions.
  • Relies on PDO's emulated parser misparsing; does not affect native prepared statements.

What’s new here 4

  • Identifies a parser misinterpretation in PDO's emulated prepared statements that turns a null byte into an extra bound parameter.
  • Demonstrates injection via column/table names using null byte and comment tricks.
  • Shows that older PHP versions (<=8.3) are vulnerable without null bytes due to missing backtick handling.
  • Provides practical guidance to disable emulation or avoid mixing manual fragments with bindings.