Iconv, set the charset to RCE: Exploiting the glibc to hack the PHP engine (part 3)
A 24-year-old buffer overflow in glibc's iconv (CVE-2024-2961) can be triggered through PHP's php://filter to corrupt the heap and achieve remote code execution, even when the file read primitive provides no output. The exploit builds an arbitrary read primitive using a blind oracle to leak memory, then locates system() and other functions to overwrite a function pointer and execute commands.
ambionics.io
Published on ambionics.io
A few months ago, I stumbled upon a 24 years old buffer overflow in the glibc, the base library for linux programs. Despite being reachable in multiple well-known libraries or executables, it proved rarely exploitable — while it didn't provide much leeway, it required hard-to-achieve preconditions.
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
Likely a buffer overflow in glibc's iconv implementation when converting to the ISO-2022-CN-EXT charset, which allows overwriting heap memory when triggered via PHP's convert.iconv filter.
Impact
An attacker could achieve remote code execution on a PHP server if they can trigger the vulnerable iconv filter, even without direct output from the file read, but the exploit is slow and may require specific heap conditions.
What the attacker needed
PHP with vulnerable glibc; Ability to pass php://filter input; No output from file read needed
The fix
The vulnerability was fixed in glibc (CVE-2024-2961), and PHP applications should be updated to use a patched glibc version.
How it was found
- Set up the heap with a page of 0x400 chunks and a page of 0x30 chunks (bucket structures) below.
- Use the convert.iconv.UTF-8.ISO-2022-CN-EXT filter to trigger CVE-2024-2961 and overflow one byte into the next chunk's next pointer, displacing a 0x400 chunk to overlap the 0x30 page.
- Leak the brigade address by inprinting a bucket structure into a 0x400 buffer and using dechunk to isolate the leaked bytes.
- Overwrite a bucket's buf and buflen to create an arbitrary read primitive, using dechunk to extract the leaked data.
- Use the arbitrary read to parse PHP heap metadata and ELF structures to find the addresses of system(), malloc(), and realloc().
- Send a final request that overwrites zend_mm_heap.custom_heap._free with system and frees a crafted chunk to execute code.
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.
This is about a bug in a common system library that PHP uses when converting text between character sets. By sending a specially crafted request that uses PHP's file-reading filters, an attacker can cause a memory corruption that lets them read arbitrary memory and eventually run their own commands on the server, even if the application does not show the file contents.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.