[HCTF 2018]WarmUp 1 滑稽图(详解)

[HCTF 2018]WarmUp 1

查看页面源代码,发现html中忽视了 source.php ,接下来我们访问 source.php

<?php
    highlight_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代码:

    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\" />";
    }  
?>

empty — 检查一个变量是否为空

is_string — 检测变量是否是字符串

最后一段 php 代码的意思是:如果我们输入的文件名 不为空,是字符串,并且传过来的文件满足emmm类中的checkFile的这个函数的话 就会对给文件进行文件包含。否则输出一张滑稽图片

再看前半段代码:

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

&$page: 引用传递 变量

public static function checkFile(&$page) : public static function 静态方法 public static 静态属性

[]是PHP数组索引标识符号

isset() 函数用于检测变量是否已设置并且非 NULL

in_array() 函数搜索数组中是否存在指定的值。

语法

in_array(search,array,type)
参数描述
search必需。规定要在数组搜索的值。
array必需。规定要搜索的数组。
type可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。

mb_substr() 函数返回字符串的一部分,之前我们学过 substr() 函数,它只针对英文字符,如果要分割的中文文字则需要使用 mb_substr()。

mb_strpos — 查找字符串在另一个字符串中首次出现的位置

在这段php代码中:
先建立一个叫 emmm 的类,建立了一个 静态方法 checkFile 并且是数值传递:

定义了一个变量 $whitelist (白名单数组)

一个是source.php 一个是hint.php

查看hint.php,发现:

flag not here, and flag in ffffllllaaaagggg

如果没有定义 p a g e 变 量 或 者 page变量或者 pagepage变量不是字符串,就输出 you can’t see it 并且返回false

如果在$page变量中搜索到 白名单里的东西 就返回true 换句话说,在搜索的时候我们要包含 source.php 或者 hint.php。

并且mb_substr函数会将 p a g e 变 量 会 截 取 从 第 零 位 开 始 到 m b s t r p o s ( _page变量会截取 从第零位开始 到 mb_strpos( pagembstrpos(page . ‘?’, ‘?’)

mb_strpos($page . ‘?’, ‘?’): $page . ‘?’ 会在后面自动对我们搜索文件路径添加一个 ?

mb_strpos($page . ‘?’, ‘?’) 整体的话,就是匹配第一次出现问号的位置。和前面的mb_substr函数一配合就会 从一开始截取到第一次问好的位置

例如:我们输入file=source.php?../…/…/…/flag

就会截取到 file=source.php?

截取完之后就又会进行一次判断 if (in_array($_page, $whitelist))

如果 $_page 在白名单里面就返true,换句话说就是检查我们输入的文件路径是否还包含 source.php或者hint.php

做题步骤
  1. 先查看 hint.php

    http://6e33302e-9f50-4d03-a55c-293092589c56.node4.buuoj.cn:81/hint.php
    
  2. 构造 我们的查询 flag 语句

    因为有 mb_substr() mb_strpos() 函数所以我们不能直接使用 问号 ,需要先对其进行 两次url编码

    ?file=source.php%253F../../../../ffffllllaaaagggg
    

    如果没有查询到flag,就接着查询下一个子文件夹 多添加一次 ../ 直到查见flag为止

在这里插入图片描述

### HCTF 2018 WarmUp 1 PHP Code Security Audit Vulnerabilities Analysis In conducting a security audit on the PHP code from HCTF 2018 WarmUp 1, several potential vulnerabilities can be identified that may compromise application integrity and confidentiality. #### Potential SQL Injection Flaw If user inputs are not properly sanitized before being used in database queries, an attacker could inject malicious SQL commands. For instance, consider this snippet: ```php $query = "SELECT * FROM users WHERE username='" . $_POST['username'] . "' AND password='" . md5($_POST['password']) . "'"; ``` This approach lacks parameterized queries or prepared statements, exposing the system to injection attacks[^1]. #### Weak Password Hashing Mechanism Using `md5` for hashing passwords represents a significant weakness since MD5 is considered cryptographically broken and unsuitable for further use due to extensive vulnerability research findings. A more robust algorithm like bcrypt should replace outdated methods. #### Lack of Input Validation Failure to validate input parameters allows arbitrary values through unchecked pathways into backend processes. An example might involve file uploads without strict MIME type checks or size limitations, leading to unauthorized resource access scenarios[^2]. #### Insecure Direct Object References (IDOR) When sensitive actions rely directly upon unverified identifiers passed via URLs or forms, there exists risk associated with IDOR issues where attackers manipulate these references gaining illegitimate privileges over resources they shouldn't have control over. To mitigate such risks effectively within PHP applications similar to those presented during CTF challenges, developers must adhere strictly to secure coding practices including but not limited to employing ORM frameworks for query construction, utilizing strong encryption algorithms alongside salting techniques when handling credentials storage securely as well as implementing comprehensive validation routines across all entry points ensuring only expected types/formats reach internal logic layers.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值