[HarekazeCTF2019]Easy Notes-代码审计

[HarekazeCTF2019]Easy Notes-代码审计

登录之后有几个功能点,可以添加节点,然后使用Export导出

image-20230824205806589

我们查看源码,

我们发现想要拿到flag的条件时$_SESSION['admin']=true

image-20230824210223434

如果我们能够控制session文件,就可以拿到flag了

image-20230824210445088

我们发现session存储的文件改为了:/var/www/tmp

重点看export.php

<?php
require_once('init.php');

if (!is_logged_in()) {
  redirect('/?page=home');
}

$notes = get_notes();

if (!isset($_GET['type']) || empty($_GET['type'])) {
  $type = 'zip';
} else {
  $type = $_GET['type'];
}

$filename = get_user() . '-' . bin2hex(random_bytes(8)) . '.' . $type;
$filename = str_replace('..', '', $filename); // avoid path traversal
$path = TEMP_DIR . '/' . $filename;

if ($type === 'tar') {
  $archive = new PharData($path);
  $archive->startBuffering();
} else {
  // use zip as default
  $archive = new ZipArchive();
  $archive->open($path, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE);
}

for ($index = 0; $index < count($notes); $index++) {
  $note = $notes[$index];
  $title = $note['title'];
  $title = preg_replace('/[^!-~]/', '-', $title);
  $title = preg_replace('#[/\\?*.]#', '-', $title); // delete suspicious characters
  $archive->addFromString("{$index}_{$title}.json", json_encode($note));
}

if ($type === 'tar') {
  $archive->stopBuffering();
} else {
  $archive->close();
}

header('Content-Disposition: attachment; filename="' . $filename . '";');
header('Content-Length: ' . filesize($path));
header('Content-Type: application/zip');
readfile($path);

这里可以看到,导出的文件也是写到/var/www/tmp目录下面,所以我们可以尝试session伪造一下,伪造一个session文件

$filename = get_user() . '-' . bin2hex(random_bytes(8)) . '.' . $type;
$filename = str_replace('..', '', $filename); // avoid path traversal
$path = TEMP_DIR . '/' . $filename;

get_user()会获取用户名,bin2hex(random_bytes(8))会生成16进制字符串,

如果我们用户名为sess_,并且$type=.,那么两个.会被替换为空,所以最终文件名就符合session文件的格式了,session文件名可控

那么看一下session内容可控吗:

$archive->addFromString("{$index}_{$title}.json", json_encode($note));

可控的

由于默认session_serialize_handler=php,那么序列化规则为:

处理器对应的存储格式
php键名 + 竖线 + 经过 serialize() 函数反序列处理的值

所以$_SESSION['admin']=true需要满足:

admin|b:1;

但是由于我们导出的文件中有一些内容,防止被污染,我们需要这么写:

|N;admin|b:1;

image-20230824211602859

添加之后导出:

/export.php?type=.

最后替换一下PHPSESSID为文件名:

image-20230824211719493

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值