index.php分析
index.php代码:
<?phpif (!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.php
当时做了半天,只知道先index传上去文件,然后example利用,但是不知道怎么绕过index的过滤,复现一波:
第一处判断
if ($_FILES['postedFile']['size'] > 1024*512)
不用管,放着就行,
第二处:
$imageinfo = getimagesize($_FILES['postedFile']['tmp_name']);
传一个图片马就绕过去了
第三处,就是卡在这里了
if ($imageinfo[0] !== 1 && $imageinfo[1] !== 1)
在文件头部加上XMB头
#define width 1#define height 1
第四处,检测文件名
if(stristr($fileName,"c") || stristr($fileName,"i") || stristr($fileName,"h") || stristr($fileName,"ph"))
参考https://blog.rubiya.kr/index.php/2018/11/29/strtoupper/,某些字母经过
mb_strtolower处理后等效普通字母。
到此为止,index.php就分析完了,接着看example.phpexample.php分析
```php <?php if (!isset($GET[“ctf”])) { highlightfile(__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);}
}
example是解压压缩包的功能,再配合上上面的绕过字符检测,可以判断就是让上传一个压缩包,解压出来webshell。<br />除了上面两个if判断,下面还有一个`imagecreatefrompng`函数,这个绕过也很简单,使用脚本即可绕过:[https://github.com/huntergregal/PNG-IDAT-Payload-Generator](https://github.com/huntergregal/PNG-IDAT-Payload-Generator)。<br />运行脚本生成一个图片马,接着压缩,并且在文件尾加上:```php#define width 1#define height 1
需要注意的是,添加在压缩包文件尾时,需要换行后添加,不然的话getimagesize绕过会失败。
接着上传时抓包,把i改为%C4%B0,来绕过最后的字符检测。
上传完之后,去example.php解压上传的压缩包,然后程序会调用imagecreatefrompng创建一个新文件到example目录下,并且这个脚本生成的图片马不会因为这个函数而乱码。
最后去example目录下执行命令读取flag。
