[Zer0pts2020]Can you guess it?

[Zer0pts2020]Can you guess it?

打开环境没有什么特殊的地方,可以点击按钮查看源码

fj3146IVa2Ecv0B0C-uyUA55sSN9oCCwtkbmdThu-Is

<?php
include 'config.php'; // FLAG is defined in config.php

if (preg_match('/config\.php\/*$/i', $_SERVER['PHP_SELF'])) {
  exit("I don't know what you are thinking, but I won't let you read it :)");
}

if (isset($_GET['source'])) {
  highlight_file(basename($_SERVER['PHP_SELF']));
  exit();
}

$secret = bin2hex(random_bytes(64));
if (isset($_POST['guess'])) {
  $guess = (string) $_POST['guess'];
  if (hash_equals($secret, $guess)) {
    $message = 'Congratulations! The flag is: ' . FLAG;
  } else {
    $message = 'Wrong.';
  }
}
?>

提示了flag在config.php中

第一个if判断中,$_SERVER用于获取当前执行脚本的文件名,与document root有关。例如,在地址为 http://xxx/test.php/foo.bar 的脚本中使用$_SERVER['PHP_SELF']将得到 /test.php/foo.bar

如果url是:http://xxx/index.php/a.php/a/b/c/d/?a=1

$_SERVER['PHP_SELF']的值就是index.php/a.php/a/b/c/d/ (忽略传参)

并且PHP在根据URI解析到对应文件后会忽略掉URL中多余的部分,即若访问存在的index.php页面,如下两种url均会访问到。

/index.php
/index.php/dosent_exist.php

这里判断$_SERVER获取到的值是否以config.php/* 结尾。相当于要求config.php/后不能有可读文件,如果是则退出。

第二个if如果传入了source参数,就highlight_file 高亮显示$_SERVER中经过basename后提取到的文件

basename可以理解为对传入的参数路径截取最后一段作为返回值,但是该函数发现最后一段为不可见字符时会退取上一层的目录,即:

$var1="/config.php/test"
basename($var1) => test
$var2="/config.php/%ff"
basename($var2) => config.php

绕过正则可以使用不存在于ascii码表中的字符,比如中文符号?、《》、中文等,例如index.php/config.php/??source(第一个为中文问号),此时正则就会失效,SERVER获取到的就是index.php/config.php(忽略传参),经过basenmae后就是config.php,这样既可以绕过正则匹配,也可以绕过basename的过滤

payload如下:

/index.php/config.php/??source
/index.php/config.php/%ff?source

OxxhcUFFKZ5fO9afl5dhBU9EXatQfF5qKhNEJk1g7cA

posted on 2024-05-02 14:34  跳河离去的鱼  阅读(1)  评论(0编辑  收藏  举报