其他分享
首页 > 其他分享> > BUUCTF之[Zer0pts2020]Can you guess it? basename函数绕过

BUUCTF之[Zer0pts2020]Can you guess it? basename函数绕过

作者:互联网

BUUCTF之[Zer0pts2020]Can you guess it? basename函数绕过

题目
在这里插入图片描述
在这里插入图片描述
后台PHP源码:

<?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.';
  }
}
?>

这题有两种解题方式,第一种看这里

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.';
  }
}

好了,废话不多说。现在开始解题吧!
首先是需要绕过这个正则表达式:

if (preg_match('/config\.php\/*$/i', $_SERVER['PHP_SELF']))

这里是表示不能以config.php为结尾,不然会被拦下。
所以这里可以在config.php后面再加一个文件名,比如:

接下来获取flag是看这个

highlight_file(basename($_SERVER['PHP_SELF']));

关键点是这个函数basename
basename() 函数返回路径中的文件名部分。但是它有个小问题,它会去掉文件名开头的非ASCII值。比如:

如图
在这里插入图片描述
关于basename函数的bug可以看这里

With the default locale setting "C", basename() drops non-ASCII-chars at the beginning of a filename.

所以payload是:

上面的payload随便哪一个都可以,主要是保证config.php后面必须是非ascii值就可以了

获取到的Flag:
在这里插入图片描述

标签:index,guess,BUUCTF,basename,PHP,php,config
来源: https://blog.csdn.net/weixin_44632787/article/details/118724480