[CISCN2021 Quals]upload
代码审计
题目给出源码index.php、example.php.
index.php明显是一个上传,做出了如下限制。
- 上传需要绕过
getimagesize
。 - 图片的长宽必须为1.
- 文件名不能有c、i、h
绕过getimagesize
比较简单使用XMB头就行了
#define test_width 1
#define test_height 1
图片名绕过stristr
这一点想了比久直到看到mb_strtolower
,发现他可以使用Unicode

在谷歌找到这么一篇文章https://blog.rubiya.kr/index.php/2018/11/29/strtoupper/
通过fuzz可以知道部分字母在经过mb_strtolower
处理过可以等效普通字母的。
如 i
可以用(%C5%BF)代替。本来以为可以直接上传php,无奈没有相关字母可以代替。这个还得看example.php
下面结合example.php
再具体分析。
<?php
if (!isset($_GET["ctf"])) {
highlight_file(__FILE__);
die();
}
if(isset($_GET["ctf"]))
$ctf = $_GET["ctf"];
if($ctf=="upload") {
if ($_FILES['postedFile']['size'] > 1024*512) {
die("这么大个的东西你是想d我吗?");
}
$imageinfo = getimagesize($_FILES['postedFile']['tmp_name']);
if ($imageinfo === FALSE) {
die("如果不能好好传图片的话就还是不要来打扰我了");
}
if ($imageinfo[0] !== 1 && $imageinfo[1] !== 1) {
die("东西不能方方正正的话就很讨厌");
}
$fileName=urldecode($_FILES['postedFile']['name']);
if(stristr($fileName,"c") || stristr($fileName,"i") || stristr($fileName,"h") || stristr($fileName,"ph")) {
die("有些东西让你传上去的话那可不得了");
}
$imagePath = "image/" . mb_strtolower($fileName);
if(move_uploaded_file($_FILES["postedFile"]["tmp_name"], $imagePath)) {
echo "upload success, image at $imagePath";
} else {
die("传都没有传上去");
}
}
从example
的代码可以直接知道咱么需要上传zip
格式然后通过这个程序解压,这个正好前面知道i
的绕过方法。但是这里需要绕过imagecreatefrompng
,imagepng
如果直接在图片最后写一个一句话木马,会被GD库给去掉。绕过GD库可以参考这篇文章:http://www.vuln.cn/6411 ,但是这个作者写的脚本有毒图片经过imagepng
处理后乱码,浪费了几个小时。最后用这个脚本来生成图片马:https://github.com/huntergregal/PNG-IDAT-Payload-Generator
<?php
if (!isset($_GET["ctf"])) {
highlight_file(__FILE__);
die();
}
if(isset($_GET["ctf"]))
$ctf = $_GET["ctf"];
if($ctf=="poc") {
$zip = new \ZipArchive();
$name_for_zip = "example/" . $_POST["file"];
if(explode(".",$name_for_zip)[count(explode(".",$name_for_zip))-1]!=="zip") {
die("要不咱们再看看?");
}
if ($zip->open($name_for_zip) !== TRUE) {
die ("都不能解压呢");
}
echo "可以解压,我想想存哪里";
$pos_for_zip = "/tmp/example/" . md5($_SERVER["REMOTE_ADDR"]);
$zip->extractTo($pos_for_zip);
$zip->close();
unlink($name_for_zip);
$files = glob("$pos_for_zip/*");
foreach($files as $file){
if (is_dir($file)) {
continue;
}
$first = imagecreatefrompng($file);
$size = min(imagesx($first), imagesy($first));
$second = imagecrop($first, ['x' => 0, 'y' => 0, 'width' => $size, 'height' => $size]);
if ($second !== FALSE) {
$final_name = pathinfo($file)["basename"];
imagepng($second, 'example/'.$final_name);
imagedestroy($second);
}
imagedestroy($first);
unlink($file);
}
}
实践
制作图片马以及压缩包
用上面的脚本直接生成图片马。

然后将图片头给压入压缩包里面。先将下面的文件头写进一个文本文件,注意一定要加一行换行否则识别不出来。因为后面需要解压,这个文件头需要放到压缩包后面。
#define test_width 1
#define test_height 1
用常见的隐写方法:
copy shell.zip/b+b.txt/a sb.zip

绕过文件名检测上传ZIP
解压文件
文件会被解压到example文件夹下