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

[SWPUCTF 2018]SimplePHP

1.打开是一个上传页面

有一个上传文件功能和查看文件功能

分别打开看一下

upload_file.php

file.php

感觉这个查看文件这个页面可以进行任意文件读取,使用bp抓包看一下

发现可以读取

2.查看文件代码

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>

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

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-->

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

然后再看一下经常出现的class.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; } } ?>

3.分析代码

function.php

里面告诉了上传的路径是upload/加文件名 ,如果有重复的就给删除 掉,然后还有一个白名单,只能上传gif,jpeg,jpg,png这几个文件没啥可用的

base.php

里面提示了一个flag在f1ag.php中

file.php

他首先是限制了只能访问/var/www/html/这个目录下

然后三元运算符判断GET传参file的值,然后赋值给$file,随后if检测$file的值如果为空,就echo输出一段话,然后将实例化一个Show类,将值赋值 给$show,随后检测$file是否存在 ,如果存在的话,就将$file的值,赋给Show类的source属性,然后再调用它的_show方法

class.php

一个三个类

C1e4r、Show、Test

C1e4r类:

两个公共属性test、str

一个construct方法,里面接收一个$name参数,触发了让$name赋值给本对象的str属性

一个destruct方法,让本类的str属性赋值给本类的test属性,然后echo输出本 类的test属性

Show类:

两个公共属性source和str

construct方法,里面接收一个参数$file,触发了会让$file赋值给本类的source属性,并echo输出source属性的值

toString方法,触发了会让 $this->str数组中的 键名为str的值访问他的 source属性 ,并将值赋给$content,然后return返回$content

set方法,里面接收两个参数$key和$value,触发了会让$value赋值给本类的$key属性

show方法

里面一个if语句,里面是一个正则表达式,里面还有一个f1ag,检查本类的source属性,如果检查到了就die退出,否就高亮显示source属性的值

wakeup方法,触发了会有一个正则表达式,里面是一些伪协议,检查source属性中是否有这些字段,如果有就echo一个hacker,然后将index.php赋值给source属性

Test类:

两个公共属性,file和params

construct方法,会将一个数组赋值给本类的params属性

get魔术方法,里面接收一个$key参数,触发了会调用本类的get方法,然后$key也是这个方法的参数

get方法,里面接收一个参数$key,触发了会if检测params属性中有没有$key这个键,有的话就将这个键的键值赋值给$value,否的话就将index.php赋值给$value,if语句外有一个return返回本类的file_get方法,里面接收一个参数$value

file_get方法,里面接收一个参数$value,触发了会先读取$value,然后再进行一个base64编码,随后将值赋值给$text,然后返回$text

4.思路分析

结合上面的这些代码,判断这是一道phar反序列化的题目

代码中的提示,flag在f1ag.php中

只能限制只能访问/var/www/html/目录下

还有上传文件的路径为upload/

接下来就是class文件中的代码来生成phar文件

调用链

C1e4r :: destruct --> Show :: toString --> Test :: get --> Test :: get --> Test :: file_get

5.开始构造

生成 出来的phar文件,需要修改为png图片,因为白名单中只有这几个,随后上传

到upload/目录 下查看上传的文件名,使用phar进行反序列化

解码得到flag

6.知识点

这题考验的是phar反序列化

然后还有一个限制访问路径的

ini_set('open_basedir','/var/www/html/'); ,后面的是它限制的 只能访问的目录

反序列化篇已完结

http://www.jsqmd.com/news/89080/

相关文章:

  • Flutter 多端落地实战:Web 与桌面应用的性能优化、SEO 与用户体验全攻略
  • 系统启动和DNS
  • Springboot连锁家政保洁管理系统03zmn(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • Flutter 测试全栈指南:从单元测试到黄金路径验证的工程化实践
  • 本凡码农引领杭州小程序开发解决方案赋能企业创新与发展
  • FlutterOpenHarmony商城App标签选择组件开发
  • Springboot连锁药店进销存业务系统98i85(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 前端与数据库交互
  • 《CAPL脚本实现CANOE工具 Bus-Off自动恢复(含重试机制)》
  • OP-TEE HelloWorld 文件保护实战:把一份 `helloworld.txt` 交给 Secure World 保管(EKB → PTA → CA)
  • download https://apkpure.com/cn/
  • web3作业
  • 基于MATLAB的SIFT特征汽车车标识别系统
  • Windows系统文件wdi.dll缺失或损坏问题 下载修复
  • 智能弹性伸缩算法在测试环境中的实践与验证
  • 掌握 Open Graph 协议:让你的网页在社交媒体上大放异彩
  • 微信小程序_WXML
  • Flink学习笔记:如何做容错
  • Windows11系统文件wer.dll丢失或损坏问题 下载修复
  • ​三天搞定企业招聘系统----|---附完整源码
  • 多云测试的智能调度方案
  • 光伏发电+boost+储能+双向dcdc+并网逆变器控制参考资料 光伏发电+boost+储能+...
  • 基于风险演进的智能测试策略设计
  • OOP题目集 4~5 及 课堂测验总结性 Blog
  • Windows系统文件werui.dll缺少或损坏 下载修复
  • 评价页岩油是否值得开采的指标有哪些
  • 能耗黑洞无处遁形!能源管理系统核心功能剖析【带源码】
  • 周日随笔 第一弹
  • 交换机.路由器.防火墙-技术提升【6.8】
  • Redis原理篇-Dict的rehash