[HarekazeCTF2019]Avatar Uploader 1

登陆进去文件上传类的题目,对png图片和文件大小进行了限制,我们上传了一个符合条件的图片,并没有给出什么

看了文章才知道原题给出了源码

upload.php 其他文件没啥用

<?php
error_reporting(0);
 
require_once('config.php');
require_once('lib/util.php');
require_once('lib/session.php');
 
$session = new SecureClientSession(CLIENT_SESSION_ID, SECRET_KEY);
 
// check whether file is uploaded
if (!file_exists($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
    error('No file was uploaded.');
}
 
// check file size
if ($_FILES['file']['size'] > 256000) {
    error('Uploaded file is too large.');
}
 
// check file type
$finfo = finfo_open(FILEINFO_MIME_TYPE);//获取文件MIME类型
$type = finfo_file($finfo, $_FILES['file']['tmp_name']);
finfo_close($finfo);
if (!in_array($type, ['image/png'])) {
    error('Uploaded file is not PNG format.');
}
 
// check file width/height
$size = getimagesize($_FILES['file']['tmp_name']);
if ($size[0] > 256 || $size[1] > 256) {
    error('Uploaded image is too large.');
}
if ($size[2] !== IMAGETYPE_PNG) {
    // I hope this never happens...
    error('What happened...? OK, the flag for part 1 is: <code>' . getenv('FLAG1') . '</code>');
}
 
// ok
$filename = bin2hex(random_bytes(4)) . '.png';
move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_DIR . '/' . $filename);
 
$session->set('avatar', $filename);
flash('info', 'Your avatar has been successfully updated!');
redirect('/');

$finfo = finfo_open(FILEINFO_MIME_TYPE);//获取文件MIME类型
$type = finfo_file($finfo, $_FILES['file']['tmp_name']);
finfo_close($finfo);
if (!in_array($type, ['image/png'])) {
    error('Uploaded file is not PNG format.');
}
 
// check file width/height
$size = getimagesize($_FILES['file']['tmp_name']);
if ($size[0] > 256 || $size[1] > 256) {
    error('Uploaded image is too large.');
}
if ($size[2] !== IMAGETYPE_PNG) {
    // I hope this never happens...
    error('What happened...? OK, the flag for part 1 is: <code>' . getenv('FLAG1') . '</code>');
}

关键点

有两个重要的函数分析一下

 $finfo = finfo_open(FILEINFO_MIME_TYPE);//获取文件MIME类型
$type = finfo_file($finfo, $_FILES['file']['tmp_name']);

就会通过图片的内容头进制分析图片的类型,jpg或者png,然后返回下面是一个判断,所以一定是一个png的文件头

$size = getimagesize($_FILES['file']['tmp_name']);

这个函数返回图片信息的数组

索引 0 给出的是图像宽度的像素值
索引 1 给出的是图像高度的像素值
索引 2 给出的是图像的类型,返回的是数字,其中1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
索引 3 给出的是一个宽度和高度的字符串,可以直接用于 HTML 的 <image> 标签
索引 bits 给出的是图像的每种颜色的位数,二进制格式
索引 channels 给出的是图像的通道值,RGB 图像默认是 3
索引 mime 给出的是图像的 MIME 信息,此信息可以用来在 HTTP Content-type 头信息中发送正确的信息,如: header("Content-type: image/jpeg");

($size[2] !== IMAGETYPE_PNG) 不是png类型也就是不等于3就可以绕过

<?php
$savepath="D:\/cun fang phpwenj'\/1671434913089.png";
$array = getimagesize("D:\/cun fang phpwenj'\/1671434913089.png");
print_r($array);

$finfo = finfo_open(FILEINFO_MIME_TYPE);
//$mime = finfo_file($finfo, dirname(__FILE__)."/".$filename);
$mime = finfo_file($finfo, $savepath);
echo $mime;

echo "\n";


$savepath1="D:/\cun fang phpwenj'/\zzz.png";
$array1 = getimagesize("D:/\cun fang phpwenj'/\zzz.png");
print_r($array1);

$finfo1 = finfo_open(FILEINFO_MIME_TYPE);
//$mime = finfo_file($finfo, dirname(__FILE__)."/".$filename);
$mime1 = finfo_file($finfo1, $savepath1);
echo $mime1;



返回结果

Array
(
    [0] => 229
    [1] => 101
    [2] => 3
    [3] => width="229" height="101"
    [bits] => 8
    [mime] => image/png
)
image/png


image/png

 第一次返回图片信息,第二次只返回类型,所以就可以绕过,也就是让getimagesize这个函数出错,原本以为删除图片里面的部分内容就可以,

最后剩下这个文件头才行 

 

上传文件获得flag 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值