buu 代码审计

代码审计

[HCTF 2018] WarmUp

查看源码

image-20240428212058776

访问 source.php

<?php
    highlight_file(__FILE__);
    class emmm
    {
        public static function checkFile(&$page)
        {
            $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
            if (! isset($page) || !is_string($page)) {
                echo "you can't see it";
                return false;
            }

            if (in_array($page, $whitelist)) {
                return true;
            }

            $_page = mb_substr(
                $page,
                0,
                mb_strpos($page . '?', '?')
            );
            if (in_array($_page, $whitelist)) {
                return true;
            }

            $_page = urldecode($page);
            $_page = mb_substr(
                $_page,
                0,
                mb_strpos($_page . '?', '?')
            );
            if (in_array($_page, $whitelist)) {
                return true;
            }
            echo "you can't see it";
            return false;
        }
    }

    if (! empty($_REQUEST['file'])
        && is_string($_REQUEST['file'])
        && emmm::checkFile($_REQUEST['file'])
    ) {
        include $_REQUEST['file'];
        exit;
    } else {
        echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
    }  

看到hint.php

image-20240428213216746

利用indlue函数 文件包含漏洞

image-20240428214402363

这边是 在source.php文件下如果满足这三个条件,就输出指定文件

因为参数在source.php文件下 所以要在source.php文件下传参

image-20240608194938348

检查file是否在白名单中,在就返回true

试着传参

image-20240608195654864

发现输出了两 遍源码

image-20240608195757455

这两段代码一样是为了防止用户传参时,符号被urlencode导致无法解析

image-20240608200044756

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

这段是说,截取传入的file的值,从开始到?处(不包含)将截取后的字符串和白名单做对比,可以就返回true

image-20240608195922106

只有一遍源码了

所以在第二个source.php?后面直接访问fllllaaaagggg

但是因为此时source.php会被当成一个文件夹,所以加个/

../返回上一级目录

image-20240428213547852

[BJDCTF2020]Mark loves cat

dirsearch扫描发现是git泄露

image-20240429193547983

image-20240429193632206

githack.py下载得到源文件 index.php

<?php
include 'flag.php';

$yds = "dog";
$is = "cat";
$handsome = 'yds';

foreach($_POST as $x => $y){
    $$x = $y;
}

foreach($_GET as $x => $y){
    $$x = $$y;
}

foreach($_GET as $x => $y){
    if($_GET['flag'] === $x && $x !== 'flag'){
        exit($handsome);
    }
}index.php

if(!isset($_GET['flag']) && !isset($_POST['flag'])){
    exit($yds);
}

if($_POST['flag'] === 'flag'  || $_GET['flag'] === 'flag'){
    exit($is);
}

echo "the flag is: ".$flag;

flag.php

<?php

$flag = file_get_contents('/flag');

foreach循环导致变量覆盖

foreach是用于数组和对象的循环语句

  • <?PHP
       $authors = array( "Java", "PHP", "CSS", "HTML" ); 
    
       foreach ( $authors as $val ) { 
           echo $val . "\n"; 
       }   
    ?>
    

    image-20240429212241852

  • <?php //from   ww w  .  ja  va2s .  c o m
    $myBook = array( "title" =>  "Learn PHP from www.w3cschool.cn", 
                    "author" =>  "www.w3cschool.cn", 
                    "pubYear" =>  2000 ); 
    
    foreach ( $myBook as $key =>  $value ) { 
       echo "$key  \n"; 
       echo "$value \n"; 
    } 
    
    ?>
    

    image-20240429212323294

  • <?PHP
    /*www .j a  va  2  s. c  o  m*/
    $authors = array( "Java", "PHP", "CSS", "HTML" );
    
    // Displays "Java PHP Javascript HTML";
    foreach ( $authors as $val ) {
        if ( $val == "CSS" ) $val = "Javascript";
        echo $val . " ";
    }
    
    print_r ( $authors );
    ?>
    

    image-20240429212711380

image-20240429221208586

可变变量

如果一个变量的值刚好是另一个变量的名字 就可以通过访问一个变量来得到另一个变量

方法; 在此变量 之前加一个 $

例如 $$x相当于 $($x)

image-20240429221508887

1.构造payoad

yds=flag

image-20240429213622672

exit()也是输出的一种

yds=flag被处理为 $yds=$flag

image-20240429213958386

因为没有传入 $_GET和$_POST所以直接输出exit()

$handsome = 'yds'; 使 yds=flag输出handsome为flag{}

image-20240429222059798

2.构造payload

image-20240429220439267

经处理 $is=$flag输出$flag 构造 flag=flag是为了exit(is)

image-20240429222140913

image-20240429222303813

image-20240429222323030

[ZJCTF 2019]NiZhuanSiWe

源码

<?php
$text = $_GET["text"];
$file = $_GET["file"];
$password = $_GET["password"];
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
    echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
    if(preg_match("/flag/",$file)){
        echo "Not now!";
        exit();
    }else{
        include($file);  //useless.php
        $password = unserialize($password);
        echo $password;
    }
}
else{
    highlight_file(__FILE__);
}
?>

file_get_contents把文件读到一个字符串中

利用伪协议php://input绕过file_get_contents函数

image-20240608112716383

读取文件里的字符串,要和 welcome to the zjctf相等

构造paylaod

image-20240608112629953

image-20240608112830390

flag被匹配掉了

尝试读取useless.php源码,用php://filter

image-20240608113025264

base64解码

看到__toString方法

image-20240608113356950

构造payload读取flag.php,将N替换为flag.php

image-20240608113906495

访问源码

image-20240608113929739

[BJDCTF2020]EasySearch

posted @ 2024-06-08 15:02  Yolololololo  阅读(9)  评论(0编辑  收藏  举报