|
1 | | -# PHP - RCE abusing object creation: new $\_GET\["a"]\($\_GET\["b"]) |
| 1 | +# PHP - RCE abusing object creation: new $_GET["a"]($_GET["b"]) |
2 | 2 |
|
3 | 3 | {{#include ../../../banners/hacktricks-training.md}} |
4 | 4 |
|
@@ -97,11 +97,34 @@ It's noted that PHP temporarily stores uploaded files in `/tmp/phpXXXXXX`. The V |
97 | 97 |
|
98 | 98 | A method described in the [**original writeup**](https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/) involves uploading files that trigger a server crash before deletion. By brute-forcing the name of the temporary file, it becomes possible for Imagick to execute arbitrary PHP code. However, this technique was found to be effective only in an outdated version of ImageMagick. |
99 | 99 |
|
100 | | -## References |
| 100 | +## Format-string in class-name resolution (PHP 7.0.0 Bug #71105) |
101 | 101 |
|
102 | | -- [https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/](https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/) |
| 102 | +When user input controls the class name (e.g., `new $_GET['model']()`), PHP 7.0.0 introduced a transient bug during the `Throwable` refactor where the engine mistakenly treated the class name as a printf format string during resolution. This enables classic printf-style primitives inside PHP: leaks with `%p`, write-count control with width specifiers, and arbitrary writes with `%n` against in-process pointers (for example, GOT entries on ELF builds). |
103 | 103 |
|
104 | | -{{#include ../../../banners/hacktricks-training.md}} |
| 104 | +Minimal repro vulnerable pattern: |
| 105 | + |
| 106 | +```php |
| 107 | +<?php |
| 108 | +$model = $_GET['model']; |
| 109 | +$object = new $model(); |
| 110 | +``` |
| 111 | + |
| 112 | +Exploitation outline (from the reference): |
| 113 | +- Leak addresses via `%p` in the class name to find a writable target: |
| 114 | + ```bash |
| 115 | + curl "http://host/index.php?model=%p-%p-%p" |
| 116 | + # Fatal error includes resolved string with leaked pointers |
| 117 | + ``` |
| 118 | +- Use positional parameters and width specifiers to set an exact byte-count, then `%n` to write that value to an address reachable on the stack, aiming at a GOT slot (e.g., `free`) to partially overwrite it to `system`. |
| 119 | +- Trigger the hijacked function by passing a class name containing a shell pipe to reach `system("id")`. |
105 | 120 |
|
| 121 | +Notes: |
| 122 | +- Works only on PHP 7.0.0 (Bug [#71105](https://bugs.php.net/bug.php?id=71105)); fixed in subsequent releases. Severity: critical if arbitrary class instantiation exists. |
| 123 | +- Typical payloads chain many `%p` to walk the stack, then `%.<width>d%<pos>$n` to land the partial overwrite. |
106 | 124 |
|
| 125 | +## References |
| 126 | + |
| 127 | +- [https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/](https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/) |
| 128 | +- [The Art of PHP: CTF‑born exploits and techniques](https://blog.orange.tw/posts/2025-08-the-art-of-php-ch/) |
107 | 129 |
|
| 130 | +{{#include ../../../banners/hacktricks-training.md}} |
0 commit comments