Skip to content
Memory corruption Write-up Advanced

@cyberguru007's analysis and PoC

A heap-based buffer overflow in PHP's mysqlnd extension allowed a malicious MySQL server to overwrite memory when the client used the legacy mysql_clear_password authentication method. An attacker controlling the database server could exploit this to achieve remote code execution on the PHP web server.

github.com

Published on github.com

History

The author, on github.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The client allocates a buffer for the password but then copies the password without checking that it fits, causing a heap overflow.

Impact

An attacker who can control the database server could execute arbitrary code on the PHP web server, but this requires the application to allow user-controlled database connections.

What the attacker needed

Attacker controls database server; PHP app allows user to set DB server; PHP built with mysqlnd

The fix

The vulnerability was fixed in PHP 7.4.30 by properly checking the buffer length before copying the password.

How it was found

  1. Set up a PHP web application that allows the user to specify a database server.
  2. Run a rogue MySQL server that sends an auth switch packet requesting the mysql_clear_password method.
  3. Send a password longer than the allocated buffer to trigger the overflow.
  4. Use POST parameters to shape the PHP heap so that the overflow hits a smallbin free list pointer.
  5. Overwrite the last byte of the pointer to create an overlap with a zend_string structure.
  6. Leak memory addresses and then overwrite the sapi_module.ub_write function pointer to execute system commands.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

When a PHP application connects to a MySQL database, it can be tricked into connecting to a malicious server. That server can send a special authentication request that causes the PHP client to write more data than it allocated, corrupting memory. This can let the attacker run their own code on the web server.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.